about summary refs log tree commit diff
path: root/pkg/lang/compiler
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-08-16 20:33:41 +0000
committerMel <einebeere@gmail.com>2022-08-16 20:33:41 +0000
commitd84c0f36afcde4e5af2907d70931de75957d5277 (patch)
tree9eb056ca70c6bedf0ee6456baa4c1f7251384463 /pkg/lang/compiler
parent51eb7626b788dc031c53016f203a54a2af762d9e (diff)
downloadjinx-d84c0f36afcde4e5af2907d70931de75957d5277.tar.zst
jinx-d84c0f36afcde4e5af2907d70931de75957d5277.zip
Implement setting values at index in an array
Diffstat (limited to 'pkg/lang/compiler')
-rw-r--r--pkg/lang/compiler/compiler.go15
-rw-r--r--pkg/lang/compiler/compiler_test.go40
2 files changed, 55 insertions, 0 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index d1b98da..193cd90 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -759,6 +759,21 @@ func (comp *Compiler) compileAssignExpr(t *code.Builder, expr ast.ExprBinary) er
 
 		t.AppendOp(code.OpSetMember)
 		t.AppendString(name)
+	case ast.ExprKindSubscription:
+		subscriptionExpr := expr.Left.Value.(ast.ExprSubscription)
+		if err := comp.compileExpr(t, subscriptionExpr.Obj); err != nil {
+			return err
+		}
+
+		if err := comp.compileExpr(t, subscriptionExpr.Key); err != nil {
+			return err
+		}
+
+		if err := comp.compileExpr(t, expr.Right); err != nil {
+			return err
+		}
+
+		t.AppendOp(code.OpSetAtIndex)
 	default:
 		return fmt.Errorf("invalid left-hand side of assignment to %v", expr.Left.Kind)
 	}
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index e40d98e..2e4e6e7 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -629,6 +629,46 @@ func TestGlobals(t *testing.T) {
 	mustCompileTo(t, src, expected)
 }
 
+func TestLvalueAssign(t *testing.T) {
+	src := `
+	var v = 1
+	var obj = null
+	var arr = [1]
+
+	v = 2
+	obj.x = 2
+	arr[0] = 2
+	`
+
+	expected := `
+	push_int 1
+
+	push_null
+
+	push_array
+	get_local 2
+	get_member "push"
+	push_int 1
+	call 1
+	drop 1
+
+	push_int 2
+	set_local 0
+
+	get_local 1
+	push_int 2
+	set_member "x"
+
+	get_local 2
+	push_int 0
+	push_int 2
+	set_at_index
+	halt
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
 func mustCompileTo(t *testing.T, src, expected string) {
 	scanner := scanner.New(strings.NewReader(src))
 	tokens, err := scanner.Scan()