about summary refs log tree commit diff
path: root/pkg/lang/vm/vm_test.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-05-27 23:34:40 +0000
committerGitHub <noreply@github.com>2022-05-27 23:34:40 +0000
commit83d1dc87f3336d70ccda476627c70c282b7b6e11 (patch)
tree70610879d3de1ddf02bbe9067076fe65b52979a9 /pkg/lang/vm/vm_test.go
parent47c4cd3705bee9d7154c42ce95aef6f8a19e0661 (diff)
downloadjinx-83d1dc87f3336d70ccda476627c70c282b7b6e11.tar.zst
jinx-83d1dc87f3336d70ccda476627c70c282b7b6e11.zip
Function envs and value escaping
Diffstat (limited to 'pkg/lang/vm/vm_test.go')
-rw-r--r--pkg/lang/vm/vm_test.go77
1 files changed, 77 insertions, 0 deletions
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)