From d84c0f36afcde4e5af2907d70931de75957d5277 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 16 Aug 2022 20:33:41 +0000 Subject: Implement setting values at index in an array --- pkg/lang/compiler/compiler.go | 15 ++++++++++++++ pkg/lang/compiler/compiler_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) (limited to 'pkg/lang/compiler') 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() -- cgit 1.4.1