-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_operator.go
85 lines (73 loc) · 1.46 KB
/
ast_operator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package spiker
// NodeBinaryOp binary operator node
type NodeBinaryOp struct {
Ast
Left AstNode
Op Symbol
Right AstNode
}
// Format .
func (bin NodeBinaryOp) Format() string {
f := " " + string(bin.Op) + " "
switch bin.Left.(type) {
case *NodeBinaryOp:
f = "(" + bin.Left.Format() + ")" + f
default:
f = bin.Left.Format() + f
}
switch bin.Right.(type) {
case *NodeBinaryOp:
f += "(" + bin.Right.Format() + ")"
default:
f += bin.Right.Format()
}
return f
}
// NodeUnaryOp unary operator node
type NodeUnaryOp struct {
Ast
Op Symbol
Right AstNode
}
// Format .
func (un NodeUnaryOp) Format() string {
return string(un.Op) + un.Right.Format()
}
// NodeAssignOp assignment operator node
type NodeAssignOp struct {
Ast
Var NodeVariable
Op Symbol
Expr AstNode
}
// Format .
func (as NodeAssignOp) Format() string {
return as.Var.Format() + " " + string(as.Op) + " " + as.Expr.Format()
}
// NodeFuncCallOp function call node
type NodeFuncCallOp struct {
Ast
Name NodeVariable
Params []AstNode
}
// Format .
func (fnc NodeFuncCallOp) Format() string {
ps := ""
for idx, as := range fnc.Params {
if idx > 0 {
ps += ", "
}
ps += as.Format()
}
return fnc.Name.Format() + "(" + ps + ")"
}
// NodeVarIndex return the value of the specified index(list, string)
type NodeVarIndex struct {
Ast
Var AstNode
Index AstNode
}
// Format .
func (vi NodeVarIndex) Format() string {
return vi.Var.Format() + "[" + vi.Index.Format() + "]"
}