about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/compiler/compiler_test.go')
-rw-r--r--pkg/lang/compiler/compiler_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 07586a6..9c21a3b 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -486,6 +486,43 @@ func TestFunctionArgs(t *testing.T) {
 	mustCompileTo(t, src, expected)
 }
 
+func TestClosureEnv(t *testing.T) {
+	src := `
+	fn create() {
+		var x = 0
+		fn closure() {
+			x = x + 1
+			return x
+		}
+
+		return closure
+	}
+	`
+
+	expected := `
+	push_function @create
+	halt
+
+	@closure:
+	get_env 0
+	push_int 1
+	add
+	set_env 0
+	get_env 0
+	ret
+
+	@create:
+	push_int 0
+	push_function @closure
+	add_to_env 0
+
+	get_local 1
+	ret
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
 func mustCompileTo(t *testing.T, src, expected string) {
 	scanner := scanner.New(strings.NewReader(src))
 	tokens, err := scanner.Scan()