From 20bd5570465c73b89458de58c9fb8cd4e5919b44 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 26 Jul 2022 03:54:06 +0200 Subject: Compile function environments --- pkg/lang/compiler/compiler_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'pkg/lang/compiler/compiler_test.go') 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() -- cgit 1.4.1