Skip to content

Commit

Permalink
Merge pull request #52 from GuanceCloud/chore-update
Browse files Browse the repository at this point in the history
Renaming literals: map and list
  • Loading branch information
vircoys authored Jul 15, 2024
2 parents c0e3767 + fdf3901 commit 6372938
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 201 deletions.
33 changes: 17 additions & 16 deletions pkg/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const (
TypeBoolLiteral
TypeNilLiteral

TypeListInitExpr
TypeMapInitExpr
TypeListLiteral
TypeMapLiteral

TypeInExpr

TypeParenExpr
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
14 changes: 7 additions & 7 deletions pkg/engine/runtime/checkstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
62 changes: 29 additions & 33 deletions pkg/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
;

Expand Down
Loading

0 comments on commit 6372938

Please sign in to comment.