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.go48
1 files changed, 48 insertions, 0 deletions
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()