From 6d32e22018bd6b088365c4f771f5163596ba122f Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 14 Jun 2022 00:15:49 +0000 Subject: Make Compiler tests pass --- pkg/lang/compiler/compiler_test.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'pkg/lang/compiler') 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) -- cgit 1.4.1