Add guide examples to tests

This commit is contained in:
Nicolas Abril 2024-05-22 21:09:14 +02:00
parent ef2198e3c9
commit b0fd8dfb91
42 changed files with 415 additions and 6 deletions

View File

@ -233,7 +233,7 @@ def slow_mul2(n):
case 0:
return 0
case _:
return 2 * slow_mul2(n-1)
return 2 + slow_mul2(n-1)
```
The `if-else` syntax is a third option to branch, other than `match` and
@ -397,7 +397,7 @@ def enum(tree):
rgt: tree.rgt(idx * 2 + 1),
}
case Tree/Leaf:
return (idx, tree.val)
return Tree/Leaf { val: (idx, tree.val) }
def main:
tree = Tree/Node {

View File

@ -0,0 +1,11 @@
type Tree:
Node { ~lft, ~rgt }
Leaf { val }
def main():
bend x = 0:
when x < 3:
tree = Tree/Node { lft: fork(x + 1), rgt: fork(x + 1) }
else:
tree = Tree/Leaf { val: 7 }
return tree

View File

@ -0,0 +1,10 @@
def foo(_):
bend idx = 0:
when idx < 10:
sum = idx + fork(idx + 1)
else:
sum = 0
return sum
def main:
return foo(*)

View File

@ -0,0 +1,7 @@
def main():
bend d = 0, i = 0:
when d < 15: # in the guide its 28, but that takes way too long
sum = fork(d+1, i*2+0) + fork(d+1, i*2+1)
else:
sum = i
return sum

View File

@ -0,0 +1,60 @@
def gen(d, x):
switch d:
case 0:
return x
case _:
return (gen(d-1, x * 2 + 1), gen(d-1, x * 2))
def sum(d, t):
switch d:
case 0:
return t
case _:
(t.a, t.b) = t
return sum(d-1, t.a) + sum(d-1, t.b)
def swap(s, a, b):
switch s:
case 0:
return (a,b)
case _:
return (b,a)
def warp(d, s, a, b):
switch d:
case 0:
return swap(s ^ (a > b), a, b)
case _:
(a.a,a.b) = a
(b.a,b.b) = b
(A.a,A.b) = warp(d-1, s, a.a, b.a)
(B.a,B.b) = warp(d-1, s, a.b, b.b)
return ((A.a,B.a),(A.b,B.b))
def flow(d, s, t):
switch d:
case 0:
return t
case _:
(t.a, t.b) = t
return down(d, s, warp(d-1, s, t.a, t.b))
def down(d,s,t):
switch d:
case 0:
return t
case _:
(t.a, t.b) = t
return (flow(d-1, s, t.a), flow(d-1, s, t.b))
def sort(d, s, t):
switch d:
case 0:
return t
case _:
(t.a, t.b) = t
return flow(d, s, (sort(d-1, 0, t.a), sort(d-1, 1, t.b)))
def main:
# In the guide it's 18
return sum(10, sort(10, 0, gen(10, 0)))

View File

@ -0,0 +1,13 @@
type Shape:
Circle { radius }
Rectangle { width, height }
def area(shape):
match shape:
case Shape/Circle:
return 3.14 * shape.radius ** 2.0
case Shape/Rectangle:
return shape.width * shape.height
def main:
return area(Shape/Circle { radius: 10.0 })

View File

@ -0,0 +1,7 @@
def distance(ax, ay, bx, by):
dx = bx - ax
dy = by - ay
return (dx * dx + dy * dy) ** 0.5
def main():
return distance(10.0, 10.0, 20.0, 20.0)

View File

@ -0,0 +1,11 @@
object V2 { x, y }
def distance(a, b):
open V2: a
open V2: b
dx = b.x - a.x
dy = b.y - a.y
return (dx * dx + dy * dy) ** 0.5
def main():
return distance(V2 { x: 10.0, y: 10.0 }, V2 { x: 20.0, y: 20.0 })

View File

@ -0,0 +1,9 @@
def distance(a, b):
(ax, ay) = a
(bx, by) = b
dx = bx - ax
dy = by - ay
return (dx * dx + dy * dy) ** 0.5
def main():
return distance((10.0, 10.0), (20.0, 20.0))

View File

@ -0,0 +1,21 @@
type Tree:
Node { ~lft, ~rgt }
Leaf { val }
def enum(tree):
idx = 0
fold tree with idx:
case Tree/Node:
return Tree/Node {
lft: tree.lft(idx * 2 + 0),
rgt: tree.rgt(idx * 2 + 1),
}
case Tree/Leaf:
return Tree/Leaf { val: (idx, tree.val) }
def main:
tree = Tree/Node {
lft: Tree/Node { lft: Tree/Leaf { val: 1 }, rgt: Tree/Leaf { val: 2 }, },
rgt: Tree/Node { lft: Tree/Leaf { val: 3 }, rgt: Tree/Leaf { val: 4 }, }
}
return enum(tree)

View File

@ -0,0 +1,8 @@
def am_i_old(age):
if age < 18:
return "you're a kid"
else:
return "you're an adult"
def main():
return am_i_old(32)

View File

@ -0,0 +1,8 @@
def is_even(n):
if n % 2 == 0:
return 1
else:
return 0
def main:
return is_even(7)

View File

@ -0,0 +1,8 @@
def is_even(x):
if x % 2 == 0:
return "even"
else:
return "odd"
def main:
return is_even(7)

View File

@ -0,0 +1,3 @@
def main:
my_list = List/Cons { head: 1, tail: List/Cons { head: 2, tail: List/Cons { head: 3, tail: List/Nil }}}
return my_list

View File

@ -0,0 +1,7 @@
def main:
my_list = [1, 2, 3]
match my_list:
case List/Cons:
return my_list.head
case List/Nil:
return 0

View File

@ -0,0 +1,3 @@
def main:
my_list = [1, 2, 3]
return my_list

View File

@ -0,0 +1,3 @@
def main:
mul_2 = lambda x: x * 2
return mul_2(7)

View File

@ -0,0 +1,9 @@
def slow_mul2(n):
switch n:
case 0:
return 0
case _:
return 2 + slow_mul2(n-1)
def main:
return slow_mul2(7)

View File

@ -0,0 +1,23 @@
# given a shader, returns a square image
def render(depth, shader):
bend d = 0, i = 0:
when d < depth:
color = (fork(d+1, i*2+0), fork(d+1, i*2+1))
else:
width = depth / 2
color = shader(i % width, i / width)
return color
# given a position, returns a color
# for this demo, it just busy loops
def demo_shader(x, y):
bend i = 0:
when i < 10:
color = fork(i + 1)
else:
color = 0x000001
return color
# renders a 256x256 image using demo_shader
def main:
return render(5, demo_shader)

View File

@ -0,0 +1,17 @@
type Tree:
Node { ~lft, ~rgt }
Leaf { val }
def sum(tree):
fold tree:
case Tree/Node:
return tree.lft + tree.rgt
case Tree/Leaf:
return tree.val
def main:
tree = Tree/Node {
lft: Tree/Node { lft: Tree/Leaf { val: 1 }, rgt: Tree/Leaf { val: 2 } },
rgt: Tree/Node { lft: Tree/Leaf { val: 3 }, rgt: Tree/Leaf { val: 4 } }
}
return sum(tree)

View File

@ -4,4 +4,4 @@ input_file: tests/golden_tests/cli/net_size_too_large.bend
---
Errors:
In definition 'Radix':
Definition is too large for hvm (size=144, max size=64). Please break it into smaller pieces.
Definition is too large for hvm (size=120, max size=64). Please break it into smaller pieces.

View File

@ -16,7 +16,7 @@ input_file: tests/golden_tests/cli/no_check_net_size.bend
@Gen.go__C0 = a
& @Arr/Leaf ~ a
@Gen.go__C1 = ({a d} ({$([*2] $([|] $(1 e))) $([*2] b)} g))
@Gen.go__C1 = ({a d} ({$([*2] $([|1] e)) $([*2] b)} g))
& @Arr/Node ~ (c (f g))
&! @Gen.go ~ (a (b c))
&! @Gen.go ~ (d (e f))
@ -71,7 +71,7 @@ input_file: tests/golden_tests/cli/no_check_net_size.bend
@Merge__C9 = ((@Merge__C4 a) a)
@Radix = ({$([&] $(8388608 a)) {$([&] $(4194304 b)) {$([&] $(2097152 c)) {$([&] $(1048576 d)) {$([&] $(524288 e)) {$([&] $(262144 f)) {$([&] $(131072 g)) {$([&] $(65536 h)) {$([&] $(32768 i)) {$([&] $(16384 j)) {$([&] $(8192 k)) {$([&] $(4096 l)) {$([&] $(2048 m)) {$([&] $(1024 n)) {$([&] $(512 o)) {$([&] $(256 p)) {$([&] $(128 q)) {$([&] $(64 r)) {$([&] $(32 s)) {$([&] $(16 t)) {$([&] $(8 u)) {$([&] $(4 v)) {$([&] $(2 w)) $([&] $(1 x))}}}}}}}}}}}}}}}}}}}}}}} vb)
@Radix = ({$([&8388608] a) {$([&4194304] b) {$([&2097152] c) {$([&1048576] d) {$([&524288] e) {$([&262144] f) {$([&131072] g) {$([&65536] h) {$([&32768] i) {$([&16384] j) {$([&8192] k) {$([&4096] l) {$([&2048] m) {$([&1024] n) {$([&512] o) {$([&256] p) {$([&128] q) {$([&64] r) {$([&32] s) {$([&16] t) {$([&8] u) {$([&4] v) {$([&2] w) $([&1] x)}}}}}}}}}}}}}}}}}}}}}}} vb)
& @Swap ~ (a (ub (@Map_/Free vb)))
& @Swap ~ (b (tb (@Map_/Free ub)))
& @Swap ~ (c (sb (@Map_/Free tb)))

View File

@ -3,4 +3,4 @@ source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/op2.bend
---
@main = a
& $(1 $([=2] $([&] $(3 $([|] $(4 $([<5] $([>6] $([:/7] $([*8] $([:-9] $([+10] $([:%11] a))))))))))))) ~ [!0]
& $(1 $([=2] $([&3] $([|4] $([<5] $([>6] $([:/7] $([*8] $([:-9] $([+10] $([:%11] a))))))))))) ~ [!0]

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_bend_7tree.bend
---
NumScott:
λa (a 0 λb (b 0 λc (c 0 λd (d 1 7) λe (e 1 7)) λf (f 0 λg (g 1 7) λh (h 1 7))) λi (i 0 λj (j 0 λk (k 1 7) λl (l 1 7)) λm (m 0 λn (n 1 7) λo (o 1 7))))
Scott:
λa λ* (a λb λ* (b λc λ* (c λ* λd (d 7) λ* λe (e 7)) λf λ* (f λ* λg (g 7) λ* λh (h 7))) λi λ* (i λj λ* (j λ* λk (k 7) λ* λl (l 7)) λm λ* (m λ* λn (n 7) λ* λo (o 7))))

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_bend_sequential.bend
---
NumScott:
45
Scott:
45

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_bend_sum_tree.bend
---
NumScott:
16760832
Scott:
16760832

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_bitonic_sort.bend
---
NumScott:
523776
Scott:
523776

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_circle_area.bend
---
NumScott:
314.000
Scott:
314.000

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_distance_4args.bend
---
NumScott:
14.142
Scott:
14.142

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_distance_obj.bend
---
NumScott:
14.142
Scott:
14.142

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_distance_tup.bend
---
NumScott:
14.142
Scott:
14.142

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_enumerate.bend
---
NumScott:
λa (a 0 λb (b 0 λc (c 1 (0, 1)) λd (d 1 (1, 2))) λe (e 0 λf (f 1 (2, 3)) λg (g 1 (3, 4))))
Scott:
λa λ* (a λb λ* (b λ* λc (c (0, 1)) λ* λd (d (1, 2))) λe λ* (e λ* λf (f (2, 3)) λ* λg (g (3, 4))))

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_if_age.bend
---
NumScott:
"you're an adult"
Scott:
"you're an adult"

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_is_even_num.bend
---
NumScott:
0
Scott:
0

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_is_even_str.bend
---
NumScott:
"odd"
Scott:
"odd"

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_list_ctrs.bend
---
NumScott:
[1, 2, 3]
Scott:
[1, 2, 3]

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_list_match.bend
---
NumScott:
1
Scott:
1

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_list_sugar.bend
---
NumScott:
[1, 2, 3]
Scott:
[1, 2, 3]

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_mul2_inline.bend
---
NumScott:
14
Scott:
14

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_mul2_rec.bend
---
NumScott:
14
Scott:
14

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_shader_dummy.bend
---
NumScott:
(((((1, 1), (1, 1)), ((1, 1), (1, 1))), (((1, 1), (1, 1)), ((1, 1), (1, 1)))), ((((1, 1), (1, 1)), ((1, 1), (1, 1))), (((1, 1), (1, 1)), ((1, 1), (1, 1)))))
Scott:
(((((1, 1), (1, 1)), ((1, 1), (1, 1))), (((1, 1), (1, 1)), ((1, 1), (1, 1)))), ((((1, 1), (1, 1)), ((1, 1), (1, 1))), (((1, 1), (1, 1)), ((1, 1), (1, 1)))))

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/guide_sum.bend
---
NumScott:
10
Scott:
10