forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.go
More file actions
386 lines (317 loc) · 6.92 KB
/
eval.go
File metadata and controls
386 lines (317 loc) · 6.92 KB
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
package expr
import (
"fmt"
"math"
"regexp"
)
// Eval parses and evaluates given input.
func Eval(input string, env interface{}) (interface{}, error) {
node, err := Parse(input)
if err != nil {
return nil, err
}
return Run(node, env)
}
// Run evaluates given ast.
func Run(node Node, env interface{}) (out interface{}, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
return node.Eval(env)
}
// eval functions
func (n nilNode) Eval(env interface{}) (interface{}, error) {
return nil, nil
}
func (n identifierNode) Eval(env interface{}) (interface{}, error) {
return n.value, nil
}
func (n numberNode) Eval(env interface{}) (interface{}, error) {
return n.value, nil
}
func (n boolNode) Eval(env interface{}) (interface{}, error) {
return n.value, nil
}
func (n textNode) Eval(env interface{}) (interface{}, error) {
return n.value, nil
}
func (n nameNode) Eval(env interface{}) (interface{}, error) {
return extract(env, n.name)
}
func (n unaryNode) Eval(env interface{}) (interface{}, error) {
val, err := n.node.Eval(env)
if err != nil {
return nil, err
}
switch n.operator {
case "not", "!":
if !isBool(val) {
return nil, fmt.Errorf("negation of non-bool type %T", val)
}
return !toBool(val), nil
}
v, err := cast(val)
if err != nil {
return nil, err
}
switch n.operator {
case "-":
return -v, nil
case "+":
return +v, nil
}
return nil, fmt.Errorf("implement unary %q operator", n.operator)
}
func (n binaryNode) Eval(env interface{}) (interface{}, error) {
left, err := n.left.Eval(env)
if err != nil {
return nil, err
}
switch n.operator {
case "or", "||":
if !isBool(left) {
return nil, fmt.Errorf("non-bool value in cond (%T)", left)
}
if toBool(left) {
return true, nil
}
right, err := n.right.Eval(env)
if err != nil {
return nil, err
}
if !isBool(right) {
return nil, fmt.Errorf("non-bool value in cond (%T)", right)
}
return toBool(right), nil
case "and", "&&":
if !isBool(left) {
return nil, fmt.Errorf("non-bool value in cond (%T)", left)
}
if toBool(left) {
right, err := n.right.Eval(env)
if err != nil {
return nil, err
}
if !isBool(right) {
return nil, fmt.Errorf("non-bool value in cond (%T)", right)
}
return toBool(right), nil
}
return false, nil
}
right, err := n.right.Eval(env)
if err != nil {
return nil, err
}
switch n.operator {
case "==":
return equal(left, right), nil
case "!=":
return !equal(left, right), nil
case "in":
ok, err := contains(left, right)
if err != nil {
return nil, err
}
return ok, nil
case "not in":
ok, err := contains(left, right)
if err != nil {
return nil, err
}
return !ok, nil
case "~":
if isText(left) && isText(right) {
return toText(left) + toText(right), nil
}
return nil, fmt.Errorf("operator ~ not defined on (%T, %T)", left, right)
}
// Next goes operators on numbers
l, err := cast(left)
if err != nil {
return nil, err
}
r, err := cast(right)
if err != nil {
return nil, err
}
switch n.operator {
case "|":
return int(l) | int(r), nil
case "^":
return int(l) ^ int(r), nil
case "&":
return int(l) & int(r), nil
case "<":
return l < r, nil
case ">":
return l > r, nil
case ">=":
return l >= r, nil
case "<=":
return l <= r, nil
case "+":
return l + r, nil
case "-":
return l - r, nil
case "*":
return l * r, nil
case "/":
div := r
if div == 0 {
return nil, fmt.Errorf("division by zero")
}
return l / div, nil
case "%":
numerator := int64(l)
denominator := int64(r)
if denominator == 0 {
return nil, fmt.Errorf("division by zero")
}
return float64(numerator % denominator), nil
case "**":
return math.Pow(l, r), nil
case "..":
return makeRange(int64(l), int64(r))
}
return nil, fmt.Errorf("implement %q operator", n.operator)
}
func makeRange(min, max int64) ([]float64, error) {
size := max - min + 1
if size > 1e6 {
return nil, fmt.Errorf("range %v..%v exceeded max size of 1e6", min, max)
}
a := make([]float64, size)
for i := range a {
a[i] = float64(min + int64(i))
}
return a, nil
}
func (n matchesNode) Eval(env interface{}) (interface{}, error) {
left, err := n.left.Eval(env)
if err != nil {
return nil, err
}
if n.r != nil {
if isText(left) {
return n.r.MatchString(toText(left)), nil
}
}
right, err := n.right.Eval(env)
if err != nil {
return nil, err
}
if isText(left) && isText(right) {
matched, err := regexp.MatchString(toText(right), toText(left))
if err != nil {
return nil, err
}
return matched, nil
}
return nil, fmt.Errorf("operator matches doesn't defined on (%T, %T): %v", left, right, n)
}
func (n propertyNode) Eval(env interface{}) (interface{}, error) {
v, err := n.node.Eval(env)
if err != nil {
return nil, err
}
return extract(v, n.property)
}
func (n indexNode) Eval(env interface{}) (interface{}, error) {
v, err := n.node.Eval(env)
if err != nil {
return nil, err
}
p, err := n.index.Eval(env)
if err != nil {
return nil, err
}
return extract(v, p)
}
func (n methodNode) Eval(env interface{}) (interface{}, error) {
v, err := n.node.Eval(env)
if err != nil {
return nil, err
}
method, err := extract(v, n.method)
if err != nil {
return nil, err
}
return call(n.method, method, n.arguments, env)
}
func (n builtinNode) Eval(env interface{}) (interface{}, error) {
if len(n.arguments) == 0 {
return nil, fmt.Errorf("missing argument to %v", n.name)
}
if len(n.arguments) > 1 {
return nil, fmt.Errorf("too many arguments to %v: %v", n.name, n)
}
a, err := n.arguments[0].Eval(env)
if err != nil {
return nil, err
}
switch n.name {
case "len":
return count(n.arguments[0], a)
}
return nil, fmt.Errorf("unknown %q builtin", n.name)
}
func (n functionNode) Eval(env interface{}) (interface{}, error) {
fn, err := extract(env, n.name)
if err != nil {
return nil, err
}
return call(n.name, fn, n.arguments, env)
}
func (n conditionalNode) Eval(env interface{}) (interface{}, error) {
cond, err := n.cond.Eval(env)
if err != nil {
return nil, err
}
// If
if !isBool(cond) {
return nil, fmt.Errorf("non-bool value used in cond (%T)", cond)
}
// Then
if toBool(cond) {
a, err := n.exp1.Eval(env)
if err != nil {
return nil, err
}
return a, nil
}
// Else
b, err := n.exp2.Eval(env)
if err != nil {
return nil, err
}
return b, nil
}
func (n arrayNode) Eval(env interface{}) (interface{}, error) {
array := make([]interface{}, 0)
for _, node := range n.nodes {
val, err := node.Eval(env)
if err != nil {
return nil, err
}
array = append(array, val)
}
return array, nil
}
func (n mapNode) Eval(env interface{}) (interface{}, error) {
m := make(map[interface{}]interface{})
for _, pair := range n.pairs {
key, err := pair.key.Eval(env)
if err != nil {
return nil, err
}
value, err := pair.value.Eval(env)
if err != nil {
return nil, err
}
m[key] = value
}
return m, nil
}