forked from zhuxiujia/GoMybatis
/
node.go
241 lines (208 loc) · 3.85 KB
/
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package GoFastExpress
type nodeType int
const (
NArg nodeType = iota
NString //string 节点
NFloat //float节点
NInt //int 节点
NUInt //uint节点
NBool //bool节点
NNil //空节点
NBinary //二元计算节点
NOpt //操作符节点
)
func (it nodeType) ToString() string {
switch it {
case NArg:
return "NArg"
case NString:
return "NString"
case NFloat:
return "NFloat"
case NInt:
return "NInt"
case NUInt:
return "NUInt"
case NBool:
return "NBool"
case NNil:
return "NNil"
case NBinary:
return "NBinary"
case NOpt:
return "NOpt"
}
return "Unknow"
}
//抽象语法树节点
type Node interface {
Type() nodeType
Eval(env interface{}) (interface{}, error)
Express() string
}
type OptNode struct {
value Operator
t nodeType
}
func (it OptNode) Type() nodeType {
return NOpt
}
func (it OptNode) IsCalculationOperator() bool {
//计算操作符
if it.value == Add ||
it.value == Reduce ||
it.value == Ride ||
it.value == Divide {
return true
}
return false
}
func (it OptNode) Express() string {
return it.value
}
func (it OptNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
//参数节点
type ArgNode struct {
value string
values []string
valuesLen int
t nodeType
}
func (it ArgNode) Type() nodeType {
return NArg
}
func (it ArgNode) Express() string {
return it.value
}
func (it ArgNode) Eval(env interface{}) (interface{}, error) {
return EvalTakes(it, env)
}
//值节点
type StringNode struct {
value string
t nodeType
}
func (it StringNode) Type() nodeType {
return NString
}
func (it StringNode) Express() string {
return it.value
}
func (it StringNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
//值节点
type FloatNode struct {
express string
value float64
t nodeType
}
func (it FloatNode) Express() string {
return it.express
}
func (it FloatNode) Type() nodeType {
return NFloat
}
func (it FloatNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
//值节点
type IntNode struct {
express string
value int64
t nodeType
}
func (it IntNode) Express() string {
return it.express
}
func (it IntNode) Type() nodeType {
return NInt
}
func (it IntNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
type UIntNode struct {
express string
value uint64
t nodeType
}
func (it UIntNode) Type() nodeType {
return NUInt
}
func (it UIntNode) Express() string {
return it.express
}
func (it UIntNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
//值节点
type BoolNode struct {
value bool
t nodeType
}
func (it BoolNode) Type() nodeType {
return NBool
}
func (it BoolNode) Express() string {
if it.value {
return "true"
} else {
return "false"
}
}
func (it BoolNode) Eval(env interface{}) (interface{}, error) {
return it.value, nil
}
//空节点
type NilNode struct {
t nodeType
}
func (it NilNode) Type() nodeType {
return NNil
}
func (it NilNode) Express() string {
return "nil"
}
func (NilNode) Eval(env interface{}) (interface{}, error) {
return nil, nil
}
//计算节点
type BinaryNode struct {
left Node
right Node
opt Operator
t nodeType
}
func (it BinaryNode) Type() nodeType {
return NBinary
}
func (it BinaryNode) Express() string {
var s = ""
if it.left != nil {
s += it.left.Express()
}
if it.right != nil {
s += it.right.Express()
}
return s
}
func (it BinaryNode) Eval(env interface{}) (interface{}, error) {
var left interface{}
var right interface{}
var e error
if it.left != nil {
left, e = it.left.Eval(env)
if e != nil {
return nil, e
}
}
if it.right != nil {
right, e = it.right.Eval(env)
if e != nil {
return nil, e
}
}
return Eval(it.Express(), it.opt, left, right)
}