Skip to content

Commit

Permalink
Reimplementing the Node structure (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
vircoys authored Jul 15, 2024
1 parent d9de33a commit c0e3767
Show file tree
Hide file tree
Showing 20 changed files with 244 additions and 244 deletions.
298 changes: 148 additions & 150 deletions pkg/ast/ast.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/engine/callref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 15 additions & 15 deletions pkg/engine/runtime/checkstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 34 additions & 33 deletions pkg/engine/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand All @@ -856,22 +857,22 @@ 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,
ast.ADDEQ,
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
}

Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/inimpl/guancecloud/funcs/fn_addpattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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)
}

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())
Expand Down
6 changes: 3 additions & 3 deletions pkg/inimpl/guancecloud/funcs/fn_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions pkg/inimpl/guancecloud/funcs/fn_datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion pkg/inimpl/guancecloud/funcs/fn_default_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pkg/inimpl/guancecloud/funcs/fn_getkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/inimpl/guancecloud/funcs/fn_grok.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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'",
Expand Down
4 changes: 2 additions & 2 deletions pkg/inimpl/guancecloud/funcs/fn_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ 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())
}

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())
Expand Down
Loading

0 comments on commit c0e3767

Please sign in to comment.