-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
413 lines (386 loc) · 9.49 KB
/
tree.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package simple_template
import (
"fmt"
"math"
"reflect"
)
var buildIns = map[string]Func{
"toNumber": toNumber,
"toBool": toBool,
"isNaN": isNaN,
}
func init() {
yyErrorVerbose = true
}
func NewExecuteContenxt(idents map[string]interface{}, identFuncs map[string]func() (interface{}, error), funcs map[string]Func) *ExecuteContext {
finalFunc := make(map[string]Func)
for k, v := range buildIns {
finalFunc[k] = v
}
for k, v := range funcs {
finalFunc[k] = v
}
return &ExecuteContext{
definedIdent: idents,
definedFunc: finalFunc,
definedIdentFunc: identFuncs,
}
}
type Func func(inputs []interface{}) (interface{}, error)
type ExecuteContext struct {
definedIdent map[string]interface{}
definedIdentFunc map[string]func() (interface{}, error)
definedFunc map[string]Func
AttrGetIgnoreEmpty bool
}
func (ec *ExecuteContext) EvaluateExpression(expr Expr) (interface{}, error) {
switch vt := expr.(type) {
case *StringExpr:
return vt.Value, nil
case *NumberExpr:
return vt.Value, nil
case *TrueExpr:
return true, nil
case *FalseExpr:
return false, nil
case *IdentExpr:
return ec.evalIdentityExpr(vt)
case *LogicalExpr:
return ec.evalLogicalExpr(vt)
case *ArithmeticOpExpr:
return ec.evalArithmethicExpr(vt)
case *IfExpr:
return ec.evalIfExpr(vt)
case *AttrGetExpr:
return ec.evalAttrGetExpr(vt)
case *FuncCallExpr:
return ec.evalFuncCallExpr(vt)
case *MapConstructExpr:
return ec.evalMapConstructExpr(vt)
case *ArrayConstructExpr:
return ec.evalArrayConstructExpr(vt)
}
return nil, fmt.Errorf("unknow expression %T", expr)
}
func (ec *ExecuteContext) evalMapConstructExpr(e *MapConstructExpr) (interface{}, error) {
result := make(map[interface{}]interface{})
for _, m := range e.Members {
mt, ok := m.(*MapItemExpr)
if !ok {
return nil, fmt.Errorf("line %v map expr with no map item expr", e.Line())
}
k, err := ec.EvaluateExpression(mt.Key)
if err != nil {
return nil, err
}
v, err := ec.EvaluateExpression(mt.Value)
if err != nil {
return nil, err
}
result[k] = v
}
return result, nil
}
func (ec *ExecuteContext) evalArrayConstructExpr(e *ArrayConstructExpr) (interface{}, error) {
result := make([]interface{}, 0, len(e.Members))
for _, m := range e.Members {
k, err := ec.EvaluateExpression(m)
if err != nil {
return nil, err
}
result = append(result, k)
}
return result, nil
}
func (ec *ExecuteContext) evalFuncCallExpr(e *FuncCallExpr) (interface{}, error) {
if ec.definedFunc == nil {
return nil, fmt.Errorf("function %v cannot be find", e.Func.Value)
}
f := ec.definedFunc[e.Func.Value]
if f != nil {
inputs := make([]interface{}, 0, len(e.Args))
for _, a := range e.Args {
va, err := ec.EvaluateExpression(a)
if err != nil {
return nil, err
}
inputs = append(inputs, va)
}
return f(inputs)
}
return nil, fmt.Errorf("function %v cannot be find", e.Func.Value)
}
func (ec *ExecuteContext) evalIdentityExpr(e *IdentExpr) (interface{}, error) {
if ec.definedIdent == nil {
return nil, fmt.Errorf("%v cannot be find", e.Value)
}
ival := ec.definedIdent[e.Value]
if ival != nil {
return ival, nil
}
if ec.definedIdentFunc != nil {
f := ec.definedIdentFunc[e.Value]
if f != nil {
return f()
}
}
return nil, fmt.Errorf("%v cannot be find", e.Value)
}
func (ec *ExecuteContext) evalLogicalExpr(e *LogicalExpr) (bool, error) {
left, err := ec.EvaluateExpression(e.Lhs)
if err != nil {
return false, err
}
// need special process for these 2 operator
if e.Operator == "&&" || e.Operator == "||" {
leftBool, ok := TryBool(left)
if !ok {
return false, fmt.Errorf("left expr value unknown, %v", left)
}
if e.Operator == "&&" && !leftBool {
return false, nil
}
if e.Operator == "||" && leftBool {
return true, nil
}
right, err := ec.EvaluateExpression(e.Rhs)
if err != nil {
return false, err
}
rightBool, ok := TryBool(right)
if !ok {
return false, fmt.Errorf("right expr value unknown, %v", left)
}
if e.Operator == "||" {
return leftBool || rightBool, nil
} else {
return leftBool && rightBool, nil
}
}
right, err := ec.EvaluateExpression(e.Rhs)
if err != nil {
return false, err
}
switch e.Operator {
case "==", "!=":
eq := true
if e.Operator == "!=" {
eq = !eq
}
// one is number, then compare the other to number
_, lok := left.(float64)
_, rok := right.(float64)
if lok || rok {
lnum, ok := TryNumber(left)
if !ok {
return false, nil
}
rnum, ok := TryNumber(right)
if !ok {
return false, nil
}
if eq {
return lnum == rnum, nil
} else {
return lnum != rnum, nil
}
}
// one is bool
_, lok = left.(bool)
_, rok = right.(bool)
if lok || rok {
lb, ok := TryBool(left)
if !ok {
return false, nil
}
rb, ok := TryBool(right)
if !ok {
return false, nil
}
if eq {
return lb == rb, nil
} else {
return lb != rb, nil
}
}
// then all should be string, or we get an err
_, lok = left.(string)
_, rok = right.(string)
if lok && rok {
lb, ok := TryString(left)
if !ok {
return false, nil
}
rb, ok := TryString(right)
if !ok {
return false, nil
}
if eq {
return lb == rb, nil
} else {
return lb != rb, nil
}
} else {
return false, fmt.Errorf("logical expression with left or right side type unknow %v => %v", left, right)
}
case ">", "<", ">=", "<=":
// will convert both side to number
lnum, ok := TryNumber(left)
if !ok {
return false, fmt.Errorf("%v cannot conver to number when compare", left)
}
rnum, ok := TryNumber(right)
if !ok {
return false, fmt.Errorf("%v cannot conver to number when compare", right)
}
switch e.Operator {
case ">":
return lnum > rnum, nil
case "<":
return lnum < rnum, nil
case ">=":
return lnum >= rnum, nil
case "<=":
return lnum <= rnum, nil
default:
panic("you should not reach here")
}
}
return false, fmt.Errorf("invalid operator %v", e.Operator)
}
func (ec *ExecuteContext) evalArithmethicExpr(e *ArithmeticOpExpr) (interface{}, error) {
left, err := ec.EvaluateExpression(e.Lhs)
if err != nil {
return false, err
}
right, err := ec.EvaluateExpression(e.Rhs)
if err != nil {
return false, err
}
switch e.Operator {
case "+":
_, lok := left.(string)
_, rok := right.(string)
if lok || rok {
lstr, ok := TryString(left)
if !ok {
return false, fmt.Errorf("%v cannot conver to string when +", left)
}
rstr, ok := TryString(right)
if !ok {
return false, fmt.Errorf("%v cannot conver to string when +", right)
}
return lstr + rstr, nil
}
fallthrough
case "-", "*", "/", "%":
lnum, ok := TryNumber(left)
if !ok {
return false, fmt.Errorf("%v cannot conver to number when +", left)
}
rnum, ok := TryNumber(right)
if !ok {
return false, fmt.Errorf("%v cannot conver to number when +", right)
}
switch e.Operator {
case "+":
return lnum + rnum, nil
case "-":
return lnum - rnum, nil
case "*":
return lnum * rnum, nil
case "/":
if rnum == 0 {
return nil, fmt.Errorf("%v / %v : divided by zero", left, right)
}
return lnum / rnum, nil
case "%":
lint := math.Trunc(lnum)
rint := math.Trunc(rnum)
if lint != lnum || rint != rnum {
return false, fmt.Errorf("%v or %v cannot be conver to int in % operation", lnum, rnum)
}
return int64(lint) % int64(rint), nil
default:
panic("you should never reach here")
}
return lnum + rnum, nil
}
return nil, fmt.Errorf("invalid operator %v", e.Operator)
}
func (ec *ExecuteContext) evalIfExpr(e *IfExpr) (interface{}, error) {
cond, err := ec.EvaluateExpression(e.Cond)
if err != nil {
return nil, err
}
if condB, ok := TryBool(cond); ok {
if condB {
return ec.EvaluateExpression(e.Then)
} else {
if e.Else != nil {
return ec.EvaluateExpression(e.Else)
}
return nil, nil
}
} else {
return nil, fmt.Errorf("condition cannot eval to bool %v", cond)
}
}
func (ec *ExecuteContext) evalAttrGetExpr(e *AttrGetExpr) (interface{}, error) {
obj, err := ec.EvaluateExpression(e.Object)
if err != nil {
return nil, err
}
key, err := ec.EvaluateExpression(e.Key)
if err != nil {
return nil, err
}
// array
objr := reflect.ValueOf(obj)
if objr.Type().Kind() == reflect.Slice || objr.Type().Kind() == reflect.Array {
index, ok := TryNumber(key)
if !ok {
return nil, nil
}
if math.Trunc(index) != index {
return nil, fmt.Errorf("index %v invald in %v", index, obj)
}
intIndex := int(index)
if 0 <= intIndex && intIndex < objr.Len() {
rs := objr.Index(intIndex)
return rs.Interface(), nil
} else {
return nil, fmt.Errorf("index %v out of range in %v", index, obj)
}
}
// map
if objr.Type().Kind() == reflect.Map {
keyType := objr.Type().Key()
if keyType.Kind() != reflect.String && keyType.Kind() != reflect.Float64 {
return nil, fmt.Errorf("map key must be string or number, current %v", keyType)
}
keyVal := reflect.ValueOf(key)
if keyType.Kind() == reflect.String && keyVal.Type().Kind() != reflect.String {
if sKey, ok := TryString(key); ok {
keyVal = reflect.ValueOf(sKey)
} else {
return nil, nil
}
} else if keyType.Kind() == reflect.Float64 && keyVal.Type().Kind() != reflect.Float64 {
if sKey, ok := TryNumber(key); ok {
keyVal = reflect.ValueOf(sKey)
} else {
return nil, nil
}
}
res := objr.MapIndex(keyVal)
if res.IsValid() {
return res.Interface(), nil
}
return nil, nil
}
if ec.AttrGetIgnoreEmpty {
return nil, nil
}
return nil, fmt.Errorf("object %v is not array or map", obj)
}