forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateful_expr.go
365 lines (348 loc) · 7.17 KB
/
stateful_expr.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
package tick
import (
"errors"
"fmt"
"math"
"regexp"
)
var ErrInvalidExpr = errors.New("expression is invalid, could not evaluate")
// Expression functions are stateful. Their state is updated with
// each call to the function. A StatefulExpr is a Node
// and its associated function state.
type StatefulExpr struct {
Node Node
Funcs Funcs
}
func NewStatefulExpr(n Node) *StatefulExpr {
return &StatefulExpr{
Node: n,
Funcs: NewFunctions(),
}
}
// Reset the state
func (s *StatefulExpr) Reset() {
for _, f := range s.Funcs {
f.Reset()
}
}
func (s *StatefulExpr) EvalBool(scope *Scope) (bool, error) {
stck := &stack{}
err := s.eval(s.Node, scope, stck)
if err != nil {
return false, err
}
if stck.Len() == 1 {
value := stck.Pop()
// Resolve reference
if ref, ok := value.(*ReferenceNode); ok {
value, err = scope.Get(ref.Reference)
if err != nil {
return false, err
}
}
b, ok := value.(bool)
if ok {
return b, nil
} else {
return false, fmt.Errorf("expression returned unexpected type %T", value)
}
}
return false, ErrInvalidExpr
}
func (s *StatefulExpr) EvalNum(scope *Scope) (float64, error) {
stck := &stack{}
err := s.eval(s.Node, scope, stck)
if err != nil {
return math.NaN(), err
}
if stck.Len() == 1 {
value := stck.Pop()
// Resolve reference
if ref, ok := value.(*ReferenceNode); ok {
value, err = scope.Get(ref.Reference)
if err != nil {
return math.NaN(), err
}
}
n, ok := value.(float64)
if ok {
return n, nil
} else {
return math.NaN(), fmt.Errorf("expression returned unexpected type %T", value)
}
}
return math.NaN(), ErrInvalidExpr
}
func (s *StatefulExpr) eval(n Node, scope *Scope, stck *stack) (err error) {
switch node := n.(type) {
case *BoolNode:
stck.Push(node.Bool)
case *NumberNode:
if node.IsInt {
stck.Push(node.Int64)
} else {
stck.Push(node.Float64)
}
case *DurationNode:
stck.Push(node.Dur)
case *StringNode:
stck.Push(node.Literal)
case *RegexNode:
stck.Push(node.Regex)
case *UnaryNode:
err = s.eval(node.Node, scope, stck)
if err != nil {
return
}
s.evalUnary(node.Operator, scope, stck)
case *BinaryNode:
err = s.eval(node.Left, scope, stck)
if err != nil {
return
}
err = s.eval(node.Right, scope, stck)
if err != nil {
return
}
err = s.evalBinary(node.Operator, scope, stck)
if err != nil {
return
}
case *FunctionNode:
args := make([]interface{}, len(node.Args))
for i, arg := range node.Args {
err = s.eval(arg, scope, stck)
if err != nil {
return
}
a := stck.Pop()
if r, ok := a.(*ReferenceNode); ok {
a, err = scope.Get(r.Reference)
if err != nil {
return err
}
}
args[i] = a
}
// Call function
f := s.Funcs[node.Func]
if f == nil {
return fmt.Errorf("undefined function %s", node.Func)
}
ret, err := f.Call(args...)
if err != nil {
return fmt.Errorf("error calling %s: %s", node.Func, err)
}
stck.Push(ret)
default:
stck.Push(node)
}
return nil
}
func (s *StatefulExpr) evalUnary(op tokenType, scope *Scope, stck *stack) error {
v := stck.Pop()
switch op {
case TokenMinus:
switch n := v.(type) {
case float64:
stck.Push(-1 * n)
case int64:
stck.Push(-1 * n)
default:
return fmt.Errorf("invalid arugument to '-' %v", v)
}
case TokenNot:
if b, ok := v.(bool); ok {
stck.Push(!b)
} else {
return fmt.Errorf("invalid arugument to '!' %v", v)
}
}
return nil
}
func errMismatched(op tokenType, l, r interface{}) error {
return fmt.Errorf("mismatched type to binary operator. got %T %v %T. see bool(), int(), float()", l, op, r)
}
func (s *StatefulExpr) evalBinary(op tokenType, scope *Scope, stck *stack) (err error) {
r := stck.Pop()
l := stck.Pop()
// Resolve any references
if ref, ok := l.(*ReferenceNode); ok {
l, err = scope.Get(ref.Reference)
if err != nil {
return err
}
}
if ref, ok := r.(*ReferenceNode); ok {
r, err = scope.Get(ref.Reference)
if err != nil {
return err
}
}
var v interface{}
switch {
case isMathOperator(op):
switch ln := l.(type) {
case int64:
rn, ok := r.(int64)
if !ok {
return errMismatched(op, l, r)
}
v, err = doIntMath(op, ln, rn)
case float64:
rn, ok := r.(float64)
if !ok {
return errMismatched(op, l, r)
}
v, err = doFloatMath(op, ln, rn)
default:
return errMismatched(op, l, r)
}
case isCompOperator(op):
switch ln := l.(type) {
case bool:
rn, ok := r.(bool)
if !ok {
return errMismatched(op, l, r)
}
v, err = doBoolComp(op, ln, rn)
case int64:
lf := float64(ln)
var rf float64
switch rn := r.(type) {
case int64:
rf = float64(rn)
case float64:
rf = rn
default:
return errMismatched(op, l, r)
}
v, err = doFloatComp(op, lf, rf)
case float64:
var rf float64
switch rn := r.(type) {
case int64:
rf = float64(rn)
case float64:
rf = rn
default:
return errMismatched(op, l, r)
}
v, err = doFloatComp(op, ln, rf)
case string:
rn, ok := r.(string)
if ok {
v, err = doStringComp(op, ln, rn)
} else if rx, ok := r.(*regexp.Regexp); ok {
v, err = doRegexComp(op, ln, rx)
} else {
return errMismatched(op, l, r)
}
default:
return errMismatched(op, l, r)
}
default:
return fmt.Errorf("return: unknown operator %v", op)
}
if err != nil {
return
}
stck.Push(v)
return
}
func doIntMath(op tokenType, l, r int64) (v int64, err error) {
switch op {
case TokenPlus:
v = l + r
case TokenMinus:
v = l - r
case TokenMult:
v = l * r
case TokenDiv:
v = l / r
case TokenMod:
v = l % r
default:
return 0, fmt.Errorf("invalid integer math operator %v", op)
}
return
}
func doFloatMath(op tokenType, l, r float64) (v float64, err error) {
switch op {
case TokenPlus:
v = l + r
case TokenMinus:
v = l - r
case TokenMult:
v = l * r
case TokenDiv:
v = l / r
default:
return math.NaN(), fmt.Errorf("invalid float math operator %v", op)
}
return
}
func doBoolComp(op tokenType, l, r bool) (v bool, err error) {
switch op {
case TokenEqual:
v = l == r
case TokenNotEqual:
v = l != r
case TokenAnd:
v = l && r
case TokenOr:
v = l || r
default:
err = fmt.Errorf("invalid boolean comparison operator %v", op)
}
return
}
func doFloatComp(op tokenType, l, r float64) (v bool, err error) {
switch op {
case TokenEqual:
v = l == r
case TokenNotEqual:
v = l != r
case TokenLess:
v = l < r
case TokenGreater:
v = l > r
case TokenLessEqual:
v = l <= r
case TokenGreaterEqual:
v = l >= r
default:
err = fmt.Errorf("invalid float comparison operator %v", op)
}
return
}
func doStringComp(op tokenType, l, r string) (v bool, err error) {
switch op {
case TokenEqual:
v = l == r
case TokenNotEqual:
v = l != r
case TokenLess:
v = l < r
case TokenGreater:
v = l > r
case TokenLessEqual:
v = l <= r
case TokenGreaterEqual:
v = l >= r
default:
err = fmt.Errorf("invalid string comparison operator %v", op)
}
return
}
func doRegexComp(op tokenType, l string, r *regexp.Regexp) (v bool, err error) {
switch op {
case TokenRegexEqual:
v = r.MatchString(l)
case TokenRegexNotEqual:
v = !r.MatchString(l)
default:
err = fmt.Errorf("invalid regex comparison operator %v", op)
}
return
}