diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 3e78059..843f5e2 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -134,324 +134,322 @@ func (e Stmts) String() string { return strings.Join(arr, "\n") } +type AstNode interface { + String() string +} + type Node struct { // node type NodeType NodeType + elem AstNode +} - // expr - Identifier *Identifier - - StringLiteral *StringLiteral - IntegerLiteral *IntegerLiteral - FloatLiteral *FloatLiteral - BoolLiteral *BoolLiteral - NilLiteral *NilLiteral - - ListInitExpr *ListInitExpr - MapInitExpr *MapInitExpr - - ParenExpr *ParenExpr - - AttrExpr *AttrExpr - IndexExpr *IndexExpr - InExpr *InExpr - - UnaryExpr *UnaryExpr - ArithmeticExpr *ArithmeticExpr - ConditionalExpr *ConditionalExpr - AssignmentExpr *AssignmentExpr - - CallExpr *CallExpr - - // stmt - BlockStmt *BlockStmt +func (n *Node) String() string { + if n.elem != nil { + return n.elem.String() + } + return n.NodeType.String() +} - IfelseStmt *IfelseStmt - ForStmt *ForStmt - ForInStmt *ForInStmt - ContinueStmt *ContinueStmt - BreakStmt *BreakStmt +func (n *Node) Identifier() *Identifier { + return n.elem.(*Identifier) +} +func (n *Node) StringLiteral() *StringLiteral { + return n.elem.(*StringLiteral) +} +func (n *Node) IntegerLiteral() *IntegerLiteral { + return n.elem.(*IntegerLiteral) +} +func (n *Node) FloatLiteral() *FloatLiteral { + return n.elem.(*FloatLiteral) +} +func (n *Node) BoolLiteral() *BoolLiteral { + return n.elem.(*BoolLiteral) +} +func (n *Node) NilLiteral() *NilLiteral { + return n.elem.(*NilLiteral) +} +func (n *Node) ListInitExpr() *ListInitExpr { + return n.elem.(*ListInitExpr) +} +func (n *Node) MapInitExpr() *MapInitExpr { + return n.elem.(*MapInitExpr) +} +func (n *Node) ParenExpr() *ParenExpr { + return n.elem.(*ParenExpr) +} +func (n *Node) AttrExpr() *AttrExpr { + return n.elem.(*AttrExpr) +} +func (n *Node) IndexExpr() *IndexExpr { + return n.elem.(*IndexExpr) +} +func (n *Node) InExpr() *InExpr { + return n.elem.(*InExpr) +} +func (n *Node) UnaryExpr() *UnaryExpr { + return n.elem.(*UnaryExpr) +} +func (n *Node) ArithmeticExpr() *ArithmeticExpr { + return n.elem.(*ArithmeticExpr) +} +func (n *Node) ConditionalExpr() *ConditionalExpr { + return n.elem.(*ConditionalExpr) +} +func (n *Node) AssignmentExpr() *AssignmentExpr { + return n.elem.(*AssignmentExpr) +} +func (n *Node) CallExpr() *CallExpr { + return n.elem.(*CallExpr) +} +func (n *Node) BlockStmt() *BlockStmt { + return n.elem.(*BlockStmt) +} +func (n *Node) IfelseStmt() *IfelseStmt { + return n.elem.(*IfelseStmt) +} +func (n *Node) ForStmt() *ForStmt { + return n.elem.(*ForStmt) +} +func (n *Node) ForInStmt() *ForInStmt { + return n.elem.(*ForInStmt) +} +func (n *Node) ContinueStmt() *ContinueStmt { + return n.elem.(*ContinueStmt) +} +func (n *Node) BreakStmt() *BreakStmt { + return n.elem.(*BreakStmt) } -func (node *Node) String() string { - switch node.NodeType { //nolint:exhaustive - case TypeIdentifier: - return node.Identifier.String() - case TypeStringLiteral: - return node.StringLiteral.String() - case TypeIntegerLiteral: - return node.IntegerLiteral.String() - case TypeFloatLiteral: - return node.FloatLiteral.String() - case TypeBoolLiteral: - return node.BoolLiteral.String() - case TypeNilLiteral: - return node.NilLiteral.String() - case TypeListInitExpr: - return node.ListInitExpr.String() - case TypeMapInitExpr: - return node.MapInitExpr.String() - case TypeParenExpr: - return node.ParenExpr.String() - case TypeAttrExpr: - return node.AttrExpr.String() - case TypeIndexExpr: - return node.IndexExpr.String() - case TypeUnaryExpr: - return node.UnaryExpr.String() - case TypeArithmeticExpr: - return node.ArithmeticExpr.String() - case TypeConditionalExpr: - return node.ConditionalExpr.String() - case TypeAssignmentExpr: - return node.AssignmentExpr.String() - case TypeCallExpr: - return node.CallExpr.String() - case TypeIfelseStmt: - return node.IfelseStmt.String() - case TypeForStmt: - return node.ForStmt.String() - case TypeForInStmt: - return node.ForInStmt.String() - case TypeContinueStmt: - return node.ContinueStmt.String() - case TypeBreakStmt: - return node.BreakStmt.String() - case TypeInExpr: - return node.InExpr.String() - } - return "node conv to string failed" +func (n *Node) StartPos() token.LnColPos { + return NodeStartPos(n) } func WrapIdentifier(node *Identifier) *Node { return &Node{ - NodeType: TypeIdentifier, - Identifier: node, + NodeType: TypeIdentifier, + elem: node, } } func WrapStringLiteral(node *StringLiteral) *Node { return &Node{ - NodeType: TypeStringLiteral, - StringLiteral: node, + NodeType: TypeStringLiteral, + elem: node, } } func WrapIntegerLiteral(node *IntegerLiteral) *Node { return &Node{ - NodeType: TypeIntegerLiteral, - IntegerLiteral: node, + NodeType: TypeIntegerLiteral, + elem: node, } } func WrapFloatLiteral(node *FloatLiteral) *Node { return &Node{ - NodeType: TypeFloatLiteral, - FloatLiteral: node, + NodeType: TypeFloatLiteral, + elem: node, } } func WrapBoolLiteral(node *BoolLiteral) *Node { return &Node{ - NodeType: TypeBoolLiteral, - BoolLiteral: node, + NodeType: TypeBoolLiteral, + elem: node, } } func WrapNilLiteral(node *NilLiteral) *Node { return &Node{ - NodeType: TypeNilLiteral, - NilLiteral: node, + NodeType: TypeNilLiteral, + elem: node, } } func WrapListInitExpr(node *ListInitExpr) *Node { return &Node{ - NodeType: TypeListInitExpr, - ListInitExpr: node, + NodeType: TypeListInitExpr, + elem: node, } } func WrapMapInitExpr(node *MapInitExpr) *Node { return &Node{ - NodeType: TypeMapInitExpr, - MapInitExpr: node, + NodeType: TypeMapInitExpr, + elem: node, } } func WrapParenExpr(node *ParenExpr) *Node { return &Node{ - NodeType: TypeParenExpr, - ParenExpr: node, + NodeType: TypeParenExpr, + elem: node, } } func WrapAttrExpr(node *AttrExpr) *Node { return &Node{ NodeType: TypeAttrExpr, - AttrExpr: node, + elem: node, } } func WrapIndexExpr(node *IndexExpr) *Node { return &Node{ - NodeType: TypeIndexExpr, - IndexExpr: node, + NodeType: TypeIndexExpr, + elem: node, } } func WrapArithmeticExpr(node *ArithmeticExpr) *Node { return &Node{ - NodeType: TypeArithmeticExpr, - ArithmeticExpr: node, + NodeType: TypeArithmeticExpr, + elem: node, } } func WrapConditionExpr(node *ConditionalExpr) *Node { return &Node{ - NodeType: TypeConditionalExpr, - ConditionalExpr: node, + NodeType: TypeConditionalExpr, + elem: node, } } func WrapInExpr(node *InExpr) *Node { return &Node{ NodeType: TypeInExpr, - InExpr: node, + elem: node, } } func WrapUnaryExpr(node *UnaryExpr) *Node { return &Node{ - NodeType: TypeUnaryExpr, - UnaryExpr: node, + NodeType: TypeUnaryExpr, + elem: node, } } func WrapAssignmentStmt(node *AssignmentExpr) *Node { return &Node{ - NodeType: TypeAssignmentExpr, - AssignmentExpr: node, + NodeType: TypeAssignmentExpr, + elem: node, } } func WrapCallExpr(node *CallExpr) *Node { return &Node{ NodeType: TypeCallExpr, - CallExpr: node, + elem: node, } } func WrapIfelseStmt(node *IfelseStmt) *Node { return &Node{ - NodeType: TypeIfelseStmt, - IfelseStmt: node, + NodeType: TypeIfelseStmt, + elem: node, } } func WrapForStmt(node *ForStmt) *Node { return &Node{ NodeType: TypeForStmt, - ForStmt: node, + elem: node, } } func WrapForInStmt(node *ForInStmt) *Node { return &Node{ - NodeType: TypeForInStmt, - ForInStmt: node, + NodeType: TypeForInStmt, + elem: node, } } func WrapContinueStmt(node *ContinueStmt) *Node { return &Node{ - NodeType: TypeContinueStmt, - ContinueStmt: node, + NodeType: TypeContinueStmt, + elem: node, } } func WrapBreakStmt(node *BreakStmt) *Node { return &Node{ - NodeType: TypeBreakStmt, - BreakStmt: node, + NodeType: TypeBreakStmt, + elem: node, } } func WrapeBlockStmt(node *BlockStmt) *Node { return &Node{ - NodeType: TypeBlockStmt, - BlockStmt: node, + NodeType: TypeBlockStmt, + elem: node, } } -func (node *Node) StartPos() token.LnColPos { - return NodeStartPos(node) -} - func NodeStartPos(node *Node) token.LnColPos { - if node == nil { + if node == nil || node.elem == nil { return token.InvalidLnColPos } switch node.NodeType { case TypeInvalid: return token.InvalidLnColPos case TypeIdentifier: - return node.Identifier.Start + return node.Identifier().Start case TypeStringLiteral: - return node.StringLiteral.Start + return node.StringLiteral().Start case TypeIntegerLiteral: - return node.IntegerLiteral.Start + return node.IntegerLiteral().Start case TypeFloatLiteral: - return node.FloatLiteral.Start + return node.FloatLiteral().Start case TypeBoolLiteral: - return node.BoolLiteral.Start + return node.BoolLiteral().Start case TypeNilLiteral: - return node.NilLiteral.Start + return node.NilLiteral().Start case TypeListInitExpr: - return node.ListInitExpr.LBracket + return node.ListInitExpr().LBracket case TypeMapInitExpr: - return node.MapInitExpr.LBrace + return node.MapInitExpr().LBrace case TypeParenExpr: - return node.ParenExpr.LParen + return node.ParenExpr().LParen case TypeAttrExpr: - return node.AttrExpr.Start + return node.AttrExpr().Start case TypeIndexExpr: - return node.IndexExpr.Obj.Start + return node.IndexExpr().Obj.Start case TypeUnaryExpr: - return node.UnaryExpr.OpPos + return node.UnaryExpr().OpPos case TypeArithmeticExpr: - return node.ArithmeticExpr.LHS.StartPos() + return node.ArithmeticExpr().LHS.StartPos() case TypeConditionalExpr: - return node.ConditionalExpr.LHS.StartPos() + return node.ConditionalExpr().LHS.StartPos() case TypeAssignmentExpr: - return node.AssignmentExpr.LHS.StartPos() + return node.AssignmentExpr().LHS.StartPos() case TypeCallExpr: - return node.CallExpr.NamePos + return node.CallExpr().NamePos case TypeBlockStmt: - return node.BlockStmt.LBracePos + return node.BlockStmt().LBracePos case TypeIfelseStmt: - if len(node.IfelseStmt.IfList) > 0 { - return node.IfelseStmt.IfList[0].Start + if len(node.IfelseStmt().IfList) > 0 { + return node.IfelseStmt().IfList[0].Start } else { return token.InvalidLnColPos } case TypeForStmt: - return node.ForStmt.ForPos + return node.ForStmt().ForPos case TypeForInStmt: - return node.ForInStmt.ForPos + return node.ForInStmt().ForPos case TypeContinueStmt: - return node.ContinueStmt.Start + return node.ContinueStmt().Start case TypeBreakStmt: - return node.BreakStmt.Start + return node.BreakStmt().Start } return token.InvalidLnColPos } diff --git a/pkg/engine/callref.go b/pkg/engine/callref.go index 50d6721..0406e46 100644 --- a/pkg/engine/callref.go +++ b/pkg/engine/callref.go @@ -149,5 +149,5 @@ func getParamRefScript(expr *ast.CallExpr) (string, error) { return "", fmt.Errorf("param type expects StringLiteral got `%s`", expr.Param[0].NodeType) } - return expr.Param[0].StringLiteral.Val, nil + return expr.Param[0].StringLiteral().Val, nil } diff --git a/pkg/engine/runtime/checkstmt.go b/pkg/engine/runtime/checkstmt.go index 5200bbd..2ee616e 100644 --- a/pkg/engine/runtime/checkstmt.go +++ b/pkg/engine/runtime/checkstmt.go @@ -48,40 +48,40 @@ func RunStmtCheck(ctx *Context, ctxCheck *ContextCheck, node *ast.Node) *errchai case ast.TypeNilLiteral: // skip case ast.TypeListInitExpr: - return RunListInitExprCheck(ctx, ctxCheck, node.ListInitExpr) + return RunListInitExprCheck(ctx, ctxCheck, node.ListInitExpr()) case ast.TypeMapInitExpr: - return RunMapInitExprCheck(ctx, ctxCheck, node.MapInitExpr) + return RunMapInitExprCheck(ctx, ctxCheck, node.MapInitExpr()) case ast.TypeParenExpr: - return RunParenExprCheck(ctx, ctxCheck, node.ParenExpr) + return RunParenExprCheck(ctx, ctxCheck, node.ParenExpr()) case ast.TypeAttrExpr: - return RunAttrExprCheck(ctx, ctxCheck, node.AttrExpr) + return RunAttrExprCheck(ctx, ctxCheck, node.AttrExpr()) case ast.TypeIndexExpr: - return RunIndexExprGetCheck(ctx, ctxCheck, node.IndexExpr) + return RunIndexExprGetCheck(ctx, ctxCheck, node.IndexExpr()) case ast.TypeArithmeticExpr: - return RunArithmeticExprCheck(ctx, ctxCheck, node.ArithmeticExpr) + return RunArithmeticExprCheck(ctx, ctxCheck, node.ArithmeticExpr()) case ast.TypeConditionalExpr: - return RunConditionExprCheck(ctx, ctxCheck, node.ConditionalExpr) + return RunConditionExprCheck(ctx, ctxCheck, node.ConditionalExpr()) case ast.TypeUnaryExpr: - return RunUnaryExprCheck(ctx, ctxCheck, node.UnaryExpr) + return RunUnaryExprCheck(ctx, ctxCheck, node.UnaryExpr()) case ast.TypeAssignmentExpr: - return RunAssignmentExprCheck(ctx, ctxCheck, node.AssignmentExpr) + return RunAssignmentExprCheck(ctx, ctxCheck, node.AssignmentExpr()) case ast.TypeCallExpr: - return RunCallExprCheck(ctx, ctxCheck, node.CallExpr) + return RunCallExprCheck(ctx, ctxCheck, node.CallExpr()) case ast.TypeIfelseStmt: - return RunIfElseStmtCheck(ctx, ctxCheck, node.IfelseStmt) + return RunIfElseStmtCheck(ctx, ctxCheck, node.IfelseStmt()) case ast.TypeForStmt: - return RunForStmtCheck(ctx, ctxCheck, node.ForStmt) + return RunForStmtCheck(ctx, ctxCheck, node.ForStmt()) case ast.TypeForInStmt: - return RunForInStmtCheck(ctx, ctxCheck, node.ForInStmt) + return RunForInStmtCheck(ctx, ctxCheck, node.ForInStmt()) case ast.TypeContinueStmt: - return RunContinueStmtCheck(ctx, ctxCheck, node.ContinueStmt) + return RunContinueStmtCheck(ctx, ctxCheck, node.ContinueStmt()) case ast.TypeBreakStmt: - return RunBreakStmtCheck(ctx, ctxCheck, node.BreakStmt) + return RunBreakStmtCheck(ctx, ctxCheck, node.BreakStmt()) } return nil diff --git a/pkg/engine/runtime/runtime.go b/pkg/engine/runtime/runtime.go index 6c16354..fc6cd93 100644 --- a/pkg/engine/runtime/runtime.go +++ b/pkg/engine/runtime/runtime.go @@ -249,7 +249,7 @@ func RunForInStmt(ctx *Context, stmt *ast.ForInStmt) (any, ast.DType, *errchain. if stmt.Varb.NodeType != ast.TypeIdentifier { return nil, ast.Invalid, err } - _ = ctx.SetVarb(stmt.Varb.Identifier.Name, char, ast.String) + _ = ctx.SetVarb(stmt.Varb.Identifier().Name, char, ast.String) if stmt.Body != nil { if err := RunStmts(ctx, stmt.Body.Stmts); err != nil { return nil, ast.Invalid, err @@ -273,7 +273,7 @@ func RunForInStmt(ctx *Context, stmt *ast.ForInStmt) (any, ast.DType, *errchain. } for x := range iter { ctx.stackCur.Clear() - _ = ctx.SetVarb(stmt.Varb.Identifier.Name, x, ast.String) + _ = ctx.SetVarb(stmt.Varb.Identifier().Name, x, ast.String) if stmt.Body != nil { if err := RunStmts(ctx, stmt.Body.Stmts); err != nil { return nil, ast.Invalid, err @@ -300,7 +300,7 @@ func RunForInStmt(ctx *Context, stmt *ast.ForInStmt) (any, ast.DType, *errchain. return nil, ast.Invalid, NewRunError(ctx, "inner type error", stmt.Iter.StartPos()) } - _ = ctx.SetVarb(stmt.Varb.Identifier.Name, x, dtype) + _ = ctx.SetVarb(stmt.Varb.Identifier().Name, x, dtype) if stmt.Body != nil { if err := RunStmts(ctx, stmt.Body.Stmts); err != nil { return nil, ast.Invalid, err @@ -355,62 +355,62 @@ func RunStmt(ctx *Context, node *ast.Node) (any, ast.DType, *errchain.PlError) { } switch node.NodeType { //nolint:exhaustive case ast.TypeParenExpr: - return RunParenExpr(ctx, node.ParenExpr) + return RunParenExpr(ctx, node.ParenExpr()) case ast.TypeArithmeticExpr: - return RunArithmeticExpr(ctx, node.ArithmeticExpr) + return RunArithmeticExpr(ctx, node.ArithmeticExpr()) case ast.TypeConditionalExpr: - return RunConditionExpr(ctx, node.ConditionalExpr) + return RunConditionExpr(ctx, node.ConditionalExpr()) case ast.TypeUnaryExpr: - return RunUnaryExpr(ctx, node.UnaryExpr) + return RunUnaryExpr(ctx, node.UnaryExpr()) case ast.TypeAssignmentExpr: - return RunAssignmentExpr(ctx, node.AssignmentExpr) + return RunAssignmentExpr(ctx, node.AssignmentExpr()) case ast.TypeCallExpr: - return RunCallExpr(ctx, node.CallExpr) + return RunCallExpr(ctx, node.CallExpr()) case ast.TypeInExpr: - return RunInExpr(ctx, node.InExpr) + return RunInExpr(ctx, node.InExpr()) case ast.TypeListInitExpr: - return RunListInitExpr(ctx, node.ListInitExpr) + return RunListInitExpr(ctx, node.ListInitExpr()) case ast.TypeIdentifier: - if v, err := ctx.GetKey(node.Identifier.Name); err != nil { + if v, err := ctx.GetKey(node.Identifier().Name); err != nil { return nil, ast.Nil, nil } else { return v.Value, v.DType, nil } case ast.TypeMapInitExpr: - return RunMapInitExpr(ctx, node.MapInitExpr) + return RunMapInitExpr(ctx, node.MapInitExpr()) // use for map, slice and array case ast.TypeIndexExpr: - return RunIndexExprGet(ctx, node.IndexExpr) + return RunIndexExprGet(ctx, node.IndexExpr()) // TODO case ast.TypeAttrExpr: return nil, ast.Void, nil case ast.TypeBoolLiteral: - return node.BoolLiteral.Val, ast.Bool, nil + return node.BoolLiteral().Val, ast.Bool, nil case ast.TypeIntegerLiteral: - return node.IntegerLiteral.Val, ast.Int, nil + return node.IntegerLiteral().Val, ast.Int, nil case ast.TypeFloatLiteral: - return node.FloatLiteral.Val, ast.Float, nil + return node.FloatLiteral().Val, ast.Float, nil case ast.TypeStringLiteral: - return node.StringLiteral.Val, ast.String, nil + return node.StringLiteral().Val, ast.String, nil case ast.TypeNilLiteral: return nil, ast.Nil, nil case ast.TypeIfelseStmt: - return RunIfElseStmt(ctx, node.IfelseStmt) + return RunIfElseStmt(ctx, node.IfelseStmt()) case ast.TypeForStmt: - return RunForStmt(ctx, node.ForStmt) + return RunForStmt(ctx, node.ForStmt()) case ast.TypeForInStmt: - return RunForInStmt(ctx, node.ForInStmt) + return RunForInStmt(ctx, node.ForInStmt()) case ast.TypeBreakStmt: - return RunBreakStmt(ctx, node.BreakStmt) + return RunBreakStmt(ctx, node.BreakStmt()) case ast.TypeContinueStmt: - return RunContinueStmt(ctx, node.ContinueStmt) + return RunContinueStmt(ctx, node.ContinueStmt()) default: return nil, ast.Invalid, NewRunError(ctx, fmt.Sprintf( "unsupported ast node: %s", reflect.TypeOf(node).String()), node.StartPos()) @@ -845,6 +845,7 @@ func runAssignArith(ctx *Context, l, r *Varb, op ast.Op, pos token.LnColPos) ( return v, dtype, nil } +// RunAssignmentExpr runs assignment expression, but actually it is a stmt func RunAssignmentExpr(ctx *Context, expr *ast.AssignmentExpr) (any, ast.DType, *errchain.PlError) { v, dtype, err := RunStmt(ctx, expr.RHS) if err != nil { @@ -856,7 +857,7 @@ func RunAssignmentExpr(ctx *Context, expr *ast.AssignmentExpr) (any, ast.DType, case ast.TypeIdentifier: switch expr.Op { case ast.EQ: - _ = ctx.SetVarb(expr.LHS.Identifier.Name, v, dtype) + _ = ctx.SetVarb(expr.LHS.Identifier().Name, v, dtype) return v, dtype, nil case ast.SUBEQ, @@ -864,14 +865,14 @@ func RunAssignmentExpr(ctx *Context, expr *ast.AssignmentExpr) (any, ast.DType, ast.MULEQ, ast.DIVEQ, ast.MODEQ: - lVarb, err := ctx.GetKey(expr.LHS.Identifier.Name) + 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) + _ = ctx.SetVarb(expr.LHS.Identifier().Name, v, dt) return v, dt, nil } @@ -882,29 +883,29 @@ func RunAssignmentExpr(ctx *Context, expr *ast.AssignmentExpr) (any, ast.DType, case ast.TypeIndexExpr: switch expr.Op { case ast.EQ: - varb, err := ctx.GetKey(expr.LHS.IndexExpr.Obj.Name) + 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 nil, ast.Invalid, NewRunError(ctx, err.Error(), expr.LHS.IndexExpr().Obj.Start) } - return changeListOrMapValue(ctx, varb.Value, expr.LHS.IndexExpr.Index, + 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) + 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 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 { + if v, dt, errR := searchListAndMap(ctx, varb.Value, expr.LHS.IndexExpr().Index); errR != 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, + return changeListOrMapValue(ctx, varb.Value, expr.LHS.IndexExpr().Index, v, dt) } default: diff --git a/pkg/engine/runtime/runtime_test.go b/pkg/engine/runtime/runtime_test.go index a84cd02..d6c4e7a 100644 --- a/pkg/engine/runtime/runtime_test.go +++ b/pkg/engine/runtime/runtime_test.go @@ -423,7 +423,7 @@ func TestUnaryErrExpr(t *testing.T) { "ckfn": ckfnErrcheck, }) if errR == nil { - t.Fatal(*errR) + t.Fatal(errR) } else { t.Log(errR.Error()) } @@ -1011,9 +1011,9 @@ func addkeytest(ctx *Context, callExpr *ast.CallExpr) *errchain.PlError { var key string switch callExpr.Param[0].NodeType { case ast.TypeIdentifier: - key = callExpr.Param[0].Identifier.Name + key = callExpr.Param[0].Identifier().Name case ast.TypeStringLiteral: - key = callExpr.Param[0].StringLiteral.Val + key = callExpr.Param[0].StringLiteral().Val default: return NewRunError(ctx, "key type", callExpr.NamePos) } diff --git a/pkg/inimpl/guancecloud/funcs/fn_addpattern.go b/pkg/inimpl/guancecloud/funcs/fn_addpattern.go index 1d293a2..57b6b91 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_addpattern.go +++ b/pkg/inimpl/guancecloud/funcs/fn_addpattern.go @@ -23,7 +23,7 @@ func AddPatternChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain. var name, pattern string switch funcExpr.Param[0].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - name = funcExpr.Param[0].StringLiteral.Val + name = funcExpr.Param[0].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteral, got %s", funcExpr.Param[0].NodeType), funcExpr.NamePos) @@ -31,7 +31,7 @@ func AddPatternChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain. switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - pattern = funcExpr.Param[1].StringLiteral.Val + pattern = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteral, got %s", funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) diff --git a/pkg/inimpl/guancecloud/funcs/fn_cast.go b/pkg/inimpl/guancecloud/funcs/fn_cast.go index dc288b3..e301c5d 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_cast.go +++ b/pkg/inimpl/guancecloud/funcs/fn_cast.go @@ -25,11 +25,11 @@ func CastChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlErro switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - switch funcExpr.Param[1].StringLiteral.Val { + switch funcExpr.Param[1].StringLiteral().Val { case "bool", "int", "float", "str", "string": default: return runtime.NewRunError(ctx, fmt.Sprintf("unsupported data type: %s", - funcExpr.Param[1].StringLiteral.Val), funcExpr.Param[1].StartPos()) + funcExpr.Param[1].StringLiteral().Val), funcExpr.Param[1].StartPos()) } default: return runtime.NewRunError(ctx, fmt.Sprintf("param type expect StringLiteral, got `%s'", @@ -53,7 +53,7 @@ func Cast(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - castType = funcExpr.Param[1].StringLiteral.Val + castType = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("param type expect StringLiteral, got `%s'", funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) diff --git a/pkg/inimpl/guancecloud/funcs/fn_datetime.go b/pkg/inimpl/guancecloud/funcs/fn_datetime.go index 30830c1..e8e8b79 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_datetime.go +++ b/pkg/inimpl/guancecloud/funcs/fn_datetime.go @@ -57,7 +57,7 @@ func DateTime(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - precision = funcExpr.Param[1].StringLiteral.Val + precision = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf( "param `precision` expect StringLiteral, got %s", @@ -66,7 +66,7 @@ func DateTime(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[2].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - fmts = funcExpr.Param[2].StringLiteral.Val + fmts = funcExpr.Param[2].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf( "param `fmt` expect StringLiteral, got %s", diff --git a/pkg/inimpl/guancecloud/funcs/fn_default_time.go b/pkg/inimpl/guancecloud/funcs/fn_default_time.go index 1dfb726..4531ebd 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_default_time.go +++ b/pkg/inimpl/guancecloud/funcs/fn_default_time.go @@ -58,7 +58,7 @@ func DefaultTime(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError if len(funcExpr.Param) > 1 { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - tz = funcExpr.Param[1].StringLiteral.Val + tz = funcExpr.Param[1].StringLiteral().Val default: err = fmt.Errorf("param key expect StringLiteral, got %s", funcExpr.Param[1].NodeType) @@ -85,6 +85,7 @@ func DefaultTime(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError } func usePointTime(ctx *runtime.Context, key string, err error) { + _ = key _ = addKey2PtWithVal(ctx.InData(), runtime.PlRunInfoField, fmt.Sprintf("time convert failed: %v", err), ast.String, input.KindPtDefault) diff --git a/pkg/inimpl/guancecloud/funcs/fn_getkey.go b/pkg/inimpl/guancecloud/funcs/fn_getkey.go index df000a6..a52dc19 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_getkey.go +++ b/pkg/inimpl/guancecloud/funcs/fn_getkey.go @@ -11,6 +11,7 @@ import ( "github.com/GuanceCloud/platypus/pkg/ast" "github.com/GuanceCloud/platypus/pkg/engine/runtime" "github.com/GuanceCloud/platypus/pkg/errchain" + "github.com/GuanceCloud/platypus/pkg/token" ) func GetkeyChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { @@ -28,7 +29,7 @@ func GetkeyChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlEr func Getkey(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { if funcExpr == nil { - return runtime.NewRunError(ctx, "unreachable", funcExpr.NamePos) + return runtime.NewRunError(ctx, "unreachable", token.InvalidLnColPos) } if len(funcExpr.Param) != 1 { return runtime.NewRunError(ctx, fmt.Sprintf("func %s expected 1 args", diff --git a/pkg/inimpl/guancecloud/funcs/fn_grok.go b/pkg/inimpl/guancecloud/funcs/fn_grok.go index 7aa40f9..8744421 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_grok.go +++ b/pkg/inimpl/guancecloud/funcs/fn_grok.go @@ -42,7 +42,7 @@ func GrokChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlErro var pattern string switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - pattern = funcExpr.Param[1].StringLiteral.Val + pattern = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf( "expect StringLiteral, got %s", @@ -81,7 +81,7 @@ func Grok(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { if len(funcExpr.Param) == 3 { switch funcExpr.Param[2].NodeType { //nolint:exhaustive case ast.TypeBoolLiteral: - trimSpace = funcExpr.Param[2].BoolLiteral.Val + trimSpace = funcExpr.Param[2].BoolLiteral().Val default: ctx.Regs.ReturnAppend(false, ast.Bool) return runtime.NewRunError(ctx, fmt.Sprintf("param key expect BoolLiteral, got `%s'", diff --git a/pkg/inimpl/guancecloud/funcs/fn_replace.go b/pkg/inimpl/guancecloud/funcs/fn_replace.go index 40bd091..9835f0b 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_replace.go +++ b/pkg/inimpl/guancecloud/funcs/fn_replace.go @@ -58,7 +58,7 @@ func Replace(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - pattern = funcExpr.Param[1].StringLiteral.Val + pattern = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteral, got %s", funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) @@ -66,7 +66,7 @@ func Replace(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[2].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - dz = funcExpr.Param[2].StringLiteral.Val + dz = funcExpr.Param[2].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteral, got %s", funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) diff --git a/pkg/inimpl/guancecloud/funcs/fn_set_measurement.go b/pkg/inimpl/guancecloud/funcs/fn_set_measurement.go index b3f5a9c..7c504df 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_set_measurement.go +++ b/pkg/inimpl/guancecloud/funcs/fn_set_measurement.go @@ -52,7 +52,7 @@ func SetMeasurement(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlEr if len(funcExpr.Param) == 2 && funcExpr.Param[1].NodeType == ast.TypeBoolLiteral { - if funcExpr.Param[1].BoolLiteral.Val { + if funcExpr.Param[1].BoolLiteral().Val { switch funcExpr.Param[0].NodeType { //nolint:exhaustive case ast.TypeIdentifier, ast.TypeAttrExpr: if key, err := getKeyName(funcExpr.Param[0]); err == nil { diff --git a/pkg/inimpl/guancecloud/funcs/fn_strfmt.go b/pkg/inimpl/guancecloud/funcs/fn_strfmt.go index da4a1fd..ee04aad 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_strfmt.go +++ b/pkg/inimpl/guancecloud/funcs/fn_strfmt.go @@ -48,7 +48,7 @@ func Strfmt(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - fmts = funcExpr.Param[1].StringLiteral.Val + fmts = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf( "param fmt expect StringLiteral, got `%s'", diff --git a/pkg/inimpl/guancecloud/funcs/fn_trim.go b/pkg/inimpl/guancecloud/funcs/fn_trim.go index ee8d282..a71b496 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_trim.go +++ b/pkg/inimpl/guancecloud/funcs/fn_trim.go @@ -50,7 +50,7 @@ func Trim(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { if len(funcExpr.Param) == 2 { switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - cutset = funcExpr.Param[1].StringLiteral.Val + cutset = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("param type expect StringLiteral, got `%s'", funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) diff --git a/pkg/inimpl/guancecloud/funcs/fn_use.go b/pkg/inimpl/guancecloud/funcs/fn_use.go index 1d7c2da..e70fe9d 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_use.go +++ b/pkg/inimpl/guancecloud/funcs/fn_use.go @@ -43,11 +43,11 @@ func Use(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { if s, ok := funcExpr.PrivateData.(*runtime.Script); ok { refScript = s } else { - l.Debugf("unknown error: %s", funcExpr.Param[0].StringLiteral.Val) + l.Debugf("unknown error: %s", funcExpr.Param[0].StringLiteral().Val) return nil } } else { - l.Debugf("script not found: %s", funcExpr.Param[0].StringLiteral.Val) + l.Debugf("script not found: %s", funcExpr.Param[0].StringLiteral().Val) return nil } default: diff --git a/pkg/inimpl/guancecloud/funcs/fn_use_test.go b/pkg/inimpl/guancecloud/funcs/fn_use_test.go index 566f7a4..15c4845 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_use_test.go +++ b/pkg/inimpl/guancecloud/funcs/fn_use_test.go @@ -36,7 +36,6 @@ func TestUse(t *testing.T) { Fields: map[string]interface{}{ "b": int64(1), }, - Time: timenow, Drop: false, } diff --git a/pkg/inimpl/guancecloud/funcs/fn_xml.go b/pkg/inimpl/guancecloud/funcs/fn_xml.go index 45f1ae0..19c9dac 100644 --- a/pkg/inimpl/guancecloud/funcs/fn_xml.go +++ b/pkg/inimpl/guancecloud/funcs/fn_xml.go @@ -68,7 +68,7 @@ func XML(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError { // XPath expression switch funcExpr.Param[1].NodeType { //nolint:exhaustive case ast.TypeStringLiteral: - xpathExpr = funcExpr.Param[1].StringLiteral.Val + xpathExpr = funcExpr.Param[1].StringLiteral().Val default: return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteStringLiteral:ral, got %s", reflect.TypeOf(funcExpr.Param[1]).String()), funcExpr.Param[1].StartPos()) diff --git a/pkg/inimpl/guancecloud/funcs/utils.go b/pkg/inimpl/guancecloud/funcs/utils.go index da78239..e9b1478 100644 --- a/pkg/inimpl/guancecloud/funcs/utils.go +++ b/pkg/inimpl/guancecloud/funcs/utils.go @@ -22,11 +22,11 @@ func getKeyName(node *ast.Node) (string, error) { switch node.NodeType { //nolint:exhaustive case ast.TypeIdentifier: - key = node.Identifier.Name + key = node.Identifier().Name case ast.TypeAttrExpr: - key = node.AttrExpr.String() + key = node.AttrExpr().String() case ast.TypeStringLiteral: - key = node.StringLiteral.Val + key = node.StringLiteral().Val default: return "", fmt.Errorf("expect StringLiteral or Identifier or AttrExpr, got %s", node.NodeType) @@ -217,7 +217,7 @@ func reIndexFuncArgs(fnStmt *ast.CallExpr, keyList []string, reqParm int) error if beforPosArg { beforPosArg = false } - kname, err := getKeyName(arg.AssignmentExpr.LHS) + kname, err := getKeyName(arg.AssignmentExpr().LHS) if err != nil { return err } @@ -225,7 +225,7 @@ func reIndexFuncArgs(fnStmt *ast.CallExpr, keyList []string, reqParm int) error if !ok { return fmt.Errorf("argument %s does not exist", kname) } - ret[kIndex] = arg.AssignmentExpr.RHS + ret[kIndex] = arg.AssignmentExpr().RHS } else { if !beforPosArg { return fmt.Errorf("positional arguments cannot follow keyword arguments") diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 942bd4d..cfa4ee3 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -198,7 +198,7 @@ func (p *parser) newListInitAppendExpr(initExpr *ast.Node, elem *ast.Node) *ast. return nil } - initExpr.ListInitExpr.List = append(initExpr.ListInitExpr.List, elem) + initExpr.ListInitExpr().List = append(initExpr.ListInitExpr().List, elem) return initExpr } @@ -208,7 +208,7 @@ func (p *parser) newListInitEndExpr(initExpr *ast.Node, pos plToken.Pos) *ast.No "%s object is not ListInitExpr", initExpr.NodeType) return nil } - initExpr.ListInitExpr.RBracket = p.posCache.LnCol(pos) + initExpr.ListInitExpr().RBracket = p.posCache.LnCol(pos) return initExpr } @@ -226,7 +226,7 @@ func (p *parser) newMapInitAppendExpr(initExpr *ast.Node, keyNode *ast.Node, val return nil } - initExpr.MapInitExpr.KeyValeList = append(initExpr.MapInitExpr.KeyValeList, + initExpr.MapInitExpr().KeyValeList = append(initExpr.MapInitExpr().KeyValeList, [2]*ast.Node{keyNode, valueNode}) return initExpr } @@ -237,7 +237,7 @@ func (p *parser) newMapInitEndExpr(initExpr *ast.Node, pos plToken.Pos) *ast.Nod "%s object is not MapInitExpr", initExpr.NodeType) return nil } - initExpr.MapInitExpr.RBrace = p.posCache.LnCol(pos) + initExpr.MapInitExpr().RBrace = p.posCache.LnCol(pos) return initExpr } @@ -299,7 +299,7 @@ func (p *parser) newForInStmt(inExpr *ast.Node, body *ast.BlockStmt, forTk Item) switch inExpr.NodeType { //nolint:exhaustive case ast.TypeInExpr: - expr = inExpr.InExpr + expr = inExpr.InExpr() default: p.addParseErrf(p.yyParser.lval.item.PositionRange(), "%s object is not identifier", inExpr.NodeType) return nil @@ -380,15 +380,15 @@ func (p *parser) newUnaryExpr(op Item, r *ast.Node) *ast.Node { switch r.NodeType { case ast.TypeFloatLiteral: if op.Typ == SUB { - r.FloatLiteral.Val = -r.FloatLiteral.Val + r.FloatLiteral().Val = -r.FloatLiteral().Val } - r.FloatLiteral.Start = p.posCache.LnCol(op.Pos) + 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().Val = -r.IntegerLiteral().Val } - r.IntegerLiteral.Start = p.posCache.LnCol(op.Pos) + r.IntegerLiteral().Start = p.posCache.LnCol(op.Pos) return r } } @@ -431,12 +431,12 @@ func (p *parser) newArithmeticExpr(l, r *ast.Node, op Item) *ast.Node { case DIV, MOD: // div 0 or mod 0 switch r.NodeType { //nolint:exhaustive case ast.TypeFloatLiteral: - if r.FloatLiteral.Val == 0 { + if r.FloatLiteral().Val == 0 { p.addParseErrf(p.yyParser.lval.item.PositionRange(), "division or modulo by zero") return nil } case ast.TypeIntegerLiteral: - if r.IntegerLiteral.Val == 0 { + if r.IntegerLiteral().Val == 0 { p.addParseErrf(p.yyParser.lval.item.PositionRange(), "division or modulo by zero") return nil } @@ -482,14 +482,14 @@ func (p *parser) newIndexExpr(obj *ast.Node, lBracket Item, index *ast.Node, rBr switch obj.NodeType { //nolint:exhaustive case ast.TypeIdentifier: return ast.WrapIndexExpr(&ast.IndexExpr{ - Obj: obj.Identifier, Index: []*ast.Node{index}, + Obj: obj.Identifier(), Index: []*ast.Node{index}, LBracket: []plToken.LnColPos{p.posCache.LnCol(lBracket.Pos)}, RBracket: []plToken.LnColPos{p.posCache.LnCol(rBracket.Pos)}, }) case ast.TypeIndexExpr: - obj.IndexExpr.Index = append(obj.IndexExpr.Index, index) - obj.IndexExpr.LBracket = append(obj.IndexExpr.LBracket, p.posCache.LnCol(lBracket.Pos)) - obj.IndexExpr.RBracket = append(obj.IndexExpr.RBracket, p.posCache.LnCol(rBracket.Pos)) + obj.IndexExpr().Index = append(obj.IndexExpr().Index, index) + obj.IndexExpr().LBracket = append(obj.IndexExpr().LBracket, p.posCache.LnCol(lBracket.Pos)) + obj.IndexExpr().RBracket = append(obj.IndexExpr().RBracket, p.posCache.LnCol(rBracket.Pos)) return obj default: p.addParseErrf(p.yyParser.lval.item.PositionRange(), @@ -503,7 +503,7 @@ func (p *parser) newCallExpr(fn *ast.Node, args []*ast.Node, lParen, rParen Item switch fn.NodeType { //nolint:exhaustive case ast.TypeIdentifier: - fname = fn.Identifier.Name + fname = fn.Identifier().Name default: p.addParseErrf(p.yyParser.lval.item.PositionRange(), fmt.Sprintf("invalid fn name object type %s", fn.NodeType)) @@ -511,7 +511,7 @@ func (p *parser) newCallExpr(fn *ast.Node, args []*ast.Node, lParen, rParen Item } f := &ast.CallExpr{ Name: fname, - NamePos: fn.Identifier.Start, + NamePos: fn.Identifier().Start, LParen: p.posCache.LnCol(lParen.Pos), RParen: p.posCache.LnCol(rParen.Pos), }