-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
vmExpr.go
761 lines (680 loc) · 17 KB
/
vmExpr.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package vm
import (
"reflect"
"github.com/mattn/anko/ast"
"github.com/mattn/anko/env"
)
// invokeExpr evaluates one expression.
func (runInfo *runInfoStruct) invokeExpr() {
switch expr := runInfo.expr.(type) {
// OpExpr
case *ast.OpExpr:
runInfo.operator = expr.Op
runInfo.invokeOperator()
// IdentExpr
case *ast.IdentExpr:
runInfo.rv, runInfo.err = runInfo.env.GetValue(expr.Lit)
if runInfo.err != nil {
runInfo.err = newError(expr, runInfo.err)
}
// LiteralExpr
case *ast.LiteralExpr:
runInfo.rv = expr.Literal
// ArrayExpr
case *ast.ArrayExpr:
if expr.TypeData == nil {
slice := make([]interface{}, len(expr.Exprs))
var i int
for i, runInfo.expr = range expr.Exprs {
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
slice[i] = runInfo.rv.Interface()
}
runInfo.rv = reflect.ValueOf(slice)
return
}
t := makeType(runInfo, expr.TypeData)
if runInfo.err != nil {
runInfo.rv = nilValue
return
}
if t == nil {
runInfo.err = newStringError(expr, "cannot make type nil")
runInfo.rv = nilValue
return
}
slice := reflect.MakeSlice(t, len(expr.Exprs), len(expr.Exprs))
var i int
valueType := t.Elem()
for i, runInfo.expr = range expr.Exprs {
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, valueType)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cannot use type "+runInfo.rv.Type().String()+" as type "+valueType.String()+" as slice value")
runInfo.rv = nilValue
return
}
slice.Index(i).Set(runInfo.rv)
}
runInfo.rv = slice
// MapExpr
case *ast.MapExpr:
if expr.TypeData == nil {
var i int
var key reflect.Value
m := make(map[interface{}]interface{}, len(expr.Keys))
for i, runInfo.expr = range expr.Keys {
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
key = runInfo.rv
runInfo.expr = expr.Values[i]
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
m[key.Interface()] = runInfo.rv.Interface()
}
runInfo.rv = reflect.ValueOf(m)
return
}
t := makeType(runInfo, expr.TypeData)
if runInfo.err != nil {
runInfo.rv = nilValue
return
}
if t == nil {
runInfo.err = newStringError(expr, "cannot make type nil")
runInfo.rv = nilValue
return
}
runInfo.rv, runInfo.err = makeValue(t)
if runInfo.err != nil {
runInfo.rv = nilValue
return
}
var i int
var key reflect.Value
m := runInfo.rv
keyType := t.Key()
valueType := t.Elem()
for i, runInfo.expr = range expr.Keys {
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
key, runInfo.err = convertReflectValueToType(runInfo.rv, keyType)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cannot use type "+key.Type().String()+" as type "+keyType.String()+" as map key")
runInfo.rv = nilValue
return
}
runInfo.expr = expr.Values[i]
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, valueType)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cannot use type "+runInfo.rv.Type().String()+" as type "+valueType.String()+" as map value")
runInfo.rv = nilValue
return
}
m.SetMapIndex(key, runInfo.rv)
}
runInfo.rv = m
// DerefExpr
case *ast.DerefExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() != reflect.Ptr {
runInfo.err = newStringError(expr.Expr, "cannot deference non-pointer")
runInfo.rv = nilValue
return
}
runInfo.rv = runInfo.rv.Elem()
// AddrExpr
case *ast.AddrExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.CanAddr() {
runInfo.rv = runInfo.rv.Addr()
} else {
i := runInfo.rv.Interface()
runInfo.rv = reflect.ValueOf(&i)
}
// UnaryExpr
case *ast.UnaryExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
switch expr.Operator {
case "-":
switch runInfo.rv.Kind() {
case reflect.Int64:
runInfo.rv = reflect.ValueOf(-runInfo.rv.Int())
case reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int, reflect.Bool:
runInfo.rv = reflect.ValueOf(-toInt64(runInfo.rv))
case reflect.Float64:
runInfo.rv = reflect.ValueOf(-runInfo.rv.Float())
default:
runInfo.rv = reflect.ValueOf(-toFloat64(runInfo.rv))
}
case "^":
runInfo.rv = reflect.ValueOf(^toInt64(runInfo.rv))
case "!":
if toBool(runInfo.rv) {
runInfo.rv = falseValue
} else {
runInfo.rv = trueValue
}
default:
runInfo.err = newStringError(expr, "unknown operator")
runInfo.rv = nilValue
}
// ParenExpr
case *ast.ParenExpr:
runInfo.expr = expr.SubExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
// MemberExpr
case *ast.MemberExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
if env, ok := runInfo.rv.Interface().(*env.Env); ok {
runInfo.rv, runInfo.err = env.GetValue(expr.Name)
if runInfo.err != nil {
runInfo.err = newError(expr, runInfo.err)
runInfo.rv = nilValue
}
return
}
value := runInfo.rv.MethodByName(expr.Name)
if value.IsValid() {
runInfo.rv = value
return
}
if runInfo.rv.Kind() == reflect.Ptr {
runInfo.rv = runInfo.rv.Elem()
}
switch runInfo.rv.Kind() {
case reflect.Struct:
field, found := runInfo.rv.Type().FieldByName(expr.Name)
if found {
runInfo.rv = runInfo.rv.FieldByIndex(field.Index)
return
}
if runInfo.rv.CanAddr() {
runInfo.rv = runInfo.rv.Addr()
method, found := runInfo.rv.Type().MethodByName(expr.Name)
if found {
runInfo.rv = runInfo.rv.Method(method.Index)
return
}
}
runInfo.err = newStringError(expr, "no member named '"+expr.Name+"' for struct")
runInfo.rv = nilValue
case reflect.Map:
runInfo.rv = getMapIndex(reflect.ValueOf(expr.Name), runInfo.rv)
default:
runInfo.err = newStringError(expr, "type "+runInfo.rv.Kind().String()+" does not support member operation")
runInfo.rv = nilValue
}
// ItemExpr
case *ast.ItemExpr:
runInfo.expr = expr.Item
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
item := runInfo.rv
runInfo.expr = expr.Index
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if item.Kind() == reflect.Interface && !item.IsNil() {
item = item.Elem()
}
switch item.Kind() {
case reflect.String, reflect.Slice, reflect.Array:
var index int
index, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
if index < 0 || index >= item.Len() {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
if item.Kind() != reflect.String {
runInfo.rv = item.Index(index)
} else {
// String
runInfo.rv = item.Index(index).Convert(stringType)
}
case reflect.Map:
runInfo.rv = getMapIndex(runInfo.rv, item)
default:
runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support index operation")
runInfo.rv = nilValue
}
// SliceExpr
case *ast.SliceExpr:
runInfo.expr = expr.Item
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
item := runInfo.rv
if item.Kind() == reflect.Interface && !item.IsNil() {
item = item.Elem()
}
switch item.Kind() {
case reflect.String, reflect.Slice, reflect.Array:
var beginIndex int
endIndex := item.Len()
if expr.Begin != nil {
runInfo.expr = expr.Begin
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
beginIndex, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
// (0 <= low) <= high <= len(a)
if beginIndex < 0 {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
}
if expr.End != nil {
runInfo.expr = expr.End
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
endIndex, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "index must be a number")
runInfo.rv = nilValue
return
}
// 0 <= low <= (high <= len(a))
if endIndex > item.Len() {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
}
// 0 <= (low <= high) <= len(a)
if beginIndex > endIndex {
runInfo.err = newStringError(expr, "index out of range")
runInfo.rv = nilValue
return
}
if item.Kind() == reflect.String {
if expr.Cap != nil {
runInfo.err = newStringError(expr, "type string does not support cap")
runInfo.rv = nilValue
return
}
runInfo.rv = item.Slice(beginIndex, endIndex)
return
}
sliceCap := item.Cap()
if expr.Cap != nil {
runInfo.expr = expr.Cap
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
sliceCap, runInfo.err = tryToInt(runInfo.rv)
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cap must be a number")
runInfo.rv = nilValue
return
}
// 0 <= low <= (high <= max <= cap(a))
if sliceCap < endIndex || sliceCap > item.Cap() {
runInfo.err = newStringError(expr, "cap out of range")
runInfo.rv = nilValue
return
}
}
runInfo.rv = item.Slice3(beginIndex, endIndex, sliceCap)
default:
runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support slice operation")
runInfo.rv = nilValue
}
// LetsExpr
case *ast.LetsExpr:
var i int
for i, runInfo.expr = range expr.RHSS {
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
if i < len(expr.LHSS) {
runInfo.expr = expr.LHSS[i]
runInfo.invokeLetExpr()
if runInfo.err != nil {
return
}
}
}
// TernaryOpExpr
case *ast.TernaryOpExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if toBool(runInfo.rv) {
runInfo.expr = expr.LHS
} else {
runInfo.expr = expr.RHS
}
runInfo.invokeExpr()
// NilCoalescingOpExpr
case *ast.NilCoalescingOpExpr:
// if left side has no error and is not nil, returns left side
// otherwise returns right side
runInfo.expr = expr.LHS
runInfo.invokeExpr()
if runInfo.err == nil {
if !isNil(runInfo.rv) {
return
}
} else {
runInfo.err = nil
}
runInfo.expr = expr.RHS
runInfo.invokeExpr()
// LenExpr
case *ast.LenExpr:
runInfo.expr = expr.Expr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
switch runInfo.rv.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.String, reflect.Chan:
runInfo.rv = reflect.ValueOf(int64(runInfo.rv.Len()))
default:
runInfo.err = newStringError(expr, "type "+runInfo.rv.Kind().String()+" does not support len operation")
runInfo.rv = nilValue
}
// ImportExpr
case *ast.ImportExpr:
runInfo.expr = expr.Name
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, stringType)
if runInfo.err != nil {
runInfo.rv = nilValue
return
}
name := runInfo.rv.String()
runInfo.rv = nilValue
methods, ok := env.Packages[name]
if !ok {
runInfo.err = newStringError(expr, "package not found: "+name)
return
}
var err error
pack := runInfo.env.NewEnv()
for methodName, methodValue := range methods {
err = pack.DefineValue(methodName, methodValue)
if err != nil {
runInfo.err = newStringError(expr, "import DefineValue error: "+err.Error())
return
}
}
types, ok := env.PackageTypes[name]
if ok {
for typeName, typeValue := range types {
err = pack.DefineReflectType(typeName, typeValue)
if err != nil {
runInfo.err = newStringError(expr, "import DefineReflectType error: "+err.Error())
return
}
}
}
runInfo.rv = reflect.ValueOf(pack)
// MakeExpr
case *ast.MakeExpr:
t := makeType(runInfo, expr.TypeData)
if runInfo.err != nil {
runInfo.rv = nilValue
return
}
if t == nil {
runInfo.err = newStringError(expr, "cannot make type nil")
runInfo.rv = nilValue
return
}
switch expr.TypeData.Kind {
case ast.TypeSlice:
aLen := 0
if expr.LenExpr != nil {
runInfo.expr = expr.LenExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
aLen = toInt(runInfo.rv)
}
cap := aLen
if expr.CapExpr != nil {
runInfo.expr = expr.CapExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
cap = toInt(runInfo.rv)
}
if aLen > cap {
runInfo.err = newStringError(expr, "make slice len > cap")
runInfo.rv = nilValue
return
}
runInfo.rv = reflect.MakeSlice(t, aLen, cap)
return
case ast.TypeChan:
aLen := 0
if expr.LenExpr != nil {
runInfo.expr = expr.LenExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
aLen = toInt(runInfo.rv)
}
runInfo.rv = reflect.MakeChan(t, aLen)
return
}
runInfo.rv, runInfo.err = makeValue(t)
// MakeTypeExpr
case *ast.MakeTypeExpr:
runInfo.expr = expr.Type
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
// if expr.Name has a dot in it, it should give a syntax error, so no needs to check err
runInfo.env.DefineReflectType(expr.Name, runInfo.rv.Type())
runInfo.rv = reflect.ValueOf(runInfo.rv.Type())
// ChanExpr
case *ast.ChanExpr:
runInfo.expr = expr.RHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
var lhs reflect.Value
rhs := runInfo.rv
if expr.LHS == nil {
// lhs is nil
if rhs.Kind() != reflect.Chan {
// rhs is not channel
runInfo.err = newStringError(expr, "receive from non-chan type "+rhs.Kind().String())
runInfo.rv = nilValue
return
}
} else {
// lhs is not nil
runInfo.expr = expr.LHS
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
runInfo.rv = runInfo.rv.Elem()
}
if runInfo.rv.Kind() != reflect.Chan {
// lhs is not channel
// lhs <- chan rhs or lhs <- rhs
runInfo.err = newStringError(expr, "send to non-chan type "+runInfo.rv.Kind().String())
runInfo.rv = nilValue
return
}
lhs = runInfo.rv
}
var chosen int
var ok bool
if rhs.Kind() == reflect.Chan {
// rhs is channel
// receive from rhs channel
cases := []reflect.SelectCase{{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(runInfo.ctx.Done()),
}, {
Dir: reflect.SelectRecv,
Chan: rhs,
}}
chosen, runInfo.rv, ok = reflect.Select(cases)
if chosen == 0 {
runInfo.err = ErrInterrupt
runInfo.rv = nilValue
return
}
if !ok {
runInfo.rv = nilValue
return
}
rhs = runInfo.rv
}
if expr.LHS == nil {
// <- chan rhs is receive
return
}
// chan lhs <- chan rhs is receive & send
// or
// chan lhs <- rhs is send
runInfo.rv = nilValue
rhs, runInfo.err = convertReflectValueToType(rhs, lhs.Type().Elem())
if runInfo.err != nil {
runInfo.err = newStringError(expr, "cannot use type "+rhs.Type().String()+" as type "+lhs.Type().Elem().String()+" to send to chan")
return
}
// send rhs to lhs channel
cases := []reflect.SelectCase{{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(runInfo.ctx.Done()),
}, {
Dir: reflect.SelectSend,
Chan: lhs,
Send: rhs,
}}
if !runInfo.options.Debug {
// captures panic
defer recoverFunc(runInfo)
}
chosen, _, _ = reflect.Select(cases)
if chosen == 0 {
runInfo.err = ErrInterrupt
}
// FuncExpr
case *ast.FuncExpr:
runInfo.expr = expr
runInfo.funcExpr()
// AnonCallExpr
case *ast.AnonCallExpr:
runInfo.expr = expr
runInfo.anonCallExpr()
// CallExpr
case *ast.CallExpr:
runInfo.expr = expr
runInfo.callExpr()
// IncludeExpr
case *ast.IncludeExpr:
runInfo.expr = expr.ItemExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
itemExpr := runInfo.rv
runInfo.expr = expr.ListExpr
runInfo.invokeExpr()
if runInfo.err != nil {
return
}
if runInfo.rv.Kind() != reflect.Slice && runInfo.rv.Kind() != reflect.Array {
runInfo.err = newStringError(expr, "second argument must be slice or array; but have "+runInfo.rv.Kind().String())
runInfo.rv = nilValue
return
}
for i := 0; i < runInfo.rv.Len(); i++ {
if equal(itemExpr, runInfo.rv.Index(i)) {
runInfo.rv = trueValue
return
}
}
runInfo.rv = falseValue
default:
runInfo.err = newStringError(expr, "unknown expression")
runInfo.rv = nilValue
}
}