Skip to content

Commit

Permalink
feat: Add some operators and change assignment expr into assignment s…
Browse files Browse the repository at this point in the history
…tmt (#38)
  • Loading branch information
vircoys authored Jul 24, 2023
1 parent ad13550 commit c610f88
Show file tree
Hide file tree
Showing 13 changed files with 1,672 additions and 815 deletions.
17 changes: 16 additions & 1 deletion pkg/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
TypeAttrExpr
TypeIndexExpr

TypeUnaryExpr
TypeArithmeticExpr
TypeConditionalExpr
TypeAssignmentExpr
Expand Down Expand Up @@ -79,6 +80,8 @@ func (t NodeType) String() string {
return "AttrExpr"
case TypeIndexExpr:
return "IndexExpr"
case TypeUnaryExpr:
return "UnaryExpr"
case TypeArithmeticExpr:
return "ArithmeticExpr"
case TypeConditionalExpr:
Expand Down Expand Up @@ -153,6 +156,7 @@ type Node struct {
IndexExpr *IndexExpr
InExpr *InExpr

UnaryExpr *UnaryExpr
ArithmeticExpr *ArithmeticExpr
ConditionalExpr *ConditionalExpr
AssignmentExpr *AssignmentExpr
Expand Down Expand Up @@ -193,6 +197,8 @@ func (node *Node) String() string {
return node.AttrExpr.String()
case TypeIndexExpr:
return node.IndexExpr.String()
case TypeUnaryExpr:
return node.UnaryExpr.String()
case TypeArithmeticExpr:
return node.ArithmeticExpr.String()
case TypeConditionalExpr:
Expand Down Expand Up @@ -315,7 +321,14 @@ func WrapInExpr(node *InExpr) *Node {
}
}

func WrapAssignmentExpr(node *AssignmentExpr) *Node {
func WrapUnaryExpr(node *UnaryExpr) *Node {
return &Node{
NodeType: TypeUnaryExpr,
UnaryExpr: node,
}
}

func WrapAssignmentStmt(node *AssignmentExpr) *Node {
return &Node{
NodeType: TypeAssignmentExpr,
AssignmentExpr: node,
Expand Down Expand Up @@ -409,6 +422,8 @@ func NodeStartPos(node *Node) token.LnColPos {
case TypeIndexExpr:
return node.IndexExpr.Obj.Start

case TypeUnaryExpr:
return node.UnaryExpr.OpPos
case TypeArithmeticExpr:
return node.ArithmeticExpr.LHS.StartPos()
case TypeConditionalExpr:
Expand Down
27 changes: 24 additions & 3 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ const (

AND Op = "&&"
OR Op = "||"

EQ Op = "="
NOT Op = "!"

EQ Op = "="
ADDEQ Op = "+="
SUBEQ Op = "-="
MULEQ Op = "*="
DIVEQ Op = "/="
MODEQ Op = "%="
)

type Identifier struct {
Expand Down Expand Up @@ -160,6 +166,20 @@ func (e *ListInitExpr) String() string {
return "[" + strings.Join(arr, ", ") + "]"
}

type UnaryExpr struct {
Op Op
RHS *Node
OpPos token.LnColPos
}

func (e *UnaryExpr) IsExpr() bool {
return true
}

func (e *UnaryExpr) String() string {
return fmt.Sprintf("%s%s", e.Op, e.RHS.String())
}

type ConditionalExpr struct {
Op Op
LHS, RHS *Node
Expand Down Expand Up @@ -296,6 +316,7 @@ func (e *CallExpr) String() string {

type AssignmentExpr struct {
LHS, RHS *Node
Op Op
OpPos token.LnColPos
}

Expand All @@ -304,5 +325,5 @@ func (e *AssignmentExpr) IsExpr() bool {
}

func (e *AssignmentExpr) String() string {
return fmt.Sprintf("%s = %s", e.LHS, e.RHS)
return fmt.Sprintf("%s %s %s", e.LHS, e.Op, e.RHS)
}
7 changes: 5 additions & 2 deletions pkg/engine/op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ func TestOp(t *testing.T) {
f = map_a["a"]
aaaa = 1.0 == (b = 1)
b = 1
a = v = a
aaaa = 1.0 == (b )
v = a
a = v
x7 = [1, 2.1, "3"]
if b == 2 {
x = 2
Expand Down
1 change: 0 additions & 1 deletion pkg/engine/runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (ctx *Context) GetKey(key string) (*Varb, error) {
case InRMap:
if v, t, err := ctx.inRMap.Get(key); err == nil {
return &Varb{
Name: key,
Value: v,
DType: t,
}, nil
Expand Down
Loading

0 comments on commit c610f88

Please sign in to comment.