/
eval.go
515 lines (433 loc) · 8.94 KB
/
eval.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2012-2014, Rolf Veen and contributors.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ogdl
import "strconv"
// Eval takes a parsed expression and evaluates it
// in the context of the current graph.
func (g *Graph) Eval(e *Graph) interface{} {
switch e.String() {
case TypePath:
return g.EvalPath(e)
case TypeExpression:
return g.EvalExpression(e)
}
if e.Len() != 0 {
return e
}
// Return constant in its normalizad native form
// either: int64, float64, string, bool or []byte
return e.Scalar()
}
// EvalBool takes a parsed expression and evaluates it in the context of the
// current graph, and converts the result to a boolean.
func (g *Graph) EvalBool(e *Graph) bool {
b, _ := _boolf(g.Eval(e))
return b
}
// EvalPath traverses g following a path p. The path needs to be previously converted
// to a Graph with NewPath().
//
// This function is similar to ogdl.Get, but for complexer paths. Code could
// be shared.
func (g *Graph) EvalPath(p *Graph) interface{} {
if p.Len() == 0 {
return nil
}
// Normalize the context graph, so that the root is
// always transparent.
var node, nodePrev *Graph
if !g.IsNil() {
node = NilGraph()
node.Add(g)
} else {
node = g
}
iknow := false
for i := 0; i < len(p.Out); i++ {
n := p.Out[i]
// For each path element, look at its type:
// token, index, selector, arglist
s := n.String()
iknow = false
switch s {
case TypeIndex:
// must evaluate to an integer
if n.Len() == 0 {
return "empty []"
}
itf := g.EvalExpression(n.Out[0])
ix, ok := _int64(itf)
if !ok || ix < 0 {
return "[] does not evaluate to a valid integer"
}
nodePrev = node
node = node.GetAt(int(ix))
case TypeSelector:
if nodePrev == nil || nodePrev.Len() == 0 || i < 1 {
return nil
}
elemPrev := p.Out[i-1].String()
if len(elemPrev) == 0 {
return nil
}
r := NilGraph()
if n.Len() == 0 {
// This case is {}, meaning that we must return
// all ocurrences of the token just before (elemPrev).
// And that means creating a new Graph object.
r.addEqualNodes(nodePrev, elemPrev, false)
if r.Len() == 0 {
return nil
}
node = r
} else {
i, err := strconv.Atoi(n.Out[0].String())
if err != nil || i < 0 {
return nil
}
// {0} must still be handled: add it to r
i++
// of all the nodes with name elemPrev, select the ith.
for _, nn := range nodePrev.Out {
if nn.String() == elemPrev {
i--
if i == 0 {
r.AddNodes(nn)
node = r
break
}
}
}
if i > 0 {
return nil
}
}
case "_len":
return node.Len()
case TypeGroup:
// The following format is supported: ( expression )
// The expression is evaluated and used as path element
itf := g.EvalExpression(n.Out[0])
str := _string(itf)
if len(str) == 0 {
return nil // expr does not evaluate to a string
}
s = str
fallthrough
default:
nn := node.Node(s)
if nn == nil {
// It may have a !type
itf, _ := node.Function(p, i, g)
if itf == nil {
itf, _ = node.Function2(p,i,g)
}
return itf
}
iknow = true
nodePrev = node
node = nn
}
}
if node == nil {
return nil
}
// iknow is true if the path includes the token that is now at the root of node.
// We don't want to return what we already know.
if iknow {
if node.Len() == 1 {
node = node.Out[0]
} else {
node2 := NilGraph()
node2.Out = node.Out
return node2
}
}
// A nil node with one subnode makes no sense. Nil root nodes
// are used as list containers.
if node.IsNil() && node.Len() == 1 {
return node.Out[0]
}
// simplify: do not return Graph if it has no subnodes
if node.Len() == 0 {
return node.This
}
return node
}
// EvalExpression evaluates expressions (!e)
// g can have native types (other things than strings), but
// p only []byte or string
//
func (g *Graph) EvalExpression(p *Graph) interface{} {
// Return nil and empty strings as is
if p.This == nil {
return nil
}
s := p.String()
if len(s) == 0 {
return ""
}
// first check if it is a number because it can have an operatorChar
// in front: the minus sign
if isNumber(s) {
return p.Number()
}
switch s {
case "!":
// Unary expression !expr
return !g.EvalBool(p.Out[0])
case TypeExpression:
return g.EvalExpression(p.GetAt(0))
case TypePath:
return g.EvalPath(p)
case TypeGroup:
// expression list
r := NewGraph(TypeGroup)
for _, expr := range p.Out {
r.Add(g.EvalExpression(expr))
}
return r
}
c := int(s[0])
// [!] Operator should be identified. Operators written as strings are
// missinterpreted.
if IsOperatorChar(c) {
if len(s)<=2 {
if len(s)==1 || IsOperatorChar(int(s[1])) {
return g.evalBinary(p)
}
}
}
if c == '"' || c == '\'' {
return s
}
if IsLetter(c) {
if s == "false" {
return false
}
if s == "true" {
return true
}
return s
}
return p
}
func (g *Graph) evalBinary(p *Graph) interface{} {
// p.String() is the operator
n1 := p.Out[0]
i2 := g.EvalExpression(p.Out[1])
switch p.String() {
case "+":
return calc(g.EvalExpression(n1), i2, '+')
case "-":
return calc(g.EvalExpression(n1), i2, '-')
case "*":
return calc(g.EvalExpression(n1), i2, '*')
case "/":
return calc(g.EvalExpression(n1), i2, '/')
case "%":
return calc(g.EvalExpression(n1), i2, '%')
case "=":
return g.assign(n1, i2, '=')
case "+=":
return g.assign(n1, i2, '+')
case "-=":
return g.assign(n1, i2, '-')
case "*=":
return g.assign(n1, i2, '*')
case "/=":
return g.assign(n1, i2, '/')
case "%=":
return g.assign(n1, i2, '%')
case "==":
return compare(g.EvalExpression(n1), i2, '=')
case ">=":
return compare(g.EvalExpression(n1), i2, '+')
case "<=":
return compare(g.EvalExpression(n1), i2, '-')
case "!=":
return compare(g.EvalExpression(n1), i2, '!')
case ">":
return compare(g.EvalExpression(n1), i2, '>')
case "<":
return compare(g.EvalExpression(n1), i2, '<')
case "&&":
return logic(g.EvalExpression(n1), i2, '&')
case "||":
return logic(g.EvalExpression(n1), i2, '|')
}
return nil
}
// int* | float* | string
// first element determines type
func compare(v1, v2 interface{}, op int) bool {
// fmt.Printf("compare [%v] [%v]\n", v1, v2)
i1, ok := _int64(v1)
if ok {
i2, ok := _int64f(v2)
if !ok {
return false
}
switch op {
case '=':
return i1 == i2
case '+':
return i1 >= i2
case '-':
return i1 <= i2
case '>':
return i1 > i2
case '<':
return i1 < i2
case '!':
return i1 != i2
}
return false
}
f1, ok := _float64(v1)
if ok {
f2, ok := _float64f(v2)
if !ok {
return false
}
switch op {
case '=':
return f1 == f2
case '+':
return f1 >= f2
case '-':
return f1 <= f2
case '>':
return f1 > f2
case '<':
return f1 < f2
case '!':
return f1 != f2
}
return false
}
s1 := _string(v1)
s2 := _string(v2)
switch op {
case '=':
return s1 == s2
case '!':
return s1 != s2
}
return false
}
func logic(i1, i2 interface{}, op int) bool {
b1, ok1 := _boolf(i1)
b2, ok2 := _boolf(i2)
if !ok1 || !ok2 {
return false
}
switch op {
case '&':
return b1 && b2
case '|':
return b1 || b2
}
return false
}
// assign modifies the context graph
func (g *Graph) assign(p *Graph, v interface{}, op int) interface{} {
if op == '=' {
return g.set(p, v)
}
// if p doesn't exist, just set it to the value given
left := g.get(p)
if left != nil {
return g.set(p, calc(left.This, v, op))
}
switch op {
case '+':
return g.set(p, v)
case '-':
return g.set(p, calc(0, v, '-'))
case '*':
return g.set(p, 0)
case '/':
return g.set(p, "infinity")
case '%':
return g.set(p, "undefined")
}
return nil
}
// calc: int64 | float64 | string
func calc(v1, v2 interface{}, op int) interface{} {
//fmt.Printf("calc: %v %v %s %s\n",v1,v2, _typeOf(v1),_typeOf(v2) )
i1, ok := _int64(v1)
i2, ok2 := _int64(v2)
var ok3, ok4 bool
var i3, i4 float64
if !ok {
i3, ok3 = _float64(v1)
}
if !ok2 {
i4, ok4 = _float64(v2)
}
if ok && ok2 {
switch op {
case '+':
return i1 + i2
case '-':
return i1 - i2
case '*':
return i1 * i2
case '/':
return i1 / i2
case '%':
return i1 % i2
}
}
if ok3 && ok4 {
switch op {
case '+':
return i3 + i4
case '-':
return i3 - i4
case '*':
return i3 * i4
case '/':
return i3 / i4
case '%':
return int(i3) % int(i4)
}
}
if ok && ok4 {
i3 = float64(i1)
switch op {
case '+':
return i3 + i4
case '-':
return i3 - i4
case '*':
return i3 * i4
case '/':
return i3 / i4
case '%':
return i1 % int64(i4)
}
}
if ok3 && ok2 {
i4 = float64(i2)
switch op {
case '+':
return i3 + i4
case '-':
return i3 - i4
case '*':
return i3 * i4
case '/':
return i3 / i4
case '%':
return int64(i3) % i2
}
}
if op != '+' {
return nil
}
return _string(v1) + _string(v2)
}