-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheval.go
169 lines (161 loc) · 5.59 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
package main
import (
"go/parser"
"go/ast"
"go/token"
"fmt"
"strconv"
"reflect"
)
type evalError string
func (this evalError) Error() string {
return string(this)
}
func handleBuildinCall(node *ast.CallExpr, vars map[string]interface{}) (reflect.Value, bool) {
if funIdent, ok := node.Fun.(*ast.Ident); ok {
switch funIdent.Name {
case "cap":
if len(node.Args) != 1 {
panic(evalError("cap() requires 1 argument"))
}
return reflect.ValueOf(visit(node.Args[0], vars).Cap()), true
case "len":
if len(node.Args) != 1 {
panic(evalError("len() requires 1 argument"))
}
return reflect.ValueOf(visit(node.Args[0], vars).Len()), true
}
}
return reflect.Value{}, false
}
func visit(_node ast.Node, vars map[string]interface{}) reflect.Value {
switch node := _node.(type) {
case *ast.Ident:
return reflect.ValueOf(vars[node.Name])
case *ast.BasicLit:
// FIXME: the type of literal is context senstive...
switch node.Kind {
case token.INT:
if v, err := strconv.ParseInt(node.Value, 0, 32); err != nil {
panic(err)
} else {
return reflect.ValueOf(int(v))
}
case token.FLOAT:
if v, err := strconv.ParseFloat(node.Value, 64); err != nil {
panic(err)
} else {
return reflect.ValueOf(v)
}
case token.CHAR:
if v, _, _, err := strconv.UnquoteChar(node.Value[1:len(node.Value)-1], '\''); err != nil {
panic(err)
} else {
return reflect.ValueOf(v)
}
case token.STRING:
if v, err := strconv.Unquote(node.Value); err != nil {
panic(err)
} else {
return reflect.ValueOf(v)
}
default:
panic(evalError("unsupported literal type:" + fmt.Sprint(node.Kind)))
}
case *ast.ParenExpr:
return visit(node.X, vars)
case *ast.SelectorExpr:
return visit(node.X, vars).FieldByName(node.Sel.Name)
case *ast.IndexExpr:
x := visit(node.X, vars)
return x.Index(visit(node.Index, vars).Interface().(int))
case *ast.SliceExpr:
x := visit(node.X, vars)
low := 0
if node.Low != nil {
low = visit(node.Low, vars).Interface().(int)
}
high := x.Len()
if node.High != nil {
high = visit(node.High, vars).Interface().(int)
}
return x.Slice(low, high)
case *ast.CallExpr:
if v, ok := handleBuildinCall(node, vars); ok {
return v
}
fun := visit(node.Fun, vars)
args := []reflect.Value{}
for _, exp := range node.Args {
args = append(args, visit(exp, vars))
}
rets := fun.Call(args)
if len(rets) == 1 {
return rets[0]
} else {
panic(evalError("wrong number of ret value"))
}
case *ast.StarExpr:
return visit(node.X, vars).Elem()
case *ast.UnaryExpr:
v := visit(node.X, vars)
switch node.Op {
case token.NOT:
return reflect.ValueOf(!v.Interface().(bool));
default:
panic(evalError("not implemented unary operator:" + fmt.Sprint(node.Op)))
}
case *ast.BinaryExpr:
// FIXME: the type of left & right value
// FIXME: the right logic of '&&' and '||'
x := visit(node.X, vars)
y := visit(node.Y, vars)
switch node.Op {
case token.ADD:
return reflect.ValueOf(x.Interface().(int) + y.Interface().(int))
case token.SUB:
return reflect.ValueOf(x.Interface().(int) - y.Interface().(int))
case token.MUL:
return reflect.ValueOf(x.Interface().(int) * y.Interface().(int))
case token.QUO:
return reflect.ValueOf(x.Interface().(int) / y.Interface().(int))
case token.REM:
return reflect.ValueOf(x.Interface().(int) % y.Interface().(int))
case token.LSS:
return reflect.ValueOf(x.Interface().(int) < y.Interface().(int))
case token.LEQ:
return reflect.ValueOf(x.Interface().(int) <= y.Interface().(int))
case token.GTR:
return reflect.ValueOf(x.Interface().(int) > y.Interface().(int))
case token.GEQ:
return reflect.ValueOf(x.Interface().(int) >= y.Interface().(int))
case token.EQL:
return reflect.ValueOf(x.Interface().(int) == y.Interface().(int))
case token.NEQ:
return reflect.ValueOf(x.Interface().(int) != y.Interface().(int))
case token.LAND:
return reflect.ValueOf(x.Interface().(bool) && y.Interface().(bool))
case token.LOR:
return reflect.ValueOf(x.Interface().(bool) || y.Interface().(bool))
default:
panic(evalError("not implemented binary operator:" + fmt.Sprint(node.Op)))
}
default:
panic(evalError("not supported sytnax : " + fmt.Sprint(node)))
}
return reflect.Value{}
}
func Eval(exprStr string, vars map[string]interface{}) (ret interface{}, err error) {
node, err := parser.ParseExpr(exprStr)
if err != nil { return }
defer func() {
if anyErr := recover(); anyErr != nil {
if _err, ok := anyErr.(error); ok {
err = _err
} else {
err = evalError(fmt.Sprint(anyErr))
}
}
}()
return visit(node, vars).Interface(), nil
}