-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
382 lines (339 loc) · 6.94 KB
/
engine.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
package eval
import (
"context"
"errors"
)
type (
VariableKey int16
Value interface{}
Operator func(ctx *Ctx, params []Value) (res Value, err error)
)
type Ctx struct {
VariableFetcher
Ctx context.Context
}
const (
// node types flag
nodeTypeMask = uint8(0b00000111)
constant = uint8(0b00000001)
variable = uint8(0b00000010)
operator = uint8(0b00000011)
fastOperator = uint8(0b00000100)
cond = uint8(0b00000101)
event = uint8(0b00000111)
// short circuit flag
scMask = uint8(0b00011000)
scIfFalse = uint8(0b00001000)
scIfTrue = uint8(0b00010000)
// parent bool op flag
parentOpMask = uint8(0b01100000)
andOp = uint8(0b00100000)
orOp = uint8(0b01000000)
)
type node struct {
flag uint8
childCnt int8
scIdx int16
osTop int16
varKey VariableKey
value Value
operator Operator
}
func (n *node) getNodeType() uint8 {
return n.flag & nodeTypeMask
}
type Expr struct {
maxStackSize int16
nodes []*node
parentIdx []int16
EventChan chan Event
}
func Eval(expr string, vals map[string]interface{}, opts ...Option) (Value, error) {
if len(opts) == 0 {
opts = append(opts, RegVarAndOp(vals))
}
conf := NewConfig(opts...)
tree, err := Compile(conf, expr)
if err != nil {
return nil, err
}
return tree.Eval(NewCtxFromVars(conf, vals))
}
func (e *Expr) EvalBool(ctx *Ctx) (bool, error) {
res, err := e.Eval(ctx)
if err != nil {
return false, err
}
b, ok := res.(bool)
if !ok {
return false, errors.New("invalid result type error")
}
return b, nil
}
func (e *Expr) TryEvalBool(ctx *Ctx) (bool, error) {
res, err := e.TryEval(ctx)
if err != nil {
return false, err
}
if res == DNE {
return false, ErrDNE
}
b, ok := res.(bool)
if !ok {
return false, errors.New("invalid result type error")
}
return b, nil
}
func (e *Expr) Eval(ctx *Ctx) (res Value, err error) {
var (
nodes = e.nodes
size = int16(len(nodes))
m = e.maxStackSize
os []Value
osTop = int16(-1)
)
switch {
case m <= 8:
os = make([]Value, 8)
case m <= 16:
os = make([]Value, 16)
default:
os = make([]Value, size)
}
var (
params []Value
param2 [2]Value
curt *node
)
for i := int16(0); i < size; i++ {
curt = nodes[i]
switch curt.flag & nodeTypeMask {
case fastOperator:
i++
child := nodes[i]
res = child.value
if child.flag&nodeTypeMask == variable {
res, err = ctx.Get(child.varKey, res.(string))
if err != nil {
return
}
}
param2[0] = res
i++
child = nodes[i]
res = child.value
if child.flag&nodeTypeMask == variable {
res, err = ctx.Get(child.varKey, res.(string))
if err != nil {
return
}
}
param2[1] = res
res, err = curt.operator(ctx, param2[:])
if err != nil {
return
}
case variable:
res, err = ctx.Get(curt.varKey, curt.value.(string))
if err != nil {
return
}
case constant:
res = curt.value
case operator:
cCnt := int16(curt.childCnt)
osTop = osTop - cCnt
if cCnt == 2 {
param2[0], param2[1] = os[osTop+1], os[osTop+2]
params = param2[:]
} else {
params = make([]Value, cCnt)
copy(params, os[osTop+1:])
}
res, err = curt.operator(ctx, params)
if err != nil {
return
}
case cond:
res, osTop = os[osTop], osTop-1
res, err = curt.operator(ctx, []Value{res})
if err != nil {
return
}
if res == true {
osTop = curt.osTop
i = curt.scIdx
}
continue
default:
reportEvent(e, os, osTop, curt.value)
continue
}
if b, ok := res.(bool); ok {
for (!b && curt.flag&scIfFalse == scIfFalse) ||
(b && curt.flag&scIfTrue == scIfTrue) {
i = curt.scIdx
if i == -1 {
return
}
curt = nodes[i]
osTop = curt.osTop - 1
}
}
os[osTop+1], osTop = res, osTop+1
}
return os[0], nil
}
func (e *Expr) TryEval(ctx *Ctx) (res Value, err error) {
var (
nodes = e.nodes
size = int16(len(nodes))
m = e.maxStackSize
os []Value
osTop = int16(-1)
)
switch {
case m <= 8:
os = make([]Value, 8)
case m <= 16:
os = make([]Value, 16)
default:
os = make([]Value, size)
}
var (
param []Value
param2 [2]Value
curt *node
)
for i := int16(0); i < size; i++ {
curt = nodes[i]
switch curt.flag & nodeTypeMask {
case fastOperator:
param2[0], err = getNodeValueProxy(ctx, nodes[i+1])
if err != nil {
return
}
param2[1], err = getNodeValueProxy(ctx, nodes[i+2])
if err != nil {
return
}
res, err = executeOperatorProxy(ctx, curt, param2[:])
if err != nil {
return
}
i += 2
case variable:
res, err = fetchVariableValueProxy(ctx, curt)
if err != nil {
return
}
case constant:
res = curt.value
case operator:
cCnt := int16(curt.childCnt)
osTop = osTop - cCnt
if cCnt == 2 {
param2[0], param2[1] = os[osTop+1], os[osTop+2]
param = param2[:]
} else {
param = make([]Value, cCnt)
copy(param, os[osTop+1:])
}
res, err = executeOperatorProxy(ctx, curt, param)
if err != nil {
return
}
case cond:
res, osTop = os[osTop], osTop-1
res, err = curt.operator(ctx, []Value{res})
if err != nil {
return
}
if res == true {
osTop = curt.osTop
i = curt.scIdx
}
continue
default:
reportEvent(e, os, osTop, curt.value)
continue
}
for matchesShortCircuit(res, curt) {
// jump to parent node
curt, i = parentNode(e, i)
if i == -1 {
return
}
if curt.flag&nodeTypeMask == cond &&
!matchesShortCircuit(res, curt) {
i = nodes[curt.scIdx].scIdx
osTop = nodes[i].osTop - 1
break
} else {
osTop = curt.osTop - 1
}
}
os[osTop+1], osTop = res, osTop+1
}
return os[0], nil
}
func matchesShortCircuit(res Value, n *node) bool {
switch n.flag & parentOpMask {
case andOp:
return res == false
case orOp:
return res == true
default:
return res == DNE
}
}
func executeOperatorProxy(ctx *Ctx, n *node, params []Value) (Value, error) {
switch {
case isAndOpNode(n) && contains(params, false):
return false, nil
case isOrOpNode(n) && contains(params, true):
return true, nil
case contains(params, DNE):
return DNE, nil
}
return n.operator(ctx, params)
}
func getNodeValueProxy(ctx *Ctx, n *node) (res Value, err error) {
if n.flag&nodeTypeMask == constant {
res = n.value
} else {
res, err = fetchVariableValueProxy(ctx, n)
}
return
}
func fetchVariableValueProxy(ctx *Ctx, n *node) (Value, error) {
var (
varKey = n.varKey
strKey = n.value.(string)
)
if !ctx.Cached(varKey, strKey) {
return DNE, nil
}
return ctx.Get(varKey, strKey)
}
type EventType string
const (
LoopEvent EventType = "LOOP"
OpExecEvent EventType = "OP_EXEC"
)
type Event struct {
EventType EventType
Stack []Value
Data interface{}
}
func reportEvent(e *Expr, os []Value, osTop int16, data Value) {
stack := make([]Value, osTop+1)
for i := int16(0); i <= osTop; i++ {
stack[i] = os[i]
}
e.EventChan <- Event{
EventType: LoopEvent,
Stack: stack,
Data: data,
}
}