/
expr.go
264 lines (238 loc) · 5.26 KB
/
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
package paint
import (
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/vm"
)
var operatorPairs = map[string]string{
"and": "or",
"&&": "||",
"==": "!=",
">=": "<",
">": "<=",
"in": "not in",
}
func init() {
for k, v := range operatorPairs {
operatorPairs[v] = k
}
}
func mustCompile(input string, ops ...expr.Option) *vm.Program {
if program, err := expr.Compile(input, ops...); err != nil {
panic(err)
} else {
return program
}
}
func mustRun(program *vm.Program, env interface{}) interface{} {
if res, err := expr.Run(program, env); err != nil {
panic(err)
} else {
return res
}
}
type expandInArray struct{}
func (*expandInArray) Enter(*ast.Node) {}
func (*expandInArray) Exit(node *ast.Node) {
if bi := asBinaryIn(*node); bi != nil {
if an, ok := bi.Right.(*ast.ArrayNode); ok {
if len(an.Nodes) == 0 {
ast.Patch(node, &ast.BoolNode{})
} else {
for i, n := range an.Nodes {
if i == 0 {
ast.Patch(node, &ast.BinaryNode{
Operator: "==",
Left: bi.Left,
Right: n,
})
} else {
ast.Patch(node, &ast.BinaryNode{
Operator: "or",
Left: *node,
Right: &ast.BinaryNode{
Operator: "==",
Left: bi.Left,
Right: n,
},
})
}
}
}
if bi.Operator == "not in" {
ast.Patch(node, &ast.UnaryNode{
Operator: "not",
Node: *node,
})
}
}
}
}
type expandInRange struct{}
func (*expandInRange) Enter(*ast.Node) {}
func (*expandInRange) Exit(node *ast.Node) {
if bi := asBinaryIn(*node); bi != nil {
if br, ok := bi.Right.(*ast.BinaryNode); ok && br.Operator == ".." {
if getValue(br.Left) == getValue(br.Right) {
ast.Patch(node, &ast.BinaryNode{
Operator: "==",
Left: bi.Left,
Right: br.Left,
})
} else {
ast.Patch(node, &ast.BinaryNode{
Operator: "and",
Left: &ast.BinaryNode{
Operator: ">=",
Left: bi.Left,
Right: br.Left,
},
Right: &ast.BinaryNode{
Operator: "<=",
Left: bi.Left,
Right: br.Right,
},
})
}
if bi.Operator == "not in" {
ast.Patch(node, &ast.UnaryNode{
Operator: "not",
Node: *node,
})
}
}
}
}
func getValue(n ast.Node) any {
switch a := n.(type) {
case *ast.NilNode:
return nil
case *ast.IntegerNode:
return a.Value
case *ast.FloatNode:
return a.Value
case *ast.BoolNode:
return a.Value
case *ast.StringNode:
return a.Value
case *ast.ConstantNode:
return a.Value
default:
return n
}
}
type distributeAndFoldNot struct{}
func (d *distributeAndFoldNot) Enter(node *ast.Node) {
if un := asUnaryNot(*node); un != nil {
if bn, ok := un.Node.(*ast.BinaryNode); ok {
if op, ok := operatorPairs[bn.Operator]; ok {
switch bn.Operator {
case "and", "&&", "or", "||":
bn.Left = &ast.UnaryNode{
Operator: un.Operator,
Node: bn.Left,
}
bn.Right = &ast.UnaryNode{
Operator: un.Operator,
Node: bn.Right,
}
}
bn.Operator = op
ast.Patch(node, bn)
}
} else if n := asUnaryNot(un.Node); n != nil {
ast.Walk(&n.Node, d)
ast.Patch(node, n.Node)
} else if b, ok := un.Node.(*ast.BoolNode); ok {
b.Value = !b.Value
ast.Patch(node, b)
}
}
}
func (*distributeAndFoldNot) Exit(*ast.Node) {}
func toDNF(node *ast.Node) {
for limit := 1000; limit >= 0; limit-- {
f := &dnf{}
ast.Walk(node, f)
if !f.applied {
return
}
}
}
type dnf struct {
depth int
applied bool
}
func (f *dnf) Enter(node *ast.Node) {
if f.depth > 0 {
f.depth++
} else if bn, ok := (*node).(*ast.BinaryNode); !ok || (bn.Operator != "and" && bn.Operator != "&&" && bn.Operator != "or" && bn.Operator != "||") {
f.depth++
}
}
func (f *dnf) Exit(node *ast.Node) {
if f.depth > 0 {
f.depth--
return
}
if ba := asBinaryAnd(*node); ba != nil {
if bo := asBinaryOr(ba.Left); bo != nil {
ast.Patch(node, &ast.BinaryNode{
Operator: bo.Operator,
Left: &ast.BinaryNode{
Operator: ba.Operator,
Left: bo.Left,
Right: ba.Right,
},
Right: &ast.BinaryNode{
Operator: ba.Operator,
Left: bo.Right,
Right: ba.Right,
},
})
f.applied = true
return
}
if bo := asBinaryOr(ba.Right); bo != nil {
ast.Patch(node, &ast.BinaryNode{
Operator: bo.Operator,
Left: &ast.BinaryNode{
Operator: ba.Operator,
Left: ba.Left,
Right: bo.Left,
},
Right: &ast.BinaryNode{
Operator: ba.Operator,
Left: ba.Left,
Right: bo.Right,
},
})
f.applied = true
return
}
}
}
func asBinaryIn(node ast.Node) *ast.BinaryNode {
if bn, ok := node.(*ast.BinaryNode); ok && (bn.Operator == "in" || bn.Operator == "not in") {
return bn
}
return nil
}
func asBinaryAnd(node ast.Node) *ast.BinaryNode {
if bn, ok := node.(*ast.BinaryNode); ok && (bn.Operator == "and" || bn.Operator == "&&") {
return bn
}
return nil
}
func asBinaryOr(node ast.Node) *ast.BinaryNode {
if bn, ok := node.(*ast.BinaryNode); ok && (bn.Operator == "or" || bn.Operator == "||") {
return bn
}
return nil
}
func asUnaryNot(node ast.Node) *ast.UnaryNode {
if un, ok := node.(*ast.UnaryNode); ok && (un.Operator == "not" || un.Operator == "!") {
return un
}
return nil
}