forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
502 lines (442 loc) · 10.9 KB
/
type.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package expr
import (
"fmt"
"reflect"
)
type typesTable map[string]reflect.Type
var (
nilType = reflect.TypeOf(nil)
boolType = reflect.TypeOf(true)
numberType = reflect.TypeOf(float64(0))
textType = reflect.TypeOf("")
arrayType = reflect.TypeOf([]interface{}{})
mapType = reflect.TypeOf(map[interface{}]interface{}{})
interfaceType = reflect.TypeOf(new(interface{})).Elem()
)
func (n nilNode) Type(table typesTable) (reflect.Type, error) {
return nil, nil
}
func (n identifierNode) Type(table typesTable) (reflect.Type, error) {
return textType, nil
}
func (n numberNode) Type(table typesTable) (reflect.Type, error) {
return numberType, nil
}
func (n boolNode) Type(table typesTable) (reflect.Type, error) {
return boolType, nil
}
func (n textNode) Type(table typesTable) (reflect.Type, error) {
return textType, nil
}
func (n nameNode) Type(table typesTable) (reflect.Type, error) {
if t, ok := table[n.name]; ok {
return t, nil
}
return nil, fmt.Errorf("unknown name %v", n)
}
func (n unaryNode) Type(table typesTable) (reflect.Type, error) {
ntype, err := n.node.Type(table)
if err != nil {
return nil, err
}
switch n.operator {
case "!", "not":
if isBoolType(ntype) || isInterfaceType(ntype) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched type %v)`, n, ntype)
}
return interfaceType, nil
}
func (n binaryNode) Type(table typesTable) (reflect.Type, error) {
var err error
ltype, err := n.left.Type(table)
if err != nil {
return nil, err
}
rtype, err := n.right.Type(table)
if err != nil {
return nil, err
}
switch n.operator {
case "==", "!=":
if isComparable(ltype, rtype) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "or", "||", "and", "&&":
if (isBoolType(ltype) || isInterfaceType(ltype)) && (isBoolType(rtype) || isInterfaceType(rtype)) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "in", "not in":
if (isStringType(ltype) || isInterfaceType(ltype)) && (isStructType(rtype) || isInterfaceType(rtype)) {
return boolType, nil
}
if isArrayType(rtype) || isMapType(rtype) || isInterfaceType(rtype) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "<", ">", ">=", "<=":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "/", "+", "-", "*", "**", "|", "^", "&", "%":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return numberType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "..":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return arrayType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
case "~":
if (isStringType(ltype) || isInterfaceType(ltype)) && (isStringType(rtype) || isInterfaceType(rtype)) {
return textType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
}
return interfaceType, nil
}
func (n matchesNode) Type(table typesTable) (reflect.Type, error) {
var err error
ltype, err := n.left.Type(table)
if err != nil {
return nil, err
}
rtype, err := n.right.Type(table)
if err != nil {
return nil, err
}
if (isStringType(ltype) || isInterfaceType(ltype)) && (isStringType(rtype) || isInterfaceType(rtype)) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)
}
func (n propertyNode) Type(table typesTable) (reflect.Type, error) {
ntype, err := n.node.Type(table)
if err != nil {
return nil, err
}
if t, ok := fieldType(ntype, n.property); ok {
return t, nil
}
return nil, fmt.Errorf("%v undefined (type %v has no field %v)", n, ntype, n.property)
}
func (n indexNode) Type(table typesTable) (reflect.Type, error) {
ntype, err := n.node.Type(table)
if err != nil {
return nil, err
}
_, err = n.index.Type(table)
if err != nil {
return nil, err
}
if t, ok := indexType(ntype); ok {
return t, nil
}
return nil, fmt.Errorf("invalid operation: %v (type %v does not support indexing)", n, ntype)
}
func (n methodNode) Type(table typesTable) (reflect.Type, error) {
ntype, err := n.node.Type(table)
if err != nil {
return nil, err
}
for _, node := range n.arguments {
_, err := node.Type(table)
if err != nil {
return nil, err
}
}
if t, ok := methodType(ntype, n.method); ok {
if f, ok := funcType(t); ok {
return f, nil
}
}
return nil, fmt.Errorf("%v undefined (type %v has no method %v)", n, ntype, n.method)
}
func (n builtinNode) Type(table typesTable) (reflect.Type, error) {
for _, node := range n.arguments {
_, err := node.Type(table)
if err != nil {
return nil, err
}
}
switch n.name {
case "len":
// TODO: Add arguments type checks.
return numberType, nil
}
return nil, fmt.Errorf("%v undefined", n)
}
func (n functionNode) Type(table typesTable) (reflect.Type, error) {
for _, node := range n.arguments {
_, err := node.Type(table)
if err != nil {
return nil, err
}
}
if t, ok := table[n.name]; ok {
if f, ok := funcType(t); ok {
return f, nil
}
}
return nil, fmt.Errorf("unknown func %v", n)
}
func (n conditionalNode) Type(table typesTable) (reflect.Type, error) {
ctype, err := n.cond.Type(table)
if err != nil {
return nil, err
}
if !isBoolType(ctype) && !isInterfaceType(ctype) {
return nil, fmt.Errorf("non-bool %v (type %v) used as condition", n.cond, ctype)
}
t1, err := n.exp1.Type(table)
if err != nil {
return nil, err
}
t2, err := n.exp2.Type(table)
if err != nil {
return nil, err
}
if t1 == nil && t2 != nil {
return t2, nil
}
if t1 != nil && t2 == nil {
return t1, nil
}
if t1 == nil && t2 == nil {
return nilType, nil
}
if t1.AssignableTo(t2) {
return t1, nil
}
return interfaceType, nil
}
func (n arrayNode) Type(table typesTable) (reflect.Type, error) {
for _, node := range n.nodes {
_, err := node.Type(table)
if err != nil {
return nil, err
}
}
return arrayType, nil
}
func (n mapNode) Type(table typesTable) (reflect.Type, error) {
for _, node := range n.pairs {
_, err := node.Type(table)
if err != nil {
return nil, err
}
}
return mapType, nil
}
func (n pairNode) Type(table typesTable) (reflect.Type, error) {
var err error
_, err = n.key.Type(table)
if err != nil {
return nil, err
}
_, err = n.value.Type(table)
if err != nil {
return nil, err
}
return nil, nil
}
// helper funcs for reflect
func isComparable(l, r reflect.Type) bool {
l = dereference(l)
r = dereference(r)
if l == nil || r == nil {
return true // It is possible to compare with nil.
}
if isNumberType(l) && isNumberType(r) {
return true
} else if l.Kind() == reflect.Interface {
return true
} else if r.Kind() == reflect.Interface {
return true
} else if l == r {
return true
}
return false
}
func isInterfaceType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Interface:
return true
}
}
return false
}
func isNumberType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Float32, reflect.Float64:
fallthrough
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fallthrough
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
}
}
return false
}
func isBoolType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Bool:
return true
}
}
return false
}
func isStringType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.String:
return true
}
}
return false
}
func isArrayType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Slice, reflect.Array:
return true
}
}
return false
}
func isMapType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Map:
return true
}
}
return false
}
func isStructType(t reflect.Type) bool {
t = dereference(t)
if t != nil {
switch t.Kind() {
case reflect.Struct:
return true
}
}
return false
}
func fieldType(ntype reflect.Type, name string) (reflect.Type, bool) {
ntype = dereference(ntype)
if ntype != nil {
switch ntype.Kind() {
case reflect.Interface:
return interfaceType, true
case reflect.Struct:
// First check all struct's fields.
for i := 0; i < ntype.NumField(); i++ {
f := ntype.Field(i)
if !f.Anonymous && f.Name == name {
return f.Type, true
}
}
// Second check fields of embedded structs.
for i := 0; i < ntype.NumField(); i++ {
f := ntype.Field(i)
if f.Anonymous {
if t, ok := fieldType(f.Type, name); ok {
return t, true
}
}
}
case reflect.Map:
return ntype.Elem(), true
}
}
return nil, false
}
func methodType(ntype reflect.Type, name string) (reflect.Type, bool) {
ntype = dereference(ntype)
if ntype != nil {
switch ntype.Kind() {
case reflect.Interface:
return interfaceType, true
case reflect.Struct:
// First check all struct's methods.
for i := 0; i < ntype.NumMethod(); i++ {
m := ntype.Method(i)
if m.Name == name {
return m.Type, true
}
}
// Second check all struct's fields.
for i := 0; i < ntype.NumField(); i++ {
f := ntype.Field(i)
if !f.Anonymous && f.Name == name {
return f.Type, true
}
}
// Third check fields of embedded structs.
for i := 0; i < ntype.NumField(); i++ {
f := ntype.Field(i)
if f.Anonymous {
if t, ok := methodType(f.Type, name); ok {
return t, true
}
}
}
case reflect.Map:
return ntype.Elem(), true
}
}
return nil, false
}
func indexType(ntype reflect.Type) (reflect.Type, bool) {
ntype = dereference(ntype)
if ntype == nil {
return nil, false
}
switch ntype.Kind() {
case reflect.Interface:
return interfaceType, true
case reflect.Map, reflect.Array, reflect.Slice:
return ntype.Elem(), true
}
return nil, false
}
func funcType(ntype reflect.Type) (reflect.Type, bool) {
ntype = dereference(ntype)
if ntype == nil {
return nil, false
}
switch ntype.Kind() {
case reflect.Interface:
return interfaceType, true
case reflect.Func:
if ntype.NumOut() > 0 {
return ntype.Out(0), true
}
return nilType, true
}
return nil, false
}
func dereference(ntype reflect.Type) reflect.Type {
if ntype == nil {
return nil
}
if ntype.Kind() == reflect.Ptr {
ntype = dereference(ntype.Elem())
}
return ntype
}