From 6aef97b52844b8868e5ada38ce0936619c1077a7 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 25 Jun 2024 01:37:52 +0100 Subject: more function tests --- tests/func2.bth | 6 ++++++ tests/func2.out | 3 +++ tests/func3.bth | 9 +++++++++ tests/func3.out | 2 ++ tests/func4.bth | 7 +++++++ tests/func4.out | 5 +++++ vm.c | 3 +++ 7 files changed, 35 insertions(+) create mode 100644 tests/func2.bth create mode 100644 tests/func2.out create mode 100644 tests/func3.bth create mode 100644 tests/func3.out create mode 100644 tests/func4.bth create mode 100644 tests/func4.out diff --git a/tests/func2.bth b/tests/func2.bth new file mode 100644 index 0000000..fd16eda --- /dev/null +++ b/tests/func2.bth @@ -0,0 +1,6 @@ +(do + (set f1 (fn (a) (* a a))) + (set f2 (fn (a b) (+ a b))) + (puts (f1 6)) + (puts (f2 20 40)) + (f1 (f2 2 1))) diff --git a/tests/func2.out b/tests/func2.out new file mode 100644 index 0000000..db0b45a --- /dev/null +++ b/tests/func2.out @@ -0,0 +1,3 @@ +36 +60 +9 diff --git a/tests/func3.bth b/tests/func3.bth new file mode 100644 index 0000000..2da8cc9 --- /dev/null +++ b/tests/func3.bth @@ -0,0 +1,9 @@ +(do + (set f1 (fn (a b) (* a b))) + (set f2 (fn (x) + (set xx x) + (let (a 3 + g (fn (y) (+ y 7))) + (* (g a) (g xx))))) + (puts (f1 6 6)) + (f2 7)) diff --git a/tests/func3.out b/tests/func3.out new file mode 100644 index 0000000..e846097 --- /dev/null +++ b/tests/func3.out @@ -0,0 +1,2 @@ +36 +140 diff --git a/tests/func4.bth b/tests/func4.bth new file mode 100644 index 0000000..7c04cf3 --- /dev/null +++ b/tests/func4.bth @@ -0,0 +1,7 @@ +(do + (set f1 (fn (x) (+ x 10))) + (set f2 (fn (x) (* x 10))) + (set g (fn (f x) (puts (f x)) (puts (f x)))) + (g f1 26) + (g f2 6) + nil) diff --git a/tests/func4.out b/tests/func4.out new file mode 100644 index 0000000..6a32404 --- /dev/null +++ b/tests/func4.out @@ -0,0 +1,5 @@ +36 +36 +60 +60 +nil diff --git a/vm.c b/vm.c index 6084f21..39bf65d 100644 --- a/vm.c +++ b/vm.c @@ -100,6 +100,9 @@ int runvm(State *S) { } Val v = ht_get(S, &S->globals, AS_STRING(varname)); PUSH(v); + if (IS_NIL(v)) { + printf("warning: nil global read %s\n", AS_CSTRING(varname)); + } break; } case OP_SETGLOBAL: { -- cgit v1.2.3