-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
429 lines (386 loc) · 7.63 KB
/
ast.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
package filterql
import (
"io"
"reflect"
)
type TArg interface {
int | string
}
type PrintableAst interface {
PrintTo(level int, out io.Writer)
}
type EvalAst interface {
PrintableAst
Eval(*Context) error
}
type CanNot interface {
Not() BoolAst
}
type BoolAst interface {
PrintableAst
CanNot
IsTrue(*Context) (bool, error)
}
type ANDs struct {
Children []BoolAst
}
func (a *ANDs) IsTrue(ctx *Context) (bool, error) {
for _, child := range a.Children {
if rv, err := child.IsTrue(ctx); err != nil {
return false, err
} else if !rv {
return false, nil
}
}
return true, nil
}
func (a *ANDs) Not() BoolAst {
children := make([]BoolAst, len(a.Children))
for i, child := range a.Children {
if n, is := child.(CanNot); is {
children[i] = n.Not()
} else {
children[i] = &NOT{Child: child}
}
}
return &ORs{Children: children}
}
type ORs struct {
Children []BoolAst
}
func (a *ORs) IsTrue(ctx *Context) (bool, error) {
for _, child := range a.Children {
if rv, err := child.IsTrue(ctx); err != nil {
return false, err
} else if rv {
return true, nil
}
}
return false, nil
}
func (a *ORs) Not() BoolAst {
children := make([]BoolAst, len(a.Children))
for i, child := range a.Children {
if n, is := child.(CanNot); is {
children[i] = n.Not()
} else {
children[i] = &NOT{Child: child}
}
}
return &ANDs{Children: children}
}
type NOT struct {
Child BoolAst
}
func (a *NOT) IsTrue(ctx *Context) (bool, error) {
if r, err := a.Child.IsTrue(ctx); err != nil {
return false, err
} else {
return !r, nil
}
}
func (a *NOT) Not() BoolAst {
return a.Child
}
type Call interface {
BoolAst
EvalAst
CanNot
}
type call[T TArg] struct {
name string
arg T
fn func(any, T) (any, error)
not bool
}
func newCall[T TArg](
fnMap map[string]func(any, T) (any, error),
defaultFn func(string, any, T) (any, error),
name string, arg T) (*call[T], error) {
fn, has := fnMap[name]
if !has {
if defaultFn != nil {
fn = func(env any, arg T) (any, error) {
return defaultFn(name, env, arg)
}
} else {
return nil, ErrNoSuchMethod
}
}
return &call[T]{
name: name,
arg: arg,
fn: fn,
}, nil
}
func (c *call[T]) Eval(ctx *Context) (err error) {
ctx.result, err = c.fn(ctx.Env, c.arg)
return
}
func (c *call[T]) IsTrue(ctx *Context) (bool, error) {
if err := c.Eval(ctx); err != nil {
return false, err
}
switch result := ctx.result.(type) {
case int:
return (result != 0) != c.not, nil
case string:
return (result != "") != c.not, nil
case bool:
return result != c.not, nil
default:
return reflect.ValueOf(result).IsZero() == c.not, nil
}
}
func (c *call[T]) Not() BoolAst {
return &call[T]{
name: c.name,
arg: c.arg,
fn: c.fn,
not: !c.not,
}
}
func compareByOp[T TArg](a, b T, op int) bool {
switch op {
case TOKEN_OP_EQ:
return a == b
case TOKEN_OP_NE:
return a != b
case TOKEN_OP_LT:
return a < b
case TOKEN_OP_LE:
return a <= b
case TOKEN_OP_GT:
return a > b
case TOKEN_OP_GE:
return a >= b
default:
panic("invalid compare op")
}
}
func reverseOp(op int) int {
switch op {
case TOKEN_OP_EQ:
return TOKEN_OP_NE
case TOKEN_OP_NE:
return TOKEN_OP_EQ
case TOKEN_OP_LT:
return TOKEN_OP_GE
case TOKEN_OP_LE:
return TOKEN_OP_GT
case TOKEN_OP_GT:
return TOKEN_OP_LE
case TOKEN_OP_GE:
return TOKEN_OP_LT
}
panic("invalid compare op")
}
type Compare[T TArg] struct {
Call Call
Op int
Target T
}
func (c *Compare[T]) IsTrue(ctx *Context) (bool, error) {
if err := c.Call.Eval(ctx); err != nil {
return false, err
}
if result, is := ctx.result.(T); !is {
return false, ErrTypeNotMatched
} else {
return compareByOp(result, c.Target, c.Op), nil
}
}
func (c *Compare[T]) Not() BoolAst {
return &Compare[T]{
Call: c.Call,
Op: reverseOp(c.Op),
Target: c.Target,
}
}
func inSlice[T TArg](val T, slice []T) bool {
for _, item := range slice {
if val == item {
return true
}
}
return false
}
type In[T TArg] struct {
Call Call
NotIn bool
Choices []T
}
func (c *In[T]) IsTrue(ctx *Context) (bool, error) {
if err := c.Call.Eval(ctx); err != nil {
return false, err
}
if result, is := ctx.result.(T); !is {
return false, ErrTypeNotMatched
} else {
return inSlice(result, c.Choices) != c.NotIn, nil
}
}
func (c *In[T]) Not() BoolAst {
return &In[T]{
Call: c.Call,
Choices: c.Choices,
NotIn: !c.NotIn,
}
}
type CompareWithCall struct {
Left, Right Call
Op int
}
func (c *CompareWithCall) IsTrue(ctx *Context) (bool, error) {
if err := c.Left.Eval(ctx); err != nil {
return false, err
}
res1 := ctx.result
if err := c.Right.Eval(ctx); err != nil {
return false, err
}
res2 := ctx.result
switch v1 := res1.(type) {
case int:
if v2, is := res2.(int); is {
return compareByOp(v1, v2, c.Op), nil
}
case string:
if v2, is := res2.(string); is {
return compareByOp(v1, v2, c.Op), nil
}
}
return false, ErrTypeNotMatched
}
func (c *CompareWithCall) Not() BoolAst {
return &CompareWithCall{
Left: c.Left,
Right: c.Right,
Op: reverseOp(c.Op),
}
}
type InWithCall struct {
Left, Right Call
NotIn bool
}
func (c *InWithCall) IsTrue(ctx *Context) (bool, error) {
if err := c.Left.Eval(ctx); err != nil {
return false, err
}
res1 := ctx.result
if err := c.Right.Eval(ctx); err != nil {
return false, err
}
res2 := ctx.result
switch v1 := res1.(type) {
case int:
if v2, is := res2.([]int); is {
return inSlice(v1, v2) != c.NotIn, nil
}
case string:
if v2, is := res2.([]string); is {
return inSlice(v1, v2) != c.NotIn, nil
}
}
return false, ErrTypeNotMatched
}
func (c *InWithCall) Not() BoolAst {
return &InWithCall{
Left: c.Left,
Right: c.Right,
NotIn: !c.NotIn,
}
}
type callThenCompare[T1, T2 TArg] struct {
name string
arg T1
fn func(any, T1) (any, error)
target T2
op int
}
func newCallThenCompare[T TArg](ci Call, op int, target T) BoolAst {
switch c := ci.(type) {
case *call[int]:
return &callThenCompare[int, T]{
name: c.name,
arg: c.arg,
fn: c.fn,
target: target,
op: op,
}
case *call[string]:
return &callThenCompare[string, T]{
name: c.name,
arg: c.arg,
fn: c.fn,
target: target,
op: op,
}
}
panic("invalid call")
}
func (c *callThenCompare[T1, T2]) IsTrue(ctx *Context) (bool, error) {
ret, err := c.fn(ctx.Env, c.arg)
if err != nil {
return false, err
} else if result, is := ret.(T2); !is {
return false, ErrTypeNotMatched
} else {
return compareByOp(result, c.target, c.op), nil
}
}
func (c *callThenCompare[T1, T2]) Not() BoolAst {
return &callThenCompare[T1, T2]{
name: c.name,
arg: c.arg,
fn: c.fn,
target: c.target,
op: reverseOp(c.op),
}
}
type callThenIn[T1, T2 TArg] struct {
name string
arg T1
fn func(any, T1) (any, error)
choices []T2
not bool
}
func newCallThenIn[T TArg](ci Call, choices []T, not bool) BoolAst {
switch c := ci.(type) {
case *call[int]:
return &callThenIn[int, T]{
name: c.name,
arg: c.arg,
fn: c.fn,
choices: choices,
not: not,
}
case *call[string]:
return &callThenIn[string, T]{
name: c.name,
arg: c.arg,
fn: c.fn,
choices: choices,
}
}
panic("invalid call")
}
func (c *callThenIn[T1, T2]) IsTrue(ctx *Context) (bool, error) {
ret, err := c.fn(ctx.Env, c.arg)
if err != nil {
return false, err
} else if result, is := ret.(T2); !is {
return false, ErrTypeNotMatched
} else {
return inSlice(result, c.choices) != c.not, nil
}
}
func (c *callThenIn[T1, T2]) Not() BoolAst {
return &callThenIn[T1, T2]{
name: c.name,
arg: c.arg,
fn: c.fn,
choices: c.choices,
not: !c.not,
}
}