diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 843f5e2..0ad7b6a 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -27,8 +27,9 @@ const ( TypeBoolLiteral TypeNilLiteral - TypeListInitExpr - TypeMapInitExpr + TypeListLiteral + TypeMapLiteral + TypeInExpr TypeParenExpr @@ -70,9 +71,9 @@ func (t NodeType) String() string { return "BoolLiteral" case TypeNilLiteral: return "NilLiteral" - case TypeListInitExpr: + case TypeListLiteral: return "ListInitExpr" - case TypeMapInitExpr: + case TypeMapLiteral: return "MapInitExpr" case TypeParenExpr: return "ParenExpr" @@ -169,11 +170,11 @@ func (n *Node) BoolLiteral() *BoolLiteral { func (n *Node) NilLiteral() *NilLiteral { return n.elem.(*NilLiteral) } -func (n *Node) ListInitExpr() *ListInitExpr { - return n.elem.(*ListInitExpr) +func (n *Node) ListLiteral() *ListLiteral { + return n.elem.(*ListLiteral) } -func (n *Node) MapInitExpr() *MapInitExpr { - return n.elem.(*MapInitExpr) +func (n *Node) MapLiteral() *MapLiteral { + return n.elem.(*MapLiteral) } func (n *Node) ParenExpr() *ParenExpr { return n.elem.(*ParenExpr) @@ -267,16 +268,16 @@ func WrapNilLiteral(node *NilLiteral) *Node { } } -func WrapListInitExpr(node *ListInitExpr) *Node { +func WrapListInitExpr(node *ListLiteral) *Node { return &Node{ - NodeType: TypeListInitExpr, + NodeType: TypeListLiteral, elem: node, } } -func WrapMapInitExpr(node *MapInitExpr) *Node { +func WrapMapLiteral(node *MapLiteral) *Node { return &Node{ - NodeType: TypeMapInitExpr, + NodeType: TypeMapLiteral, elem: node, } } @@ -406,10 +407,10 @@ func NodeStartPos(node *Node) token.LnColPos { case TypeNilLiteral: return node.NilLiteral().Start - case TypeListInitExpr: - return node.ListInitExpr().LBracket - case TypeMapInitExpr: - return node.MapInitExpr().LBrace + case TypeListLiteral: + return node.ListLiteral().LBracket + case TypeMapLiteral: + return node.MapLiteral().LBrace case TypeParenExpr: return node.ParenExpr().LParen diff --git a/pkg/ast/expr.go b/pkg/ast/expr.go index 87f29b0..0d15c2b 100644 --- a/pkg/ast/expr.go +++ b/pkg/ast/expr.go @@ -126,17 +126,17 @@ func (e *NilLiteral) String() string { return "nil" } -type MapInitExpr struct { +type MapLiteral struct { KeyValeList [][2]*Node // key,value list LBrace token.LnColPos RBrace token.LnColPos } -func (e *MapInitExpr) IsExpr() bool { +func (e *MapLiteral) IsExpr() bool { return true } -func (e *MapInitExpr) String() string { +func (e *MapLiteral) String() string { v := "{" for i, item := range e.KeyValeList { if i == 0 { @@ -148,17 +148,17 @@ func (e *MapInitExpr) String() string { return v + "}" } -type ListInitExpr struct { +type ListLiteral struct { List []*Node LBracket token.LnColPos RBracket token.LnColPos } -func (e *ListInitExpr) IsExpr() bool { +func (e *ListLiteral) IsExpr() bool { return true } -func (e *ListInitExpr) String() string { +func (e *ListLiteral) String() string { arr := []string{} for _, x := range e.List { arr = append(arr, x.String()) diff --git a/pkg/engine/runtime/checkstmt.go b/pkg/engine/runtime/checkstmt.go index 2ee616e..8d89b76 100644 --- a/pkg/engine/runtime/checkstmt.go +++ b/pkg/engine/runtime/checkstmt.go @@ -47,10 +47,10 @@ func RunStmtCheck(ctx *Context, ctxCheck *ContextCheck, node *ast.Node) *errchai // skip case ast.TypeNilLiteral: // skip - case ast.TypeListInitExpr: - return RunListInitExprCheck(ctx, ctxCheck, node.ListInitExpr()) - case ast.TypeMapInitExpr: - return RunMapInitExprCheck(ctx, ctxCheck, node.MapInitExpr()) + case ast.TypeListLiteral: + return RunListInitExprCheck(ctx, ctxCheck, node.ListLiteral()) + case ast.TypeMapLiteral: + return RunMapInitExprCheck(ctx, ctxCheck, node.MapLiteral()) case ast.TypeParenExpr: return RunParenExprCheck(ctx, ctxCheck, node.ParenExpr()) @@ -87,7 +87,7 @@ func RunStmtCheck(ctx *Context, ctxCheck *ContextCheck, node *ast.Node) *errchai return nil } -func RunListInitExprCheck(ctx *Context, ctxCheck *ContextCheck, expr *ast.ListInitExpr) *errchain.PlError { +func RunListInitExprCheck(ctx *Context, ctxCheck *ContextCheck, expr *ast.ListLiteral) *errchain.PlError { for _, v := range expr.List { if err := RunStmtCheck(ctx, ctxCheck, v); err != nil { return err.ChainAppend(ctx.name, expr.LBracket) @@ -96,12 +96,12 @@ func RunListInitExprCheck(ctx *Context, ctxCheck *ContextCheck, expr *ast.ListIn return nil } -func RunMapInitExprCheck(ctx *Context, ctxCheck *ContextCheck, expr *ast.MapInitExpr) *errchain.PlError { +func RunMapInitExprCheck(ctx *Context, ctxCheck *ContextCheck, expr *ast.MapLiteral) *errchain.PlError { for _, v := range expr.KeyValeList { switch v[0].NodeType { //nolint:exhaustive case ast.TypeFloatLiteral, ast.TypeIntegerLiteral, ast.TypeBoolLiteral, ast.TypeNilLiteral, - ast.TypeListInitExpr, ast.TypeMapInitExpr: + ast.TypeListLiteral, ast.TypeMapLiteral: return NewRunError(ctx, "map key expect string", ast.NodeStartPos(v[0])) default: diff --git a/pkg/engine/runtime/runtime.go b/pkg/engine/runtime/runtime.go index fc6cd93..a5a1cad 100644 --- a/pkg/engine/runtime/runtime.go +++ b/pkg/engine/runtime/runtime.go @@ -368,16 +368,16 @@ func RunStmt(ctx *Context, node *ast.Node) (any, ast.DType, *errchain.PlError) { return RunCallExpr(ctx, node.CallExpr()) case ast.TypeInExpr: return RunInExpr(ctx, node.InExpr()) - case ast.TypeListInitExpr: - return RunListInitExpr(ctx, node.ListInitExpr()) + case ast.TypeListLiteral: + return RunListInitExpr(ctx, node.ListLiteral()) case ast.TypeIdentifier: 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()) + case ast.TypeMapLiteral: + return RunMapInitExpr(ctx, node.MapLiteral()) // use for map, slice and array case ast.TypeIndexExpr: return RunIndexExprGet(ctx, node.IndexExpr()) @@ -515,7 +515,7 @@ func RunUnaryExpr(ctx *Context, expr *ast.UnaryExpr) (any, ast.DType, *errchain. } } -func RunListInitExpr(ctx *Context, expr *ast.ListInitExpr) (any, ast.DType, *errchain.PlError) { +func RunListInitExpr(ctx *Context, expr *ast.ListLiteral) (any, ast.DType, *errchain.PlError) { ret := []any{} for _, v := range expr.List { v, _, err := RunStmt(ctx, v) @@ -527,7 +527,7 @@ func RunListInitExpr(ctx *Context, expr *ast.ListInitExpr) (any, ast.DType, *err return ret, ast.List, nil } -func RunMapInitExpr(ctx *Context, expr *ast.MapInitExpr) (any, ast.DType, *errchain.PlError) { +func RunMapInitExpr(ctx *Context, expr *ast.MapLiteral) (any, ast.DType, *errchain.PlError) { ret := map[string]any{} for _, v := range expr.KeyValeList { diff --git a/pkg/parser/gram.y b/pkg/parser/gram.y index 671253c..b03b3f8 100644 --- a/pkg/parser/gram.y +++ b/pkg/parser/gram.y @@ -100,16 +100,12 @@ NIL NULL IF ELIF ELSE attr_expr in_expr expr - map_init - map_init_start - list_init - list_init_start + map_literal + map_literal_start + list_literal + list_literal_start basic_literal for_stmt_elem - /* bool_literal */ - /* string_literal */ - /* nil_literal */ - /* number_literal */ value_stmt //columnref @@ -199,8 +195,8 @@ stmt: ifelse_stmt /* expression */ expr: basic_literal -| list_init -| map_init +| list_literal +| map_literal | paren_expr | call_expr | unary_expr @@ -531,57 +527,57 @@ attr_expr: identifier DOT index_expr ; -list_init : list_init_start RIGHT_BRACKET +list_literal : list_literal_start RIGHT_BRACKET { - $$ = yylex.(*parser).newListInitEndExpr($$, $2.Pos) + $$ = yylex.(*parser).newListLiteralEnd($$, $2.Pos) } -| list_init_start COMMA SPACE_EOLS RIGHT_BRACKET +| list_literal_start COMMA SPACE_EOLS RIGHT_BRACKET { - $$ = yylex.(*parser).newListInitEndExpr($$, $4.Pos) + $$ = yylex.(*parser).newListLiteralEnd($$, $4.Pos) } | LEFT_BRACKET SPACE_EOLS RIGHT_BRACKET { - $$ = yylex.(*parser).newListInitStartExpr($1.Pos) - $$ = yylex.(*parser).newListInitEndExpr($$, $3.Pos) + $$ = yylex.(*parser).newListLiteralStart($1.Pos) + $$ = yylex.(*parser).newListLiteralEnd($$, $3.Pos) } ; -list_init_start : LEFT_BRACKET SPACE_EOLS expr +list_literal_start : LEFT_BRACKET SPACE_EOLS expr { - $$ = yylex.(*parser).newListInitStartExpr($1.Pos) - $$ = yylex.(*parser).newListInitAppendExpr($$, $3) + $$ = yylex.(*parser).newListLiteralStart($1.Pos) + $$ = yylex.(*parser).newListLiteralAppendExpr($$, $3) } -| list_init_start COMMA SPACE_EOLS expr +| list_literal_start COMMA SPACE_EOLS expr { - $$ = yylex.(*parser).newListInitAppendExpr($$, $4) + $$ = yylex.(*parser).newListLiteralAppendExpr($$, $4) } -| list_init_start EOL +| list_literal_start EOL ; -map_init : map_init_start SPACE_EOLS RIGHT_BRACE +map_literal : map_literal_start SPACE_EOLS RIGHT_BRACE { - $$ = yylex.(*parser).newMapInitEndExpr($$, $3.Pos) + $$ = yylex.(*parser).newMapLiteralEnd($$, $3.Pos) } -| map_init_start COMMA SPACE_EOLS RIGHT_BRACE +| map_literal_start COMMA SPACE_EOLS RIGHT_BRACE { - $$ = yylex.(*parser).newMapInitEndExpr($$, $4.Pos) + $$ = yylex.(*parser).newMapLiteralEnd($$, $4.Pos) } | empty_block { - $$ = yylex.(*parser).newMapInitStartExpr($1.LBracePos.Pos) - $$ = yylex.(*parser).newMapInitEndExpr($$, $1.RBracePos.Pos) + $$ = yylex.(*parser).newMapLiteralStart($1.LBracePos.Pos) + $$ = yylex.(*parser).newMapLiteralEnd($$, $1.RBracePos.Pos) } ; -map_init_start: LEFT_BRACE SPACE_EOLS expr COLON SPACE_EOLS expr +map_literal_start: LEFT_BRACE SPACE_EOLS expr COLON SPACE_EOLS expr { - $$ = yylex.(*parser).newMapInitStartExpr($1.Pos) - $$ = yylex.(*parser).newMapInitAppendExpr($$, $3, $6) + $$ = yylex.(*parser).newMapLiteralStart($1.Pos) + $$ = yylex.(*parser).newMapLiteralAppendExpr($$, $3, $6) } -| map_init_start COMMA SPACE_EOLS expr COLON SPACE_EOLS expr +| map_literal_start COMMA SPACE_EOLS expr COLON SPACE_EOLS expr { - $$ = yylex.(*parser).newMapInitAppendExpr($1, $4, $7) + $$ = yylex.(*parser).newMapLiteralAppendExpr($1, $4, $7) } ; diff --git a/pkg/parser/gram_y.go b/pkg/parser/gram_y.go index 201cd4d..8e6292e 100644 --- a/pkg/parser/gram_y.go +++ b/pkg/parser/gram_y.go @@ -173,7 +173,7 @@ const yyEofCode = 1 const yyErrCode = 2 const yyInitialStackSize = 16 -//line gram.y:632 +//line gram.y:628 //line yacctab:1 var yyExca = [...]int16{ @@ -786,31 +786,31 @@ yydefault: case 8: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:143 +//line gram.y:139 { yylex.(*parser).parseResult = yyDollar[2].aststmts } case 9: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:147 +//line gram.y:143 { yylex.(*parser).parseResult = ast.Stmts{} } case 10: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:151 +//line gram.y:147 { yylex.(*parser).parseResult = yyDollar[3].aststmts } case 12: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:156 +//line gram.y:152 { yylex.(*parser).unexpected("", "") } case 13: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:163 +//line gram.y:159 { s := yyDollar[1].aststmts s = append(s, yyDollar[2].node) @@ -818,25 +818,25 @@ yydefault: } case 15: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:170 +//line gram.y:166 { yyVAL.aststmts = ast.Stmts{yyDollar[1].node} } case 16: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:176 +//line gram.y:172 { yyVAL.aststmts = ast.Stmts{yyDollar[1].node} } case 17: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:180 +//line gram.y:176 { yyVAL.aststmts = ast.Stmts{} } case 18: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:184 +//line gram.y:180 { s := yyDollar[1].aststmts s = append(s, yyDollar[2].node) @@ -844,482 +844,482 @@ yydefault: } case 42: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:226 +//line gram.y:222 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 43: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:230 +//line gram.y:226 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 44: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:234 +//line gram.y:230 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 45: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:238 +//line gram.y:234 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 46: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:242 +//line gram.y:238 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 47: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:246 +//line gram.y:242 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 48: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:253 +//line gram.y:249 { yyVAL.node = yylex.(*parser).newInExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 49: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:258 +//line gram.y:254 { yyVAL.node = yylex.(*parser).newBreakStmt(yyDollar[1].item.Pos) } case 50: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:264 +//line gram.y:260 { yyVAL.node = yylex.(*parser).newContinueStmt(yyDollar[1].item.Pos) } case 51: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:275 +//line gram.y:271 { yyVAL.node = yylex.(*parser).newForInStmt(yyDollar[2].node, yyDollar[3].astblock, yyDollar[1].item) } case 52: yyDollar = yyS[yypt-7 : yypt+1] -//line gram.y:287 +//line gram.y:283 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, yyDollar[4].node, yyDollar[6].node, yyDollar[7].astblock) } case 53: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:291 +//line gram.y:287 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, yyDollar[4].node, nil, yyDollar[6].astblock) } case 54: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:295 +//line gram.y:291 { yyVAL.node = yylex.(*parser).newForStmt(nil, yyDollar[3].node, yyDollar[5].node, yyDollar[6].astblock) } case 55: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:299 +//line gram.y:295 { yyVAL.node = yylex.(*parser).newForStmt(nil, yyDollar[3].node, nil, yyDollar[5].astblock) } case 56: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:303 +//line gram.y:299 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, nil, yyDollar[5].node, yyDollar[6].astblock) } case 57: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:307 +//line gram.y:303 { yyVAL.node = yylex.(*parser).newForStmt(yyDollar[2].node, nil, nil, yyDollar[5].astblock) } case 58: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:311 +//line gram.y:307 { yyVAL.node = yylex.(*parser).newForStmt(nil, nil, yyDollar[4].node, yyDollar[5].astblock) } case 59: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:315 +//line gram.y:311 { yyVAL.node = yylex.(*parser).newForStmt(nil, nil, nil, yyDollar[4].astblock) } case 62: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:324 +//line gram.y:320 { yyVAL.node = yylex.(*parser).newIfElifStmt(yyDollar[1].iflist) } case 63: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:328 +//line gram.y:324 { yyVAL.node = yylex.(*parser).newIfElifelseStmt(yyDollar[1].iflist, yyDollar[2].item, yyDollar[3].astblock) } case 64: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:334 +//line gram.y:330 { yyVAL.ifitem = yylex.(*parser).newIfElem(yyDollar[1].item, yyDollar[2].node, yyDollar[3].astblock) } case 65: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:340 +//line gram.y:336 { yyVAL.iflist = []*ast.IfStmtElem{yyDollar[1].ifitem} } case 66: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:344 +//line gram.y:340 { yyVAL.iflist = append(yyDollar[1].iflist, yyDollar[2].ifitem) } case 67: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:350 +//line gram.y:346 { yyVAL.ifitem = yylex.(*parser).newIfElem(yyDollar[1].item, yyDollar[2].node, yyDollar[3].astblock) } case 69: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:358 +//line gram.y:354 { yyVAL.astblock = yylex.(*parser).newBlockStmt(yyDollar[1].item, yyDollar[3].aststmts, yyDollar[4].item) } case 70: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:364 +//line gram.y:360 { yyVAL.astblock = yylex.(*parser).newBlockStmt(yyDollar[1].item, ast.Stmts{}, yyDollar[3].item) } case 71: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:370 +//line gram.y:366 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[4].nodes, yyDollar[2].item, yyDollar[6].item) } case 72: yyDollar = yyS[yypt-7 : yypt+1] -//line gram.y:374 +//line gram.y:370 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, yyDollar[4].nodes, yyDollar[2].item, yyDollar[7].item) } case 73: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:378 +//line gram.y:374 { yyVAL.node = yylex.(*parser).newCallExpr(yyDollar[1].node, nil, yyDollar[2].item, yyDollar[4].item) } case 74: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:385 +//line gram.y:381 { yyVAL.nodes = append(yyVAL.nodes, yyDollar[4].node) } case 75: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:389 +//line gram.y:385 { yyVAL.nodes = append(yyVAL.nodes, yyDollar[4].node) } case 76: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:393 +//line gram.y:389 { yyVAL.nodes = []*ast.Node{yyDollar[1].node} } case 77: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:397 +//line gram.y:393 { yyVAL.nodes = []*ast.Node{yyDollar[1].node} } case 78: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:403 +//line gram.y:399 { yyVAL.node = yylex.(*parser).newAssignmentStmt(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 79: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:409 +//line gram.y:405 { yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) } case 80: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:413 +//line gram.y:409 { yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) } case 81: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:417 +//line gram.y:413 { yyVAL.node = yylex.(*parser).newUnaryExpr(yyDollar[1].item, yyDollar[2].node) } case 84: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:425 +//line gram.y:421 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 85: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:429 +//line gram.y:425 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 86: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:433 +//line gram.y:429 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 87: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:437 +//line gram.y:433 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 88: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:441 +//line gram.y:437 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 89: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:445 +//line gram.y:441 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 90: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:449 +//line gram.y:445 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 91: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:453 +//line gram.y:449 { yyVAL.node = yylex.(*parser).newConditionalExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 92: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:460 +//line gram.y:456 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 93: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:464 +//line gram.y:460 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 94: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:468 +//line gram.y:464 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 95: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:472 +//line gram.y:468 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 96: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:476 +//line gram.y:472 { yyVAL.node = yylex.(*parser).newArithmeticExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[2].item) } case 97: yyDollar = yyS[yypt-5 : yypt+1] -//line gram.y:483 +//line gram.y:479 { yyVAL.node = yylex.(*parser).newParenExpr(yyDollar[1].item, yyDollar[3].node, yyDollar[5].item) } case 98: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:490 +//line gram.y:486 { yyVAL.node = yylex.(*parser).newIndexExpr(yyDollar[1].node, yyDollar[2].item, yyDollar[4].node, yyDollar[6].item) } case 99: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:495 +//line gram.y:491 { yyVAL.node = yylex.(*parser).newIndexExpr(nil, yyDollar[2].item, yyDollar[4].node, yyDollar[6].item) } case 100: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:499 +//line gram.y:495 { yyVAL.node = yylex.(*parser).newIndexExpr(yyDollar[1].node, yyDollar[2].item, yyDollar[4].node, yyDollar[6].item) } case 101: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:508 +//line gram.y:504 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 102: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:512 +//line gram.y:508 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 103: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:516 +//line gram.y:512 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 104: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:520 +//line gram.y:516 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 105: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:524 +//line gram.y:520 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 106: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:528 +//line gram.y:524 { yyVAL.node = yylex.(*parser).newAttrExpr(yyDollar[1].node, yyDollar[3].node) } case 107: yyDollar = yyS[yypt-2 : yypt+1] -//line gram.y:535 +//line gram.y:531 { - yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[2].item.Pos) + yyVAL.node = yylex.(*parser).newListLiteralEnd(yyVAL.node, yyDollar[2].item.Pos) } case 108: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:539 +//line gram.y:535 { - yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[4].item.Pos) + yyVAL.node = yylex.(*parser).newListLiteralEnd(yyVAL.node, yyDollar[4].item.Pos) } case 109: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:543 +//line gram.y:539 { - yyVAL.node = yylex.(*parser).newListInitStartExpr(yyDollar[1].item.Pos) - yyVAL.node = yylex.(*parser).newListInitEndExpr(yyVAL.node, yyDollar[3].item.Pos) + yyVAL.node = yylex.(*parser).newListLiteralStart(yyDollar[1].item.Pos) + yyVAL.node = yylex.(*parser).newListLiteralEnd(yyVAL.node, yyDollar[3].item.Pos) } case 110: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:550 +//line gram.y:546 { - yyVAL.node = yylex.(*parser).newListInitStartExpr(yyDollar[1].item.Pos) - yyVAL.node = yylex.(*parser).newListInitAppendExpr(yyVAL.node, yyDollar[3].node) + yyVAL.node = yylex.(*parser).newListLiteralStart(yyDollar[1].item.Pos) + yyVAL.node = yylex.(*parser).newListLiteralAppendExpr(yyVAL.node, yyDollar[3].node) } case 111: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:555 +//line gram.y:551 { - yyVAL.node = yylex.(*parser).newListInitAppendExpr(yyVAL.node, yyDollar[4].node) + yyVAL.node = yylex.(*parser).newListLiteralAppendExpr(yyVAL.node, yyDollar[4].node) } case 113: yyDollar = yyS[yypt-3 : yypt+1] -//line gram.y:563 +//line gram.y:559 { - yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[3].item.Pos) + yyVAL.node = yylex.(*parser).newMapLiteralEnd(yyVAL.node, yyDollar[3].item.Pos) } case 114: yyDollar = yyS[yypt-4 : yypt+1] -//line gram.y:567 +//line gram.y:563 { - yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[4].item.Pos) + yyVAL.node = yylex.(*parser).newMapLiteralEnd(yyVAL.node, yyDollar[4].item.Pos) } case 115: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:571 +//line gram.y:567 { - yyVAL.node = yylex.(*parser).newMapInitStartExpr(yyDollar[1].astblock.LBracePos.Pos) - yyVAL.node = yylex.(*parser).newMapInitEndExpr(yyVAL.node, yyDollar[1].astblock.RBracePos.Pos) + yyVAL.node = yylex.(*parser).newMapLiteralStart(yyDollar[1].astblock.LBracePos.Pos) + yyVAL.node = yylex.(*parser).newMapLiteralEnd(yyVAL.node, yyDollar[1].astblock.RBracePos.Pos) } case 116: yyDollar = yyS[yypt-6 : yypt+1] -//line gram.y:578 +//line gram.y:574 { - yyVAL.node = yylex.(*parser).newMapInitStartExpr(yyDollar[1].item.Pos) - yyVAL.node = yylex.(*parser).newMapInitAppendExpr(yyVAL.node, yyDollar[3].node, yyDollar[6].node) + yyVAL.node = yylex.(*parser).newMapLiteralStart(yyDollar[1].item.Pos) + yyVAL.node = yylex.(*parser).newMapLiteralAppendExpr(yyVAL.node, yyDollar[3].node, yyDollar[6].node) } case 117: yyDollar = yyS[yypt-7 : yypt+1] -//line gram.y:583 +//line gram.y:579 { - yyVAL.node = yylex.(*parser).newMapInitAppendExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[7].node) + yyVAL.node = yylex.(*parser).newMapLiteralAppendExpr(yyDollar[1].node, yyDollar[4].node, yyDollar[7].node) } case 118: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:590 +//line gram.y:586 { yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) } case 119: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:594 +//line gram.y:590 { yyDollar[1].item.Val = yylex.(*parser).unquoteString(yyDollar[1].item.Val) yyVAL.node = yylex.(*parser).newIdentifierLiteral(yyDollar[1].item) } case 120: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:601 +//line gram.y:597 { yyVAL.node = yylex.(*parser).newNumberLiteral(yyDollar[1].item) } case 121: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:605 +//line gram.y:601 { yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, true) } case 122: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:609 +//line gram.y:605 { yyVAL.node = yylex.(*parser).newBoolLiteral(yyDollar[1].item.Pos, false) } case 123: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:613 +//line gram.y:609 { yyDollar[1].item.Val = yylex.(*parser).unquoteString(yyDollar[1].item.Val) yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) } case 124: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:618 +//line gram.y:614 { yyDollar[1].item.Val = yylex.(*parser).unquoteMultilineString(yyDollar[1].item.Val) yyVAL.node = yylex.(*parser).newStringLiteral(yyDollar[1].item) } case 125: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:623 +//line gram.y:619 { yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) } case 126: yyDollar = yyS[yypt-1 : yypt+1] -//line gram.y:627 +//line gram.y:623 { yyVAL.node = yylex.(*parser).newNilLiteral(yyDollar[1].item.Pos) } diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index cfa4ee3..91e1933 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -184,60 +184,60 @@ func (p *parser) newParenExpr(lParen Item, node *ast.Node, rParen Item) *ast.Nod }) } -func (p *parser) newListInitStartExpr(pos plToken.Pos) *ast.Node { - return ast.WrapListInitExpr(&ast.ListInitExpr{ +func (p *parser) newListLiteralStart(pos plToken.Pos) *ast.Node { + return ast.WrapListInitExpr(&ast.ListLiteral{ List: []*ast.Node{}, LBracket: p.posCache.LnCol(pos), }) } -func (p *parser) newListInitAppendExpr(initExpr *ast.Node, elem *ast.Node) *ast.Node { - if initExpr.NodeType != ast.TypeListInitExpr { +func (p *parser) newListLiteralAppendExpr(initExpr *ast.Node, elem *ast.Node) *ast.Node { + if initExpr.NodeType != ast.TypeListLiteral { p.addParseErrf(p.yyParser.lval.item.PositionRange(), - "%s object is not ListInitExpr", initExpr.NodeType) + "%s object is not ListLiteral", initExpr.NodeType) return nil } - initExpr.ListInitExpr().List = append(initExpr.ListInitExpr().List, elem) + initExpr.ListLiteral().List = append(initExpr.ListLiteral().List, elem) return initExpr } -func (p *parser) newListInitEndExpr(initExpr *ast.Node, pos plToken.Pos) *ast.Node { - if initExpr.NodeType != ast.TypeListInitExpr { +func (p *parser) newListLiteralEnd(initExpr *ast.Node, pos plToken.Pos) *ast.Node { + if initExpr.NodeType != ast.TypeListLiteral { p.addParseErrf(p.yyParser.lval.item.PositionRange(), - "%s object is not ListInitExpr", initExpr.NodeType) + "%s object is not ListLiteral", initExpr.NodeType) return nil } - initExpr.ListInitExpr().RBracket = p.posCache.LnCol(pos) + initExpr.ListLiteral().RBracket = p.posCache.LnCol(pos) return initExpr } -func (p *parser) newMapInitStartExpr(pos plToken.Pos) *ast.Node { - return ast.WrapMapInitExpr(&ast.MapInitExpr{ +func (p *parser) newMapLiteralStart(pos plToken.Pos) *ast.Node { + return ast.WrapMapLiteral(&ast.MapLiteral{ KeyValeList: [][2]*ast.Node{}, LBrace: p.posCache.LnCol(pos), }) } -func (p *parser) newMapInitAppendExpr(initExpr *ast.Node, keyNode *ast.Node, valueNode *ast.Node) *ast.Node { - if initExpr.NodeType != ast.TypeMapInitExpr { +func (p *parser) newMapLiteralAppendExpr(initExpr *ast.Node, keyNode *ast.Node, valueNode *ast.Node) *ast.Node { + if initExpr.NodeType != ast.TypeMapLiteral { p.addParseErrf(p.yyParser.lval.item.PositionRange(), - "%s object is not MapInitExpr", initExpr.NodeType) + "%s object is not MapLiteral", initExpr.NodeType) return nil } - initExpr.MapInitExpr().KeyValeList = append(initExpr.MapInitExpr().KeyValeList, + initExpr.MapLiteral().KeyValeList = append(initExpr.MapLiteral().KeyValeList, [2]*ast.Node{keyNode, valueNode}) return initExpr } -func (p *parser) newMapInitEndExpr(initExpr *ast.Node, pos plToken.Pos) *ast.Node { - if initExpr.NodeType != ast.TypeMapInitExpr { +func (p *parser) newMapLiteralEnd(initExpr *ast.Node, pos plToken.Pos) *ast.Node { + if initExpr.NodeType != ast.TypeMapLiteral { p.addParseErrf(p.yyParser.lval.item.PositionRange(), - "%s object is not MapInitExpr", initExpr.NodeType) + "%s object is not MapLiteral", initExpr.NodeType) return nil } - initExpr.MapInitExpr().RBrace = p.posCache.LnCol(pos) + initExpr.MapLiteral().RBrace = p.posCache.LnCol(pos) return initExpr } diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index d3645e1..0c4c075 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -89,7 +89,7 @@ func TestExprSeparation(t *testing.T) { } `, expected: ast.Stmts{ - ast.WrapListInitExpr(&ast.ListInitExpr{ + ast.WrapListInitExpr(&ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 3}), @@ -99,7 +99,7 @@ func TestExprSeparation(t *testing.T) { }, }), ast.WrapListInitExpr( - &ast.ListInitExpr{ + &ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 3}), @@ -110,7 +110,7 @@ func TestExprSeparation(t *testing.T) { }), ast.WrapListInitExpr( - &ast.ListInitExpr{ + &ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 3}), @@ -118,7 +118,7 @@ func TestExprSeparation(t *testing.T) { ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), ast.WrapFloatLiteral(&ast.FloatLiteral{Val: 1.}), ast.WrapListInitExpr( - &ast.ListInitExpr{ + &ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 3}), @@ -127,7 +127,7 @@ func TestExprSeparation(t *testing.T) { ast.WrapFloatLiteral(&ast.FloatLiteral{Val: 1.}), }, }), - ast.WrapMapInitExpr(&ast.MapInitExpr{}), + ast.WrapMapLiteral(&ast.MapLiteral{}), }, }), ast.WrapAssignmentStmt(&ast.AssignmentExpr{ @@ -438,7 +438,7 @@ func TestParserFor(t *testing.T) { expected: ast.Stmts{ ast.WrapForInStmt(&ast.ForInStmt{ Varb: ast.WrapIdentifier(&ast.Identifier{Name: "x"}), - Iter: ast.WrapListInitExpr(&ast.ListInitExpr{ + Iter: ast.WrapListInitExpr(&ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapFloatLiteral(&ast.FloatLiteral{Val: 2.}), @@ -1115,7 +1115,7 @@ func TestParser(t *testing.T) { expected: ast.Stmts{ ast.WrapCallExpr(&ast.CallExpr{ Name: "f", - Param: []*ast.Node{ast.WrapListInitExpr(&ast.ListInitExpr{ + Param: []*ast.Node{ast.WrapListInitExpr(&ast.ListLiteral{ List: []*ast.Node{ ast.WrapIntegerLiteral(&ast.IntegerLiteral{Val: 1}), ast.WrapFloatLiteral(&ast.FloatLiteral{Val: 2.0}), @@ -1169,7 +1169,7 @@ func TestParser(t *testing.T) { ast.WrapAssignmentStmt(&ast.AssignmentExpr{ Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a1"}), - RHS: ast.WrapListInitExpr(&ast.ListInitExpr{ + RHS: ast.WrapListInitExpr(&ast.ListLiteral{ List: []*ast.Node{ ast.WrapStringLiteral(&ast.StringLiteral{Val: "b"}), ast.WrapFloatLiteral(&ast.FloatLiteral{Val: 1.1}), @@ -1274,11 +1274,11 @@ func TestParser(t *testing.T) { }), }, }), - ast.WrapMapInitExpr(&ast.MapInitExpr{}), + ast.WrapMapLiteral(&ast.MapLiteral{}), ast.WrapAssignmentStmt(&ast.AssignmentExpr{ Op: ast.EQ, LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), - RHS: ast.WrapMapInitExpr(&ast.MapInitExpr{ + RHS: ast.WrapMapLiteral(&ast.MapLiteral{ KeyValeList: [][2]*ast.Node{ { ast.WrapStringLiteral(&ast.StringLiteral{Val: "1"}),