-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
vmOperator.go
228 lines (205 loc) · 5.99 KB
/
vmOperator.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
package vm
import (
"reflect"
"strings"
"github.com/mattn/anko/ast"
)
// invokeOperator evaluates one Operator.
func (runInfo *runInfoStruct) invokeOperator() {
switch operator := runInfo.operator.(type) {
// BinaryOperator
case *ast.BinaryOperator:
runInfo.expr = operator.LHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
switch operator.Operator {
case "||":
if toBool(runInfo.rv) {
runInfo.rv = trueValue
return
}
case "&&":
if !toBool(runInfo.rv) {
runInfo.rv = falseValue
return
}
default:
runInfo.err = newStringError(operator, "unknown operator")
runInfo.rv = nilValue
return
}
runInfo.expr = operator.RHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
if toBool(runInfo.rv) {
runInfo.rv = trueValue
} else {
runInfo.rv = falseValue
}
// ComparisonOperator
case *ast.ComparisonOperator:
runInfo.expr = operator.LHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
lhsV := runInfo.rv
runInfo.expr = operator.RHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
switch operator.Operator {
case "==":
runInfo.rv = reflect.ValueOf(equal(lhsV, runInfo.rv))
case "!=":
runInfo.rv = reflect.ValueOf(!equal(lhsV, runInfo.rv))
case "<":
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) < toFloat64(runInfo.rv))
case "<=":
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) <= toFloat64(runInfo.rv))
case ">":
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) > toFloat64(runInfo.rv))
case ">=":
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) >= toFloat64(runInfo.rv))
default:
runInfo.err = newStringError(operator, "unknown operator")
runInfo.rv = nilValue
}
// AddOperator
case *ast.AddOperator:
runInfo.expr = operator.LHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
lhsV := runInfo.rv
runInfo.expr = operator.RHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
switch operator.Operator {
case "+":
lhsKind := lhsV.Kind()
rhsKind := runInfo.rv.Kind()
if lhsKind == reflect.Slice || lhsKind == reflect.Array {
if rhsKind == reflect.Slice || rhsKind == reflect.Array {
// append slice to slice
runInfo.rv, runInfo.err = appendSlice(operator, lhsV, runInfo.rv)
return
}
// try to append rhs non-slice to lhs slice
runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, lhsV.Type().Elem())
if runInfo.err != nil {
runInfo.err = newStringError(operator, "invalid type conversion")
runInfo.rv = nilValue
return
}
runInfo.rv = reflect.Append(lhsV, runInfo.rv)
return
}
if rhsKind == reflect.Slice || rhsKind == reflect.Array {
// can not append rhs slice to lhs non-slice
runInfo.err = newStringError(operator, "invalid type conversion")
runInfo.rv = nilValue
return
}
kind := precedenceOfKinds(lhsKind, rhsKind)
switch kind {
case reflect.String:
runInfo.rv = reflect.ValueOf(toString(lhsV) + toString(runInfo.rv))
case reflect.Float64, reflect.Float32:
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) + toFloat64(runInfo.rv))
default:
runInfo.rv = reflect.ValueOf(toInt64(lhsV) + toInt64(runInfo.rv))
}
case "-":
switch lhsV.Kind() {
case reflect.Float64, reflect.Float32:
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) - toFloat64(runInfo.rv))
return
}
switch runInfo.rv.Kind() {
case reflect.Float64, reflect.Float32:
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) - toFloat64(runInfo.rv))
default:
runInfo.rv = reflect.ValueOf(toInt64(lhsV) - toInt64(runInfo.rv))
}
case "|":
runInfo.rv = reflect.ValueOf(toInt64(lhsV) | toInt64(runInfo.rv))
default:
runInfo.err = newStringError(operator, "unknown operator")
runInfo.rv = nilValue
}
// MultiplyOperator
case *ast.MultiplyOperator:
runInfo.expr = operator.LHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
lhsV := runInfo.rv
runInfo.expr = operator.RHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
switch operator.Operator {
case "*":
if lhsV.Kind() == reflect.String && (runInfo.rv.Kind() == reflect.Int || runInfo.rv.Kind() == reflect.Int32 || runInfo.rv.Kind() == reflect.Int64) {
runInfo.rv = reflect.ValueOf(strings.Repeat(toString(lhsV), int(toInt64(runInfo.rv))))
return
}
if lhsV.Kind() == reflect.Float64 || runInfo.rv.Kind() == reflect.Float64 {
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) * toFloat64(runInfo.rv))
return
}
runInfo.rv = reflect.ValueOf(toInt64(lhsV) * toInt64(runInfo.rv))
case "/":
runInfo.rv = reflect.ValueOf(toFloat64(lhsV) / toFloat64(runInfo.rv))
case "%":
runInfo.rv = reflect.ValueOf(toInt64(lhsV) % toInt64(runInfo.rv))
case ">>":
runInfo.rv = reflect.ValueOf(toInt64(lhsV) >> uint64(toInt64(runInfo.rv)))
case "<<":
runInfo.rv = reflect.ValueOf(toInt64(lhsV) << uint64(toInt64(runInfo.rv)))
case "&":
runInfo.rv = reflect.ValueOf(toInt64(lhsV) & toInt64(runInfo.rv))
default:
runInfo.err = newStringError(operator, "unknown operator")
runInfo.rv = nilValue
}
default:
runInfo.err = newStringError(operator, "unknown operator")
runInfo.rv = nilValue
}
}