Skip to content

Commit

Permalink
Change tests to curly braces syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Sep 2, 2024
1 parent 7f99ef1 commit 2917e33
Show file tree
Hide file tree
Showing 146 changed files with 1,423 additions and 1,155 deletions.
15 changes: 9 additions & 6 deletions test/autogen.t/closure.smu
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
fun hmm():
fun hmm() {
let a, _ = (1, 0)
fun capture(): a + 1
fun capture() {a + 1}
capture
}

let _, _ = (copy(hmm), 0)

fun hmm_move():
fun hmm_move() {
let a, _ = (1, 0)
-- a is not explicitly copied, thus moved
fun capture(): a + 1
fun capture() {a + 1}
capture
}

let _, _ = (copy(hmm_move), 0)

fun test():
fun test() {
let a = ["hello"]
fun () [a]: println(a.[0])
fun () [a] {println(a.[0])}
}

let c = test()
copy(c)()
5 changes: 3 additions & 2 deletions test/autogen.t/copy_string_lit.smu
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let a = ["aoeu"]
let b& = "aoeu"
string/modify_buf(&b, fun(arr&):
__unsafe_ptr_set(&array/data(arr), 1, !'i'))
string/modify_buf(&b, fun(arr&) {
__unsafe_ptr_set(&array/data(arr), 1, !'i')
})
println(b)
println("aoeu")
println(a.[0])
40 changes: 0 additions & 40 deletions test/autogen.t/records.smu
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
-- (def a 'a')

-- (module a
-- (type t int)
-- (module a
-- (type t float)
-- (def a 10)))

-- (print (fmt-str a))

-- -- TODO This module was indeed used
-- (open a)

-- (print (fmt-str a))
-- (print (fmt-str a/a))

-- TODO nest mutable borrows test

