-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
214 lines (196 loc) · 4.79 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
package eval
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"strconv"
"sync"
)
var funcMap = map[string]func(args []ast.Expr, data map[string]any) (any, error){}
var emptyExpression = errors.New("field is nil")
var illegalParams = errors.New("illegal params")
var expressionCache sync.Map // cache expression parsed result, key is expression, value is ast.Expr
func Eval(expression string, data map[string]any) (any, error) {
expr, err := parseExpression(expression)
if err != nil {
return nil, err
}
return eval(expr, data)
}
func parseExpression(expression string) (expr ast.Expr, err error) {
if len(expression) == 0 {
return nil, emptyExpression
}
if exp, ok := expressionCache.Load(expression); ok {
expr = exp.(ast.Expr)
return
}
expr, err = parser.ParseExpr(expression)
if err != nil {
return nil, err
}
expressionCache.Store(expression, expr)
return
}
func eval(expr ast.Expr, data map[string]any) (any, error) {
switch expr := expr.(type) {
case *ast.BasicLit: // base type
return basicLit(expr)
case *ast.BinaryExpr: // binary field
return evalForBinaryExpr(expr, data)
case *ast.CallExpr:
return evalForFunc(expr.Fun.(*ast.Ident).Name, expr.Args, data)
case *ast.ParenExpr: // parenthesized field
return eval(expr.X, data)
case *ast.UnaryExpr: // unary field
return evalForUnaryExpr(expr, data)
case *ast.Ident: // identifier
return evalForIdent(expr, data)
default:
return nil, fmt.Errorf("unknown ast node type [%s]", expr)
}
}
func basicLit(lit *ast.BasicLit) (value any, err error) {
switch lit.Kind {
case token.INT:
value, err = strconv.ParseInt(lit.Value, 10, 64)
case token.FLOAT:
value, err = strconv.ParseFloat(lit.Value, 64)
case token.STRING:
value, err = strconv.Unquote(lit.Value)
default:
err = fmt.Errorf("unknown lit type [%s]", lit.Kind) // token.CHAR token.IMAG
}
return value, err
}
func evalForBinaryExpr(expr *ast.BinaryExpr, data map[string]any) (any, error) {
x, xErr := eval(expr.X, data)
if xErr != nil {
return nil, xErr
}
if x == nil {
return nil, fmt.Errorf("x [%v] is nil", x)
}
y, yErr := eval(expr.Y, data)
if yErr != nil {
return nil, yErr
}
if x == nil {
return nil, fmt.Errorf("y [%v] is nil", y)
}
switch x := x.(type) {
case int, int32, int64:
xInt, err := Int64(x)
if err != nil {
return nil, err
}
yInt, err := Int64(y)
if err != nil {
return nil, err
}
return evalForNum[int64](xInt, yInt, expr.Op)
case float32, float64:
xFloat, err := Float64(x)
if err != nil {
return nil, err
}
yFloat, err := Float64(y)
if err != nil {
return nil, err
}
return evalForNum[float64](xFloat, yFloat, expr.Op)
case string:
xString := String(x)
yString := String(y)
switch expr.Op {
case token.EQL:
return xString == yString, nil
case token.NEQ:
return xString != yString, nil
case token.ADD:
return xString + yString, nil
default:
return nil, fmt.Errorf("unsupported operator: [%s]", expr.Op)
}
case bool:
xb, errX := Bool(x)
yb, errY := Bool(y)
if errX != nil || errY != nil {
return nil, fmt.Errorf("eval field [%v %s %v] failed", x, expr.Op, y)
}
switch expr.Op {
case token.LAND:
return xb && yb, nil
case token.LOR:
return xb || yb, nil
case token.EQL:
return xb == yb, nil
case token.NEQ:
return xb != yb, nil
default:
return nil, fmt.Errorf("unsupported operator: [%s]", expr.Op)
}
default:
return nil, fmt.Errorf("unknown operation [%s]", expr.Op)
}
}
func evalForFunc(funcName string, args []ast.Expr, data map[string]any) (any, error) {
handler, ok := funcMap[funcName]
if !ok {
return nil, fmt.Errorf("unknown func %s", funcName)
}
return handler(args, data)
}
func evalForNum[T int | int32 | int64 | float32 | float64](x, y T, op token.Token) (any, error) {
switch op {
case token.EQL:
return x == y, nil
case token.NEQ:
return x != y, nil
case token.GTR:
return x > y, nil
case token.LSS:
return x < y, nil
case token.GEQ:
return x >= y, nil
case token.LEQ:
return x <= y, nil
case token.ADD:
return x + y, nil
case token.SUB:
return x - y, nil
case token.MUL:
return x * y, nil
case token.QUO:
if y == 0 {
return 0, nil
}
return x / y, nil
default:
return nil, fmt.Errorf("unsupported operator for number: [%s]", op)
}
}
func evalForUnaryExpr(expr *ast.UnaryExpr, data map[string]any) (any, error) {
x, err := eval(expr.X, data)
if err != nil {
return nil, err
}
if x == nil {
return nil, fmt.Errorf("x [%v] is nil", x)
}
if b, ok := x.(bool); ok && expr.Op == token.NOT {
return !b, nil
}
return nil, fmt.Errorf("unknown unary field [%v]", expr)
}
func evalForIdent(expr *ast.Ident, data map[string]any) (any, error) {
if expr.Name == "true" { // true
return true, nil
}
if expr.Name == "false" { // false
return false, nil
}
return data[expr.Name], nil
}