-
Notifications
You must be signed in to change notification settings - Fork 2
Snippets
Nikitin Ilya edited this page Sep 15, 2020
·
3 revisions
# Target: C#
fizz-buzz(1, 100)
@static fn fizz-buzz (start: int, end: int)
for i in start..end
s = ""
if i % 3 == 0
s = "Fizz"
elif i % 5 == 0
s = "Buzz"
if s == ""
s = i.to-string()
Console.write-line(s)
# finds where the function becomes 0 in range using bolzano
fn bisection (func: double -> double, start: double, end: double) -> double
if func(start) == 0
return start
elif func(end) == 0
return end
# if none of range start/end are root and they are both positive or negative.
elif func(start) * func(end) > 0
new Exception("couldn't find root in specified range.") |> throw
else
mid = start + (end - start) / 2
# until we achieve precise equals to 10^-7
while Math.abs(start - mid) > 10 ** -7
if func(mid) == 0
return mid
elif func(mid) * func(start) < 0
end = mid
else
start = mid
mid = start + (end - start) / 2
return mid
# example function
fn f (x: double) -> double
return x ** 3 - 2 * x - 5
Console.write-line(bisection(f, 0, 10))
Early version, doesn't use macros and other extensions. Next version 'll be shorter.
# Target: C#
h = 10
x = 2
y = 2
decorations = '@*+0O'
rnd = new Random()
Console.write-line(''.pad-right(y, `\n`))
# Level 1:
# Tree head - a star.
tree-spaces = h
Console.foreground-color = ConsoleColor.red
Console.write-line(''.pad-right(x + tree-spaces, ` `) + "*")
level-width = 1
tree-spaces--
last-was-decor = false
# Levels 2 to n-1:
# Tree layers
while level-width < h
tree-level-height = 1 if level-width == 1 or level-width == h - 1 else 2
k = 0
while k < tree-level-height
# Leading whitespaces (X position + tree alignment)
Console.write(''.pad-right(x + tree-spaces, ` `))
# Left side /////
r = 0
while r < level-width
if rnd.next(5) == 0 and not last-was-decor
Console.foreground-color = ConsoleColor.cyan
Console.write(decorations[rnd.next(decorations.length)])
last-was-decor = true
else
Console.foreground-color = ConsoleColor.dark-green
Console.write('/')
last-was-decor = false
r++
Console.foreground-color = ConsoleColor.dark-green
# Thin stem
Console.write('|')
# Right side \\\\\
r = 0
while r < level-width
if rnd.next(5) == 0 and not last-was-decor
Console.foreground-color = ConsoleColor.cyan
Console.write(decorations[rnd.next(decorations.length)])
last-was-decor = true
else
Console.foreground-color = ConsoleColor.dark-green
Console.write('\\')
last-was-decor = false
r++
Console.write-line()
k++
level-width++
tree-spaces--
Console.foreground-color = ConsoleColor.dark-yellow
# Stem below the tree
stem-width: int = h / 3
if stem-width % 2 == 0
stem-width++
stem-height: int = 1 + h / 8
stem-spaces: int = level-width - stem-width / 2
l = 0
while l < stem-height
Console.write(''.pad-right(x + stem-spaces, ` `))
Console.write(''.pad-right(stem-width, `|`))
Console.write-line()
l++
Expressions
Public compiler interface
-
Units & Modules
- TODO -
Compiler
class
Backend
- Lexer (Tokenizer)
- Syntax tree
- Traversing