about summary refs log tree commit diff
path: root/pkg/lang/compiler
diff options
context:
space:
mode:
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()