aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang/compiler
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-06-26 21:54:38 +0200
committerMel <einebeere@gmail.com>2022-06-26 21:54:38 +0200
commit18c7ab70ded45c76abb0b35c090b942a7bfcc3b4 (patch)
tree6505f6197bd276b4a4bfbac3707064492c5626c8 /pkg/lang/compiler
parent621f624f50a7bef16eeed02113b470e79e790cd9 (diff)
downloadjinx-18c7ab70ded45c76abb0b35c090b942a7bfcc3b4.tar.zst
jinx-18c7ab70ded45c76abb0b35c090b942a7bfcc3b4.zip
Change arguments order in VM to match expectation
Diffstat (limited to 'pkg/lang/compiler')
-rw-r--r--pkg/lang/compiler/compiler.go18
-rw-r--r--pkg/lang/compiler/compiler_test.go36
2 files changed, 27 insertions, 27 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index d2b332d..4d7f4a1 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -103,11 +103,11 @@ func (comp *Compiler) compileBinaryExpr(expr ast.ExprBinary) error {
return comp.compileAssignExpr(expr)
}
- if err := comp.compileExpr(expr.Right); err != nil {
+ if err := comp.compileExpr(expr.Left); err != nil {
return err
}
- if err := comp.compileExpr(expr.Left); err != nil {
+ if err := comp.compileExpr(expr.Right); err != nil {
return err
}
@@ -189,16 +189,16 @@ func (comp *Compiler) compileUnaryExpr(expr ast.ExprUnary) error {
}
func (comp *Compiler) compileCallExpr(expr ast.ExprCall) error {
- for i := len(expr.Args) - 1; i >= 0; i-- {
+ if err := comp.compileExpr(expr.Callee); err != nil {
+ return err
+ }
+
+ for i := 0; i < len(expr.Args); i++ {
if err := comp.compileExpr(expr.Args[i]); err != nil {
return err
}
}
- if err := comp.compileExpr(expr.Callee); err != nil {
- return err
- }
-
comp.builder.AppendOp(code.OpCall)
comp.builder.AppendInt(int64(len(expr.Args)))
@@ -206,11 +206,11 @@ func (comp *Compiler) compileCallExpr(expr ast.ExprCall) error {
}
func (comp *Compiler) compileSubscriptionExpr(expr ast.ExprSubscription) error {
- if err := comp.compileExpr(expr.Key); err != nil {
+ if err := comp.compileExpr(expr.Obj); err != nil {
return err
}
- if err := comp.compileExpr(expr.Obj); err != nil {
+ if err := comp.compileExpr(expr.Key); err != nil {
return err
}
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 5347830..cf0fee2 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -19,8 +19,8 @@ func TestSimpleAddExpr(t *testing.T) {
`
expected := `
- push_int 2
push_int 1
+ push_int 2
add
`
@@ -37,12 +37,12 @@ func TestOperationOrder(t *testing.T) {
`
expected := `
- push_int 4
- push_int 3
- push_int 2
push_int 1
+ push_int 2
add
+ push_int 3
sub
+ push_int 4
add
`
@@ -57,25 +57,25 @@ func TestNestedExpr(t *testing.T) {
`
expected := `
- push_int 321
- push_int 456
- index
+ push_int 1
+ push_int 2
+ add
+
+ push_int 3
+ sub
- push_string "c"
- push_string "b"
- push_string "a"
push_int 123
+ push_string "a"
+ push_string "b"
+ push_string "c"
call 3
- add
-
- push_int 3
+ push_int 456
+ push_int 321
+ index
- push_int 2
- push_int 1
add
sub
- sub
`
mustCompileTo(t, src, expected)
@@ -95,13 +95,13 @@ func TestVars(t *testing.T) {
push_int 25
- push_int 7
get_local 0
+ push_int 7
add
set_local 0
- get_local 1
get_local 0
+ get_local 1
add
`