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 package starlark
67
68
69
70 import (
71 "fmt"
72 "math"
73 "math/big"
74 "reflect"
75 "strconv"
76 "strings"
77 "unicode/utf8"
78
79 "go.starlark.net/internal/compile"
80 "go.starlark.net/syntax"
81 )
82
83
84 type Value interface {
85
86
87 String() string
88
89
90 Type() string
91
92
93
94
95
96
97
98 Freeze()
99
100
101 Truth() Bool
102
103
104
105
106
107 Hash() (uint32, error)
108 }
109
110
111
112 type Comparable interface {
113 Value
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 CompareSameType(op syntax.Token, y Value, depth int) (bool, error)
132 }
133
134
135
136
137
138
139
140 type TotallyOrdered interface {
141 Value
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 Cmp(y Value, depth int) (int, error)
158 }
159
160 var (
161 _ TotallyOrdered = Int{}
162 _ TotallyOrdered = Float(0)
163 _ Comparable = False
164 _ Comparable = String("")
165 _ Comparable = (*Dict)(nil)
166 _ Comparable = (*List)(nil)
167 _ Comparable = Tuple(nil)
168 _ Comparable = (*Set)(nil)
169 )
170
171
172
173
174 type Callable interface {
175 Value
176 Name() string
177 CallInternal(thread *Thread, args Tuple, kwargs []Tuple) (Value, error)
178 }
179
180 type callableWithPosition interface {
181 Callable
182 Position() syntax.Position
183 }
184
185 var (
186 _ Callable = (*Builtin)(nil)
187 _ Callable = (*Function)(nil)
188 _ callableWithPosition = (*Function)(nil)
189 )
190
191
192
193
194
195 type Iterable interface {
196 Value
197 Iterate() Iterator
198 }
199
200
201 type Sequence interface {
202 Iterable
203 Len() int
204 }
205
206 var (
207 _ Sequence = (*Dict)(nil)
208 _ Sequence = (*Set)(nil)
209 )
210
211
212
213 type Indexable interface {
214 Value
215 Index(i int) Value
216 Len() int
217 }
218
219
220
221
222
223 type Sliceable interface {
224 Indexable
225
226
227
228
229 Slice(start, end, step int) Value
230 }
231
232
233
234
235
236 type HasSetIndex interface {
237 Indexable
238 SetIndex(index int, v Value) error
239 }
240
241 var (
242 _ HasSetIndex = (*List)(nil)
243 _ Indexable = Tuple(nil)
244 _ Indexable = String("")
245 _ Sliceable = Tuple(nil)
246 _ Sliceable = String("")
247 _ Sliceable = (*List)(nil)
248 )
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 type Iterator interface {
264
265
266
267 Next(p *Value) bool
268 Done()
269 }
270
271
272
273
274
275 type Mapping interface {
276 Value
277
278
279
280
281
282 Get(Value) (v Value, found bool, err error)
283 }
284
285
286 type IterableMapping interface {
287 Mapping
288 Iterate() Iterator
289 Items() []Tuple
290 }
291
292 var _ IterableMapping = (*Dict)(nil)
293
294
295 type HasSetKey interface {
296 Mapping
297 SetKey(k, v Value) error
298 }
299
300 var _ HasSetKey = (*Dict)(nil)
301
302
303
304
305
306
307
308
309
310 type HasBinary interface {
311 Value
312 Binary(op syntax.Token, y Value, side Side) (Value, error)
313 }
314
315 type Side bool
316
317 const (
318 Left Side = false
319 Right Side = true
320 )
321
322
323
324
325
326
327
328 type HasUnary interface {
329 Value
330 Unary(op syntax.Token) (Value, error)
331 }
332
333
334
335
336
337
338
339 type HasAttrs interface {
340 Value
341 Attr(name string) (Value, error)
342 AttrNames() []string
343 }
344
345 var (
346 _ HasAttrs = String("")
347 _ HasAttrs = new(List)
348 _ HasAttrs = new(Dict)
349 _ HasAttrs = new(Set)
350 )
351
352
353
354
355
356
357 type HasSetField interface {
358 HasAttrs
359 SetField(name string, val Value) error
360 }
361
362
363
364
365
366 type NoSuchAttrError string
367
368 func (e NoSuchAttrError) Error() string { return string(e) }
369
370
371
372 type NoneType byte
373
374 const None = NoneType(0)
375
376 func (NoneType) String() string { return "None" }
377 func (NoneType) Type() string { return "NoneType" }
378 func (NoneType) Freeze() {}
379 func (NoneType) Truth() Bool { return False }
380 func (NoneType) Hash() (uint32, error) { return 0, nil }
381
382
383 type Bool bool
384
385 const (
386 False Bool = false
387 True Bool = true
388 )
389
390 func (b Bool) String() string {
391 if b {
392 return "True"
393 } else {
394 return "False"
395 }
396 }
397 func (b Bool) Type() string { return "bool" }
398 func (b Bool) Freeze() {}
399 func (b Bool) Truth() Bool { return b }
400 func (b Bool) Hash() (uint32, error) { return uint32(b2i(bool(b))), nil }
401 func (x Bool) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
402 y := y_.(Bool)
403 return threeway(op, b2i(bool(x))-b2i(bool(y))), nil
404 }
405
406
407 type Float float64
408
409 func (f Float) String() string {
410 var buf strings.Builder
411 f.format(&buf, 'g')
412 return buf.String()
413 }
414
415 func (f Float) format(buf *strings.Builder, conv byte) {
416 ff := float64(f)
417 if !isFinite(ff) {
418 if math.IsInf(ff, +1) {
419 buf.WriteString("+inf")
420 } else if math.IsInf(ff, -1) {
421 buf.WriteString("-inf")
422 } else {
423 buf.WriteString("nan")
424 }
425 return
426 }
427
428
429
430
431
432 if conv == 'g' || conv == 'G' {
433 s := strconv.FormatFloat(ff, conv, -1, 64)
434 buf.WriteString(s)
435
436
437 if strings.IndexByte(s, conv-'g'+'e') < 0 && strings.IndexByte(s, '.') < 0 {
438 buf.WriteString(".0")
439 }
440 return
441 }
442
443
444 buf.WriteString(strconv.FormatFloat(ff, conv, 6, 64))
445 }
446
447 func (f Float) Type() string { return "float" }
448 func (f Float) Freeze() {}
449 func (f Float) Truth() Bool { return f != 0.0 }
450 func (f Float) Hash() (uint32, error) {
451
452
453
454 if isFinite(float64(f)) {
455 return finiteFloatToInt(f).Hash()
456 }
457 return 1618033, nil
458 }
459
460 func floor(f Float) Float { return Float(math.Floor(float64(f))) }
461
462
463
464 func isFinite(f float64) bool {
465 return math.Abs(f) <= math.MaxFloat64
466 }
467
468 func (x Float) Cmp(y_ Value, depth int) (int, error) {
469 y := y_.(Float)
470 return floatCmp(x, y), nil
471 }
472
473
474
475 func floatCmp(x, y Float) int {
476 if x > y {
477 return +1
478 } else if x < y {
479 return -1
480 } else if x == y {
481 return 0
482 }
483
484
485 if x == x {
486 return -1
487 } else if y == y {
488 return +1
489 }
490 return 0
491 }
492
493 func (f Float) rational() *big.Rat { return new(big.Rat).SetFloat64(float64(f)) }
494
495
496
497
498 func AsFloat(x Value) (f float64, ok bool) {
499 switch x := x.(type) {
500 case Float:
501 return float64(x), true
502 case Int:
503 return float64(x.Float()), true
504 }
505 return 0, false
506 }
507
508 func (x Float) Mod(y Float) Float {
509 z := Float(math.Mod(float64(x), float64(y)))
510 if (x < 0) != (y < 0) && z != 0 {
511 z += y
512 }
513 return z
514 }
515
516
517 func (f Float) Unary(op syntax.Token) (Value, error) {
518 switch op {
519 case syntax.MINUS:
520 return -f, nil
521 case syntax.PLUS:
522 return +f, nil
523 }
524 return nil, nil
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546 type String string
547
548 func (s String) String() string { return syntax.Quote(string(s), false) }
549 func (s String) GoString() string { return string(s) }
550 func (s String) Type() string { return "string" }
551 func (s String) Freeze() {}
552 func (s String) Truth() Bool { return len(s) > 0 }
553 func (s String) Hash() (uint32, error) { return hashString(string(s)), nil }
554 func (s String) Len() int { return len(s) }
555 func (s String) Index(i int) Value { return s[i : i+1] }
556
557 func (s String) Slice(start, end, step int) Value {
558 if step == 1 {
559 return s[start:end]
560 }
561
562 sign := signum(step)
563 var str []byte
564 for i := start; signum(end-i) == sign; i += step {
565 str = append(str, s[i])
566 }
567 return String(str)
568 }
569
570 func (s String) Attr(name string) (Value, error) { return builtinAttr(s, name, stringMethods) }
571 func (s String) AttrNames() []string { return builtinAttrNames(stringMethods) }
572
573 func (x String) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
574 y := y_.(String)
575 return threeway(op, strings.Compare(string(x), string(y))), nil
576 }
577
578 func AsString(x Value) (string, bool) { v, ok := x.(String); return string(v), ok }
579
580
581
582
583 type stringElems struct {
584 s String
585 ords bool
586 }
587
588 var (
589 _ Iterable = (*stringElems)(nil)
590 _ Indexable = (*stringElems)(nil)
591 )
592
593 func (si stringElems) String() string {
594 if si.ords {
595 return si.s.String() + ".elem_ords()"
596 } else {
597 return si.s.String() + ".elems()"
598 }
599 }
600 func (si stringElems) Type() string { return "string.elems" }
601 func (si stringElems) Freeze() {}
602 func (si stringElems) Truth() Bool { return True }
603 func (si stringElems) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", si.Type()) }
604 func (si stringElems) Iterate() Iterator { return &stringElemsIterator{si, 0} }
605 func (si stringElems) Len() int { return len(si.s) }
606 func (si stringElems) Index(i int) Value {
607 if si.ords {
608 return MakeInt(int(si.s[i]))
609 } else {
610
611
612 return si.s[i : i+1]
613 }
614 }
615
616 type stringElemsIterator struct {
617 si stringElems
618 i int
619 }
620
621 func (it *stringElemsIterator) Next(p *Value) bool {
622 if it.i == len(it.si.s) {
623 return false
624 }
625 *p = it.si.Index(it.i)
626 it.i++
627 return true
628 }
629
630 func (*stringElemsIterator) Done() {}
631
632
633
634
635 type stringCodepoints struct {
636 s String
637 ords bool
638 }
639
640 var _ Iterable = (*stringCodepoints)(nil)
641
642 func (si stringCodepoints) String() string {
643 if si.ords {
644 return si.s.String() + ".codepoint_ords()"
645 } else {
646 return si.s.String() + ".codepoints()"
647 }
648 }
649 func (si stringCodepoints) Type() string { return "string.codepoints" }
650 func (si stringCodepoints) Freeze() {}
651 func (si stringCodepoints) Truth() Bool { return True }
652 func (si stringCodepoints) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", si.Type()) }
653 func (si stringCodepoints) Iterate() Iterator { return &stringCodepointsIterator{si, 0} }
654
655 type stringCodepointsIterator struct {
656 si stringCodepoints
657 i int
658 }
659
660 func (it *stringCodepointsIterator) Next(p *Value) bool {
661 s := it.si.s[it.i:]
662 if s == "" {
663 return false
664 }
665 r, sz := utf8.DecodeRuneInString(string(s))
666 if !it.si.ords {
667 if r == utf8.RuneError {
668 *p = String(r)
669 } else {
670 *p = s[:sz]
671 }
672 } else {
673 *p = MakeInt(int(r))
674 }
675 it.i += sz
676 return true
677 }
678
679 func (*stringCodepointsIterator) Done() {}
680
681
682
683 type Function struct {
684 funcode *compile.Funcode
685 module *module
686 defaults Tuple
687 freevars Tuple
688 }
689
690
691
692 type module struct {
693 program *compile.Program
694 predeclared StringDict
695 globals []Value
696 constants []Value
697 }
698
699
700
701 func (m *module) makeGlobalDict() StringDict {
702 r := make(StringDict, len(m.program.Globals))
703 for i, id := range m.program.Globals {
704 if v := m.globals[i]; v != nil {
705 r[id.Name] = v
706 }
707 }
708 return r
709 }
710
711 func (fn *Function) Name() string { return fn.funcode.Name }
712 func (fn *Function) Doc() string { return fn.funcode.Doc }
713 func (fn *Function) Hash() (uint32, error) { return hashString(fn.funcode.Name), nil }
714 func (fn *Function) Freeze() { fn.defaults.Freeze(); fn.freevars.Freeze() }
715 func (fn *Function) String() string { return toString(fn) }
716 func (fn *Function) Type() string { return "function" }
717 func (fn *Function) Truth() Bool { return true }
718
719
720
721 func (fn *Function) Globals() StringDict { return fn.module.makeGlobalDict() }
722
723 func (fn *Function) Position() syntax.Position { return fn.funcode.Pos }
724 func (fn *Function) NumParams() int { return fn.funcode.NumParams }
725 func (fn *Function) NumKwonlyParams() int { return fn.funcode.NumKwonlyParams }
726
727
728
729
730
731 func (fn *Function) Param(i int) (string, syntax.Position) {
732 if i >= fn.NumParams() {
733 panic(i)
734 }
735 id := fn.funcode.Locals[i]
736 return id.Name, id.Pos
737 }
738
739
740
741 func (fn *Function) ParamDefault(i int) Value {
742 if i < 0 || i >= fn.NumParams() {
743 panic(i)
744 }
745
746
747
748 firstOptIdx := fn.NumParams() - len(fn.defaults)
749 if fn.HasVarargs() {
750 firstOptIdx--
751 }
752 if fn.HasKwargs() {
753 firstOptIdx--
754 }
755 if i < firstOptIdx || i >= firstOptIdx+len(fn.defaults) {
756 return nil
757 }
758
759 dflt := fn.defaults[i-firstOptIdx]
760 if _, ok := dflt.(mandatory); ok {
761 return nil
762 }
763 return dflt
764 }
765
766 func (fn *Function) HasVarargs() bool { return fn.funcode.HasVarargs }
767 func (fn *Function) HasKwargs() bool { return fn.funcode.HasKwargs }
768
769
770 type Builtin struct {
771 name string
772 fn func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error)
773 recv Value
774 }
775
776 func (b *Builtin) Name() string { return b.name }
777 func (b *Builtin) Freeze() {
778 if b.recv != nil {
779 b.recv.Freeze()
780 }
781 }
782 func (b *Builtin) Hash() (uint32, error) {
783 h := hashString(b.name)
784 if b.recv != nil {
785 h ^= 5521
786 }
787 return h, nil
788 }
789 func (b *Builtin) Receiver() Value { return b.recv }
790 func (b *Builtin) String() string { return toString(b) }
791 func (b *Builtin) Type() string { return "builtin_function_or_method" }
792 func (b *Builtin) CallInternal(thread *Thread, args Tuple, kwargs []Tuple) (Value, error) {
793 return b.fn(thread, b, args, kwargs)
794 }
795 func (b *Builtin) Truth() Bool { return true }
796
797
798
799 func NewBuiltin(name string, fn func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error)) *Builtin {
800 return &Builtin{name: name, fn: fn}
801 }
802
803
804
805
806
807
808
809
810
811
812
813
814
815 func (b *Builtin) BindReceiver(recv Value) *Builtin {
816 return &Builtin{name: b.name, fn: b.fn, recv: recv}
817 }
818
819
820
821
822
823 type Dict struct {
824 ht hashtable
825 }
826
827
828
829 func NewDict(size int) *Dict {
830 dict := new(Dict)
831 dict.ht.init(size)
832 return dict
833 }
834
835 func (d *Dict) Clear() error { return d.ht.clear() }
836 func (d *Dict) Delete(k Value) (v Value, found bool, err error) { return d.ht.delete(k) }
837 func (d *Dict) Get(k Value) (v Value, found bool, err error) { return d.ht.lookup(k) }
838 func (d *Dict) Items() []Tuple { return d.ht.items() }
839 func (d *Dict) Keys() []Value { return d.ht.keys() }
840 func (d *Dict) Len() int { return int(d.ht.len) }
841 func (d *Dict) Iterate() Iterator { return d.ht.iterate() }
842 func (d *Dict) SetKey(k, v Value) error { return d.ht.insert(k, v) }
843 func (d *Dict) String() string { return toString(d) }
844 func (d *Dict) Type() string { return "dict" }
845 func (d *Dict) Freeze() { d.ht.freeze() }
846 func (d *Dict) Truth() Bool { return d.Len() > 0 }
847 func (d *Dict) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: dict") }
848
849 func (x *Dict) Union(y *Dict) *Dict {
850 z := new(Dict)
851 z.ht.init(x.Len())
852 z.ht.addAll(&x.ht)
853 z.ht.addAll(&y.ht)
854 return z
855 }
856
857 func (d *Dict) Attr(name string) (Value, error) { return builtinAttr(d, name, dictMethods) }
858 func (d *Dict) AttrNames() []string { return builtinAttrNames(dictMethods) }
859
860 func (x *Dict) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
861 y := y_.(*Dict)
862 switch op {
863 case syntax.EQL:
864 ok, err := dictsEqual(x, y, depth)
865 return ok, err
866 case syntax.NEQ:
867 ok, err := dictsEqual(x, y, depth)
868 return !ok, err
869 default:
870 return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type())
871 }
872 }
873
874 func dictsEqual(x, y *Dict, depth int) (bool, error) {
875 if x.Len() != y.Len() {
876 return false, nil
877 }
878 for e := x.ht.head; e != nil; e = e.next {
879 key, xval := e.key, e.value
880
881 if yval, found, _ := y.Get(key); !found {
882 return false, nil
883 } else if eq, err := EqualDepth(xval, yval, depth-1); err != nil {
884 return false, err
885 } else if !eq {
886 return false, nil
887 }
888 }
889 return true, nil
890 }
891
892
893 type List struct {
894 elems []Value
895 frozen bool
896 itercount uint32
897 }
898
899
900
901 func NewList(elems []Value) *List { return &List{elems: elems} }
902
903 func (l *List) Freeze() {
904 if !l.frozen {
905 l.frozen = true
906 for _, elem := range l.elems {
907 elem.Freeze()
908 }
909 }
910 }
911
912
913
914 func (l *List) checkMutable(verb string) error {
915 if l.frozen {
916 return fmt.Errorf("cannot %s frozen list", verb)
917 }
918 if l.itercount > 0 {
919 return fmt.Errorf("cannot %s list during iteration", verb)
920 }
921 return nil
922 }
923
924 func (l *List) String() string { return toString(l) }
925 func (l *List) Type() string { return "list" }
926 func (l *List) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: list") }
927 func (l *List) Truth() Bool { return l.Len() > 0 }
928 func (l *List) Len() int { return len(l.elems) }
929 func (l *List) Index(i int) Value { return l.elems[i] }
930
931 func (l *List) Slice(start, end, step int) Value {
932 if step == 1 {
933 elems := append([]Value{}, l.elems[start:end]...)
934 return NewList(elems)
935 }
936
937 sign := signum(step)
938 var list []Value
939 for i := start; signum(end-i) == sign; i += step {
940 list = append(list, l.elems[i])
941 }
942 return NewList(list)
943 }
944
945 func (l *List) Attr(name string) (Value, error) { return builtinAttr(l, name, listMethods) }
946 func (l *List) AttrNames() []string { return builtinAttrNames(listMethods) }
947
948 func (l *List) Iterate() Iterator {
949 if !l.frozen {
950 l.itercount++
951 }
952 return &listIterator{l: l}
953 }
954
955 func (x *List) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
956 y := y_.(*List)
957
958
959 return sliceCompare(op, x.elems, y.elems, depth)
960 }
961
962 func sliceCompare(op syntax.Token, x, y []Value, depth int) (bool, error) {
963
964 if len(x) != len(y) && (op == syntax.EQL || op == syntax.NEQ) {
965 return op == syntax.NEQ, nil
966 }
967
968
969 for i := 0; i < len(x) && i < len(y); i++ {
970 if eq, err := EqualDepth(x[i], y[i], depth-1); err != nil {
971 return false, err
972 } else if !eq {
973 switch op {
974 case syntax.EQL:
975 return false, nil
976 case syntax.NEQ:
977 return true, nil
978 default:
979 return CompareDepth(op, x[i], y[i], depth-1)
980 }
981 }
982 }
983
984 return threeway(op, len(x)-len(y)), nil
985 }
986
987 type listIterator struct {
988 l *List
989 i int
990 }
991
992 func (it *listIterator) Next(p *Value) bool {
993 if it.i < it.l.Len() {
994 *p = it.l.elems[it.i]
995 it.i++
996 return true
997 }
998 return false
999 }
1000
1001 func (it *listIterator) Done() {
1002 if !it.l.frozen {
1003 it.l.itercount--
1004 }
1005 }
1006
1007 func (l *List) SetIndex(i int, v Value) error {
1008 if err := l.checkMutable("assign to element of"); err != nil {
1009 return err
1010 }
1011 l.elems[i] = v
1012 return nil
1013 }
1014
1015 func (l *List) Append(v Value) error {
1016 if err := l.checkMutable("append to"); err != nil {
1017 return err
1018 }
1019 l.elems = append(l.elems, v)
1020 return nil
1021 }
1022
1023 func (l *List) Clear() error {
1024 if err := l.checkMutable("clear"); err != nil {
1025 return err
1026 }
1027 for i := range l.elems {
1028 l.elems[i] = nil
1029 }
1030 l.elems = l.elems[:0]
1031 return nil
1032 }
1033
1034
1035 type Tuple []Value
1036
1037 func (t Tuple) Len() int { return len(t) }
1038 func (t Tuple) Index(i int) Value { return t[i] }
1039
1040 func (t Tuple) Slice(start, end, step int) Value {
1041 if step == 1 {
1042 return t[start:end]
1043 }
1044
1045 sign := signum(step)
1046 var tuple Tuple
1047 for i := start; signum(end-i) == sign; i += step {
1048 tuple = append(tuple, t[i])
1049 }
1050 return tuple
1051 }
1052
1053 func (t Tuple) Iterate() Iterator { return &tupleIterator{elems: t} }
1054 func (t Tuple) Freeze() {
1055 for _, elem := range t {
1056 elem.Freeze()
1057 }
1058 }
1059 func (t Tuple) String() string { return toString(t) }
1060 func (t Tuple) Type() string { return "tuple" }
1061 func (t Tuple) Truth() Bool { return len(t) > 0 }
1062
1063 func (x Tuple) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
1064 y := y_.(Tuple)
1065 return sliceCompare(op, x, y, depth)
1066 }
1067
1068 func (t Tuple) Hash() (uint32, error) {
1069
1070 var x, mult uint32 = 0x345678, 1000003
1071 for _, elem := range t {
1072 y, err := elem.Hash()
1073 if err != nil {
1074 return 0, err
1075 }
1076 x = x ^ y*mult
1077 mult += 82520 + uint32(len(t)+len(t))
1078 }
1079 return x, nil
1080 }
1081
1082 type tupleIterator struct{ elems Tuple }
1083
1084 func (it *tupleIterator) Next(p *Value) bool {
1085 if len(it.elems) > 0 {
1086 *p = it.elems[0]
1087 it.elems = it.elems[1:]
1088 return true
1089 }
1090 return false
1091 }
1092
1093 func (it *tupleIterator) Done() {}
1094
1095
1096
1097
1098
1099 type Set struct {
1100 ht hashtable
1101 }
1102
1103
1104
1105 func NewSet(size int) *Set {
1106 set := new(Set)
1107 set.ht.init(size)
1108 return set
1109 }
1110
1111 func (s *Set) Delete(k Value) (found bool, err error) { _, found, err = s.ht.delete(k); return }
1112 func (s *Set) Clear() error { return s.ht.clear() }
1113 func (s *Set) Has(k Value) (found bool, err error) { _, found, err = s.ht.lookup(k); return }
1114 func (s *Set) Insert(k Value) error { return s.ht.insert(k, None) }
1115 func (s *Set) Len() int { return int(s.ht.len) }
1116 func (s *Set) Iterate() Iterator { return s.ht.iterate() }
1117 func (s *Set) String() string { return toString(s) }
1118 func (s *Set) Type() string { return "set" }
1119 func (s *Set) Freeze() { s.ht.freeze() }
1120 func (s *Set) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable type: set") }
1121 func (s *Set) Truth() Bool { return s.Len() > 0 }
1122
1123 func (s *Set) Attr(name string) (Value, error) { return builtinAttr(s, name, setMethods) }
1124 func (s *Set) AttrNames() []string { return builtinAttrNames(setMethods) }
1125
1126 func (x *Set) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
1127 y := y_.(*Set)
1128 switch op {
1129 case syntax.EQL:
1130 ok, err := setsEqual(x, y, depth)
1131 return ok, err
1132 case syntax.NEQ:
1133 ok, err := setsEqual(x, y, depth)
1134 return !ok, err
1135 default:
1136 return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type())
1137 }
1138 }
1139
1140 func setsEqual(x, y *Set, depth int) (bool, error) {
1141 if x.Len() != y.Len() {
1142 return false, nil
1143 }
1144 for e := x.ht.head; e != nil; e = e.next {
1145 if found, _ := y.Has(e.key); !found {
1146 return false, nil
1147 }
1148 }
1149 return true, nil
1150 }
1151
1152 func (s *Set) Union(iter Iterator) (Value, error) {
1153 set := new(Set)
1154 for e := s.ht.head; e != nil; e = e.next {
1155 set.Insert(e.key)
1156 }
1157 var x Value
1158 for iter.Next(&x) {
1159 if err := set.Insert(x); err != nil {
1160 return nil, err
1161 }
1162 }
1163 return set, nil
1164 }
1165
1166
1167
1168 func toString(v Value) string {
1169 buf := new(strings.Builder)
1170 writeValue(buf, v, nil)
1171 return buf.String()
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181 func writeValue(out *strings.Builder, x Value, path []Value) {
1182 switch x := x.(type) {
1183 case nil:
1184 out.WriteString("<nil>")
1185
1186
1187 case NoneType:
1188 out.WriteString("None")
1189
1190 case Int:
1191 out.WriteString(x.String())
1192
1193 case Bool:
1194 if x {
1195 out.WriteString("True")
1196 } else {
1197 out.WriteString("False")
1198 }
1199
1200 case String:
1201 out.WriteString(syntax.Quote(string(x), false))
1202
1203 case *List:
1204 out.WriteByte('[')
1205 if pathContains(path, x) {
1206 out.WriteString("...")
1207 } else {
1208 for i, elem := range x.elems {
1209 if i > 0 {
1210 out.WriteString(", ")
1211 }
1212 writeValue(out, elem, append(path, x))
1213 }
1214 }
1215 out.WriteByte(']')
1216
1217 case Tuple:
1218 out.WriteByte('(')
1219 for i, elem := range x {
1220 if i > 0 {
1221 out.WriteString(", ")
1222 }
1223 writeValue(out, elem, path)
1224 }
1225 if len(x) == 1 {
1226 out.WriteByte(',')
1227 }
1228 out.WriteByte(')')
1229
1230 case *Function:
1231 fmt.Fprintf(out, "<function %s>", x.Name())
1232
1233 case *Builtin:
1234 if x.recv != nil {
1235 fmt.Fprintf(out, "<built-in method %s of %s value>", x.Name(), x.recv.Type())
1236 } else {
1237 fmt.Fprintf(out, "<built-in function %s>", x.Name())
1238 }
1239
1240 case *Dict:
1241 out.WriteByte('{')
1242 if pathContains(path, x) {
1243 out.WriteString("...")
1244 } else {
1245 sep := ""
1246 for e := x.ht.head; e != nil; e = e.next {
1247 k, v := e.key, e.value
1248 out.WriteString(sep)
1249 writeValue(out, k, path)
1250 out.WriteString(": ")
1251 writeValue(out, v, append(path, x))
1252 sep = ", "
1253 }
1254 }
1255 out.WriteByte('}')
1256
1257 case *Set:
1258 out.WriteString("set([")
1259 for e := x.ht.head; e != nil; e = e.next {
1260 if e != x.ht.head {
1261 out.WriteString(", ")
1262 }
1263 writeValue(out, e.key, path)
1264 }
1265 out.WriteString("])")
1266
1267 default:
1268 out.WriteString(x.String())
1269 }
1270 }
1271
1272 func pathContains(path []Value, x Value) bool {
1273 for _, y := range path {
1274 if x == y {
1275 return true
1276 }
1277 }
1278 return false
1279 }
1280
1281
1282
1283 var CompareLimit = 10
1284
1285
1286 func Equal(x, y Value) (bool, error) {
1287 if x, ok := x.(String); ok {
1288 return x == y, nil
1289 }
1290 return EqualDepth(x, y, CompareLimit)
1291 }
1292
1293
1294
1295
1296
1297 func EqualDepth(x, y Value, depth int) (bool, error) {
1298 return CompareDepth(syntax.EQL, x, y, depth)
1299 }
1300
1301
1302
1303
1304
1305
1306
1307
1308 func Compare(op syntax.Token, x, y Value) (bool, error) {
1309 return CompareDepth(op, x, y, CompareLimit)
1310 }
1311
1312
1313
1314
1315
1316
1317
1318
1319 func CompareDepth(op syntax.Token, x, y Value, depth int) (bool, error) {
1320 if depth < 1 {
1321 return false, fmt.Errorf("comparison exceeded maximum recursion depth")
1322 }
1323 if sameType(x, y) {
1324 if xcomp, ok := x.(Comparable); ok {
1325 return xcomp.CompareSameType(op, y, depth)
1326 }
1327
1328 if xcomp, ok := x.(TotallyOrdered); ok {
1329 t, err := xcomp.Cmp(y, depth)
1330 if err != nil {
1331 return false, err
1332 }
1333 return threeway(op, t), nil
1334 }
1335
1336
1337 switch op {
1338 case syntax.EQL:
1339 return x == y, nil
1340 case syntax.NEQ:
1341 return x != y, nil
1342 }
1343 return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type())
1344 }
1345
1346
1347
1348
1349 switch x := x.(type) {
1350 case Int:
1351 if y, ok := y.(Float); ok {
1352 var cmp int
1353 if y != y {
1354 cmp = -1
1355 } else if !math.IsInf(float64(y), 0) {
1356 cmp = x.rational().Cmp(y.rational())
1357 } else if y > 0 {
1358 cmp = -1
1359 } else {
1360 cmp = +1
1361 }
1362 return threeway(op, cmp), nil
1363 }
1364 case Float:
1365 if y, ok := y.(Int); ok {
1366 var cmp int
1367 if x != x {
1368 cmp = +1
1369 } else if !math.IsInf(float64(x), 0) {
1370 cmp = x.rational().Cmp(y.rational())
1371 } else if x > 0 {
1372 cmp = +1
1373 } else {
1374 cmp = -1
1375 }
1376 return threeway(op, cmp), nil
1377 }
1378 }
1379
1380
1381 switch op {
1382 case syntax.EQL:
1383 return false, nil
1384 case syntax.NEQ:
1385 return true, nil
1386 }
1387 return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type())
1388 }
1389
1390 func sameType(x, y Value) bool {
1391 return reflect.TypeOf(x) == reflect.TypeOf(y) || x.Type() == y.Type()
1392 }
1393
1394
1395
1396 func threeway(op syntax.Token, cmp int) bool {
1397 switch op {
1398 case syntax.EQL:
1399 return cmp == 0
1400 case syntax.NEQ:
1401 return cmp != 0
1402 case syntax.LE:
1403 return cmp <= 0
1404 case syntax.LT:
1405 return cmp < 0
1406 case syntax.GE:
1407 return cmp >= 0
1408 case syntax.GT:
1409 return cmp > 0
1410 }
1411 panic(op)
1412 }
1413
1414 func b2i(b bool) int {
1415 if b {
1416 return 1
1417 } else {
1418 return 0
1419 }
1420 }
1421
1422
1423
1424
1425
1426
1427 func Len(x Value) int {
1428 switch x := x.(type) {
1429 case String:
1430 return x.Len()
1431 case Indexable:
1432 return x.Len()
1433 case Sequence:
1434 return x.Len()
1435 }
1436 return -1
1437 }
1438
1439
1440
1441
1442
1443
1444 func Iterate(x Value) Iterator {
1445 if x, ok := x.(Iterable); ok {
1446 return x.Iterate()
1447 }
1448 return nil
1449 }
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465 type Bytes string
1466
1467 var (
1468 _ Comparable = Bytes("")
1469 _ Sliceable = Bytes("")
1470 _ Indexable = Bytes("")
1471 )
1472
1473 func (b Bytes) String() string { return syntax.Quote(string(b), true) }
1474 func (b Bytes) Type() string { return "bytes" }
1475 func (b Bytes) Freeze() {}
1476 func (b Bytes) Truth() Bool { return len(b) > 0 }
1477 func (b Bytes) Hash() (uint32, error) { return String(b).Hash() }
1478 func (b Bytes) Len() int { return len(b) }
1479 func (b Bytes) Index(i int) Value { return b[i : i+1] }
1480
1481 func (b Bytes) Attr(name string) (Value, error) { return builtinAttr(b, name, bytesMethods) }
1482 func (b Bytes) AttrNames() []string { return builtinAttrNames(bytesMethods) }
1483
1484 func (b Bytes) Slice(start, end, step int) Value {
1485 if step == 1 {
1486 return b[start:end]
1487 }
1488
1489 sign := signum(step)
1490 var str []byte
1491 for i := start; signum(end-i) == sign; i += step {
1492 str = append(str, b[i])
1493 }
1494 return Bytes(str)
1495 }
1496
1497 func (x Bytes) CompareSameType(op syntax.Token, y_ Value, depth int) (bool, error) {
1498 y := y_.(Bytes)
1499 return threeway(op, strings.Compare(string(x), string(y))), nil
1500 }
1501
View as plain text