From c610f88a5bde59ad160b262d99ce027e9c255a19 Mon Sep 17 00:00:00 2001 From: vircoys Date: Mon, 24 Jul 2023 16:27:26 +0800 Subject: [PATCH] feat: Add some operators and change assignment expr into assignment stmt (#38) --- pkg/ast/ast.go | 17 +- pkg/ast/expr.go | 27 +- pkg/engine/op_test.go | 7 +- pkg/engine/runtime/context.go | 1 - pkg/engine/runtime/runtime.go | 247 +++++- pkg/engine/runtime/runtime_test.go | 169 +++- pkg/engine/runtime/stack.go | 2 - pkg/inimpl/guancecloud/funcs/utilts_test.go | 18 +- pkg/parser/gram.y | 796 ++++++++++-------- pkg/parser/gram_y.go | 881 +++++++++++--------- pkg/parser/lex.go | 84 +- pkg/parser/parser.go | 34 +- pkg/parser/parser_test.go | 204 ++++- 13 files changed, 1672 insertions(+), 815 deletions(-) diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 6cf3d9e..3e78059 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -36,6 +36,7 @@ const ( TypeAttrExpr TypeIndexExpr + TypeUnaryExpr TypeArithmeticExpr TypeConditionalExpr TypeAssignmentExpr @@ -79,6 +80,8 @@ func (t NodeType) String() string { return "AttrExpr" case TypeIndexExpr: return "IndexExpr" + case TypeUnaryExpr: + return "UnaryExpr" case TypeArithmeticExpr: return "ArithmeticExpr" case TypeConditionalExpr: @@ -153,6 +156,7 @@ type Node struct { IndexExpr *IndexExpr InExpr *InExpr + UnaryExpr *UnaryExpr ArithmeticExpr *ArithmeticExpr ConditionalExpr *ConditionalExpr AssignmentExpr *AssignmentExpr @@ -193,6 +197,8 @@ func (node *Node) String() string { return node.AttrExpr.String() case TypeIndexExpr: return node.IndexExpr.String() + case TypeUnaryExpr: + return node.UnaryExpr.String() case TypeArithmeticExpr: return node.ArithmeticExpr.String() case TypeConditionalExpr: @@ -315,7 +321,14 @@ func WrapInExpr(node *InExpr) *Node { } } -func WrapAssignmentExpr(node *AssignmentExpr) *Node { +func WrapUnaryExpr(node *UnaryExpr) *Node { + return &Node{ + NodeType: TypeUnaryExpr, + UnaryExpr: node, + } +} + +func WrapAssignmentStmt(node *AssignmentExpr) *Node { return &Node{ NodeType: TypeAssignmentExpr, AssignmentExpr: node, @@ -409,6 +422,8 @@ func NodeStartPos(node *Node) token.LnColPos { case TypeIndexExpr: return node.IndexExpr.Obj.Start + case TypeUnaryExpr: + return node.UnaryExpr.OpPos case TypeArithmeticExpr: return node.ArithmeticExpr.LHS.StartPos() case TypeConditionalExpr: diff --git a/pkg/ast/expr.go b/pkg/ast/expr.go index 2327347..87f29b0 100644 --- a/pkg/ast/expr.go +++ b/pkg/ast/expr.go @@ -35,8 +35,14 @@ const ( AND Op = "&&" OR Op = "||" - - EQ Op = "=" + NOT Op = "!" + + EQ Op = "=" + ADDEQ Op = "+=" + SUBEQ Op = "-=" + MULEQ Op = "*=" + DIVEQ Op = "/=" + MODEQ Op = "%=" ) type Identifier struct { @@ -160,6 +166,20 @@ func (e *ListInitExpr) String() string { return "[" + strings.Join(arr, ", ") + "]" } +type UnaryExpr struct { + Op Op + RHS *Node + OpPos token.LnColPos +} + +func (e *UnaryExpr) IsExpr() bool { + return true +} + +func (e *UnaryExpr) String() string { + return fmt.Sprintf("%s%s", e.Op, e.RHS.String()) +} + type ConditionalExpr struct { Op Op LHS, RHS *Node @@ -296,6 +316,7 @@ func (e *CallExpr) String() string { type AssignmentExpr struct { LHS, RHS *Node + Op Op OpPos token.LnColPos } @@ -304,5 +325,5 @@ func (e *AssignmentExpr) IsExpr() bool { } func (e *AssignmentExpr) String() string { - return fmt.Sprintf("%s = %s", e.LHS, e.RHS) + return fmt.Sprintf("%s %s %s", e.LHS, e.Op, e.RHS) } diff --git a/pkg/engine/op_test.go b/pkg/engine/op_test.go index 7d61ea5..0ae8f77 100644 --- a/pkg/engine/op_test.go +++ b/pkg/engine/op_test.go @@ -29,9 +29,12 @@ func TestOp(t *testing.T) { f = map_a["a"] - aaaa = 1.0 == (b = 1) + b = 1 - a = v = a + aaaa = 1.0 == (b ) + + v = a + a = v x7 = [1, 2.1, "3"] if b == 2 { x = 2 diff --git a/pkg/engine/runtime/context.go b/pkg/engine/runtime/context.go index 94a0010..9d6eaf3 100644 --- a/pkg/engine/runtime/context.go +++ b/pkg/engine/runtime/context.go @@ -186,7 +186,6 @@ func (ctx *Context) GetKey(key string) (*Varb, error) { case InRMap: if v, t, err := ctx.inRMap.Get(key); err == nil { return &Varb{ - Name: key, Value: v, DType: t, }, nil diff --git a/pkg/engine/runtime/runtime.go b/pkg/engine/runtime/runtime.go index cb4f98b..6c16354 100644 --- a/pkg/engine/runtime/runtime.go +++ b/pkg/engine/runtime/runtime.go @@ -13,6 +13,7 @@ import ( "github.com/GuanceCloud/platypus/pkg/ast" "github.com/GuanceCloud/platypus/pkg/errchain" + "github.com/GuanceCloud/platypus/pkg/token" "github.com/spf13/cast" ) @@ -359,6 +360,8 @@ func RunStmt(ctx *Context, node *ast.Node) (any, ast.DType, *errchain.PlError) { return RunArithmeticExpr(ctx, node.ArithmeticExpr) case ast.TypeConditionalExpr: return RunConditionExpr(ctx, node.ConditionalExpr) + case ast.TypeUnaryExpr: + return RunUnaryExpr(ctx, node.UnaryExpr) case ast.TypeAssignmentExpr: return RunAssignmentExpr(ctx, node.AssignmentExpr) case ast.TypeCallExpr: @@ -414,6 +417,104 @@ func RunStmt(ctx *Context, node *ast.Node) (any, ast.DType, *errchain.PlError) { } } +func RunUnaryExpr(ctx *Context, expr *ast.UnaryExpr) (any, ast.DType, *errchain.PlError) { + switch expr.Op { + case ast.SUB, ast.ADD: + v, dtype, err := RunStmt(ctx, expr.RHS) + if err != nil { + return nil, ast.Invalid, err + } + switch dtype { + case ast.Bool: + val, _ := v.(bool) + if expr.Op == ast.SUB { + if val { + return int64(-1), ast.Int, nil + } else { + return 0, ast.Int, nil + } + } else { + if val { + return int64(1), ast.Int, nil + } else { + return 0, ast.Int, nil + } + } + case ast.Float: + val, _ := v.(float64) + if expr.Op == ast.SUB { + return -val, ast.Float, nil + } else { + return val, ast.Float, nil + } + case ast.Int: + val, _ := v.(int64) + if expr.Op == ast.SUB { + return -val, ast.Int, nil + } else { + return val, ast.Int, nil + } + default: + return nil, ast.Invalid, NewRunError(ctx, + fmt.Sprintf("unsuppored operand type for unary op %s: %s", + expr.Op, reflect.TypeOf(expr).String()), expr.OpPos) + } + + case ast.NOT: + v, _, err := RunStmt(ctx, expr.RHS) + if err != nil { + return nil, ast.Invalid, err + } + + if v == nil { + return true, ast.Bool, nil + } + + switch v := v.(type) { + case bool: + return !v, ast.Bool, nil + case float64: + if v == 0 { + return true, ast.Bool, nil + } else { + return false, ast.Bool, nil + } + case int64: + if v == 0 { + return true, ast.Bool, nil + } else { + return false, ast.Bool, nil + } + case string: + if len(v) == 0 { + return true, ast.Bool, nil + } else { + return false, ast.Bool, nil + } + case map[string]any: + if len(v) == 0 { + return true, ast.Bool, nil + } else { + return false, ast.Bool, nil + } + case []any: + if len(v) == 0 { + return true, ast.Bool, nil + } else { + return false, ast.Bool, nil + } + + default: + return nil, ast.Invalid, NewRunError(ctx, + fmt.Sprintf("unsuppored operand type for unary op %s: %s", + expr.Op, reflect.TypeOf(expr).String()), expr.OpPos) + } + default: + return nil, ast.Invalid, NewRunError(ctx, + fmt.Sprintf("unsupported op for unary expr: %s", expr.Op), expr.OpPos) + } +} + func RunListInitExpr(ctx *Context, expr *ast.ListInitExpr) (any, ast.DType, *errchain.PlError) { ret := []any{} for _, v := range expr.List { @@ -639,16 +740,17 @@ func RunArithmeticExpr(ctx *Context, expr *ast.ArithmeticExpr) (any, ast.DType, if errOpInt != nil { return nil, ast.Invalid, errOpInt } - if !arithType(lhsValType) { - return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( - "unsupported lhs data type: %s", lhsValType), expr.OpPos) - } rhsVal, rhsValType, errOpInt := RunStmt(ctx, expr.RHS) if errOpInt != nil { return nil, ast.Invalid, errOpInt } + if !arithType(lhsValType) { + return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( + "unsupported lhs data type: %s", lhsValType), expr.OpPos) + } + if !arithType(rhsValType) { return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( "unsupported rhs data type: %s", rhsValType), expr.OpPos) @@ -689,23 +791,127 @@ func RunArithmeticExpr(ctx *Context, expr *ast.ArithmeticExpr) (any, ast.DType, return v, dtype, nil } +func runAssignArith(ctx *Context, l, r *Varb, op ast.Op, pos token.LnColPos) ( + any, ast.DType, *errchain.PlError) { + + arithOp, ok := assign2arithOp(op) + if !ok { + return nil, ast.Invalid, NewRunError(ctx, + "unsupported op", pos) + } + + if !arithType(l.DType) { + return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( + "unsupported lhs data type: %s", l.DType), pos) + } + + if !arithType(r.DType) { + return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( + "unsupported rhs data type: %s", r.DType), pos) + } + + // string + if l.DType == ast.String || r.DType == ast.String { + if arithOp != ast.ADD { + return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( + "unsupported operand type(s) for %s: %s and %s", + op, l.DType, r.DType), pos) + } + if l.DType == ast.String && r.DType == ast.String { + return cast.ToString(l.Value) + cast.ToString(r.Value), ast.String, nil + } else { + return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( + "unsupported operand type(s) for %s: %s and %s", + op, l.DType, r.DType), pos) + } + } + + // float + if l.DType == ast.Float || r.DType == ast.Float { + v, dtype, err := arithOpFloat(cast.ToFloat64(l.Value), cast.ToFloat64(r.Value), arithOp) + if err != nil { + return nil, ast.Invalid, NewRunError(ctx, err.Error(), pos) + } + return v, dtype, nil + } + + // bool or int + + v, dtype, errOp := arithOpInt(cast.ToInt64(l.Value), cast.ToInt64(r.Value), arithOp) + + if errOp != nil { + return nil, ast.Invalid, NewRunError(ctx, errOp.Error(), pos) + } + return v, dtype, nil +} + func RunAssignmentExpr(ctx *Context, expr *ast.AssignmentExpr) (any, ast.DType, *errchain.PlError) { v, dtype, err := RunStmt(ctx, expr.RHS) if err != nil { return nil, ast.Invalid, err } + rVarb := &Varb{Value: v, DType: dtype} switch expr.LHS.NodeType { //nolint:exhaustive case ast.TypeIdentifier: - _ = ctx.SetVarb(expr.LHS.Identifier.Name, v, dtype) - return v, dtype, nil + switch expr.Op { + case ast.EQ: + _ = ctx.SetVarb(expr.LHS.Identifier.Name, v, dtype) + return v, dtype, nil + + case ast.SUBEQ, + ast.ADDEQ, + ast.MULEQ, + ast.DIVEQ, + ast.MODEQ: + lVarb, err := ctx.GetKey(expr.LHS.Identifier.Name) + if err != nil { + return nil, ast.Nil, nil + } + if v, dt, errR := runAssignArith(ctx, lVarb, rVarb, expr.Op, expr.OpPos); errR != nil { + return nil, ast.Void, errR + } else { + _ = ctx.SetVarb(expr.LHS.Identifier.Name, v, dt) + return v, dt, nil + } + + default: + return nil, ast.Invalid, NewRunError(ctx, + "unsupported op", expr.OpPos) + } case ast.TypeIndexExpr: - varb, err := ctx.GetKey(expr.LHS.IndexExpr.Obj.Name) - if err != nil { - return nil, ast.Invalid, NewRunError(ctx, err.Error(), expr.LHS.IndexExpr.Obj.Start) + switch expr.Op { + case ast.EQ: + varb, err := ctx.GetKey(expr.LHS.IndexExpr.Obj.Name) + if err != nil { + return nil, ast.Invalid, NewRunError(ctx, err.Error(), expr.LHS.IndexExpr.Obj.Start) + } + return changeListOrMapValue(ctx, varb.Value, expr.LHS.IndexExpr.Index, + v, dtype) + case ast.ADDEQ, + ast.SUBEQ, + ast.MULEQ, + ast.DIVEQ, + ast.MODEQ: + varb, err := ctx.GetKey(expr.LHS.IndexExpr.Obj.Name) + if err != nil { + return nil, ast.Invalid, NewRunError(ctx, err.Error(), expr.LHS.IndexExpr.Obj.Start) + } + if v, dt, errR := searchListAndMap(ctx, varb.Value, expr.LHS.IndexExpr.Index); err != nil { + return nil, ast.Invalid, errR + } else { + v, dt, err := runAssignArith(ctx, &Varb{Value: v, DType: dt}, rVarb, expr.Op, expr.OpPos) + if err != nil { + return nil, ast.Invalid, err + } + return changeListOrMapValue(ctx, varb.Value, expr.LHS.IndexExpr.Index, + v, dt) + } + default: + return nil, ast.Invalid, NewRunError(ctx, + "unsupported op", expr.OpPos) } - return changeListOrMapValue(ctx, varb.Value, expr.LHS.IndexExpr.Index, - v, dtype) + default: return nil, ast.Void, nil } @@ -906,6 +1112,23 @@ func cmpType(dtype ast.DType) bool { return false } +func assign2arithOp(op ast.Op) (ast.Op, bool) { + switch op { + case ast.ADDEQ: + return ast.ADD, true + case ast.SUBEQ: + return ast.SUB, true + case ast.MULEQ: + return ast.MUL, true + case ast.DIVEQ: + return ast.DIV, true + case ast.MODEQ: + return ast.MOD, true + default: + return "", false + } +} + func arithType(dtype ast.DType) bool { switch dtype { //nolint:exhaustive case ast.Int, ast.Float, ast.Bool, ast.String: @@ -952,6 +1175,6 @@ func arithOpFloat(l float64, r float64, op ast.Op) (float64, ast.DType, error) { } return l / r, ast.Float, nil default: - return 0, ast.Invalid, fmt.Errorf("unsupported op: %s", op) + return 0, ast.Invalid, fmt.Errorf("float does not support modulo operations") } } diff --git a/pkg/engine/runtime/runtime_test.go b/pkg/engine/runtime/runtime_test.go index 3c7edca..21e9d52 100644 --- a/pkg/engine/runtime/runtime_test.go +++ b/pkg/engine/runtime/runtime_test.go @@ -59,10 +59,11 @@ func TestRuntime(t *testing.T) { f = map_a["nil"][-1] + b = 1 + aaaa = 1.0 == b - aaaa = 1.0 == (b = 1) - - a = v = a + v = a + a = v x7 = [1, 2.1, "3"] if b == 2 { x = 2 @@ -346,6 +347,168 @@ func TestInExpr(t *testing.T) { }, inData.data) } +func TestUnaryAndAssignOP(t *testing.T) { + cases := []struct { + name string + pl string + v map[string]any + }{ + { + name: "unary op", + pl: ` + add_key("t1", !nil) + add_key("t2", !"") + add_key("t3", !-1) + add_key("t4", !1.) + add_key("t5", !false) + add_key("t6", !(true + false)) + add_key("t7", !!0) + add_key("t8", !!!"abv") + add_key("t9", --!"") + add_key("t10", -+++-!"" ) + add_key("t11", -+++-!"" + +1 + -1 ++++1) + add_key("t12", -(1.1+0)) + add_key("t12_1", -(0.0)) + add_key("t13", +(1.1)) + add_key("t14", +1) + add_key("t15", -true) + add_key("t16", +true) + add_key("t17", -false) + add_key("t18", +false) + v19 = {} + v19_1 = {"a":1} + add_key("t19", !{}) + add_key("t20", !v19) + add_key("t21", !v19_1) + add_key("t21_1", ! {"a": 1}) + + v22 = [] + v22_1 = [1] + add_key("t22", ![]) + add_key("t23", !v22) + add_key("t24", !v22_1) + add_key("t24_1", ![1]) + `, + v: map[string]any{ + "t1": true, + "t2": true, + "t3": false, + "t4": false, + "t5": true, + "t6": false, + "t7": false, + "t8": false, + "t9": int64(1), + "t10": int64(1), + "t11": int64(2), + "t12": float64(-1.1), + "t12_1": float64(0), + "t13": float64(1.1), + "t14": int64(1), + "t15": int64(-1), + "t16": int64(1), + "t17": int64(0), + "t18": int64(0), + "t19": true, + "t20": true, + "t21": false, + "t21_1": false, + "t22": true, + "t23": true, + "t24": false, + "t24_1": false, + }, + }, + { + name: "assign", + pl: ` + v1 = 1 + v1 += 1 + add_key(v1) + v2 = 1.1 + v2 += 1 + add_key(v2) + v3 = "" + v3 += "aaa" + "b" + v3 += "bb" + add_key(v3) + v4 = [true,2,2] + v4[2] += 1 + add_key(v4) + v4[0] += 1 + v5 = v4[0] + add_key(v5) + v6 = 1 + v6 -=2 + add_key(v6) + v7 = 1.1 + v7 *= 2 + add_key(v7) + v8 = 10 + v8 %= 2 + add_key(v8) + v9 = 1 + v9 -= 2.0 + add_key(v9) + v10 = [1, 2,3 ,{"a": 2, "c": [5.0]}] + v10[3]["c"][0] /= 2 + add_key(v10) + `, + v: map[string]any{ + "v1": int64(2), + "v2": float64(2.1), + "v3": "aaabbb", + "v4": "[true,2,3]", + "v5": int64(2), + "v6": int64(-1), + "v7": float64(2.2), + "v8": int64(0), + "v9": float64(-1.0), + "v10": "[1,2,3,{\"a\":2,\"c\":[2.5]}]", + }, + }, + } + + for _, v := range cases { + t.Run(v.name, func(t *testing.T) { + stmts, err := parseScript(v.pl) + if err != nil { + t.Fatal(err) + } + script := &Script{ + CallRef: nil, + FuncCall: map[string]FuncCall{ + "test": callexprtest, + "add_key": addkeytest, + "len": lentest, + }, + Name: "abc", + Namespace: "default", + Category: "", + FilePath: "", + Content: v.pl, + Ast: stmts, + } + errR := CheckScript(script, map[string]FuncCheck{ + "add_key": addkeycheck, + "len": lencheck, + }) + if errR != nil { + t.Fatal(*errR) + } + + inData := &inputImpl{ + data: map[string]any{}, + } + + errR = RunScriptWithRMapIn(script, inData, nil) + if errR != nil { + t.Fatal(errR.Error()) + } + assert.Equal(t, v.v, inData.data) + }) + } +} func TestCondTrue(t *testing.T) { cases := []struct { val any diff --git a/pkg/engine/runtime/stack.go b/pkg/engine/runtime/stack.go index 8f06351..6c83f1a 100644 --- a/pkg/engine/runtime/stack.go +++ b/pkg/engine/runtime/stack.go @@ -13,7 +13,6 @@ import ( ) type Varb struct { - Name string Value any DType ast.DType } @@ -71,7 +70,6 @@ func (stack *PlProcStack) Set(key string, value any, dType ast.DType) { // new stack.data[key] = &Varb{ - Name: key, Value: value, DType: dType, } diff --git a/pkg/inimpl/guancecloud/funcs/utilts_test.go b/pkg/inimpl/guancecloud/funcs/utilts_test.go index 7162381..9a19c5f 100644 --- a/pkg/inimpl/guancecloud/funcs/utilts_test.go +++ b/pkg/inimpl/guancecloud/funcs/utilts_test.go @@ -45,7 +45,7 @@ func TestReIndexFuncArgs(t *testing.T) { reqParm: 2, fnArgs: &ast.CallExpr{ Param: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), }), @@ -61,7 +61,7 @@ func TestReIndexFuncArgs(t *testing.T) { fnArgs: &ast.CallExpr{ Param: []*ast.Node{ ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "x"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), @@ -77,7 +77,7 @@ func TestReIndexFuncArgs(t *testing.T) { reqParm: 2, fnArgs: &ast.CallExpr{ Param: []*ast.Node{ - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), @@ -93,13 +93,13 @@ func TestReIndexFuncArgs(t *testing.T) { reqParm: 2, fnArgs: &ast.CallExpr{ Param: []*ast.Node{ - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), }, ), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), @@ -122,13 +122,13 @@ func TestReIndexFuncArgs(t *testing.T) { fnArgs: &ast.CallExpr{ Param: []*ast.Node{ ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), }, ), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), @@ -151,7 +151,7 @@ func TestReIndexFuncArgs(t *testing.T) { fnArgs: &ast.CallExpr{ Param: []*ast.Node{ ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), @@ -174,7 +174,7 @@ func TestReIndexFuncArgs(t *testing.T) { fnArgs: &ast.CallExpr{ Param: []*ast.Node{ ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), - ast.WrapAssignmentExpr( + ast.WrapAssignmentStmt( &ast.AssignmentExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), diff --git a/pkg/parser/gram.y b/pkg/parser/gram.y index 334110f..80fba10 100644 --- a/pkg/parser/gram.y +++ b/pkg/parser/gram.y @@ -33,9 +33,11 @@ import ( // operator %token operatorsStart %token ADD - DIV GTE GT + DIV GTE GT NOT LT LTE MOD MUL NEQ EQ EQEQ SUB + ADD_EQ SUB_EQ DIV_EQ + MUL_EQ MOD_EQ %token operatorsEnd // keywords @@ -53,8 +55,8 @@ NIL NULL IF ELIF ELSE //////////////////////////////////////////////////// // grammar rules //////////////////////////////////////////////////// -%type - unary_op +/* %type + unary_op */ %type @@ -78,16 +80,18 @@ NIL NULL IF ELIF ELSE %type stmt - assignment_expr + assignment_stmt for_in_stmt for_stmt continue_stmt break_stmt ifelse_stmt call_expr + named_arg %type identifier + unary_expr binary_expr conditional_expr arithmeticExpr @@ -100,87 +104,146 @@ NIL NULL IF ELIF ELSE map_init_start list_init list_init_start - array_elem - bool_literal - string_literal - nil_literal - number_literal + basic_literal + for_stmt_elem + /* bool_literal */ + /* string_literal */ + /* nil_literal */ + /* number_literal */ value_stmt //columnref %start start // operator listed with increasing precedence -%right EQ +%right EQ SUB_EQ ADD_EQ MUL_EQ DIV_EQ MOD_EQ %left OR %left AND %left IN %left GTE GT NEQ EQEQ LTE LT %left ADD SUB %left MUL DIV MOD - +%right NOT UMINUS +%left LEFT_BRACKET RIGHT_BRACKET LEFT_PAREN RIGHT_PAREN DOT %% -sep : SEMICOLON - | EOL - | sep SEMICOLON - | sep EOL +sep: SEMICOLON +| EOL +| sep SEMICOLON +| sep EOL +; -start : START_STMTS stmts - { yylex.(*parser).parseResult = $2 } - | start EOF - | error - { yylex.(*parser).unexpected("", "") } - ; +start: START_STMTS stmts +{ + yylex.(*parser).parseResult = $2 +} +| start EOF +| error +{ + yylex.(*parser).unexpected("", "") +} +; stmts: stmts_list stmt - { - s := $1 - s = append(s, $2) - $$ = s - } - | stmts_list - | stmt - { $$ = ast.Stmts{$1} } - ; - -stmts_list : stmt sep - { $$ = ast.Stmts{$1} } - | sep - { $$ = ast.Stmts{} } - | stmts_list stmt sep - { - s := $1 - s = append(s, $2) - $$ = s - } - ; - -stmt : ifelse_stmt - | for_in_stmt - | for_stmt - | continue_stmt - | break_stmt - | value_stmt - ; +{ + s := $1 + s = append(s, $2) + $$ = s +} +| stmts_list +| stmt +{ + $$ = ast.Stmts{$1} +} +; + +stmts_list: stmt sep +{ + $$ = ast.Stmts{$1} +} +| sep +{ + $$ = ast.Stmts{} +} +| stmts_list stmt sep +{ + s := $1 + s = append(s, $2) + $$ = s +} +; +stmt: ifelse_stmt +| for_in_stmt +| for_stmt +| continue_stmt +| break_stmt +| value_stmt +| assignment_stmt +; + +/* expression */ +expr: basic_literal +| list_init +| map_init +| paren_expr +| call_expr +| unary_expr +| binary_expr +| attr_expr +| index_expr +| in_expr +| identifier +; // arithmeticExpr value_stmt: expr - ; +; -/* expression */ -expr : array_elem | list_init | map_init | paren_expr | call_expr | binary_expr | attr_expr | index_expr | in_expr; // arithmeticExpr +assignment_stmt: expr EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +| expr ADD_EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +| expr SUB_EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +| expr MUL_EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +| expr DIV_EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +| expr MOD_EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +; +in_expr: expr IN expr +{ + $$ = yylex.(*parser).newInExpr($1, $3, $2) +} + break_stmt: BREAK - { $$ = yylex.(*parser).newBreakStmt($1.Pos) } - ; +{ + $$ = yylex.(*parser).newBreakStmt($1.Pos) +} +; continue_stmt: CONTINUE - { $$ = yylex.(*parser).newContinueStmt($1.Pos) } - ; +{ + $$ = yylex.(*parser).newContinueStmt($1.Pos) +} +; /* for identifier IN identifier @@ -188,326 +251,383 @@ continue_stmt: CONTINUE for identifier IN string */ for_in_stmt : FOR in_expr stmt_block - { $$ = yylex.(*parser).newForInStmt($2, $3, $1) } - ; - +{ + $$ = yylex.(*parser).newForInStmt($2, $3, $1) +} +; /* - for init expr; cond expr; loop expr block_smt - for init expr; cond expr; block_stmt - for ; cond expr; loop expr block_stmt - for ; cond expr; block_stmt + for init ; cond expr; loop { stmts } + for init ; cond expr; { stmts } + for ; cond expr; loop { stmts } + for ; cond expr; { stmts } */ -for_stmt : FOR expr SEMICOLON expr SEMICOLON expr stmt_block - { $$ = yylex.(*parser).newForStmt($2, $4, $6, $7) } - | FOR expr SEMICOLON expr SEMICOLON stmt_block - { $$ = yylex.(*parser).newForStmt($2, $4, nil, $6) } - | FOR SEMICOLON expr SEMICOLON expr stmt_block - { $$ = yylex.(*parser).newForStmt(nil, $3, $5, $6) } - | FOR SEMICOLON expr SEMICOLON stmt_block - { $$ = yylex.(*parser).newForStmt(nil, $3, nil, $5) } - - | FOR expr SEMICOLON SEMICOLON expr stmt_block - { $$ = yylex.(*parser).newForStmt($2, nil, $5, $6) } - | FOR expr SEMICOLON SEMICOLON stmt_block - { $$ = yylex.(*parser).newForStmt($2, nil, nil, $5) } - | FOR SEMICOLON SEMICOLON expr stmt_block - { $$ = yylex.(*parser).newForStmt(nil, nil, $4, $5) } - | FOR SEMICOLON SEMICOLON stmt_block - { $$ = yylex.(*parser).newForStmt(nil, nil, nil, $4) } - ; +for_stmt : FOR for_stmt_elem SEMICOLON expr SEMICOLON for_stmt_elem stmt_block +{ + $$ = yylex.(*parser).newForStmt($2, $4, $6, $7) +} +| FOR for_stmt_elem SEMICOLON expr SEMICOLON stmt_block +{ + $$ = yylex.(*parser).newForStmt($2, $4, nil, $6) +} +| FOR SEMICOLON expr SEMICOLON for_stmt_elem stmt_block +{ + $$ = yylex.(*parser).newForStmt(nil, $3, $5, $6) +} +| FOR SEMICOLON expr SEMICOLON stmt_block +{ + $$ = yylex.(*parser).newForStmt(nil, $3, nil, $5) +} +| FOR for_stmt_elem SEMICOLON SEMICOLON for_stmt_elem stmt_block +{ + $$ = yylex.(*parser).newForStmt($2, nil, $5, $6) +} +| FOR for_stmt_elem SEMICOLON SEMICOLON stmt_block +{ + $$ = yylex.(*parser).newForStmt($2, nil, nil, $5) +} +| FOR SEMICOLON SEMICOLON for_stmt_elem stmt_block +{ + $$ = yylex.(*parser).newForStmt(nil, nil, $4, $5) +} +| FOR SEMICOLON SEMICOLON stmt_block +{ + $$ = yylex.(*parser).newForStmt(nil, nil, nil, $4) +} +; + +for_stmt_elem: expr | assignment_stmt +; ifelse_stmt: if_elif_list - { - $$ = yylex.(*parser).newIfElifStmt($1) - } - | if_elif_list ELSE stmt_block - { - $$ = yylex.(*parser).newIfElifelseStmt($1, $2, $3) - } - ; +{ + $$ = yylex.(*parser).newIfElifStmt($1) +} +| if_elif_list ELSE stmt_block +{ + $$ = yylex.(*parser).newIfElifelseStmt($1, $2, $3) +} +; if_elem: IF expr stmt_block - { $$ = yylex.(*parser).newIfElem($1, $2, $3) } - ; +{ + $$ = yylex.(*parser).newIfElem($1, $2, $3) +} +; if_elif_list: if_elem - { $$ = []*ast.IfStmtElem{ $1 } } - | if_elif_list elif_elem - { $$ = append($1, $2) } - ; +{ + $$ = []*ast.IfStmtElem{ $1 } +} +| if_elif_list elif_elem +{ + $$ = append($1, $2) +} +; elif_elem: ELIF expr stmt_block - { $$ = yylex.(*parser).newIfElem($1, $2, $3) } - ; +{ + $$ = yylex.(*parser).newIfElem($1, $2, $3) +} +; -stmt_block : empty_block - | LEFT_BRACE stmts RIGHT_BRACE - { $$ = yylex.(*parser).newBlockStmt($1, $2, $3) } - ; +stmt_block: empty_block +| LEFT_BRACE stmts RIGHT_BRACE +{ + $$ = yylex.(*parser).newBlockStmt($1, $2, $3) +} +; empty_block : LEFT_BRACE RIGHT_BRACE - { $$ = yylex.(*parser).newBlockStmt($1, ast.Stmts{} , $2) } - ; - - -in_expr : expr IN expr - { $$ = yylex.(*parser).newInExpr($1, $3, $2) } - ; - - -call_expr : identifier LEFT_PAREN function_args RIGHT_PAREN - { - $$ = yylex.(*parser).newCallExpr($1, $3, $2, $4) - } - | identifier LEFT_PAREN RIGHT_PAREN - { - $$ = yylex.(*parser).newCallExpr($1, nil, $2, $3) - } - | identifier LEFT_PAREN function_args EOLS RIGHT_PAREN - { - $$ = yylex.(*parser).newCallExpr($1, $3, $2, $5) - } - | identifier LEFT_PAREN EOLS RIGHT_PAREN - { - $$ = yylex.(*parser).newCallExpr($1, nil, $2, $4) - } - ; - - -function_args : function_args COMMA expr - { - $$ = append($$, $3) - } - | function_args COMMA - | expr - { $$ = []*ast.Node{$1} } - ; - - -binary_expr: conditional_expr | assignment_expr | arithmeticExpr ; - -assignment_expr : expr EQ expr - { $$ = yylex.(*parser).newAssignmentExpr($1, $3, $2) } - ; - -conditional_expr : expr GTE expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr GT expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr OR expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr AND expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr LT expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr LTE expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr NEQ expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - | expr EQEQ expr - { $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) } - ; - - -arithmeticExpr : expr ADD expr - { $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) } - | expr SUB expr - { $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) } - | expr MUL expr - { $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) } - | expr DIV expr - { $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) } - | expr MOD expr - { $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) } - ; +{ + $$ = yylex.(*parser).newBlockStmt($1, ast.Stmts{} , $2) +} +; + +call_expr: identifier LEFT_PAREN function_args RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, $3, $2, $4) +} +| identifier LEFT_PAREN function_args COMMA RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, $3, $2, $5) +} +| identifier LEFT_PAREN RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, nil, $2, $3) +} +| identifier LEFT_PAREN function_args EOLS RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, $3, $2, $5) +} +| identifier LEFT_PAREN function_args COMMA EOLS RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, $3, $2, $6) +} +| identifier LEFT_PAREN EOLS RIGHT_PAREN +{ + $$ = yylex.(*parser).newCallExpr($1, nil, $2, $4) +} +; + + +function_args: function_args COMMA expr +{ + $$ = append($$, $3) +} +| function_args COMMA named_arg +{ + $$ = append($$, $3) +} + +| named_arg +{ + $$ = []*ast.Node{$1} +} +| expr +{ + $$ = []*ast.Node{$1} +} +; + +named_arg: identifier EQ expr +{ + $$ = yylex.(*parser).newAssignmentStmt($1, $3, $2) +} +; + +unary_expr: ADD expr %prec UMINUS +{ + $$ = yylex.(*parser).newUnaryExpr($1, $2) +} +| SUB expr %prec UMINUS +{ + $$ = yylex.(*parser).newUnaryExpr($1, $2) +} +| NOT expr +{ + $$ = yylex.(*parser).newUnaryExpr($1, $2) +} +; + +binary_expr: conditional_expr | arithmeticExpr ; + +conditional_expr: expr GTE expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr GT expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr OR expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr AND expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr LT expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr LTE expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr NEQ expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +| expr EQEQ expr +{ + $$ = yylex.(*parser).newConditionalExpr($1, $3, $2) +} +; + + +arithmeticExpr: expr ADD expr +{ + $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) +} +| expr SUB expr +{ + $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) +} +| expr MUL expr +{ + $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) +} +| expr DIV expr +{ + $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) +} +| expr MOD expr +{ + $$ = yylex.(*parser).newArithmeticExpr($1, $3, $2) +} +; // TODO: 支持多个表达式构成的括号表达式 paren_expr: LEFT_PAREN expr RIGHT_PAREN - { $$ = yylex.(*parser).newParenExpr($1, $2, $3) } - | LEFT_PAREN expr EOLS RIGHT_PAREN - { $$ = yylex.(*parser).newParenExpr($1, $2, $4) } - ; +{ + $$ = yylex.(*parser).newParenExpr($1, $2, $3) +} +| LEFT_PAREN expr EOLS RIGHT_PAREN +{ + $$ = yylex.(*parser).newParenExpr($1, $2, $4) +} +; EOLS: EOL - | EOLS EOL - ; +| EOLS EOL +; -index_expr : identifier LEFT_BRACKET expr RIGHT_BRACKET - { $$ = yylex.(*parser).newIndexExpr($1, $2 ,$3, $4) } - | DOT LEFT_BRACKET expr RIGHT_BRACKET - // 兼容原有语法,仅作为 json 函数的第二个参数 - { $$ = yylex.(*parser).newIndexExpr(nil, $2, $3, $4) } - | index_expr LEFT_BRACKET expr RIGHT_BRACKET - { $$ = yylex.(*parser).newIndexExpr($1, $2, $3, $4) } - ; +index_expr: identifier LEFT_BRACKET expr RIGHT_BRACKET +{ + $$ = yylex.(*parser).newIndexExpr($1, $2 ,$3, $4) +} +| DOT LEFT_BRACKET expr RIGHT_BRACKET +// 兼容原有语法,仅作为 json 函数的第二个参数 +{ + $$ = yylex.(*parser).newIndexExpr(nil, $2, $3, $4) +} +| index_expr LEFT_BRACKET expr RIGHT_BRACKET +{ + $$ = yylex.(*parser).newIndexExpr($1, $2, $3, $4) +} +; // TODO 实现结构体或类,当前不进行取值操作 // 仅用于 json 函数 -attr_expr : identifier DOT index_expr - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - | identifier DOT identifier - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - | index_expr DOT index_expr - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - | index_expr DOT identifier - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - | attr_expr DOT index_expr - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - | attr_expr DOT identifier - { - $$ = yylex.(*parser).newAttrExpr($1, $3) - } - ; +attr_expr: identifier DOT index_expr +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +| identifier DOT identifier +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +| index_expr DOT index_expr +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +| index_expr DOT identifier +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +| attr_expr DOT index_expr +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +| attr_expr DOT identifier +{ + $$ = yylex.(*parser).newAttrExpr($1, $3) +} +; list_init : list_init_start RIGHT_BRACKET - { - $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) - } - | list_init_start COMMA RIGHT_BRACKET - { - $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) - } - | LEFT_BRACKET RIGHT_BRACKET - { - $$ = yylex.(*parser).newListInitStartExpr($1.Pos) - $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) - - } - ; +{ + $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) +} +| list_init_start COMMA RIGHT_BRACKET +{ + $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) +} +| LEFT_BRACKET RIGHT_BRACKET +{ + $$ = yylex.(*parser).newListInitStartExpr($1.Pos) + $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) +} +; list_init_start : LEFT_BRACKET expr - { - $$ = yylex.(*parser).newListInitStartExpr($1.Pos) - $$ = yylex.(*parser).newListInitAppendExpr($$, $2) - } - | list_init_start COMMA expr - { - $$ = yylex.(*parser).newListInitAppendExpr($$, $3) - } - | list_init_start EOL - ; +{ + $$ = yylex.(*parser).newListInitStartExpr($1.Pos) + $$ = yylex.(*parser).newListInitAppendExpr($$, $2) +} +| list_init_start COMMA expr +{ + $$ = yylex.(*parser).newListInitAppendExpr($$, $3) +} +| list_init_start EOL +; map_init : map_init_start RIGHT_BRACE - { - $$ = yylex.(*parser).newMapInitEndExpr($$, $2.Pos) - } - | map_init_start COMMA RIGHT_BRACE - { - $$ = yylex.(*parser).newMapInitEndExpr($$, $3.Pos) - } - | empty_block - { - $$ = yylex.(*parser).newMapInitStartExpr($1.LBracePos.Pos) - $$ = yylex.(*parser).newMapInitEndExpr($$, $1.RBracePos.Pos) - } - ; +{ + $$ = yylex.(*parser).newMapInitEndExpr($$, $2.Pos) +} +| map_init_start COMMA RIGHT_BRACE +{ + $$ = yylex.(*parser).newMapInitEndExpr($$, $3.Pos) +} +| empty_block +{ + $$ = yylex.(*parser).newMapInitStartExpr($1.LBracePos.Pos) + $$ = yylex.(*parser).newMapInitEndExpr($$, $1.RBracePos.Pos) +} +; map_init_start: LEFT_BRACE expr COLON expr - { - $$ = yylex.(*parser).newMapInitStartExpr($1.Pos) - $$ = yylex.(*parser).newMapInitAppendExpr($$, $2, $4) - } - | map_init_start COMMA expr COLON expr - { - $$ = yylex.(*parser).newMapInitAppendExpr($1, $3, $5) - } - | map_init_start EOL - ; - - - -array_elem : bool_literal - | string_literal - | nil_literal - | number_literal - | identifier - ; +{ + $$ = yylex.(*parser).newMapInitStartExpr($1.Pos) + $$ = yylex.(*parser).newMapInitAppendExpr($$, $2, $4) +} +| map_init_start COMMA expr COLON expr +{ + $$ = yylex.(*parser).newMapInitAppendExpr($1, $3, $5) +} +| map_init_start EOL +; -/* - literal: - bool - number (int float) - nil -*/ -bool_literal : TRUE - { $$ = yylex.(*parser).newBoolLiteral($1.Pos, true) } - | FALSE - { $$ = yylex.(*parser).newBoolLiteral($1.Pos, false) } - ; - - -string_literal : STRING - { - $1.Val = yylex.(*parser).unquoteString($1.Val) - $$ = yylex.(*parser).newStringLiteral($1) - } - | MULTILINE_STRING - { - $1.Val = yylex.(*parser).unquoteMultilineString($1.Val) - $$ = yylex.(*parser).newStringLiteral($1) - } - ; - - -nil_literal : NIL - { $$ = yylex.(*parser).newNilLiteral($1.Pos) } - | NULL - { $$ = yylex.(*parser).newNilLiteral($1.Pos) } - ; - - - -number_literal : NUMBER - { $$ = yylex.(*parser).newNumberLiteral($1) } - | unary_op NUMBER - { - num := yylex.(*parser).newNumberLiteral($2) - switch $1.Typ { - case ADD: // pass - case SUB: - if num.NodeType == ast.TypeFloatLiteral { - num.FloatLiteral.Val = -num.FloatLiteral.Val - num.FloatLiteral.Start = yylex.(*parser).posCache.LnCol($1.Pos) - } else { - num.IntegerLiteral.Val = -num.IntegerLiteral.Val - num.IntegerLiteral.Start = yylex.(*parser).posCache.LnCol($1.Pos) - - } - } - $$ = num - } - ; identifier: ID - { - $$ = yylex.(*parser).newIdentifierLiteral($1) - } - | QUOTED_STRING - { - $1.Val = yylex.(*parser).unquoteString($1.Val) - $$ = yylex.(*parser).newIdentifierLiteral($1) - } - ; - - -unary_op : ADD | SUB ; +{ + $$ = yylex.(*parser).newIdentifierLiteral($1) +} +| QUOTED_STRING +{ + $1.Val = yylex.(*parser).unquoteString($1.Val) + $$ = yylex.(*parser).newIdentifierLiteral($1) +} +; +basic_literal: NUMBER +{ + $$ = yylex.(*parser).newNumberLiteral($1) +} +| TRUE +{ + $$ = yylex.(*parser).newBoolLiteral($1.Pos, true) +} +| FALSE +{ + $$ = yylex.(*parser).newBoolLiteral($1.Pos, false) +} +| STRING +{ + $1.Val = yylex.(*parser).unquoteString($1.Val) + $$ = yylex.(*parser).newStringLiteral($1) +} +| MULTILINE_STRING +{ + $1.Val = yylex.(*parser).unquoteMultilineString($1.Val) + $$ = yylex.(*parser).newStringLiteral($1) +} +| NIL +{ + $$ = yylex.(*parser).newNilLiteral($1.Pos) +} +| NULL +{ + $$ = yylex.(*parser).newNilLiteral($1.Pos) +} +; %% - diff --git a/pkg/parser/gram_y.go b/pkg/parser/gram_y.go index 04ba55c..44ac682 100644 --- a/pkg/parser/gram_y.go +++ b/pkg/parser/gram_y.go @@ -61,30 +61,37 @@ const ADD = 57379 const DIV = 57380 const GTE = 57381 const GT = 57382 -const LT = 57383 -const LTE = 57384 -const MOD = 57385 -const MUL = 57386 -const NEQ = 57387 -const EQ = 57388 -const EQEQ = 57389 -const SUB = 57390 -const operatorsEnd = 57391 -const keywordsStart = 57392 -const TRUE = 57393 -const FALSE = 57394 -const IDENTIFIER = 57395 -const AND = 57396 -const OR = 57397 -const NIL = 57398 -const NULL = 57399 -const IF = 57400 -const ELIF = 57401 -const ELSE = 57402 -const keywordsEnd = 57403 -const startSymbolsStart = 57404 -const START_STMTS = 57405 -const startSymbolsEnd = 57406 +const NOT = 57383 +const LT = 57384 +const LTE = 57385 +const MOD = 57386 +const MUL = 57387 +const NEQ = 57388 +const EQ = 57389 +const EQEQ = 57390 +const SUB = 57391 +const ADD_EQ = 57392 +const SUB_EQ = 57393 +const DIV_EQ = 57394 +const MUL_EQ = 57395 +const MOD_EQ = 57396 +const operatorsEnd = 57397 +const keywordsStart = 57398 +const TRUE = 57399 +const FALSE = 57400 +const IDENTIFIER = 57401 +const AND = 57402 +const OR = 57403 +const NIL = 57404 +const NULL = 57405 +const IF = 57406 +const ELIF = 57407 +const ELSE = 57408 +const keywordsEnd = 57409 +const startSymbolsStart = 57410 +const START_STMTS = 57411 +const startSymbolsEnd = 57412 +const UMINUS = 57413 var yyToknames = [...]string{ "$end", @@ -127,6 +134,7 @@ var yyToknames = [...]string{ "DIV", "GTE", "GT", + "NOT", "LT", "LTE", "MOD", @@ -135,6 +143,11 @@ var yyToknames = [...]string{ "EQ", "EQEQ", "SUB", + "ADD_EQ", + "SUB_EQ", + "DIV_EQ", + "MUL_EQ", + "MOD_EQ", "operatorsEnd", "keywordsStart", "TRUE", @@ -151,6 +164,7 @@ var yyToknames = [...]string{ "startSymbolsStart", "START_STMTS", "startSymbolsEnd", + "UMINUS", } var yyStatenames = [...]string{} @@ -159,232 +173,247 @@ const yyEofCode = 1 const yyErrCode = 2 const yyInitialStackSize = 16 -//line gram.y:512 +//line gram.y:633 //line yacctab:1 var yyExca = [...]int16{ -1, 1, 1, -1, -2, 0, - -1, 158, - 1, 47, - 4, 47, - 8, 47, - 15, 47, - 28, 47, - -2, 96, + -1, 171, + 1, 59, + 4, 59, + 8, 59, + 15, 59, + 28, 59, + -2, 112, } const yyPrivate = 57344 -const yyLast = 1012 +const yyLast = 1084 var yyAct = [...]uint8{ - 21, 46, 137, 3, 55, 53, 42, 39, 57, 108, - 136, 171, 30, 49, 56, 50, 66, 64, 90, 68, - 83, 41, 139, 37, 62, 84, 82, 106, 80, 83, - 163, 58, 86, 88, 84, 82, 8, 98, 15, 81, - 96, 161, 59, 100, 61, 47, 48, 97, 63, 179, - 51, 52, 46, 139, 168, 55, 164, 87, 104, 101, - 99, 165, 16, 86, 2, 56, 165, 109, 165, 31, - 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, - 123, 124, 125, 126, 127, 128, 107, 131, 67, 107, - 138, 140, 102, 4, 144, 110, 85, 105, 129, 146, - 132, 91, 149, 148, 141, 70, 89, 90, 1, 130, - 107, 133, 153, 14, 156, 142, 134, 36, 35, 80, - 83, 72, 73, 76, 77, 84, 82, 78, 70, 79, - 81, 107, 7, 34, 33, 158, 75, 152, 162, 60, - 23, 157, 80, 83, 72, 73, 76, 77, 84, 82, - 78, 170, 79, 81, 38, 173, 175, 93, 151, 24, - 178, 5, 40, 25, 180, 29, 26, 45, 181, 92, - 43, 28, 27, 182, 9, 13, 158, 158, 107, 12, - 94, 11, 174, 176, 177, 10, 44, 135, 17, 65, - 22, 6, 54, 0, 158, 107, 0, 107, 0, 0, - 183, 184, 0, 185, 107, 15, 0, 0, 46, 0, - 186, 55, 53, 42, 39, 57, 103, 0, 0, 0, - 49, 56, 50, 18, 0, 0, 20, 19, 0, 16, - 0, 0, 0, 0, 0, 0, 0, 0, 58, 0, - 80, 83, 72, 73, 76, 77, 84, 82, 78, 59, - 79, 81, 47, 48, 0, 0, 0, 51, 52, 32, - 15, 0, 0, 46, 0, 0, 55, 53, 42, 39, - 57, 0, 0, 0, 0, 49, 56, 50, 18, 0, - 0, 20, 19, 0, 16, 0, 0, 0, 0, 0, - 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 59, 0, 0, 47, 48, 0, - 0, 0, 51, 52, 32, 46, 0, 0, 55, 53, - 42, 39, 57, 0, 0, 0, 0, 49, 56, 50, - 18, 0, 0, 20, 19, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 59, 0, 0, 47, - 48, 0, 0, 0, 51, 52, 32, 154, 0, 0, - 46, 0, 0, 55, 53, 42, 39, 57, 0, 0, - 0, 0, 49, 56, 50, 0, 0, 0, 0, 0, - 0, 46, 0, 0, 55, 53, 42, 39, 57, 145, - 58, 0, 0, 49, 56, 50, 0, 0, 0, 0, - 0, 59, 0, 0, 47, 48, 0, 0, 0, 51, - 52, 58, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 59, 0, 0, 47, 48, 0, 0, 46, - 51, 52, 55, 53, 42, 39, 57, 0, 0, 143, - 0, 49, 56, 50, 0, 0, 0, 113, 0, 0, - 46, 0, 0, 55, 53, 42, 39, 57, 0, 58, - 0, 0, 49, 56, 50, 0, 0, 0, 0, 0, - 59, 0, 0, 47, 48, 0, 0, 0, 51, 52, - 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 59, 0, 0, 47, 48, 0, 0, 46, 51, - 52, 55, 53, 42, 39, 57, 103, 0, 0, 0, - 49, 56, 50, 0, 0, 0, 0, 0, 0, 46, - 0, 0, 55, 53, 42, 39, 57, 0, 58, 95, - 0, 49, 56, 50, 0, 0, 0, 0, 0, 59, - 0, 0, 47, 48, 0, 0, 0, 51, 52, 58, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 59, 0, 0, 47, 48, 0, 0, 69, 51, 52, - 46, 0, 0, 55, 53, 42, 39, 57, 0, 0, - 0, 0, 49, 56, 50, 0, 0, 0, 0, 0, - 0, 46, 0, 0, 55, 53, 42, 39, 159, 0, - 58, 0, 0, 49, 56, 50, 0, 0, 0, 0, - 0, 59, 0, 0, 47, 48, 0, 0, 0, 51, - 52, 58, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 59, 0, 0, 47, 48, 0, 0, 46, - 51, 52, 55, 53, 42, 39, 57, 0, 0, 0, - 0, 49, 56, 50, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, - 147, 0, 0, 0, 0, 0, 0, 70, 0, 0, - 59, 0, 139, 47, 48, 0, 0, 0, 51, 52, - 0, 80, 83, 72, 73, 76, 77, 84, 82, 78, - 71, 79, 81, 108, 0, 0, 0, 0, 75, 74, - 0, 0, 70, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 80, 83, 72, 73, - 76, 77, 84, 82, 78, 71, 79, 81, 0, 70, - 0, 0, 0, 75, 74, 150, 172, 0, 0, 0, - 0, 0, 0, 80, 83, 72, 73, 76, 77, 84, - 82, 78, 71, 79, 81, 70, 0, 0, 0, 0, - 75, 74, 0, 0, 0, 0, 0, 0, 0, 80, - 83, 72, 73, 76, 77, 84, 82, 78, 71, 79, - 81, 0, 169, 0, 0, 0, 75, 74, 70, 0, + 67, 46, 147, 22, 8, 148, 65, 22, 97, 34, + 68, 160, 58, 15, 63, 61, 32, 15, 75, 94, + 96, 185, 114, 150, 92, 93, 93, 112, 90, 3, + 108, 98, 84, 87, 76, 77, 95, 80, 81, 88, + 86, 82, 102, 83, 85, 100, 84, 87, 104, 105, + 106, 107, 101, 88, 86, 79, 78, 110, 85, 179, + 4, 89, 111, 113, 115, 103, 113, 118, 117, 1, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, + 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, + 14, 142, 116, 149, 152, 186, 2, 113, 157, 141, + 204, 144, 151, 159, 154, 24, 140, 161, 143, 162, + 197, 153, 178, 87, 75, 22, 43, 113, 166, 88, + 86, 171, 178, 155, 25, 15, 169, 175, 84, 87, + 76, 77, 182, 80, 81, 88, 86, 82, 174, 83, + 85, 177, 7, 165, 178, 45, 26, 170, 176, 57, + 150, 79, 78, 178, 84, 87, 76, 77, 31, 80, + 81, 88, 86, 82, 184, 83, 85, 27, 59, 171, + 171, 113, 52, 192, 187, 189, 195, 51, 194, 30, + 198, 196, 199, 15, 75, 151, 29, 16, 171, 113, + 28, 113, 60, 200, 9, 188, 190, 191, 84, 87, + 76, 77, 113, 80, 81, 88, 86, 82, 13, 83, + 85, 17, 91, 12, 201, 202, 16, 203, 90, 53, + 11, 79, 54, 36, 47, 44, 56, 109, 205, 10, + 33, 39, 55, 40, 19, 145, 18, 21, 20, 53, + 17, 94, 54, 62, 23, 164, 92, 93, 5, 48, + 64, 6, 55, 50, 0, 0, 0, 0, 0, 0, + 0, 49, 0, 0, 0, 0, 0, 0, 0, 37, + 38, 0, 0, 0, 41, 42, 35, 16, 0, 0, + 53, 0, 0, 54, 36, 47, 44, 56, 0, 0, + 0, 0, 39, 55, 40, 19, 0, 0, 21, 20, + 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, + 48, 0, 0, 0, 50, 0, 0, 0, 0, 0, + 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, + 37, 38, 0, 0, 0, 41, 42, 35, 53, 0, + 0, 54, 36, 47, 44, 56, 0, 0, 0, 0, + 39, 55, 40, 19, 0, 0, 21, 20, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, + 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, + 49, 0, 0, 0, 75, 0, 0, 0, 37, 38, + 163, 0, 0, 41, 42, 35, 0, 0, 84, 87, + 76, 77, 0, 80, 81, 88, 86, 82, 69, 83, + 85, 70, 71, 73, 72, 74, 0, 0, 0, 0, + 53, 79, 78, 54, 36, 47, 44, 56, 0, 193, + 0, 0, 39, 55, 40, 0, 0, 0, 0, 53, + 0, 150, 54, 36, 47, 44, 56, 0, 146, 0, + 48, 39, 55, 40, 50, 0, 0, 0, 0, 0, + 150, 0, 49, 0, 0, 0, 0, 0, 0, 48, + 37, 38, 0, 50, 0, 41, 42, 0, 0, 0, + 0, 49, 75, 0, 0, 0, 0, 0, 0, 37, + 38, 0, 0, 0, 41, 42, 84, 87, 76, 77, + 0, 80, 81, 88, 86, 82, 69, 83, 85, 70, + 71, 73, 72, 74, 0, 167, 0, 0, 53, 79, + 78, 54, 36, 47, 44, 56, 0, 0, 0, 0, + 39, 55, 40, 53, 0, 0, 54, 36, 47, 44, + 56, 158, 0, 0, 0, 39, 55, 40, 48, 0, + 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, + 49, 0, 0, 48, 0, 0, 0, 50, 37, 38, + 0, 0, 0, 41, 42, 49, 0, 0, 0, 0, + 0, 0, 0, 37, 38, 0, 0, 53, 41, 42, + 54, 36, 47, 44, 56, 0, 0, 156, 0, 39, + 55, 40, 0, 119, 0, 0, 53, 0, 0, 54, + 36, 47, 44, 56, 0, 0, 0, 48, 39, 55, + 40, 50, 0, 0, 0, 0, 0, 0, 0, 49, + 0, 0, 0, 0, 0, 0, 48, 37, 38, 0, + 50, 0, 41, 42, 0, 0, 53, 0, 49, 54, + 36, 47, 44, 56, 109, 0, 37, 38, 39, 55, + 40, 41, 42, 0, 0, 53, 0, 0, 54, 36, + 47, 44, 56, 0, 0, 99, 48, 39, 55, 40, + 50, 0, 0, 0, 0, 0, 0, 0, 49, 0, + 0, 0, 0, 0, 0, 48, 37, 38, 0, 50, + 0, 41, 42, 0, 0, 0, 0, 49, 0, 0, + 0, 0, 0, 0, 0, 37, 38, 0, 0, 66, + 41, 42, 53, 0, 0, 54, 36, 47, 44, 56, + 0, 0, 0, 0, 39, 55, 40, 53, 0, 0, + 54, 36, 47, 44, 172, 0, 0, 0, 0, 39, + 55, 40, 48, 0, 0, 0, 50, 0, 0, 0, + 0, 0, 0, 0, 49, 0, 0, 48, 0, 0, + 0, 50, 37, 38, 0, 0, 0, 41, 42, 49, + 0, 0, 0, 0, 0, 0, 0, 37, 38, 0, + 0, 53, 41, 42, 54, 36, 47, 44, 56, 0, + 0, 0, 0, 39, 55, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 80, 83, 72, 73, 76, 77, 84, 82, - 78, 71, 79, 81, 0, 70, 0, 0, 0, 75, - 74, 167, 0, 0, 0, 0, 0, 0, 0, 80, - 83, 72, 73, 76, 77, 84, 82, 78, 71, 79, - 81, 0, 166, 0, 0, 0, 75, 74, 70, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 80, 83, 72, 73, 76, 77, 84, 82, - 78, 71, 79, 81, 0, 160, 0, 0, 0, 75, - 74, 70, 0, 0, 0, 0, 0, 0, 155, 0, - 0, 0, 0, 0, 0, 80, 83, 72, 73, 76, - 77, 84, 82, 78, 71, 79, 81, 70, 0, 0, - 0, 0, 75, 74, 111, 0, 0, 0, 0, 0, - 0, 80, 83, 72, 73, 76, 77, 84, 82, 78, - 71, 79, 81, 70, 0, 0, 0, 0, 75, 74, - 0, 0, 0, 0, 0, 0, 0, 80, 83, 72, - 73, 76, 77, 84, 82, 78, 71, 79, 81, 70, - 0, 0, 0, 0, 75, 74, 0, 0, 0, 0, - 0, 0, 0, 80, 83, 72, 73, 76, 77, 84, - 82, 78, 71, 79, 81, 0, 0, 0, 0, 0, - 75, 74, + 0, 48, 0, 0, 0, 50, 0, 0, 183, 0, + 0, 0, 0, 49, 75, 0, 0, 0, 0, 0, + 0, 37, 38, 0, 0, 0, 41, 42, 84, 87, + 76, 77, 0, 80, 81, 88, 86, 82, 0, 83, + 85, 75, 0, 0, 0, 0, 0, 181, 0, 0, + 0, 79, 78, 0, 0, 84, 87, 76, 77, 0, + 80, 81, 88, 86, 82, 180, 83, 85, 0, 0, + 0, 75, 0, 0, 0, 0, 0, 0, 79, 78, + 0, 0, 0, 0, 0, 84, 87, 76, 77, 0, + 80, 81, 88, 86, 82, 173, 83, 85, 0, 0, + 0, 75, 0, 0, 0, 0, 0, 0, 79, 78, + 168, 0, 0, 0, 0, 84, 87, 76, 77, 0, + 80, 81, 88, 86, 82, 0, 83, 85, 0, 75, + 0, 0, 0, 0, 0, 0, 0, 0, 79, 78, + 0, 0, 0, 84, 87, 76, 77, 114, 80, 81, + 88, 86, 82, 0, 83, 85, 75, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 79, 78, 0, 0, + 84, 87, 76, 77, 0, 80, 81, 88, 86, 82, + 0, 83, 85, 75, 0, 0, 0, 0, 0, 163, + 0, 0, 0, 79, 78, 0, 0, 84, 87, 76, + 77, 0, 80, 81, 88, 86, 82, 0, 83, 85, + 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 79, 78, 0, 0, 84, 87, 76, 77, 0, 80, + 81, 88, 86, 82, 0, 83, 85, 75, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 79, 78, 0, + 0, 84, 87, 76, 77, 0, 80, 81, 88, 86, + 82, 0, 83, 85, } var yyPact = [...]int16{ - 1, 85, 256, -1000, -1000, -1000, 308, 34, 20, -1000, - -1000, -1000, -1000, -1000, -1000, -1000, -1000, -43, 573, -1000, - -1000, 956, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 89, - 50, -1000, 642, -1000, -1000, -1000, -1000, 94, 152, 522, - 32, -1000, 642, -1000, -1000, -1000, 46, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, 81, -1000, -1000, 501, -1000, -1000, - 34, 20, -1000, -1000, -5, -1000, 642, -5, 930, 453, - 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, - 642, 642, 642, 642, 642, 45, 642, 45, 699, -6, - 642, 45, -1000, 432, -1000, -1000, 956, -1000, 384, -1000, - 664, 642, -1000, -1000, 726, 20, -1000, -1000, 201, 699, - -1000, 363, 904, 594, 203, 956, -9, -9, 82, 105, - -9, -9, -9, -9, -18, -18, -1000, -1000, -1000, 19, - 5, 878, 19, 5, -1000, 25, -1000, 40, 956, -1000, - 845, 19, 5, -1000, 956, -1000, 812, -1000, 38, 785, - 642, -4, -1000, 752, 594, 594, 699, -1000, -1000, 201, - -1000, -1000, 33, 642, -1000, -1000, -1000, 642, -1000, -1000, - 956, -1000, 594, 699, -1000, 699, -1000, -1000, 726, -1000, - 956, 956, 699, -1000, -1000, -1000, -1000, + 27, 52, 273, -1000, -1000, -1000, 331, 183, 164, -1000, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -51, 715, + -1000, -1000, 459, -1000, -1000, -1000, -1000, -1000, -1000, -1000, + -1000, 54, 205, -1000, 234, 784, -1000, -1000, -1000, -1000, + -1000, -1000, -1000, 3, 658, 37, -1000, 784, 784, 784, + 784, -1000, -1000, 17, -1000, -1000, 639, 183, 164, -1000, + -1000, 8, -1000, 784, 8, 64, 599, 459, -1000, 784, + 784, 784, 784, 784, 784, 784, 784, 784, 784, 784, + 784, 784, 784, 784, 784, 784, 784, 784, 784, 232, + 784, 232, 432, 784, 232, 953, -1000, 580, -1000, -1000, + 1007, -1000, 526, -1000, -5, -1000, -1000, -1000, 784, -1000, + 980, 164, -1000, -1000, 212, 953, -1000, 511, 926, 730, + 1007, 1007, 1007, 1007, 1007, 1007, 117, 9, 9, 161, + 1034, 9, 9, 9, 9, 75, 75, -1000, -1000, -1000, + 15, 13, 898, 15, 13, 122, -1000, 125, -1000, 1007, + -1000, 12, 868, 15, 13, -1000, -1000, 1007, -1000, 838, + -1000, 116, 811, 784, 6, -1000, 91, 730, 730, 8, + -1000, -1000, 212, -1000, -1000, 413, 94, -1000, -1000, 784, + -1000, 784, -1000, -1000, 1007, -1000, 730, 8, -1000, 8, + -1000, -1000, 361, -1000, 84, 1007, -1000, -1000, 1007, 1007, + 8, -1000, -1000, -1000, -1000, -1000, } var yyPgo = [...]uint8{ - 0, 192, 27, 21, 158, 191, 190, 189, 188, 187, - 132, 186, 185, 181, 179, 175, 174, 172, 23, 171, - 170, 167, 166, 12, 165, 69, 0, 163, 162, 159, - 154, 140, 134, 133, 118, 117, 113, 108, 36, 2, + 0, 27, 1, 245, 251, 244, 243, 236, 235, 142, + 10, 229, 220, 213, 208, 194, 190, 5, 9, 186, + 179, 177, 172, 167, 16, 158, 230, 0, 146, 145, + 124, 116, 105, 6, 90, 69, 4, 2, } var yyR1 = [...]int8{ - 0, 38, 38, 38, 38, 37, 37, 37, 4, 4, - 4, 5, 5, 5, 10, 10, 10, 10, 10, 10, - 36, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 15, 14, 12, 13, 13, 13, 13, 13, 13, 13, - 13, 16, 16, 6, 8, 8, 7, 2, 2, 3, - 25, 17, 17, 17, 17, 9, 9, 9, 19, 19, - 19, 11, 20, 20, 20, 20, 20, 20, 20, 20, - 21, 21, 21, 21, 21, 22, 22, 39, 39, 23, - 23, 23, 24, 24, 24, 24, 24, 24, 29, 29, - 29, 30, 30, 30, 27, 27, 27, 28, 28, 28, - 31, 31, 31, 31, 31, 32, 32, 33, 33, 34, - 34, 35, 35, 18, 18, 1, 1, + 0, 36, 36, 36, 36, 35, 35, 35, 3, 3, + 3, 4, 4, 4, 9, 9, 9, 9, 9, 9, + 9, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 34, 10, 10, 10, 10, 10, 10, 26, + 14, 13, 11, 12, 12, 12, 12, 12, 12, 12, + 12, 33, 33, 15, 15, 5, 7, 7, 6, 1, + 1, 2, 16, 16, 16, 16, 16, 16, 8, 8, + 8, 8, 17, 19, 19, 19, 20, 20, 21, 21, + 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, + 22, 23, 23, 37, 37, 24, 24, 24, 25, 25, + 25, 25, 25, 25, 30, 30, 30, 31, 31, 31, + 28, 28, 28, 29, 29, 29, 18, 18, 32, 32, + 32, 32, 32, 32, 32, } var yyR2 = [...]int8{ 0, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 7, 6, 6, 5, 6, 5, 5, - 4, 1, 3, 3, 1, 2, 3, 1, 3, 2, - 3, 4, 3, 5, 4, 3, 2, 1, 1, 1, - 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 4, 1, 2, 4, - 4, 4, 3, 3, 3, 3, 3, 3, 2, 3, - 2, 2, 3, 2, 2, 3, 1, 4, 5, 2, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 2, 1, 1, 1, 1, + 4, 1, 1, 1, 3, 3, 1, 2, 3, 1, + 3, 2, 4, 5, 3, 5, 6, 4, 3, 3, + 1, 1, 3, 2, 2, 2, 1, 1, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 4, 1, 2, 4, 4, 4, 3, 3, + 3, 3, 3, 3, 2, 3, 2, 2, 3, 2, + 2, 3, 1, 4, 5, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, } var yyChk = [...]int16{ - -1000, -37, 63, 2, 8, -4, -5, -10, -38, -16, - -12, -13, -14, -15, -36, 4, 28, -8, 22, 26, - 25, -26, -6, -31, -29, -27, -22, -17, -19, -24, - -23, -25, 58, -32, -33, -34, -35, -18, -30, 13, - -28, -3, 12, -20, -11, -21, 7, 51, 52, 19, - 21, 56, 57, 11, -1, 10, 20, 14, 37, 48, - -10, -38, 4, 28, 60, -7, 59, -25, -26, 4, - 23, 46, 39, 40, 55, 54, 41, 42, 45, 47, - 37, 48, 44, 38, 43, 7, 13, 7, -26, 12, - 13, 7, 17, 5, 28, 17, -26, 15, 5, 28, - -26, 13, 11, 15, -26, -38, -2, -3, 14, -26, - -2, 4, -26, 4, -26, -26, -26, -26, -26, -26, - -26, -26, -26, -26, -26, -26, -26, -26, -26, -23, - -18, -26, -23, -18, -2, -9, 16, -39, -26, 28, - -26, -23, -18, 17, -26, 15, -26, 16, -39, -26, - 29, -4, -2, -26, 4, 4, -26, -2, -3, 14, - 17, 16, -39, 5, 16, 28, 17, 29, 16, 17, - -26, 15, 4, -26, -2, -26, -2, -2, -26, 16, - -26, -26, -26, -2, -2, -2, -2, + -1000, -35, 69, 2, 8, -3, -4, -9, -36, -15, + -11, -12, -13, -14, -34, -10, 4, 28, -7, 22, + 26, 25, -27, -5, -32, -30, -28, -23, -16, -19, + -20, -25, -24, -26, -18, 64, 11, 57, 58, 19, + 21, 62, 63, -31, 13, -29, -2, 12, 37, 49, + 41, -21, -22, 7, 10, 20, 14, -9, -36, 4, + 28, 66, -6, 65, -26, -33, 4, -27, -10, 47, + 50, 51, 53, 52, 54, 23, 39, 40, 61, 60, + 42, 43, 46, 48, 37, 49, 45, 38, 44, 7, + 13, 7, 12, 13, 7, -27, 17, 5, 28, 17, + -27, 15, 5, 28, -27, -27, -27, -27, 13, 15, + -27, -36, -1, -2, 14, -27, -1, 4, -27, 4, + -27, -27, -27, -27, -27, -27, -27, -27, -27, -27, + -27, -27, -27, -27, -27, -27, -27, -27, -27, -27, + -24, -18, -27, -24, -18, -8, 16, -37, -17, -27, + 28, -18, -27, -24, -18, -1, 17, -27, 15, -27, + 16, -37, -27, 29, -3, -1, -27, 4, 4, -33, + -1, -2, 14, 17, 16, 5, -37, 16, 28, 47, + 17, 29, 16, 17, -27, 15, 4, -33, -1, -33, + -1, -1, -27, 16, -37, -27, -17, 16, -27, -27, + -33, -1, -1, -1, 16, -1, } var yyDef = [...]int8{ 0, -2, 0, 7, 6, 5, 9, 10, 12, 14, - 15, 16, 17, 18, 19, 1, 2, 41, 0, 31, - 30, 20, 44, 21, 22, 23, 24, 25, 26, 27, - 28, 29, 0, 100, 101, 102, 103, 104, 0, 0, - 0, 96, 0, 58, 59, 60, 0, 105, 106, 107, - 108, 109, 110, 111, 0, 113, 114, 0, 115, 116, - 8, 11, 3, 4, 0, 45, 0, 29, 0, 0, + 15, 16, 17, 18, 19, 20, 1, 2, 53, 0, + 41, 40, 32, 56, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 0, 118, 119, 120, 121, + 122, 123, 124, 0, 0, 0, 112, 0, 0, 0, + 0, 76, 77, 0, 116, 117, 0, 8, 11, 3, + 4, 0, 57, 0, 30, 0, 0, 51, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 88, 0, 93, 90, 91, 94, 0, 99, - 0, 0, 112, 49, 0, 13, 42, 47, 0, 0, - 32, 0, 0, 0, 50, 61, 62, 63, 64, 65, - 66, 67, 68, 69, 70, 71, 72, 73, 74, 86, - 87, 0, 84, 85, 43, 0, 52, 0, 57, 77, - 0, 82, 83, 89, 92, 95, 0, 75, 0, 0, - 0, 0, 46, 0, 0, 0, 0, 40, -2, 0, - 81, 51, 0, 56, 54, 78, 79, 0, 76, 80, - 97, 48, 0, 0, 38, 0, 36, 39, 20, 53, - 55, 98, 0, 34, 37, 35, 33, + 0, 0, 0, 0, 0, 0, 104, 0, 109, 106, + 107, 110, 0, 115, 0, 73, 74, 75, 0, 61, + 0, 13, 54, 59, 0, 0, 42, 0, 0, 0, + 33, 34, 35, 36, 37, 38, 39, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 102, 103, 0, 100, 101, 0, 64, 0, 70, 71, + 93, 31, 0, 98, 99, 55, 105, 108, 111, 0, + 91, 0, 0, 0, 0, 58, 0, 0, 0, 0, + 50, -2, 0, 97, 62, 0, 0, 67, 94, 0, + 95, 0, 92, 96, 113, 60, 0, 0, 48, 0, + 46, 49, 32, 63, 0, 68, 69, 65, 72, 114, + 0, 44, 47, 45, 66, 43, } var yyTok1 = [...]int8{ @@ -398,7 +427,7 @@ var yyTok2 = [...]int8{ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - 62, 63, 64, + 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, } var yyTok3 = [...]int8{ @@ -744,19 +773,19 @@ yydefault: case 5: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:131 +//line gram.y:138 { yylex.(*parser).parseResult = yyDollar[2].aststmts } case 7: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:134 +//line gram.y:143 { yylex.(*parser).unexpected("", "") } case 8: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:139 +//line gram.y:150 { s := yyDollar[1].aststmts s = append(s, yyDollar[2].node) @@ -764,476 +793,534 @@ yydefault: } case 10: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:146 +//line gram.y:157 { yyVAL.aststmts = ast.Stmts{yyDollar[1].node} } case 11: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:150 +//line gram.y:163 { yyVAL.aststmts = ast.Stmts{yyDollar[1].node} } case 12: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:152 +//line gram.y:167 { yyVAL.aststmts = ast.Stmts{} } case 13: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:154 +//line gram.y:171 { s := yyDollar[1].aststmts s = append(s, yyDollar[2].node) yyVAL.aststmts = s } - case 30: + case 33: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:205 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 34: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:209 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 35: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:213 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 36: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:217 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 37: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:221 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 38: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:225 + { + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 39: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:232 + { + yyVAL.node = yylex.(*parser).newInExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + } + case 40: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:178 +//line gram.y:237 { yyVAL.node = yylex.(*parser).newBreakStmt(yyDollar[1].item.Pos) } - case 31: + case 41: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:182 +//line gram.y:243 { yyVAL.node = yylex.(*parser).newContinueStmt(yyDollar[1].item.Pos) } - case 32: + case 42: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:191 +//line gram.y:254 { yyVAL.node = yylex.(*parser).newForInStmt(yyDollar[2].node, yyDollar[3].astblock, yyDollar[1].item) } - case 33: + case 43: yyDollar = yyS[yypt-7 : yypt+1] -//line gram.y:202 +//line gram.y:266 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, yyDollar[4].node, yyDollar[6].node, yyDollar[7].astblock) } - case 34: + case 44: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:204 +//line gram.y:270 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, yyDollar[4].node, nil, yyDollar[6].astblock) } - case 35: + case 45: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:206 +//line gram.y:274 { yyVAL.node = yylex.(*parser).newForStmt(nil, yyDollar[3].node, yyDollar[5].node, yyDollar[6].astblock) } - case 36: + case 46: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:208 +//line gram.y:278 { yyVAL.node = yylex.(*parser).newForStmt(nil, yyDollar[3].node, nil, yyDollar[5].astblock) } - case 37: + case 47: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:211 +//line gram.y:282 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, nil, yyDollar[5].node, yyDollar[6].astblock) } - case 38: + case 48: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:213 +//line gram.y:286 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, nil, nil, yyDollar[5].astblock) } - case 39: + case 49: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:215 +//line gram.y:290 { yyVAL.node = yylex.(*parser).newForStmt(nil, nil, yyDollar[4].node, yyDollar[5].astblock) } - case 40: + case 50: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:217 +//line gram.y:294 { yyVAL.node = yylex.(*parser).newForStmt(nil, nil, nil, yyDollar[4].astblock) } - case 41: + case 53: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:221 +//line gram.y:303 { yyVAL.node = yylex.(*parser).newIfElifStmt(yyDollar[1].iflist) } - case 42: + case 54: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:225 +//line gram.y:307 { yyVAL.node = yylex.(*parser).newIfElifelseStmt(yyDollar[1].iflist, yyDollar[2].item, yyDollar[3].astblock) } - case 43: + case 55: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:231 +//line gram.y:313 { yyVAL.ifitem = yylex.(*parser).newIfElem(yyDollar[1].item, yyDollar[2].node, yyDollar[3].astblock) } - case 44: + case 56: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:235 +//line gram.y:319 { yyVAL.iflist = []*ast.IfStmtElem{yyDollar[1].ifitem} } - case 45: + case 57: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:237 +//line gram.y:323 { yyVAL.iflist = append(yyDollar[1].iflist, yyDollar[2].ifitem) } - case 46: + case 58: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:241 +//line gram.y:329 { yyVAL.ifitem = yylex.(*parser).newIfElem(yyDollar[1].item, yyDollar[2].node, yyDollar[3].astblock) } - case 48: + case 60: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:247 +//line gram.y:337 { yyVAL.astblock = yylex.(*parser).newBlockStmt(yyDollar[1].item, yyDollar[2].aststmts, yyDollar[3].item) } - case 49: + case 61: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:251 +//line gram.y:343 { yyVAL.astblock = yylex.(*parser).newBlockStmt(yyDollar[1].item, ast.Stmts{}, yyDollar[2].item) } - case 50: - yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:256 - { - yyVAL.node = yylex.(*parser).newInExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) - } - case 51: + case 62: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:261 +//line gram.y:349 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[3].nodes, yyDollar[2].item, yyDollar[4].item) } - case 52: + case 63: + yyDollar = yyS[yypt-5 : yypt+1] +//line gram.y:353 + { + yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[3].nodes, yyDollar[2].item, yyDollar[5].item) + } + case 64: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:265 +//line gram.y:357 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, nil, yyDollar[2].item, yyDollar[3].item) } - case 53: + case 65: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:269 +//line gram.y:361 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[3].nodes, yyDollar[2].item, yyDollar[5].item) } - case 54: + case 66: + yyDollar = yyS[yypt-6 : yypt+1] +//line gram.y:365 + { + yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[3].nodes, yyDollar[2].item, yyDollar[6].item) + } + case 67: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:273 +//line gram.y:369 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, nil, yyDollar[2].item, yyDollar[4].item) } - case 55: + case 68: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:280 +//line gram.y:376 { yyVAL.nodes = append(yyVAL.nodes, yyDollar[3].node) } - case 57: + case 69: + yyDollar = yyS[yypt-3 : yypt+1] +//line gram.y:380 + { + yyVAL.nodes = append(yyVAL.nodes, yyDollar[3].node) + } + case 70: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:285 +//line gram.y:385 { yyVAL.nodes = []*ast.Node{yyDollar[1].node} } - case 61: + case 71: + yyDollar = yyS[yypt-1 : yypt+1] +//line gram.y:389 + { + yyVAL.nodes = []*ast.Node{yyDollar[1].node} + } + case 72: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:292 +//line gram.y:395 { - yyVAL.node = yylex.(*parser).newAssignmentExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) + yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 62: + case 73: + yyDollar = yyS[yypt-2 : yypt+1] +//line gram.y:401 + { + yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) + } + case 74: + yyDollar = yyS[yypt-2 : yypt+1] +//line gram.y:405 + { + yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) + } + case 75: + yyDollar = yyS[yypt-2 : yypt+1] +//line gram.y:409 + { + yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) + } + case 78: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:296 +//line gram.y:417 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 63: + case 79: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:298 +//line gram.y:421 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 64: + case 80: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:300 +//line gram.y:425 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 65: + case 81: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:302 +//line gram.y:429 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 66: + case 82: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:304 +//line gram.y:433 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 67: + case 83: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:306 +//line gram.y:437 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 68: + case 84: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:308 +//line gram.y:441 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 69: + case 85: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:310 +//line gram.y:445 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 70: + case 86: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:315 +//line gram.y:452 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 71: + case 87: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:317 +//line gram.y:456 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 72: + case 88: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:319 +//line gram.y:460 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 73: + case 89: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:321 +//line gram.y:464 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 74: + case 90: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:323 +//line gram.y:468 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[2].item) } - case 75: + case 91: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:328 +//line gram.y:475 { yyVAL.node = yylex.(*parser).newParenExpr(yyDollar[1].item, yyDollar[2].node, yyDollar[3].item) } - case 76: + case 92: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:330 +//line gram.y:479 { yyVAL.node = yylex.(*parser).newParenExpr(yyDollar[1].item, yyDollar[2].node, yyDollar[4].item) } - case 79: + case 95: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:339 +//line gram.y:490 { yyVAL.node = yylex.(*parser).newIndexExpr(yyDollar[1].node, yyDollar[2].item, yyDollar[3].node, yyDollar[4].item) } - case 80: + case 96: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:342 +//line gram.y:495 { yyVAL.node = yylex.(*parser).newIndexExpr(nil, yyDollar[2].item, yyDollar[3].node, yyDollar[4].item) } - case 81: + case 97: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:344 +//line gram.y:499 { yyVAL.node = yylex.(*parser).newIndexExpr(yyDollar[1].node, yyDollar[2].item, yyDollar[3].node, yyDollar[4].item) } - case 82: + case 98: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:351 +//line gram.y:508 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 83: + case 99: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:355 +//line gram.y:512 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 84: + case 100: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:359 +//line gram.y:516 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 85: + case 101: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:363 +//line gram.y:520 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 86: + case 102: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:367 +//line gram.y:524 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 87: + case 103: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:371 +//line gram.y:528 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } - case 88: + case 104: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:378 +//line gram.y:535 { yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[2].item.Pos) } - case 89: + case 105: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:382 +//line gram.y:539 { yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[2].item.Pos) } - case 90: + case 106: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:386 +//line gram.y:543 { yyVAL.node = yylex.(*parser).newListInitStartExpr(yyDollar[1].item.Pos) yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[2].item.Pos) - } - case 91: + case 107: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:394 +//line gram.y:550 { yyVAL.node = yylex.(*parser).newListInitStartExpr(yyDollar[1].item.Pos) yyVAL.node = yylex.(*parser).newListInitAppendExpr(yyVAL.node, yyDollar[2].node) } - case 92: + case 108: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:399 +//line gram.y:555 { yyVAL.node = yylex.(*parser).newListInitAppendExpr(yyVAL.node, yyDollar[3].node) } - case 94: + case 110: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:407 +//line gram.y:563 { yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[2].item.Pos) } - case 95: + case 111: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:411 +//line gram.y:567 { yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[3].item.Pos) } - case 96: + case 112: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:415 +//line gram.y:571 { yyVAL.node = yylex.(*parser).newMapInitStartExpr(yyDollar[1].astblock.LBracePos.Pos) yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[1].astblock.RBracePos.Pos) } - case 97: + case 113: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:422 +//line gram.y:578 { yyVAL.node = yylex.(*parser).newMapInitStartExpr(yyDollar[1].item.Pos) yyVAL.node = yylex.(*parser).newMapInitAppendExpr(yyVAL.node, yyDollar[2].node, yyDollar[4].node) } - case 98: + case 114: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:427 +//line gram.y:583 { yyVAL.node = yylex.(*parser).newMapInitAppendExpr(yyDollar[1].node, yyDollar[3].node, yyDollar[5].node) } - case 105: - yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:449 - { - yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, true) - } - case 106: + case 116: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:451 +//line gram.y:591 { - yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, false) + yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) } - case 107: + case 117: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:456 +//line gram.y:595 { yyDollar[1].item.Val = yylex.(*parser).unquoteString(yyDollar[1].item.Val) - yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) + yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) } - case 108: + case 118: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:461 +//line gram.y:602 { - yyDollar[1].item.Val = yylex.(*parser).unquoteMultilineString(yyDollar[1].item.Val) - yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) + yyVAL.node = yylex.(*parser).newNumberLiteral(yyDollar[1].item) } - case 109: + case 119: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:469 +//line gram.y:606 { - yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) + yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, true) } - case 110: + case 120: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:471 +//line gram.y:610 { - yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) + yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, false) } - case 111: + case 121: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:477 +//line gram.y:614 { - yyVAL.node = yylex.(*parser).newNumberLiteral(yyDollar[1].item) + yyDollar[1].item.Val = yylex.(*parser).unquoteString(yyDollar[1].item.Val) + yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) } - case 112: - yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:479 + case 122: + yyDollar = yyS[yypt-1 : yypt+1] +//line gram.y:619 { - num := yylex.(*parser).newNumberLiteral(yyDollar[2].item) - switch yyDollar[1].item.Typ { - case ADD: // pass - case SUB: - if num.NodeType == ast.TypeFloatLiteral { - num.FloatLiteral.Val = -num.FloatLiteral.Val - num.FloatLiteral.Start = yylex.(*parser).posCache.LnCol(yyDollar[1].item.Pos) - } else { - num.IntegerLiteral.Val = -num.IntegerLiteral.Val - num.IntegerLiteral.Start = yylex.(*parser).posCache.LnCol(yyDollar[1].item.Pos) - - } - } - yyVAL.node = num + yyDollar[1].item.Val = yylex.(*parser).unquoteMultilineString(yyDollar[1].item.Val) + yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) } - case 113: + case 123: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:498 +//line gram.y:624 { - yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) + yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) } - case 114: + case 124: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:502 +//line gram.y:628 { - yyDollar[1].item.Val = yylex.(*parser).unquoteString(yyDollar[1].item.Val) - yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) + yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) } } goto yystack /* stack new state and value */ diff --git a/pkg/parser/lex.go b/pkg/parser/lex.go index 56eacd3..20db4c0 100644 --- a/pkg/parser/lex.go +++ b/pkg/parser/lex.go @@ -117,23 +117,33 @@ var ( LEFT_BRACE: "{", RIGHT_BRACE: "}", COMMA: ",", - EQ: "=", - EQEQ: "==", - SEMICOLON: ";", - DOT: ".", - SPACE: "", - COLON: ":", + EQ: "=", + ADD_EQ: "+=", + SUB_EQ: "-=", + MUL_EQ: "*=", + DIV_EQ: "/=", + MOD_EQ: "%=", + + SEMICOLON: ";", + DOT: ".", + SPACE: "", + COLON: ":", + + EQEQ: "==", + NEQ: "!=", + LTE: "<=", + LT: "<", + GTE: ">=", + GT: ">", + + NOT: "!", SUB: "-", ADD: "+", MUL: "*", MOD: "%", DIV: "/", - NEQ: "!=", - LTE: "<=", - LT: "<", - GTE: ">=", - GT: ">", + // XOR: "^", AND: "&&", OR: "||", @@ -244,24 +254,47 @@ func lexStatements(l *Lexer) stateFn { return lexSpaceNotEOL case r == '*': - l.emit(MUL) - return lexSpace + if t := l.peek(); t == '=' { + l.next() + l.emit(MUL_EQ) + } else { + l.emit(MUL) + return lexSpace + } case r == '/': - l.emit(DIV) - return lexSpace - + if t := l.peek(); t == '=' { + l.next() + l.emit(DIV_EQ) + } else { + l.emit(DIV) + return lexSpace + } case r == '%': - l.emit(MOD) - return lexSpace - + if t := l.peek(); t == '=' { + l.next() + l.emit(MOD_EQ) + } else { + l.emit(MOD) + return lexSpace + } case r == '+': - l.emit(ADD) - return lexSpace + if t := l.peek(); t == '=' { + l.next() + l.emit(ADD_EQ) + } else { + l.emit(ADD) + return lexSpace + } case r == '-': - l.emit(SUB) - return lexSpace + if t := l.peek(); t == '=' { + l.next() + l.emit(SUB_EQ) + } else { + l.emit(SUB) + return lexSpace + } // case r == '^': // l.emit(XOR) @@ -310,11 +343,12 @@ func lexStatements(l *Lexer) stateFn { return lexSpace case r == '!': - switch nr := l.next(); { + switch nr := l.peek(); { case nr == '=': + l.next() l.emit(NEQ) default: - return l.errorf("unexpected character `%q' after `!'", nr) + l.emit(NOT) } return lexSpace diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 54f5aea..942bd4d 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -373,11 +373,39 @@ func (p *parser) newIfElem(ifTk Item, condition *ast.Node, block *ast.BlockStmt) return ifElem } -func (p *parser) newAssignmentExpr(l, r *ast.Node, eqOp Item) *ast.Node { - return ast.WrapAssignmentExpr(&ast.AssignmentExpr{ +func (p *parser) newUnaryExpr(op Item, r *ast.Node) *ast.Node { + switch op.Typ { + case ADD, SUB: + // 负数 + switch r.NodeType { + case ast.TypeFloatLiteral: + if op.Typ == SUB { + r.FloatLiteral.Val = -r.FloatLiteral.Val + } + r.FloatLiteral.Start = p.posCache.LnCol(op.Pos) + return r + case ast.TypeIntegerLiteral: + if op.Typ == SUB { + r.IntegerLiteral.Val = -r.IntegerLiteral.Val + } + r.IntegerLiteral.Start = p.posCache.LnCol(op.Pos) + return r + } + } + + return ast.WrapUnaryExpr(&ast.UnaryExpr{ + Op: AstOp(op.Typ), + RHS: r, + OpPos: p.posCache.LnCol(op.Pos), + }) +} + +func (p *parser) newAssignmentStmt(l, r *ast.Node, op Item) *ast.Node { + return ast.WrapAssignmentStmt(&ast.AssignmentExpr{ LHS: l, + Op: AstOp(op.Typ), RHS: r, - OpPos: p.posCache.LnCol(eqOp.Pos), + OpPos: p.posCache.LnCol(op.Pos), }) } diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index bd6e8f3..acc0d9b 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -130,7 +130,8 @@ func TestExprSeparation(t *testing.T) { ast.WrapMapInitExpr(&ast.MapInitExpr{}), }, }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), @@ -164,7 +165,8 @@ func TestExprSeparation(t *testing.T) { RHS: ast.WrapIdentifier(&ast.Identifier{Name: "y"}), }), Block: &ast.BlockStmt{Stmts: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), @@ -177,15 +179,18 @@ func TestExprSeparation(t *testing.T) { }, }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapIdentifier(&ast.Identifier{Name: "d"}), }), @@ -202,7 +207,8 @@ func TestExprSeparation(t *testing.T) { RHS: ast.WrapIdentifier(&ast.Identifier{Name: "y"}), }), Block: &ast.BlockStmt{Stmts: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), @@ -255,6 +261,114 @@ func TestExprSeparation(t *testing.T) { ), }, }, + { + name: "+||", + in: `a * -1 + b || x + 1 && 1 / 2 == 1 + a += b + +!-1 + a -= 1 + a *= 1 + a /= 1 + a %= 1 + `, + expected: ast.Stmts{ + ast.WrapConditionExpr( + &ast.ConditionalExpr{ + Op: AstOp(EQEQ), + LHS: ast.WrapConditionExpr(&ast.ConditionalExpr{ + Op: AstOp(OR), + LHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ + Op: AstOp(ADD), + LHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ + Op: AstOp(MUL), + LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: -1}), + }), + RHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), + }), + RHS: ast.WrapConditionExpr(&ast.ConditionalExpr{ + Op: AstOp(AND), + LHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ + Op: AstOp(ADD), + LHS: ast.WrapIdentifier(&ast.Identifier{Name: "x"}), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), + }), + RHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ + Op: AstOp(DIV), + LHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 2}), + }), + }), + }), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), + }, + ), + ast.WrapAssignmentStmt( + &ast.AssignmentExpr{ + Op: ast.ADDEQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "a", + }), + RHS: ast.WrapArithmeticExpr( + &ast.ArithmeticExpr{ + Op: ast.ADD, + LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), + RHS: ast.WrapUnaryExpr(&ast.UnaryExpr{ + Op: ast.ADD, + RHS: ast.WrapUnaryExpr(&ast.UnaryExpr{ + Op: ast.NOT, + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: -1}), + }), + }), + }, + ), + }, + ), + ast.WrapAssignmentStmt( + &ast.AssignmentExpr{ + Op: ast.SUBEQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "a", + }), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{ + Val: 1, + }), + }, + ), + ast.WrapAssignmentStmt( + &ast.AssignmentExpr{ + Op: ast.MULEQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "a", + }), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{ + Val: 1, + }), + }, + ), + ast.WrapAssignmentStmt( + &ast.AssignmentExpr{ + Op: ast.DIVEQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "a", + }), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{ + Val: 1, + }), + }, + ), + ast.WrapAssignmentStmt( + &ast.AssignmentExpr{ + Op: ast.MODEQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "a", + }), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{ + Val: 1, + }), + }, + ), + }, + }, } // for idx := len(cases) - 1; idx >= 0; idx-- { @@ -276,7 +390,7 @@ func TestExprSeparation(t *testing.T) { y = Stmts.String() assert.Nil(t, err) assert.Equal(t, x, y) - t.Logf("ok %s -> %s", tc.in, y) + // t.Logf("ok %s -> %s", tc.in, y) } else { t.Logf("%s -> expect fail: %v", tc.in, err) assert.NotNil(t, err, "") @@ -303,7 +417,8 @@ func TestParserFor(t *testing.T) { Varb: ast.WrapIdentifier(&ast.Identifier{Name: "x"}), Iter: ast.WrapIdentifier(&ast.Identifier{Name: "y"}), Body: &ast.BlockStmt{Stmts: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), @@ -348,7 +463,8 @@ func TestParserFor(t *testing.T) { ast.WrapBreakStmt(&ast.BreakStmt{}), }}, }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapArithmeticExpr(&ast.ArithmeticExpr{ LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), @@ -375,7 +491,8 @@ func TestParserFor(t *testing.T) { }), }), Body: &ast.BlockStmt{Stmts: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), @@ -396,7 +513,48 @@ func TestParserFor(t *testing.T) { Name: "func", }), }), - Loop: ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + Loop: ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "x", + }), + RHS: ast.WrapFloatLiteral(&ast.FloatLiteral{ + Val: 2., + }), + }), + Body: &ast.BlockStmt{Stmts: ast.Stmts{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, + LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), + RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), + }), + }}, + })}, + }, + { + name: "for init; cond ; loop ", + in: `for y=2; a == func() ; x = 2. { + b=2 + }`, + expected: ast.Stmts{ast.WrapForStmt(&ast.ForStmt{ + Init: ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, + LHS: ast.WrapIdentifier(&ast.Identifier{ + Name: "y", + }), + RHS: ast.WrapFloatLiteral(&ast.FloatLiteral{ + Val: 2., + }), + }), + Cond: ast.WrapConditionExpr(&ast.ConditionalExpr{ + Op: AstOp(EQEQ), + LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), + RHS: ast.WrapCallExpr(&ast.CallExpr{ + Name: "func", + }), + }), + Loop: ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{ Name: "x", }), @@ -405,7 +563,8 @@ func TestParserFor(t *testing.T) { }), }), Body: &ast.BlockStmt{Stmts: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), @@ -978,7 +1137,8 @@ func TestParser(t *testing.T) { expected: ast.Stmts{ ast.WrapCallExpr(&ast.CallExpr{ Name: "f", - Param: []*ast.Node{ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + Param: []*ast.Node{ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "arg"}), RHS: ast.WrapAttrExpr(&ast.AttrExpr{ Obj: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), @@ -998,14 +1158,16 @@ func TestParser(t *testing.T) { name: "func_call_in_assignement_right", in: `a = fn("a", true, a1=["b", 1.1])`, expected: ast.Stmts{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapCallExpr(&ast.CallExpr{ Name: "fn", Param: []*ast.Node{ ast.WrapStringLiteral(&ast.StringLiteral{Val: "a"}), ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a1"}), RHS: ast.WrapListInitExpr(&ast.ListInitExpr{ List: []*ast.Node{ @@ -1028,11 +1190,13 @@ func TestParser(t *testing.T) { &ast.CallExpr{ Name: "f", Param: []*ast.Node{ - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "arg1"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "arg2"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 2}), }), @@ -1055,7 +1219,8 @@ func TestParser(t *testing.T) { Op: ast.GT, RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), }), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "arg2"}), RHS: ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 2}), }), @@ -1110,7 +1275,8 @@ func TestParser(t *testing.T) { }, }), ast.WrapMapInitExpr(&ast.MapInitExpr{}), - ast.WrapAssignmentExpr(&ast.AssignmentExpr{ + ast.WrapAssignmentStmt(&ast.AssignmentExpr{ + Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), RHS: ast.WrapMapInitExpr(&ast.MapInitExpr{ KeyValeList: [][2]*ast.Node{