-- TODO allow all expressions for mutable bind, but check in type system
-- (type ease-kind (#linear #circ-in))

-- (defn ease-circ-in [_] 0.0)
-- (defn ease-linear [_] 0.0)

-- (defn ease [anim]
-- (match anim
-- (#linear (ease-linear anim))
-- (#circ-in (ease-circ-in anim))))

-- (defn anim
-- "Progress animation of [ent] along delta time [dt]"
-- [anim dt]
-- (match anim
-- ((#some anim)
-- (copy anim)
-- )
-- (#none #none)))

-- (print (fmt-str (copy "test ") 1))

type t = {c : float, a : string, b : int, d : array[int]}
type cont['a] = {a : 'a}

Expand Down
3 changes: 2 additions & 1 deletion test/autogen.t/variants.smu
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let a = #some(("thing", 0))

match copy(a):
match copy(a) {
#some((t, _)): println(t)
#none: ()
}
11 changes: 7 additions & 4 deletions test/functions.t/closure.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ type capturable = {a& : int}

let a = {a = 10}

fun capture_a():
fun capture_a() {
a.a + 2
}

fun capture_a_wrapped():
fun capture_a_wrapped() {
-- empty function. In order for this to work, the closure
-- needs to be propagated in env
fun wrap():
fun inner(): a.a + 2
fun wrap() {
fun inner() {a.a + 2}
inner()
}
wrap()
}

ignore(capture_a())
capture_a_wrapped()
5 changes: 3 additions & 2 deletions test/functions.t/closure_dtor.smu
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
fun return_closure(name):
fun return_closure(name) {
-- create object on heap
let n = fmt("++", name)
fun (): println(n)
fun () {println(n)}
}

let f = return_closure("aoeu")
f()
37 changes: 23 additions & 14 deletions test/functions.t/closure_inference.smu
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,48 @@ type view = {pbuf : string, start : int, len : int}
type ok = {rem : lexbuf, mtch : view}
type parse_result = #ok(ok) | #err(lexbuf)

fun char(c):
fun buf:
if char_equal(string/get(buf.buf, buf.curr), c):
fun char(c) {
fun buf {
if char_equal(string/get(buf.buf, buf.curr), c) {
#ok({mtch = {pbuf = copy(buf.buf), start = buf.curr, len = 1},
rem = {copy(buf) with curr = buf.curr + 1}})
else:
} else {
#err(copy(buf))
}
}
}

fun string_of_view(view):
fun string_of_view(view) {
let ret& = array/create(view.len + 1)
iter_range(view.start, view.start + view.len, fun c:
array/push(&ret, !string/get(view.pbuf, c)))
iter_range(view.start, view.start + view.len, fun c {
array/push(&ret, !string/get(view.pbuf, c))
})
array/push(&ret, !'\000')
array/drop_back(&ret)
ret
}

fun view_of_lexbuf(lb):
fun view_of_lexbuf(lb) {
{pbuf = copy(lb.buf), start = lb.curr, len = string/len(lb.buf) - lb.curr}
}

fun print_result(res):
match res:
fun print_result(res) {
match res {
#ok({rem, mtch}):
fmt("(\"", string_of_view(view_of_lexbuf(rem)), "\", \"",
string_of_view(mtch), "\")")
#err(lexbuf):
fmt("\"", string_of_view(view_of_lexbuf(lexbuf)), "\", \"")
.println()
}.println()
}

fun alt(a, b):
fun (buf) [a, b]: -- [buf] is polymorphic
match a(buf):
fun alt(a, b) {
fun (buf) [a, b] { -- [buf] is polymorphic
match a(buf) {
#ok(r): #ok(r)
#err(_): b(buf)
}}
}

let lx = {buf = "x", curr = 0}
let li = {buf = "ix", curr = 0}
Expand Down
7 changes: 4 additions & 3 deletions test/functions.t/closure_move_upward.smu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let data& = array/create(16)

iter_range(0, 10, fun i:
array/push(&data, !fun () [i]: println(fmt("on iteration: ", i))))
iter_range(0, 10, fun i {
array/push(&data, !fun () [i] {println(fmt("on iteration: ", i))})
})

array/iter(data, fun f: f())
array/iter(data, fun f {f()})
5 changes: 3 additions & 2 deletions test/functions.t/closures_to_env.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ external printf : (string/cstr, int) -> unit

let a = 20

fun close_over_a(): a
fun close_over_a() {a}

fun use_above():
fun use_above() {
printf(string/data("%i\n"), close_over_a())
}

use_above()
7 changes: 4 additions & 3 deletions test/functions.t/fib.smu
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fun rec fib(n):
if n < 2: n
else: fib(n - 1) + fib(n - 2)
fun rec fib(n) {
if n < 2 {n}
else {fib(n - 1) + fib(n - 2)}
}

println(fmt(fib(30)))
19 changes: 10 additions & 9 deletions test/functions.t/first_class.smu
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
external printi : (int) -> unit

fun apply(x, f): f(x)
fun apply(x, f) {f(x)}

fun add1(x): x + 1
fun add1(x) {x + 1}

-- We also pass polymorphic functions
fun pass(x): copy(x)
fun pass(x) {copy(x)}

-- ..and a lambda
let pass2 = fun x: copy(x)
let pass2 = fun x {copy(x)}

fun makefalse(b):
if b: false else: b
fun makefalse(b) {
if b {false} else {b}
}

fun int_of_bool(b): if b: 1 else: 0
fun int_of_bool(b) {if b {1} else {0}}

-- TODO polymorphic recursion example

apply(0, add1).printi()
apply(1, fun x: x + 1).printi()
apply(1, fun x {x + 1}).printi()
true.apply(makefalse).int_of_bool().printi()
printi(apply(3, fun x: copy(x)))
printi(apply(3, fun x {copy(x)}))
printi(apply(4, pass))
printi(apply(5, pass2))
34 changes: 20 additions & 14 deletions test/functions.t/generic_fun_arg.smu
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,44 @@ external printi : (int) -> unit
type t['a] = {x : 'a}

-- This fn is generic
fun apply(x : 'a, f : ('a) -> 'b):
fun apply(x : 'a, f : ('a) -> 'b) {
f(x)
}

let a = 2

fun add_closed(x):
fun add_closed(x) {
-- we close over
x + a
}

-- simple (int) -> int
fun add1(x): x + 1
fun add1(x) {x + 1}

fun print_bool(b):
if b: printi(1)
else: printi(0)
fun print_bool(b) {
if b {printi(1)}
else {printi(0)}
}

-- simple (bool) -> bool
fun makefalse(b):
if b: false else: b
fun makefalse(b) {
if b {false} else {b}
}

-- bool t -> bool t
fun make_rec_false(r):
if r.x:
fun make_rec_false(r) {
if r.x {
{x = false}
else: r
} else {r}
}

-- simple t -> t
fun add3_rec(t):
fun add3_rec(t) {
{x = t.x + 3}
}

-- A polymorphic function which will get monomorphized
let f = fun x : copy(x)
let f = fun x {copy(x)}

printi(apply(20, add1))
printi(apply(20, add_closed))
Expand All @@ -43,4 +49,4 @@ print_bool(apply({x = true}, make_rec_false).x)
print_bool(apply(true, makefalse))
printi(f({x = 17}).x)
-- inline polymorphic function
18.(fun x: copy(x))().printi()
18.(fun x {copy(x)})().printi()
4 changes: 2 additions & 2 deletions test/functions.t/generic_pass.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ external printi : (int) -> unit

type t = {i : int, b : bool}

fun pass(x): copy(x)
fun apply(f, x): f(x)
fun pass(x) {copy(x)}
fun apply(f, x) {f(x)}

printi(apply(pass, 20))
({i = 700, b = false} |> apply(pass)).i.printi()
13 changes: 7 additions & 6 deletions test/functions.t/if_return_void.smu
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
external printi : (int) -> unit

fun rec foo(i):
if i < 2: printi(i - 1)
else:
if i < 400: printi(i)
else: printi(i + 1)
fun rec foo(i) {
if i < 2 {printi(i - 1)}
else {
if i < 400 {printi(i)}
else {printi(i + 1)}
foo(i - 1)

}
}
foo(4)
10 changes: 6 additions & 4 deletions test/functions.t/indirect_closure.smu
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ external printi : (int) -> unit

type t['a] = {x : 'a}

fun apply(x, f, env): f(x, env)
fun apply(x, f, env) {f(x, env)}

fun apply2(x : 'a, f : ('a, ('a) -> 'b) -> 'b, env : ('a) -> 'b):
fun apply2(x : 'a, f : ('a, ('a) -> 'b) -> 'b, env : ('a) -> 'b) {
f(x, env)
}

fun add1(x : int): x + 1
fun add1(x : int) {x + 1}

fun boxed2int_int(t, env):
fun boxed2int_int(t, env) {
let a = env(t.x)
{x = a}
}

let a = apply({x = 15}, boxed2int_int, add1).x
printi(a)
Expand Down
13 changes: 8 additions & 5 deletions test/functions.t/man_or_boy.smu
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
fun rec a(k, x1, x2, x3, x4, x5):
if k <= 0:
fun rec a(k, x1, x2, x3, x4, x5) {
if k <= 0 {
x4() + x5()
else:
} else {
let m& = !k
fun rec b():
fun rec b() {
&m = m - 1
a(m, b, x1, x2, x3, x4)
}
b ()
}
}

a(10, fun (): 1, fun (): -1, fun (): -1, fun (): 1, fun (): 0).fmt().println()
a(10, fun () {1}, fun () {-1}, fun () {-1}, fun () {1}, fun () {0}).fmt().println()
Loading

0 comments on commit 2917e33

Please sign in to comment.