about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler_test.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-03 18:11:55 +0200
committerMel <einebeere@gmail.com>2022-07-03 18:11:55 +0200
commited9a0c8f0f3c1eed3582d722935cd1df1d055afd (patch)
treede68dd7510f03457693593d7dbd8636804f89ded /pkg/lang/compiler/compiler_test.go
parent4774f6373d3e41acba54cb4c63ca51f1b3de2ddd (diff)
downloadjinx-ed9a0c8f0f3c1eed3582d722935cd1df1d055afd.tar.zst
jinx-ed9a0c8f0f3c1eed3582d722935cd1df1d055afd.zip
Compile If Stmts
Diffstat (limited to 'pkg/lang/compiler/compiler_test.go')
-rw-r--r--pkg/lang/compiler/compiler_test.go100
1 files changed, 96 insertions, 4 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index cf0fee2..a6a1ba7 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -1,7 +1,6 @@
 package compiler_test
 
 import (
-	"fmt"
 	"jinx/pkg/lang/compiler"
 	"jinx/pkg/lang/parser"
 	"jinx/pkg/lang/scanner"
@@ -22,6 +21,7 @@ func TestSimpleAddExpr(t *testing.T) {
 	push_int 1
 	push_int 2
 	add
+	halt
 	`
 
 	mustCompileTo(t, src, expected)
@@ -44,6 +44,7 @@ func TestOperationOrder(t *testing.T) {
 	sub
 	push_int 4
 	add
+	halt
 	`
 
 	mustCompileTo(t, grouped, expected)
@@ -76,6 +77,8 @@ func TestNestedExpr(t *testing.T) {
 
 	add
 	sub
+
+	halt
 	`
 
 	mustCompileTo(t, src, expected)
@@ -103,6 +106,98 @@ func TestVars(t *testing.T) {
 	get_local 0
 	get_local 1
 	add
+
+	halt
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
+func TestIf(t *testing.T) {
+	src := `
+	":("
+	if 1 <= 5 {
+		"hello " + "world"
+	}
+	`
+
+	expected := `
+	push_string ":("
+
+	push_int 1
+	push_int 5
+	lte
+
+	jf @end
+
+	push_string "hello "
+	push_string "world"
+	add
+
+	@end:
+	halt
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
+func TestIfElifElse(t *testing.T) {
+	src := `
+		if false {
+			1
+		} elif true {
+			2
+		} else {
+			3
+		}
+	`
+
+	expected := `
+	push_false
+	jf @elif
+
+	push_int 1
+	jmp @end
+
+	@elif:
+	push_true
+	jf @else
+	push_int 2
+	jmp @end
+
+	@else:
+	push_int 3
+
+	@end:
+	halt
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
+func TestIfNoElse(t *testing.T) {
+	src := `
+		if false {
+			1
+		} elif true {
+			2
+		}
+	`
+
+	expected := `
+	push_false
+	jf @elif
+
+	push_int 1
+	jmp @end
+
+	@elif:
+	push_true
+	jf @end
+	push_int 2
+
+	@end:
+	halt
 	`
 
 	mustCompileTo(t, src, expected)
@@ -117,9 +212,6 @@ func mustCompileTo(t *testing.T, src, expected string) {
 	program, err := parser.Parse()
 	require.NoError(t, err)
 
-	// spew.Dump(program)
-	fmt.Printf("%#v\n", program)
-
 	langCompiler := compiler.New(program)
 	testResult, err := langCompiler.Compile()
 	require.NoError(t, err)