/
expr.go
338 lines (304 loc) · 8.83 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
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
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* expr.go
*
* Created on: Feb 15, 2017
* Author: Massimiliano Ghilardi
*/
package classic
import (
"go/ast"
"go/token"
r "reflect"
. "github.com/cosmos72/gomacro/base"
"github.com/cosmos72/gomacro/base/genimport"
"github.com/cosmos72/gomacro/base/reflect"
etoken "github.com/cosmos72/gomacro/go/etoken"
)
func (env *Env) evalExprsMultipleValues(nodes []ast.Expr, expectedValuesN int) []r.Value {
n := len(nodes)
var values []r.Value
if n != expectedValuesN {
if n != 1 {
env.Errorf("value count mismatch: cannot assign %d values to %d places: %v",
n, expectedValuesN, nodes)
return nil
}
node := nodes[0]
// collect multiple values
values = reflect.PackValuesR(env.EvalNode(node))
n = len(values)
if n < expectedValuesN {
env.Errorf("expression returned %d values, cannot assign them to %d places: %v returned %v",
n, expectedValuesN, node, values)
return nil
} else if n > expectedValuesN {
env.Warnf("expression returned %d values, using only %d of them: %v returned %v",
n, expectedValuesN, node, values)
}
} else {
values = env.evalExprs(nodes)
}
return values
}
func (env *Env) evalExprs(nodes []ast.Expr) []r.Value {
switch n := len(nodes); n {
case 0:
return nil
case 1:
ret := env.evalExpr1(nodes[0])
return []r.Value{ret}
default:
rets := make([]r.Value, n)
for i := range nodes {
rets[i] = env.evalExpr1(nodes[i])
}
return rets
}
}
func (env *Env) evalExpr1(node ast.Expr) r.Value {
// treat failed type assertions specially: in compiled Go, they panic in single-value context
for {
switch expr := node.(type) {
case *ast.ParenExpr:
node = expr.X
continue
case *ast.TypeAssertExpr:
value, _ := env.evalTypeAssertExpr(expr, true)
return value
}
break
}
value, extraValues := env.evalExpr(node)
if len(extraValues) > 1 {
env.WarnExtraValues(extraValues)
}
return value
}
func (env *Env) evalExpr(in ast.Expr) (r.Value, []r.Value) {
for {
if in != nil {
env.Pos = in.Pos()
}
// env.Debugf("evalExpr() %v", node)
switch node := in.(type) {
case *ast.BasicLit:
ret := env.evalLiteral0(node)
return r.ValueOf(ret), nil
case *ast.BinaryExpr:
xv := env.evalExpr1(node.X)
switch op := node.Op; op {
case token.LAND, token.LOR:
if xv.Kind() != r.Bool {
return env.unsupportedLogicalOperand(op, xv)
}
// implement short-circuit logic
if (op == token.LOR) == xv.Bool() {
// env.Debugf("evalExpr() %v: %v = %v, skipping %v...", node, node.X, xv, node.Y)
return xv, nil
}
// env.Debugf("evalExpr() %v: %v = %v, evaluating %v...", node, node.X, xv, node.Y)
yv := env.evalExpr1(node.Y)
if yv.Kind() != r.Bool {
return env.unsupportedLogicalOperand(op, yv)
}
return yv, nil
default:
yv := env.evalExpr1(node.Y)
return env.evalBinaryExpr(xv, node.Op, yv), nil
}
case *ast.CallExpr:
return env.evalCall(node)
case *ast.CompositeLit:
return env.evalCompositeLiteral(node)
case *ast.FuncLit:
return env.evalDeclFunction(nil, node.Type, node.Body)
case *ast.Ident:
return env.evalIdentifier(node), nil
case *ast.IndexExpr:
return env.evalIndexExpr(node)
case *ast.ParenExpr:
in = node.X
continue
case *ast.UnaryExpr:
return env.evalUnaryExpr(node)
case *ast.SelectorExpr:
return env.evalSelectorExpr(node)
case *ast.SliceExpr:
return env.evalSliceExpr(node)
case *ast.StarExpr:
val := env.evalExpr1(node.X)
if val.Kind() != r.Ptr {
return env.Errorf("dereference of non-pointer: %v <%v>", val, typeOf(val))
}
return val.Elem(), nil
case *ast.TypeAssertExpr:
return env.evalTypeAssertExpr(node, false)
// case *ast.KeyValueExpr:
}
return env.Errorf("unimplemented Eval() for: %v <%v>", in, r.TypeOf(in))
}
}
func (env *Env) unsupportedLogicalOperand(op token.Token, xv r.Value) (r.Value, []r.Value) {
return env.Errorf("unsupported type in logical operation %s: expecting bool, found %v <%v>", etoken.String(op), xv, typeOf(xv))
}
func (env *Env) evalSliceExpr(node *ast.SliceExpr) (r.Value, []r.Value) {
obj := env.evalExpr1(node.X)
if obj.Kind() == r.Ptr {
obj = obj.Elem()
}
switch obj.Kind() {
case r.Array, r.Slice, r.String:
// ok
default:
return env.Errorf("slice operation %v expects array, slice or string. found: %v <%v>", node, obj, typeOf(obj))
}
lo, hi := 0, obj.Len()
if node.Low != nil {
lo = int(env.valueToType(env.evalExpr1(node.Low), TypeOfInt).Int())
}
if node.High != nil {
hi = int(env.valueToType(env.evalExpr1(node.High), TypeOfInt).Int())
}
if node.Slice3 {
max := hi
if node.Max != nil {
max = int(env.valueToType(env.evalExpr1(node.Max), TypeOfInt).Int())
}
return obj.Slice3(lo, hi, max), nil
} else {
return obj.Slice(lo, hi), nil
}
}
func (env *Env) evalIndexExpr(node *ast.IndexExpr) (r.Value, []r.Value) {
// respect left-to-right order of evaluation
obj := env.evalExpr1(node.X)
index := env.evalExpr1(node.Index)
if obj.Kind() == r.Ptr {
obj = obj.Elem()
}
switch obj.Kind() {
case r.Map:
index = env.valueToType(index, obj.Type().Key())
ret, present, _ := env.mapIndex(obj, index)
return ret, []r.Value{ret, r.ValueOf(present)}
case r.Array, r.Slice, r.String:
i, ok := env.toInt(index)
if !ok {
return env.Errorf("invalid index, expecting an int: %v <%v>", index, typeOf(index))
}
return obj.Index(int(i)), nil
default:
return env.Errorf("unsupported index operation: %v [ %v ]. not an array, map, slice or string: %v <%v>", node.X, index, obj, typeOf(obj))
}
}
// mapIndex reproduces the exact behaviour of the map[key] builtin. given:
// var x = map[ktype]vtype
// x[key] does the following:
// 1. if key is present, return (the value associated to key, true, value.Type())
// 2. otherwise, return (the zero value of vtype, false, vtype)
// note: converting key to ktype is caller's responsibility
func (env *Env) mapIndex(obj r.Value, key r.Value) (r.Value, bool, r.Type) {
value := obj.MapIndex(key)
present := value != NilR
var t r.Type
if present {
t = value.Type()
} else {
t = obj.Type().Elem()
value = r.Zero(t)
}
return value, present, t
}
func (env *Env) evalSelectorExpr(node *ast.SelectorExpr) (r.Value, []r.Value) {
obj := env.evalExpr1(node.X)
name := node.Sel.Name
var val r.Value
switch obj.Kind() {
case r.Ptr:
if pkg, ok := obj.Interface().(*genimport.PackageRef); ok {
// access symbol from imported package, for example fmt.Printf
if bind, ok := pkg.Binds[name]; ok {
return bind, nil
}
return env.Errorf("package %v %#v has no binding %s", pkg.Name, pkg.Path, name)
}
elem := obj.Elem()
if elem.Kind() == r.Struct {
if val = elem.FieldByName(name); val != NilR {
break
}
}
// search for methods with pointer receiver first
if val = env.ObjMethodByName(obj, name); val != NilR {
break
}
if val = env.ObjMethodByName(elem, name); val != NilR {
break
}
return env.Errorf("pointer to struct <%v> has no field or method %s", typeOf(obj), name)
case r.Interface:
val = obj.MethodByName(name)
if val != NilR {
break
}
return env.Errorf("interface <%v> has no method %s", typeOf(obj), name)
case r.Struct:
if val = obj.FieldByName(name); val != NilR {
break
}
fallthrough
default:
// search for methods with pointer receiver first
if obj.CanAddr() {
if val = env.ObjMethodByName(obj.Addr(), name); val != NilR {
break
}
}
if val = env.ObjMethodByName(obj, name); val != NilR {
break
}
if obj.Kind() == r.Struct {
return env.Errorf("struct <%v> has no field or method %s", typeOf(obj), name)
} else {
return env.Errorf("value <%v> has no method %s", typeOf(obj), name)
}
}
return val, nil
}
func (env *Env) evalTypeAssertExpr(node *ast.TypeAssertExpr, panicOnFail bool) (r.Value, []r.Value) {
val := env.evalExpr1(node.X)
t2 := env.evalType(node.Type)
if val == NoneR || val == NilR {
if panicOnFail {
return env.Errorf("type assertion failed: %v <%v> is not a <%v>", val, nil, t2)
}
} else if t2 == TypeOfInterface {
val = val.Convert(t2)
return val, []r.Value{val, True}
} else {
t0 := val.Type()
fval := val.Interface()
t1 := r.TypeOf(fval) // extract the actual runtime type of fval
if t1 != nil && t1.AssignableTo(t2) {
val = r.ValueOf(fval).Convert(t2)
return val, []r.Value{val, True}
} else if panicOnFail {
if t1 == nil {
return env.Errorf("type assertion failed: %v <%v> is nil, not a <%v>", fval, t0, t2)
} else {
return env.Errorf("type assertion failed: %v <%v> is a <%v>, not a <%v>", fval, t0, t1, t2)
}
}
}
zero := r.Zero(t2)
return zero, []r.Value{zero, False}
}