1
2
3
4
5 package starlark
6
7 import (
8 "fmt"
9 "io"
10 "io/ioutil"
11 "log"
12 "math/big"
13 "sort"
14 "strings"
15 "sync/atomic"
16 "time"
17 "unicode"
18 "unicode/utf8"
19 "unsafe"
20
21 "go.starlark.net/internal/compile"
22 "go.starlark.net/internal/spell"
23 "go.starlark.net/resolve"
24 "go.starlark.net/syntax"
25 )
26
27
28
29
30 type Thread struct {
31
32 Name string
33
34
35 stack []*frame
36
37
38
39
40 Print func(thread *Thread, msg string)
41
42
43
44
45
46
47
48 Load func(thread *Thread, module string) (StringDict, error)
49
50
51
52 OnMaxSteps func(thread *Thread)
53
54
55
56
57
58
59
60 Steps, maxSteps uint64
61
62
63 cancelReason *string
64
65
66
67 locals map[string]interface{}
68
69
70 proftime time.Duration
71 }
72
73
74 func (thread *Thread) ExecutionSteps() uint64 {
75 return thread.Steps
76 }
77
78
79
80
81
82
83 func (thread *Thread) SetMaxExecutionSteps(max uint64) {
84 thread.maxSteps = max
85 }
86
87
88
89
90
91 func (thread *Thread) Uncancel() {
92 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&thread.cancelReason)), nil)
93 }
94
95
96
97
98
99
100
101
102
103
104 func (thread *Thread) Cancel(reason string) {
105
106 atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&thread.cancelReason)), nil, unsafe.Pointer(&reason))
107 }
108
109
110
111 func (thread *Thread) SetLocal(key string, value interface{}) {
112 if thread.locals == nil {
113 thread.locals = make(map[string]interface{})
114 }
115 thread.locals[key] = value
116 }
117
118
119 func (thread *Thread) Local(key string) interface{} {
120 return thread.locals[key]
121 }
122
123
124
125
126
127
128 func (thread *Thread) CallFrame(depth int) CallFrame {
129 return thread.frameAt(depth).asCallFrame()
130 }
131
132 func (thread *Thread) frameAt(depth int) *frame {
133 return thread.stack[len(thread.stack)-1-depth]
134 }
135
136
137 func (thread *Thread) CallStack() CallStack {
138 frames := make([]CallFrame, len(thread.stack))
139 for i, fr := range thread.stack {
140 frames[i] = fr.asCallFrame()
141 }
142 return frames
143 }
144
145
146 func (thread *Thread) CallStackDepth() int { return len(thread.stack) }
147
148
149
150
151 type StringDict map[string]Value
152
153
154 func (d StringDict) Keys() []string {
155 names := make([]string, 0, len(d))
156 for name := range d {
157 names = append(names, name)
158 }
159 sort.Strings(names)
160 return names
161 }
162
163 func (d StringDict) String() string {
164 buf := new(strings.Builder)
165 buf.WriteByte('{')
166 sep := ""
167 for _, name := range d.Keys() {
168 buf.WriteString(sep)
169 buf.WriteString(name)
170 buf.WriteString(": ")
171 writeValue(buf, d[name], nil)
172 sep = ", "
173 }
174 buf.WriteByte('}')
175 return buf.String()
176 }
177
178 func (d StringDict) Freeze() {
179 for _, v := range d {
180 v.Freeze()
181 }
182 }
183
184
185 func (d StringDict) Has(key string) bool { _, ok := d[key]; return ok }
186
187
188
189 type frame struct {
190 callable Callable
191 pc uint32
192 locals []Value
193 spanStart int64
194 }
195
196
197 func (fr *frame) Position() syntax.Position {
198 switch c := fr.callable.(type) {
199 case *Function:
200
201 return c.funcode.Position(fr.pc)
202 case callableWithPosition:
203
204
205 return c.Position()
206 }
207 return syntax.MakePosition(&builtinFilename, 0, 0)
208 }
209
210 var builtinFilename = "<builtin>"
211
212
213 func (fr *frame) Callable() Callable { return fr.callable }
214
215
216 type CallStack []CallFrame
217
218
219
220 func (stack CallStack) At(i int) CallFrame { return stack[len(stack)-1-i] }
221
222
223 func (stack *CallStack) Pop() CallFrame {
224 last := len(*stack) - 1
225 top := (*stack)[last]
226 *stack = (*stack)[:last]
227 return top
228 }
229
230
231 func (stack CallStack) String() string {
232 out := new(strings.Builder)
233 if len(stack) > 0 {
234 fmt.Fprintf(out, "Traceback (most recent call last):\n")
235 }
236 for _, fr := range stack {
237 fmt.Fprintf(out, " %s: in %s\n", fr.Pos, fr.Name)
238 }
239 return out.String()
240 }
241
242
243
244 type EvalError struct {
245 Msg string
246 CallStack CallStack
247 cause error
248 }
249
250
251
252 type CallFrame struct {
253 Name string
254 Pos syntax.Position
255 }
256
257 func (fr *frame) asCallFrame() CallFrame {
258 return CallFrame{
259 Name: fr.Callable().Name(),
260 Pos: fr.Position(),
261 }
262 }
263
264 func (thread *Thread) evalError(err error) *EvalError {
265 return &EvalError{
266 Msg: err.Error(),
267 CallStack: thread.CallStack(),
268 cause: err,
269 }
270 }
271
272 func (e *EvalError) Error() string { return e.Msg }
273
274
275
276 func (e *EvalError) Backtrace() string {
277
278
279 stack := e.CallStack
280 suffix := ""
281 if last := len(stack) - 1; last >= 0 && stack[last].Pos.Filename() == builtinFilename {
282 suffix = " in " + stack[last].Name
283 stack = stack[:last]
284 }
285 return fmt.Sprintf("%sError%s: %s", stack, suffix, e.Msg)
286 }
287
288 func (e *EvalError) Unwrap() error { return e.cause }
289
290
291
292
293
294
295 type Program struct {
296 compiled *compile.Program
297 }
298
299
300
301
302
303 const CompilerVersion = compile.Version
304
305
306 func (prog *Program) Filename() string { return prog.compiled.Toplevel.Pos.Filename() }
307
308 func (prog *Program) String() string { return prog.Filename() }
309
310
311 func (prog *Program) NumLoads() int { return len(prog.compiled.Loads) }
312
313
314
315
316 func (prog *Program) Load(i int) (string, syntax.Position) {
317 id := prog.compiled.Loads[i]
318 return id.Name, id.Pos
319 }
320
321
322 func (prog *Program) Write(out io.Writer) error {
323 data := prog.compiled.Encode()
324 _, err := out.Write(data)
325 return err
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 func ExecFile(thread *Thread, filename string, src interface{}, predeclared StringDict) (StringDict, error) {
346
347 _, mod, err := SourceProgram(filename, src, predeclared.Has)
348 if err != nil {
349 return nil, err
350 }
351
352 g, err := mod.Init(thread, predeclared)
353 g.Freeze()
354 return g, err
355 }
356
357
358
359
360
361
362
363
364
365
366 func SourceProgram(filename string, src interface{}, isPredeclared func(string) bool) (*syntax.File, *Program, error) {
367 f, err := syntax.Parse(filename, src, 0)
368 if err != nil {
369 return nil, nil, err
370 }
371 prog, err := FileProgram(f, isPredeclared)
372 return f, prog, err
373 }
374
375
376
377
378
379
380
381
382
383
384
385
386 func FileProgram(f *syntax.File, isPredeclared func(string) bool) (*Program, error) {
387 if err := resolve.File(f, isPredeclared, Universe.Has); err != nil {
388 return nil, err
389 }
390
391 var pos syntax.Position
392 if len(f.Stmts) > 0 {
393 pos = syntax.Start(f.Stmts[0])
394 } else {
395 pos = syntax.MakePosition(&f.Path, 1, 1)
396 }
397
398 module := f.Module.(*resolve.Module)
399 compiled := compile.File(f.Stmts, pos, "<toplevel>", module.Locals, module.Globals)
400
401 return &Program{compiled}, nil
402 }
403
404
405
406 func CompiledProgram(in io.Reader) (*Program, error) {
407 data, err := ioutil.ReadAll(in)
408 if err != nil {
409 return nil, err
410 }
411 compiled, err := compile.DecodeProgram(data)
412 if err != nil {
413 return nil, err
414 }
415 return &Program{compiled}, nil
416 }
417
418
419
420
421 func (prog *Program) Init(thread *Thread, predeclared StringDict) (StringDict, error) {
422 toplevel := makeToplevelFunction(prog.compiled, predeclared)
423
424 _, err := Call(thread, toplevel, nil, nil)
425
426
427
428 return toplevel.Globals(), err
429 }
430
431
432
433
434
435
436
437
438
439 func ExecREPLChunk(f *syntax.File, thread *Thread, globals StringDict) error {
440 var predeclared StringDict
441
442
443
444 if err := resolve.REPLChunk(f, globals.Has, predeclared.Has, Universe.Has); err != nil {
445 return err
446 }
447
448 var pos syntax.Position
449 if len(f.Stmts) > 0 {
450 pos = syntax.Start(f.Stmts[0])
451 } else {
452 pos = syntax.MakePosition(&f.Path, 1, 1)
453 }
454
455 module := f.Module.(*resolve.Module)
456 compiled := compile.File(f.Stmts, pos, "<toplevel>", module.Locals, module.Globals)
457 prog := &Program{compiled}
458
459
460
461 toplevel := makeToplevelFunction(prog.compiled, predeclared)
462
463
464 for i, id := range prog.compiled.Globals {
465 if v := globals[id.Name]; v != nil {
466 toplevel.module.globals[i] = v
467 }
468 }
469
470 _, err := Call(thread, toplevel, nil, nil)
471
472
473 for i, id := range prog.compiled.Globals {
474 if v := toplevel.module.globals[i]; v != nil {
475 globals[id.Name] = v
476 }
477 }
478
479 return err
480 }
481
482 func makeToplevelFunction(prog *compile.Program, predeclared StringDict) *Function {
483
484 constants := make([]Value, len(prog.Constants))
485 for i, c := range prog.Constants {
486 var v Value
487 switch c := c.(type) {
488 case int64:
489 v = MakeInt64(c)
490 case *big.Int:
491 v = MakeBigInt(c)
492 case string:
493 v = String(c)
494 case compile.Bytes:
495 v = Bytes(c)
496 case float64:
497 v = Float(c)
498 default:
499 log.Panicf("unexpected constant %T: %v", c, c)
500 }
501 constants[i] = v
502 }
503
504 return &Function{
505 funcode: prog.Toplevel,
506 module: &module{
507 program: prog,
508 predeclared: predeclared,
509 globals: make([]Value, len(prog.Globals)),
510 constants: constants,
511 },
512 }
513 }
514
515
516
517
518
519
520
521
522
523
524
525 func Eval(thread *Thread, filename string, src interface{}, env StringDict) (Value, error) {
526 expr, err := syntax.ParseExpr(filename, src, 0)
527 if err != nil {
528 return nil, err
529 }
530 f, err := makeExprFunc(expr, env)
531 if err != nil {
532 return nil, err
533 }
534 return Call(thread, f, nil, nil)
535 }
536
537
538
539
540
541
542
543
544
545
546
547
548
549 func EvalExpr(thread *Thread, expr syntax.Expr, env StringDict) (Value, error) {
550 fn, err := makeExprFunc(expr, env)
551 if err != nil {
552 return nil, err
553 }
554 return Call(thread, fn, nil, nil)
555 }
556
557
558
559 func ExprFunc(filename string, src interface{}, env StringDict) (*Function, error) {
560 expr, err := syntax.ParseExpr(filename, src, 0)
561 if err != nil {
562 return nil, err
563 }
564 return makeExprFunc(expr, env)
565 }
566
567
568 func makeExprFunc(expr syntax.Expr, env StringDict) (*Function, error) {
569 locals, err := resolve.Expr(expr, env.Has, Universe.Has)
570 if err != nil {
571 return nil, err
572 }
573
574 return makeToplevelFunction(compile.Expr(expr, "<expr>", locals), env), nil
575 }
576
577
578
579
580 func listExtend(x *List, y Iterable) {
581 if ylist, ok := y.(*List); ok {
582
583 x.elems = append(x.elems, ylist.elems...)
584 } else {
585 iter := y.Iterate()
586 defer iter.Done()
587 var z Value
588 for iter.Next(&z) {
589 x.elems = append(x.elems, z)
590 }
591 }
592 }
593
594
595 func getAttr(x Value, name string) (Value, error) {
596 hasAttr, ok := x.(HasAttrs)
597 if !ok {
598 return nil, fmt.Errorf("%s has no .%s field or method", x.Type(), name)
599 }
600
601 var errmsg string
602 v, err := hasAttr.Attr(name)
603 if err == nil {
604 if v != nil {
605 return v, nil
606 }
607
608 errmsg = fmt.Sprintf("%s has no .%s field or method", x.Type(), name)
609 } else if nsa, ok := err.(NoSuchAttrError); ok {
610 errmsg = string(nsa)
611 } else {
612 return nil, err
613 }
614
615
616 if n := spell.Nearest(name, hasAttr.AttrNames()); n != "" {
617 errmsg = fmt.Sprintf("%s (did you mean .%s?)", errmsg, n)
618 }
619
620 return nil, fmt.Errorf("%s", errmsg)
621 }
622
623
624 func setField(x Value, name string, y Value) error {
625 if x, ok := x.(HasSetField); ok {
626 err := x.SetField(name, y)
627 if _, ok := err.(NoSuchAttrError); ok {
628
629 if n := spell.Nearest(name, x.AttrNames()); n != "" {
630 err = fmt.Errorf("%s (did you mean .%s?)", err, n)
631 }
632 }
633 return err
634 }
635
636 return fmt.Errorf("can't assign to .%s field of %s", name, x.Type())
637 }
638
639
640 func getIndex(x, y Value) (Value, error) {
641 switch x := x.(type) {
642 case Mapping:
643 z, found, err := x.Get(y)
644 if err != nil {
645 return nil, err
646 }
647 if !found {
648 return nil, fmt.Errorf("key %v not in %s", y, x.Type())
649 }
650 return z, nil
651
652 case Indexable:
653 n := x.Len()
654 i, err := AsInt32(y)
655 if err != nil {
656 return nil, fmt.Errorf("%s index: %s", x.Type(), err)
657 }
658 origI := i
659 if i < 0 {
660 i += n
661 }
662 if i < 0 || i >= n {
663 return nil, outOfRange(origI, n, x)
664 }
665 return x.Index(i), nil
666 }
667 return nil, fmt.Errorf("unhandled index operation %s[%s]", x.Type(), y.Type())
668 }
669
670 func outOfRange(i, n int, x Value) error {
671 if n == 0 {
672 return fmt.Errorf("index %d out of range: empty %s", i, x.Type())
673 } else {
674 return fmt.Errorf("%s index %d out of range [%d:%d]", x.Type(), i, -n, n-1)
675 }
676 }
677
678
679 func setIndex(x, y, z Value) error {
680 switch x := x.(type) {
681 case HasSetKey:
682 if err := x.SetKey(y, z); err != nil {
683 return err
684 }
685
686 case HasSetIndex:
687 n := x.Len()
688 i, err := AsInt32(y)
689 if err != nil {
690 return err
691 }
692 origI := i
693 if i < 0 {
694 i += n
695 }
696 if i < 0 || i >= n {
697 return outOfRange(origI, n, x)
698 }
699 return x.SetIndex(i, z)
700
701 default:
702 return fmt.Errorf("%s value does not support item assignment", x.Type())
703 }
704 return nil
705 }
706
707
708 func Unary(op syntax.Token, x Value) (Value, error) {
709
710 if op == syntax.NOT {
711 return !x.Truth(), nil
712 }
713
714
715 if x, ok := x.(HasUnary); ok {
716
717 y, err := x.Unary(op)
718 if y != nil || err != nil {
719 return y, err
720 }
721 }
722
723 return nil, fmt.Errorf("unknown unary op: %s %s", op, x.Type())
724 }
725
726
727
728 func Binary(op syntax.Token, x, y Value) (Value, error) {
729 switch op {
730 case syntax.PLUS:
731 switch x := x.(type) {
732 case String:
733 if y, ok := y.(String); ok {
734 return x + y, nil
735 }
736 case Int:
737 switch y := y.(type) {
738 case Int:
739 return x.Add(y), nil
740 case Float:
741 xf, err := x.finiteFloat()
742 if err != nil {
743 return nil, err
744 }
745 return xf + y, nil
746 }
747 case Float:
748 switch y := y.(type) {
749 case Float:
750 return x + y, nil
751 case Int:
752 yf, err := y.finiteFloat()
753 if err != nil {
754 return nil, err
755 }
756 return x + yf, nil
757 }
758 case *List:
759 if y, ok := y.(*List); ok {
760 z := make([]Value, 0, x.Len()+y.Len())
761 z = append(z, x.elems...)
762 z = append(z, y.elems...)
763 return NewList(z), nil
764 }
765 case Tuple:
766 if y, ok := y.(Tuple); ok {
767 z := make(Tuple, 0, len(x)+len(y))
768 z = append(z, x...)
769 z = append(z, y...)
770 return z, nil
771 }
772 }
773
774 case syntax.MINUS:
775 switch x := x.(type) {
776 case Int:
777 switch y := y.(type) {
778 case Int:
779 return x.Sub(y), nil
780 case Float:
781 xf, err := x.finiteFloat()
782 if err != nil {
783 return nil, err
784 }
785 return xf - y, nil
786 }
787 case Float:
788 switch y := y.(type) {
789 case Float:
790 return x - y, nil
791 case Int:
792 yf, err := y.finiteFloat()
793 if err != nil {
794 return nil, err
795 }
796 return x - yf, nil
797 }
798 }
799
800 case syntax.STAR:
801 switch x := x.(type) {
802 case Int:
803 switch y := y.(type) {
804 case Int:
805 return x.Mul(y), nil
806 case Float:
807 xf, err := x.finiteFloat()
808 if err != nil {
809 return nil, err
810 }
811 return xf * y, nil
812 case String:
813 return stringRepeat(y, x)
814 case Bytes:
815 return bytesRepeat(y, x)
816 case *List:
817 elems, err := tupleRepeat(Tuple(y.elems), x)
818 if err != nil {
819 return nil, err
820 }
821 return NewList(elems), nil
822 case Tuple:
823 return tupleRepeat(y, x)
824 }
825 case Float:
826 switch y := y.(type) {
827 case Float:
828 return x * y, nil
829 case Int:
830 yf, err := y.finiteFloat()
831 if err != nil {
832 return nil, err
833 }
834 return x * yf, nil
835 }
836 case String:
837 if y, ok := y.(Int); ok {
838 return stringRepeat(x, y)
839 }
840 case Bytes:
841 if y, ok := y.(Int); ok {
842 return bytesRepeat(x, y)
843 }
844 case *List:
845 if y, ok := y.(Int); ok {
846 elems, err := tupleRepeat(Tuple(x.elems), y)
847 if err != nil {
848 return nil, err
849 }
850 return NewList(elems), nil
851 }
852 case Tuple:
853 if y, ok := y.(Int); ok {
854 return tupleRepeat(x, y)
855 }
856
857 }
858
859 case syntax.SLASH:
860 switch x := x.(type) {
861 case Int:
862 xf, err := x.finiteFloat()
863 if err != nil {
864 return nil, err
865 }
866 switch y := y.(type) {
867 case Int:
868 yf, err := y.finiteFloat()
869 if err != nil {
870 return nil, err
871 }
872 if yf == 0.0 {
873 return nil, fmt.Errorf("floating-point division by zero")
874 }
875 return xf / yf, nil
876 case Float:
877 if y == 0.0 {
878 return nil, fmt.Errorf("floating-point division by zero")
879 }
880 return xf / y, nil
881 }
882 case Float:
883 switch y := y.(type) {
884 case Float:
885 if y == 0.0 {
886 return nil, fmt.Errorf("floating-point division by zero")
887 }
888 return x / y, nil
889 case Int:
890 yf, err := y.finiteFloat()
891 if err != nil {
892 return nil, err
893 }
894 if yf == 0.0 {
895 return nil, fmt.Errorf("floating-point division by zero")
896 }
897 return x / yf, nil
898 }
899 }
900
901 case syntax.SLASHSLASH:
902 switch x := x.(type) {
903 case Int:
904 switch y := y.(type) {
905 case Int:
906 if y.Sign() == 0 {
907 return nil, fmt.Errorf("floored division by zero")
908 }
909 return x.Div(y), nil
910 case Float:
911 xf, err := x.finiteFloat()
912 if err != nil {
913 return nil, err
914 }
915 if y == 0.0 {
916 return nil, fmt.Errorf("floored division by zero")
917 }
918 return floor(xf / y), nil
919 }
920 case Float:
921 switch y := y.(type) {
922 case Float:
923 if y == 0.0 {
924 return nil, fmt.Errorf("floored division by zero")
925 }
926 return floor(x / y), nil
927 case Int:
928 yf, err := y.finiteFloat()
929 if err != nil {
930 return nil, err
931 }
932 if yf == 0.0 {
933 return nil, fmt.Errorf("floored division by zero")
934 }
935 return floor(x / yf), nil
936 }
937 }
938
939 case syntax.PERCENT:
940 switch x := x.(type) {
941 case Int:
942 switch y := y.(type) {
943 case Int:
944 if y.Sign() == 0 {
945 return nil, fmt.Errorf("integer modulo by zero")
946 }
947 return x.Mod(y), nil
948 case Float:
949 xf, err := x.finiteFloat()
950 if err != nil {
951 return nil, err
952 }
953 if y == 0 {
954 return nil, fmt.Errorf("floating-point modulo by zero")
955 }
956 return xf.Mod(y), nil
957 }
958 case Float:
959 switch y := y.(type) {
960 case Float:
961 if y == 0.0 {
962 return nil, fmt.Errorf("floating-point modulo by zero")
963 }
964 return x.Mod(y), nil
965 case Int:
966 if y.Sign() == 0 {
967 return nil, fmt.Errorf("floating-point modulo by zero")
968 }
969 yf, err := y.finiteFloat()
970 if err != nil {
971 return nil, err
972 }
973 return x.Mod(yf), nil
974 }
975 case String:
976 return interpolate(string(x), y)
977 }
978
979 case syntax.NOT_IN:
980 z, err := Binary(syntax.IN, x, y)
981 if err != nil {
982 return nil, err
983 }
984 return !z.Truth(), nil
985
986 case syntax.IN:
987 switch y := y.(type) {
988 case *List:
989 for _, elem := range y.elems {
990 if eq, err := Equal(elem, x); err != nil {
991 return nil, err
992 } else if eq {
993 return True, nil
994 }
995 }
996 return False, nil
997 case Tuple:
998 for _, elem := range y {
999 if eq, err := Equal(elem, x); err != nil {
1000 return nil, err
1001 } else if eq {
1002 return True, nil
1003 }
1004 }
1005 return False, nil
1006 case Mapping:
1007
1008
1009 _, found, _ := y.Get(x)
1010 return Bool(found), nil
1011 case *Set:
1012 ok, err := y.Has(x)
1013 return Bool(ok), err
1014 case String:
1015 needle, ok := x.(String)
1016 if !ok {
1017 return nil, fmt.Errorf("'in <string>' requires string as left operand, not %s", x.Type())
1018 }
1019 return Bool(strings.Contains(string(y), string(needle))), nil
1020 case Bytes:
1021 switch needle := x.(type) {
1022 case Bytes:
1023 return Bool(strings.Contains(string(y), string(needle))), nil
1024 case Int:
1025 var b byte
1026 if err := AsInt(needle, &b); err != nil {
1027 return nil, fmt.Errorf("int in bytes: %s", err)
1028 }
1029 return Bool(strings.IndexByte(string(y), b) >= 0), nil
1030 default:
1031 return nil, fmt.Errorf("'in bytes' requires bytes or int as left operand, not %s", x.Type())
1032 }
1033 case rangeValue:
1034 i, err := NumberToInt(x)
1035 if err != nil {
1036 return nil, fmt.Errorf("'in <range>' requires integer as left operand, not %s", x.Type())
1037 }
1038 return Bool(y.contains(i)), nil
1039 }
1040
1041 case syntax.PIPE:
1042 switch x := x.(type) {
1043 case Int:
1044 if y, ok := y.(Int); ok {
1045 return x.Or(y), nil
1046 }
1047
1048 case *Dict:
1049 if y, ok := y.(*Dict); ok {
1050 return x.Union(y), nil
1051 }
1052
1053 case *Set:
1054 if y, ok := y.(*Set); ok {
1055 iter := Iterate(y)
1056 defer iter.Done()
1057 return x.Union(iter)
1058 }
1059 }
1060
1061 case syntax.AMP:
1062 switch x := x.(type) {
1063 case Int:
1064 if y, ok := y.(Int); ok {
1065 return x.And(y), nil
1066 }
1067 case *Set:
1068 if y, ok := y.(*Set); ok {
1069 set := new(Set)
1070 if x.Len() > y.Len() {
1071 x, y = y, x
1072 }
1073 for xe := x.ht.head; xe != nil; xe = xe.next {
1074
1075 if found, _ := y.Has(xe.key); found {
1076 set.Insert(xe.key)
1077 }
1078 }
1079 return set, nil
1080 }
1081 }
1082
1083 case syntax.CIRCUMFLEX:
1084 switch x := x.(type) {
1085 case Int:
1086 if y, ok := y.(Int); ok {
1087 return x.Xor(y), nil
1088 }
1089 case *Set:
1090 if y, ok := y.(*Set); ok {
1091 set := new(Set)
1092 for xe := x.ht.head; xe != nil; xe = xe.next {
1093 if found, _ := y.Has(xe.key); !found {
1094 set.Insert(xe.key)
1095 }
1096 }
1097 for ye := y.ht.head; ye != nil; ye = ye.next {
1098 if found, _ := x.Has(ye.key); !found {
1099 set.Insert(ye.key)
1100 }
1101 }
1102 return set, nil
1103 }
1104 }
1105
1106 case syntax.LTLT, syntax.GTGT:
1107 if x, ok := x.(Int); ok {
1108 y, err := AsInt32(y)
1109 if err != nil {
1110 return nil, err
1111 }
1112 if y < 0 {
1113 return nil, fmt.Errorf("negative shift count: %v", y)
1114 }
1115 if op == syntax.LTLT {
1116 if y >= 512 {
1117 return nil, fmt.Errorf("shift count too large: %v", y)
1118 }
1119 return x.Lsh(uint(y)), nil
1120 } else {
1121 return x.Rsh(uint(y)), nil
1122 }
1123 }
1124
1125 default:
1126
1127 goto unknown
1128 }
1129
1130
1131
1132 if x, ok := x.(HasBinary); ok {
1133 z, err := x.Binary(op, y, Left)
1134 if z != nil || err != nil {
1135 return z, err
1136 }
1137 }
1138 if y, ok := y.(HasBinary); ok {
1139 z, err := y.Binary(op, x, Right)
1140 if z != nil || err != nil {
1141 return z, err
1142 }
1143 }
1144
1145
1146 unknown:
1147 return nil, fmt.Errorf("unknown binary op: %s %s %s", x.Type(), op, y.Type())
1148 }
1149
1150
1151
1152 const maxAlloc = 1 << 30
1153
1154 func tupleRepeat(elems Tuple, n Int) (Tuple, error) {
1155 if len(elems) == 0 {
1156 return nil, nil
1157 }
1158 i, err := AsInt32(n)
1159 if err != nil {
1160 return nil, fmt.Errorf("repeat count %s too large", n)
1161 }
1162 if i < 1 {
1163 return nil, nil
1164 }
1165
1166 sz := len(elems) * i
1167 if sz < 0 || sz >= maxAlloc {
1168
1169 return nil, fmt.Errorf("excessive repeat (%d * %d elements)", len(elems), i)
1170 }
1171 res := make([]Value, sz)
1172
1173 x := copy(res, elems)
1174 for x < len(res) {
1175 copy(res[x:], res[:x])
1176 x *= 2
1177 }
1178 return res, nil
1179 }
1180
1181 func bytesRepeat(b Bytes, n Int) (Bytes, error) {
1182 res, err := stringRepeat(String(b), n)
1183 return Bytes(res), err
1184 }
1185
1186 func stringRepeat(s String, n Int) (String, error) {
1187 if s == "" {
1188 return "", nil
1189 }
1190 i, err := AsInt32(n)
1191 if err != nil {
1192 return "", fmt.Errorf("repeat count %s too large", n)
1193 }
1194 if i < 1 {
1195 return "", nil
1196 }
1197
1198 sz := len(s) * i
1199 if sz < 0 || sz >= maxAlloc {
1200
1201 return "", fmt.Errorf("excessive repeat (%d * %d elements)", len(s), i)
1202 }
1203 return String(strings.Repeat(string(s), i)), nil
1204 }
1205
1206
1207 func Call(thread *Thread, fn Value, args Tuple, kwargs []Tuple) (Value, error) {
1208 c, ok := fn.(Callable)
1209 if !ok {
1210 return nil, fmt.Errorf("invalid call of non-function (%s)", fn.Type())
1211 }
1212
1213
1214 var fr *frame
1215
1216
1217 if n := len(thread.stack); n < cap(thread.stack) {
1218 fr = thread.stack[n : n+1][0]
1219 }
1220 if fr == nil {
1221 fr = new(frame)
1222 }
1223
1224 if thread.stack == nil {
1225
1226 if thread.maxSteps == 0 {
1227 thread.maxSteps--
1228 }
1229 }
1230
1231 thread.stack = append(thread.stack, fr)
1232
1233 fr.callable = c
1234
1235 thread.beginProfSpan()
1236
1237
1238
1239
1240 defer func() {
1241 thread.endProfSpan()
1242
1243
1244
1245
1246 *fr = frame{}
1247
1248 thread.stack = thread.stack[:len(thread.stack)-1]
1249 }()
1250
1251 result, err := c.CallInternal(thread, args, kwargs)
1252
1253
1254 if result == nil && err == nil {
1255 err = fmt.Errorf("internal error: nil (not None) returned from %s", fn)
1256 }
1257
1258
1259 if err != nil {
1260 if _, ok := err.(*EvalError); !ok {
1261 err = thread.evalError(err)
1262 }
1263 }
1264
1265 return result, err
1266 }
1267
1268 func slice(x, lo, hi, step_ Value) (Value, error) {
1269 sliceable, ok := x.(Sliceable)
1270 if !ok {
1271 return nil, fmt.Errorf("invalid slice operand %s", x.Type())
1272 }
1273
1274 n := sliceable.Len()
1275 step := 1
1276 if step_ != None {
1277 var err error
1278 step, err = AsInt32(step_)
1279 if err != nil {
1280 return nil, fmt.Errorf("invalid slice step: %s", err)
1281 }
1282 if step == 0 {
1283 return nil, fmt.Errorf("zero is not a valid slice step")
1284 }
1285 }
1286
1287
1288
1289 var start, end int
1290 if step > 0 {
1291
1292
1293 var err error
1294 start, end, err = indices(lo, hi, n)
1295 if err != nil {
1296 return nil, err
1297 }
1298
1299 if end < start {
1300 end = start
1301 }
1302 } else {
1303
1304
1305
1306
1307 start = n - 1
1308 if err := asIndex(lo, n, &start); err != nil {
1309 return nil, fmt.Errorf("invalid start index: %s", err)
1310 }
1311 if start >= n {
1312 start = n - 1
1313 }
1314
1315 end = -1
1316 if err := asIndex(hi, n, &end); err != nil {
1317 return nil, fmt.Errorf("invalid end index: %s", err)
1318 }
1319 if end < -1 {
1320 end = -1
1321 }
1322
1323 if start < end {
1324 start = end
1325 }
1326 }
1327
1328 return sliceable.Slice(start, end, step), nil
1329 }
1330
1331
1332 func signum64(x int64) int { return int(uint64(x>>63) | uint64(-x)>>63) }
1333 func signum(x int) int { return signum64(int64(x)) }
1334
1335
1336
1337
1338
1339
1340
1341 func indices(start_, end_ Value, len int) (start, end int, err error) {
1342 start = 0
1343 if err := asIndex(start_, len, &start); err != nil {
1344 return 0, 0, fmt.Errorf("invalid start index: %s", err)
1345 }
1346
1347 if start < 0 {
1348 start = 0
1349 } else if start > len {
1350 start = len
1351 }
1352
1353 end = len
1354 if err := asIndex(end_, len, &end); err != nil {
1355 return 0, 0, fmt.Errorf("invalid end index: %s", err)
1356 }
1357
1358 if end < 0 {
1359 end = 0
1360 } else if end > len {
1361 end = len
1362 }
1363
1364 return start, end, nil
1365 }
1366
1367
1368
1369 func asIndex(v Value, len int, result *int) error {
1370 if v != nil && v != None {
1371 var err error
1372 *result, err = AsInt32(v)
1373 if err != nil {
1374 return err
1375 }
1376 if *result < 0 {
1377 *result += len
1378 }
1379 }
1380 return nil
1381 }
1382
1383
1384
1385 func setArgs(locals []Value, fn *Function, args Tuple, kwargs []Tuple) error {
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406 if fn.NumParams() == 0 {
1407 if nactual := len(args) + len(kwargs); nactual > 0 {
1408 return fmt.Errorf("function %s accepts no arguments (%d given)", fn.Name(), nactual)
1409 }
1410 return nil
1411 }
1412
1413 cond := func(x bool, y, z interface{}) interface{} {
1414 if x {
1415 return y
1416 }
1417 return z
1418 }
1419
1420
1421 nparams := fn.NumParams()
1422 var kwdict *Dict
1423 if fn.HasKwargs() {
1424 nparams--
1425 kwdict = new(Dict)
1426 locals[nparams] = kwdict
1427 }
1428 if fn.HasVarargs() {
1429 nparams--
1430 }
1431
1432
1433 nonkwonly := nparams - fn.NumKwonlyParams()
1434
1435
1436 n := len(args)
1437 if len(args) > nonkwonly {
1438 if !fn.HasVarargs() {
1439 return fmt.Errorf("function %s accepts %s%d positional argument%s (%d given)",
1440 fn.Name(),
1441 cond(len(fn.defaults) > fn.NumKwonlyParams(), "at most ", ""),
1442 nonkwonly,
1443 cond(nonkwonly == 1, "", "s"),
1444 len(args))
1445 }
1446 n = nonkwonly
1447 }
1448
1449
1450 for i := 0; i < n; i++ {
1451 locals[i] = args[i]
1452 }
1453
1454
1455 if fn.HasVarargs() {
1456 tuple := make(Tuple, len(args)-n)
1457 for i := n; i < len(args); i++ {
1458 tuple[i-n] = args[i]
1459 }
1460 locals[nparams] = tuple
1461 }
1462
1463
1464 paramIdents := fn.funcode.Locals[:nparams]
1465 for _, pair := range kwargs {
1466 k, v := pair[0].(String), pair[1]
1467 if i := findParam(paramIdents, string(k)); i >= 0 {
1468 if locals[i] != nil {
1469 return fmt.Errorf("function %s got multiple values for parameter %s", fn.Name(), k)
1470 }
1471 locals[i] = v
1472 continue
1473 }
1474 if kwdict == nil {
1475 return fmt.Errorf("function %s got an unexpected keyword argument %s", fn.Name(), k)
1476 }
1477 oldlen := kwdict.Len()
1478 kwdict.SetKey(k, v)
1479 if kwdict.Len() == oldlen {
1480 return fmt.Errorf("function %s got multiple values for parameter %s", fn.Name(), k)
1481 }
1482 }
1483
1484
1485 if n < nparams || fn.NumKwonlyParams() > 0 {
1486 m := nparams - len(fn.defaults)
1487
1488
1489 var missing []string
1490 var i int
1491 for i = n; i < m; i++ {
1492 if locals[i] == nil {
1493 missing = append(missing, paramIdents[i].Name)
1494 }
1495 }
1496
1497
1498 for ; i < nparams; i++ {
1499 if locals[i] == nil {
1500 dflt := fn.defaults[i-m]
1501 if _, ok := dflt.(mandatory); ok {
1502 missing = append(missing, paramIdents[i].Name)
1503 continue
1504 }
1505 locals[i] = dflt
1506 }
1507 }
1508
1509 if missing != nil {
1510 return fmt.Errorf("function %s missing %d argument%s (%s)",
1511 fn.Name(), len(missing), cond(len(missing) > 1, "s", ""), strings.Join(missing, ", "))
1512 }
1513 }
1514 return nil
1515 }
1516
1517 func findParam(params []compile.Binding, name string) int {
1518 for i, param := range params {
1519 if param.Name == name {
1520 return i
1521 }
1522 }
1523 return -1
1524 }
1525
1526
1527 func interpolate(format string, x Value) (Value, error) {
1528 buf := new(strings.Builder)
1529 index := 0
1530 nargs := 1
1531 if tuple, ok := x.(Tuple); ok {
1532 nargs = len(tuple)
1533 }
1534 for {
1535 i := strings.IndexByte(format, '%')
1536 if i < 0 {
1537 buf.WriteString(format)
1538 break
1539 }
1540 buf.WriteString(format[:i])
1541 format = format[i+1:]
1542
1543 if format != "" && format[0] == '%' {
1544 buf.WriteByte('%')
1545 format = format[1:]
1546 continue
1547 }
1548
1549 var arg Value
1550 if format != "" && format[0] == '(' {
1551
1552 format = format[1:]
1553 j := strings.IndexByte(format, ')')
1554 if j < 0 {
1555 return nil, fmt.Errorf("incomplete format key")
1556 }
1557 key := format[:j]
1558 if dict, ok := x.(Mapping); !ok {
1559 return nil, fmt.Errorf("format requires a mapping")
1560 } else if v, found, _ := dict.Get(String(key)); found {
1561 arg = v
1562 } else {
1563 return nil, fmt.Errorf("key not found: %s", key)
1564 }
1565 format = format[j+1:]
1566 } else {
1567
1568 if index >= nargs {
1569 return nil, fmt.Errorf("not enough arguments for format string")
1570 }
1571 if tuple, ok := x.(Tuple); ok {
1572 arg = tuple[index]
1573 } else {
1574 arg = x
1575 }
1576 }
1577
1578
1579
1580
1581
1582
1583
1584
1585 if format == "" {
1586 return nil, fmt.Errorf("incomplete format")
1587 }
1588 switch c := format[0]; c {
1589 case 's', 'r':
1590 if str, ok := AsString(arg); ok && c == 's' {
1591 buf.WriteString(str)
1592 } else {
1593 writeValue(buf, arg, nil)
1594 }
1595 case 'd', 'i', 'o', 'x', 'X':
1596 i, err := NumberToInt(arg)
1597 if err != nil {
1598 return nil, fmt.Errorf("%%%c format requires integer: %v", c, err)
1599 }
1600 switch c {
1601 case 'd', 'i':
1602 fmt.Fprintf(buf, "%d", i)
1603 case 'o':
1604 fmt.Fprintf(buf, "%o", i)
1605 case 'x':
1606 fmt.Fprintf(buf, "%x", i)
1607 case 'X':
1608 fmt.Fprintf(buf, "%X", i)
1609 }
1610 case 'e', 'f', 'g', 'E', 'F', 'G':
1611 f, ok := AsFloat(arg)
1612 if !ok {
1613 return nil, fmt.Errorf("%%%c format requires float, not %s", c, arg.Type())
1614 }
1615 Float(f).format(buf, c)
1616 case 'c':
1617 switch arg := arg.(type) {
1618 case Int:
1619
1620 r, err := AsInt32(arg)
1621 if err != nil || r < 0 || r > unicode.MaxRune {
1622 return nil, fmt.Errorf("%%c format requires a valid Unicode code point, got %s", arg)
1623 }
1624 buf.WriteRune(rune(r))
1625 case String:
1626 r, size := utf8.DecodeRuneInString(string(arg))
1627 if size != len(arg) || len(arg) == 0 {
1628 return nil, fmt.Errorf("%%c format requires a single-character string")
1629 }
1630 buf.WriteRune(r)
1631 default:
1632 return nil, fmt.Errorf("%%c format requires int or single-character string, not %s", arg.Type())
1633 }
1634 case '%':
1635 buf.WriteByte('%')
1636 default:
1637 return nil, fmt.Errorf("unknown conversion %%%c", c)
1638 }
1639 format = format[1:]
1640 index++
1641 }
1642
1643 if index < nargs {
1644 return nil, fmt.Errorf("too many arguments for format string")
1645 }
1646
1647 return String(buf.String()), nil
1648 }
1649
View as plain text