From 7a60e91c8c45663127cfa3aa31a14c930f717aeb Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 11 Jul 2022 03:00:21 +0200 Subject: Function compilation and minor fixes --- pkg/lang/compiler/compiler_test.go | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 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 f3a20a5..cd62088 100644 --- a/pkg/lang/compiler/compiler_test.go +++ b/pkg/lang/compiler/compiler_test.go @@ -350,6 +350,54 @@ func TestForIn(t *testing.T) { mustCompileTo(t, src, expected) } +func TestSimpleFunction(t *testing.T) { + src := ` + var result = the_meaning_of_life() + + fn the_meaning_of_life() { + return 42 + } + ` + + expected := ` + push_function @the_meaning_of_life + call 0 + halt + + @the_meaning_of_life: + push_int 42 + ret + ` + + mustCompileTo(t, src, expected) +} + +func TestFunctionArgs(t *testing.T) { + src := ` + fn add(a, b) { + return a + b + } + + add(4, 5) + ` + + expected := ` + push_function @add + push_int 4 + push_int 5 + call 2 + halt + + @add: + get_local 0 + get_local 1 + add + 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