From 83d1dc87f3336d70ccda476627c70c282b7b6e11 Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 27 May 2022 23:34:40 +0000 Subject: Function envs and value escaping --- pkg/lang/vm/vm_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'pkg/lang/vm/vm_test.go') diff --git a/pkg/lang/vm/vm_test.go b/pkg/lang/vm/vm_test.go index 74e64d0..fd4f961 100644 --- a/pkg/lang/vm/vm_test.go +++ b/pkg/lang/vm/vm_test.go @@ -108,6 +108,83 @@ func TestFunction(t *testing.T) { test(t, src, "42") } +func TestSimpleEnv(t *testing.T) { + src := ` + push_int 404 + push_int 1 + push_int 405 + + push_function @test + # Add the local 1 to the environment. + add_to_env 1 + call + halt + + @test: + push_int 2 + get_env 0 + add + ret + ` + + test(t, src, "3") +} + +func TestEscapedEnv(t *testing.T) { + /* + fn create() { + var x = 0 + return fn () { + x = x + 1 + return x + } + } + + var f = create() + var res = [f(), f(), f()] + */ + + src := ` + push_function @create + call + + push_array + + get_local 0 + call + get_local 1 + temp_arr_push + + get_local 0 + call + get_local 1 + temp_arr_push + + get_local 0 + call + get_local 1 + temp_arr_push + halt + + @create: + push_int 0 + push_int 404 + push_int 405 + push_function @create:anon_0 + add_to_env 0 + ret + @create:anon_0: + push_int 1 + get_env 0 + add + set_env 0 + get_env 0 + ret + ` + + test(t, src, "[1, 2, 3]") +} + func test(t *testing.T, src string, expected string) { bc := compile(t, src) vm := vm.New(&bc) -- cgit 1.4.1