-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_node.go
161 lines (138 loc) · 3.36 KB
/
ast_node.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package formula_engine
const (
astGeneralNodeName = "astGeneralNode"
astBinNodeName = "astBinNode"
astUnNodeName = "astUnNode"
astSinNodeName = "astSinNode"
)
type AstNode interface {
GetName() string
GetTok() *token
}
func DeepCopyAstNode(node AstNode) AstNode {
if node == nil {
return nil
}
switch node.GetName() {
case astSinNodeName:
sNode := node.(*astSinNode)
t := &token{
Type: sNode.Tok.Type,
Value: sNode.Tok.Value,
Start: sNode.Tok.Start,
End: sNode.Tok.End,
}
return newAstSinNode(t)
case astUnNodeName:
sNode := node.(*astUnNode)
t := &token{
Type: sNode.Tok.Type,
Value: sNode.Tok.Value,
Start: sNode.Tok.Start,
End: sNode.Tok.End,
}
child := DeepCopyAstNode(sNode.Node)
return newAstUnNode(t, child)
case astBinNodeName:
sNode := node.(*astBinNode)
t := &token{
Type: sNode.Tok.Type,
Value: sNode.Tok.Value,
Start: sNode.Tok.Start,
End: sNode.Tok.End,
}
lNode := DeepCopyAstNode(sNode.LNode)
RNode := DeepCopyAstNode(sNode.RNode)
return newAstBinNode(t, lNode, RNode)
case astGeneralNodeName:
sNode := node.(*astGeneralNode)
t := &token{
Type: sNode.Tok.Type,
Value: sNode.Tok.Value,
Start: sNode.Tok.Start,
End: sNode.Tok.End,
}
children := make([]AstNode, 0)
for _, c := range sNode.Nodes {
children = append(children, DeepCopyAstNode(c))
}
return newAstGeneralNode(t, children...)
}
return nil
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// astGeneralNode 一般树节点
type astGeneralNode struct {
Tok *token
Nodes []AstNode
}
func newAstGeneralNode(t *token, node ...AstNode) *astGeneralNode {
return &astGeneralNode{
Tok: t,
Nodes: node,
}
}
func (a *astGeneralNode) GetName() string {
return astGeneralNodeName
}
func (a *astGeneralNode) GetTok() *token {
return a.Tok
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// astBinNode BinaryNode 二叉树
type astBinNode struct {
Tok *token
LNode AstNode
RNode AstNode
}
func newAstBinNode(t *token, lNode AstNode, rNode AstNode) *astBinNode {
return &astBinNode{
Tok: t,
LNode: lNode,
RNode: rNode,
}
}
func (a *astBinNode) GetName() string {
return astBinNodeName
}
func (a *astBinNode) GetTok() *token {
return a.Tok
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// astUnNode UnaryNode,单叉树
type astUnNode struct {
Tok *token
Node AstNode
}
func newAstUnNode(t *token, node AstNode) *astUnNode {
return &astUnNode{
Tok: t,
Node: node,
}
}
func (a *astUnNode) GetName() string {
return astUnNodeName
}
func (a *astUnNode) GetTok() *token {
return a.Tok
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// astSinNode single,单个节点
type astSinNode struct {
Tok *token
}
func newAstSinNode(t *token) *astSinNode {
return &astSinNode{
Tok: t,
}
}
func (a *astSinNode) GetName() string {
return astSinNodeName
}
func (a *astSinNode) GetTok() *token {
return a.Tok
}