aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/lang/compiler/compiler.go24
-rw-r--r--pkg/lang/compiler/compiler_test.go26
2 files changed, 49 insertions, 1 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index 0356411..b1fd961 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -197,7 +197,7 @@ func (comp *Compiler) compileExpr(t *code.Builder, expr ast.Expr) error {
case ast.ExprKindFnLit:
panic("not implemented")
case ast.ExprKindArrayLit:
- panic("not implemented")
+ return comp.compileArrayLitExpr(t, expr.Value.(ast.ExprArrayLit))
case ast.ExprKindIdent:
return comp.compileIdentExpr(t, expr.Value.(ast.ExprIdent))
case ast.ExprKindIntLit:
@@ -341,6 +341,28 @@ func (comp *Compiler) compileGroupExpr(t *code.Builder, expr ast.ExprGroup) erro
return comp.compileExpr(t, expr.Value)
}
+func (comp *Compiler) compileArrayLitExpr(t *code.Builder, expr ast.ExprArrayLit) error {
+ t.AppendOp(code.OpPushArray)
+ arrayLocal := comp.scopes.DeclareTemporary()
+
+ for _, value := range expr.Values {
+ t.AppendOp(code.OpGetLocal)
+ t.AppendInt(int64(arrayLocal))
+
+ t.AppendOp(code.OpGetMember)
+ t.AppendString("push")
+
+ if err := comp.compileExpr(t, value); err != nil {
+ return err
+ }
+
+ t.AppendOp(code.OpCall)
+ t.AppendInt(1)
+ }
+
+ return nil
+}
+
func (comp *Compiler) compileIdentExpr(t *code.Builder, expr ast.ExprIdent) error {
symbol, ok := comp.scopes.Lookup(expr.Value.Value)
if !ok {
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 1a759f8..4fd4bac 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -113,6 +113,32 @@ func TestVars(t *testing.T) {
mustCompileTo(t, src, expected)
}
+func TestArrayLit(t *testing.T) {
+ src := `var x = [1, 2, 3]`
+
+ expected := `
+ push_array
+
+ get_local 0
+ get_member "push"
+ push_int 1
+ call 1
+
+ get_local 0
+ get_member "push"
+ push_int 2
+ call 1
+
+ get_local 0
+ get_member "push"
+ push_int 3
+ call 1
+ halt
+ `
+
+ mustCompileTo(t, src, expected)
+}
+
func TestIf(t *testing.T) {
src := `
":("