about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-06-14 00:15:49 +0000
committerGitHub <noreply@github.com>2022-06-14 00:15:49 +0000
commit6d32e22018bd6b088365c4f771f5163596ba122f (patch)
treecbc69c1dc9be00975e03cffe118f8242f65ff834
parentae56d386c5885f58432f162923462749cc816562 (diff)
downloadjinx-6d32e22018bd6b088365c4f771f5163596ba122f.tar.zst
jinx-6d32e22018bd6b088365c4f771f5163596ba122f.zip
Make Compiler tests pass
-rw-r--r--pkg/lang/compiler/compiler_test.go34
1 files changed, 30 insertions, 4 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 2477b21..5f0cd2c 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -1,6 +1,7 @@
 package compiler_test
 
 import (
+	"fmt"
 	"jinx/pkg/lang/compiler"
 	"jinx/pkg/lang/parser"
 	"jinx/pkg/lang/scanner"
@@ -26,6 +27,29 @@ func TestSimpleAddExpr(t *testing.T) {
 	mustCompileTo(t, src, expected)
 }
 
+func TestOperationOrder(t *testing.T) {
+	grouped := `
+	(((1 + 2) - 3) + 4)
+	`
+
+	free := `
+	1 + 2 - 3 + 4
+	`
+
+	expected := `
+	push_int 4
+	push_int 3
+	push_int 2
+	push_int 1
+	add
+	sub
+	add
+	`
+
+	mustCompileTo(t, grouped, expected)
+	mustCompileTo(t, free, expected)
+}
+
 func TestNestedExpr(t *testing.T) {
 	// Yeah, we don't compile identifiers yet, so here we call and index directly on literals.
 	src := `
@@ -41,18 +65,17 @@ func TestNestedExpr(t *testing.T) {
 	push_string "b"
 	push_string "a"
 	push_int 123
-	call
+	call 3
 
 	add
 
 	push_int 3
-	sub
 
 	push_int 2
-	sub
-
 	push_int 1
 	add
+	sub
+	sub
 	`
 
 	mustCompileTo(t, src, expected)
@@ -67,6 +90,9 @@ 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)