-
Notifications
You must be signed in to change notification settings - Fork 304
/
objects.go
1612 lines (1435 loc) · 35.3 KB
/
objects.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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tengo
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/d5/tengo/v2/parser"
"github.com/d5/tengo/v2/token"
)
var (
// TrueValue represents a true value.
TrueValue Object = &Bool{value: true}
// FalseValue represents a false value.
FalseValue Object = &Bool{value: false}
// UndefinedValue represents an undefined value.
UndefinedValue Object = &Undefined{}
)
// Object represents an object in the VM.
type Object interface {
// TypeName should return the name of the type.
TypeName() string
// String should return a string representation of the type's value.
String() string
// BinaryOp should return another object that is the result of a given
// binary operator and a right-hand side object. If BinaryOp returns an
// error, the VM will treat it as a run-time error.
BinaryOp(op token.Token, rhs Object) (Object, error)
// IsFalsy should return true if the value of the type should be considered
// as falsy.
IsFalsy() bool
// Equals should return true if the value of the type should be considered
// as equal to the value of another object.
Equals(another Object) bool
// Copy should return a copy of the type (and its value). Copy function
// will be used for copy() builtin function which is expected to deep-copy
// the values generally.
Copy() Object
// IndexGet should take an index Object and return a result Object or an
// error for indexable objects. Indexable is an object that can take an
// index and return an object. If error is returned, the runtime will treat
// it as a run-time error and ignore returned value. If Object is not
// indexable, ErrNotIndexable should be returned as error. If nil is
// returned as value, it will be converted to UndefinedToken value by the
// runtime.
IndexGet(index Object) (value Object, err error)
// IndexSet should take an index Object and a value Object for index
// assignable objects. Index assignable is an object that can take an index
// and a value on the left-hand side of the assignment statement. If Object
// is not index assignable, ErrNotIndexAssignable should be returned as
// error. If an error is returned, it will be treated as a run-time error.
IndexSet(index, value Object) error
// Iterate should return an Iterator for the type.
Iterate() Iterator
// CanIterate should return whether the Object can be Iterated.
CanIterate() bool
// Call should take an arbitrary number of arguments and returns a return
// value and/or an error, which the VM will consider as a run-time error.
Call(args ...Object) (ret Object, err error)
// CanCall should return whether the Object can be Called.
CanCall() bool
}
// ObjectImpl represents a default Object Implementation. To defined a new
// value type, one can embed ObjectImpl in their type declarations to avoid
// implementing all non-significant methods. TypeName() and String() methods
// still need to be implemented.
type ObjectImpl struct {
}
// TypeName returns the name of the type.
func (o *ObjectImpl) TypeName() string {
panic(ErrNotImplemented)
}
func (o *ObjectImpl) String() string {
panic(ErrNotImplemented)
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *ObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *ObjectImpl) Copy() Object {
return nil
}
// IsFalsy returns true if the value of the type is falsy.
func (o *ObjectImpl) IsFalsy() bool {
return false
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *ObjectImpl) Equals(x Object) bool {
return o == x
}
// IndexGet returns an element at a given index.
func (o *ObjectImpl) IndexGet(_ Object) (res Object, err error) {
return nil, ErrNotIndexable
}
// IndexSet sets an element at a given index.
func (o *ObjectImpl) IndexSet(_, _ Object) (err error) {
return ErrNotIndexAssignable
}
// Iterate returns an iterator.
func (o *ObjectImpl) Iterate() Iterator {
return nil
}
// CanIterate returns whether the Object can be Iterated.
func (o *ObjectImpl) CanIterate() bool {
return false
}
// Call takes an arbitrary number of arguments and returns a return value
// and/or an error.
func (o *ObjectImpl) Call(_ ...Object) (ret Object, err error) {
return nil, nil
}
// CanCall returns whether the Object can be Called.
func (o *ObjectImpl) CanCall() bool {
return false
}
// Array represents an array of objects.
type Array struct {
ObjectImpl
Value []Object
}
// TypeName returns the name of the type.
func (o *Array) TypeName() string {
return "array"
}
func (o *Array) String() string {
var elements []string
for _, e := range o.Value {
elements = append(elements, e.String())
}
return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error) {
if rhs, ok := rhs.(*Array); ok {
switch op {
case token.Add:
if len(rhs.Value) == 0 {
return o, nil
}
return &Array{Value: append(o.Value, rhs.Value...)}, nil
}
}
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Array) Copy() Object {
var c []Object
for _, elem := range o.Value {
c = append(c, elem.Copy())
}
return &Array{Value: c}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Array) IsFalsy() bool {
return len(o.Value) == 0
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Array) Equals(x Object) bool {
var xVal []Object
switch x := x.(type) {
case *Array:
xVal = x.Value
case *ImmutableArray:
xVal = x.Value
default:
return false
}
if len(o.Value) != len(xVal) {
return false
}
for i, e := range o.Value {
if !e.Equals(xVal[i]) {
return false
}
}
return true
}
// IndexGet returns an element at a given index.
func (o *Array) IndexGet(index Object) (res Object, err error) {
intIdx, ok := index.(*Int)
if !ok {
err = ErrInvalidIndexType
return
}
idxVal := int(intIdx.Value)
if idxVal < 0 || idxVal >= len(o.Value) {
res = UndefinedValue
return
}
res = o.Value[idxVal]
return
}
// IndexSet sets an element at a given index.
func (o *Array) IndexSet(index, value Object) (err error) {
intIdx, ok := ToInt(index)
if !ok {
err = ErrInvalidIndexType
return
}
if intIdx < 0 || intIdx >= len(o.Value) {
err = ErrIndexOutOfBounds
return
}
o.Value[intIdx] = value
return nil
}
// Iterate creates an array iterator.
func (o *Array) Iterate() Iterator {
return &ArrayIterator{
v: o.Value,
l: len(o.Value),
}
}
// CanIterate returns whether the Object can be Iterated.
func (o *Array) CanIterate() bool {
return true
}
// Bool represents a boolean value.
type Bool struct {
ObjectImpl
// this is intentionally non-public to force using objects.TrueValue and
// FalseValue always
value bool
}
func (o *Bool) String() string {
if o.value {
return "true"
}
return "false"
}
// TypeName returns the name of the type.
func (o *Bool) TypeName() string {
return "bool"
}
// Copy returns a copy of the type.
func (o *Bool) Copy() Object {
return o
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Bool) IsFalsy() bool {
return !o.value
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Bool) Equals(x Object) bool {
return o == x
}
// GobDecode decodes bool value from input bytes.
func (o *Bool) GobDecode(b []byte) (err error) {
o.value = b[0] == 1
return
}
// GobEncode encodes bool values into bytes.
func (o *Bool) GobEncode() (b []byte, err error) {
if o.value {
b = []byte{1}
} else {
b = []byte{0}
}
return
}
// BuiltinFunction represents a builtin function.
type BuiltinFunction struct {
ObjectImpl
Name string
Value CallableFunc
}
// TypeName returns the name of the type.
func (o *BuiltinFunction) TypeName() string {
return "builtin-function:" + o.Name
}
func (o *BuiltinFunction) String() string {
return "<builtin-function>"
}
// Copy returns a copy of the type.
func (o *BuiltinFunction) Copy() Object {
return &BuiltinFunction{Value: o.Value}
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *BuiltinFunction) Equals(_ Object) bool {
return false
}
// Call executes a builtin function.
func (o *BuiltinFunction) Call(args ...Object) (Object, error) {
return o.Value(args...)
}
// CanCall returns whether the Object can be Called.
func (o *BuiltinFunction) CanCall() bool {
return true
}
// BuiltinModule is an importable module that's written in Go.
type BuiltinModule struct {
Attrs map[string]Object
}
// Import returns an immutable map for the module.
func (m *BuiltinModule) Import(moduleName string) (interface{}, error) {
return m.AsImmutableMap(moduleName), nil
}
// AsImmutableMap converts builtin module into an immutable map.
func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap {
attrs := make(map[string]Object, len(m.Attrs))
for k, v := range m.Attrs {
attrs[k] = v.Copy()
}
attrs["__module_name__"] = &String{Value: moduleName}
return &ImmutableMap{Value: attrs}
}
// Bytes represents a byte array.
type Bytes struct {
ObjectImpl
Value []byte
}
func (o *Bytes) String() string {
return string(o.Value)
}
// TypeName returns the name of the type.
func (o *Bytes) TypeName() string {
return "bytes"
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) {
switch op {
case token.Add:
switch rhs := rhs.(type) {
case *Bytes:
if len(o.Value)+len(rhs.Value) > MaxBytesLen {
return nil, ErrBytesLimit
}
return &Bytes{Value: append(o.Value, rhs.Value...)}, nil
}
}
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Bytes) Copy() Object {
return &Bytes{Value: append([]byte{}, o.Value...)}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Bytes) IsFalsy() bool {
return len(o.Value) == 0
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Bytes) Equals(x Object) bool {
t, ok := x.(*Bytes)
if !ok {
return false
}
return bytes.Equal(o.Value, t.Value)
}
// IndexGet returns an element (as Int) at a given index.
func (o *Bytes) IndexGet(index Object) (res Object, err error) {
intIdx, ok := index.(*Int)
if !ok {
err = ErrInvalidIndexType
return
}
idxVal := int(intIdx.Value)
if idxVal < 0 || idxVal >= len(o.Value) {
res = UndefinedValue
return
}
res = &Int{Value: int64(o.Value[idxVal])}
return
}
// Iterate creates a bytes iterator.
func (o *Bytes) Iterate() Iterator {
return &BytesIterator{
v: o.Value,
l: len(o.Value),
}
}
// CanIterate returns whether the Object can be Iterated.
func (o *Bytes) CanIterate() bool {
return true
}
// Char represents a character value.
type Char struct {
ObjectImpl
Value rune
}
func (o *Char) String() string {
return string(o.Value)
}
// TypeName returns the name of the type.
func (o *Char) TypeName() string {
return "char"
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *Char) BinaryOp(op token.Token, rhs Object) (Object, error) {
switch rhs := rhs.(type) {
case *Char:
switch op {
case token.Add:
r := o.Value + rhs.Value
if r == o.Value {
return o, nil
}
return &Char{Value: r}, nil
case token.Sub:
r := o.Value - rhs.Value
if r == o.Value {
return o, nil
}
return &Char{Value: r}, nil
case token.Less:
if o.Value < rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.Greater:
if o.Value > rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.LessEq:
if o.Value <= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.GreaterEq:
if o.Value >= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
case *Int:
switch op {
case token.Add:
r := o.Value + rune(rhs.Value)
if r == o.Value {
return o, nil
}
return &Char{Value: r}, nil
case token.Sub:
r := o.Value - rune(rhs.Value)
if r == o.Value {
return o, nil
}
return &Char{Value: r}, nil
case token.Less:
if int64(o.Value) < rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.Greater:
if int64(o.Value) > rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.LessEq:
if int64(o.Value) <= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.GreaterEq:
if int64(o.Value) >= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
}
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Char) Copy() Object {
return &Char{Value: o.Value}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Char) IsFalsy() bool {
return o.Value == 0
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Char) Equals(x Object) bool {
t, ok := x.(*Char)
if !ok {
return false
}
return o.Value == t.Value
}
// CompiledFunction represents a compiled function.
type CompiledFunction struct {
ObjectImpl
Instructions []byte
NumLocals int // number of local variables (including function parameters)
NumParameters int
VarArgs bool
SourceMap map[int]parser.Pos
Free []*ObjectPtr
}
// TypeName returns the name of the type.
func (o *CompiledFunction) TypeName() string {
return "compiled-function"
}
func (o *CompiledFunction) String() string {
return "<compiled-function>"
}
// Copy returns a copy of the type.
func (o *CompiledFunction) Copy() Object {
return &CompiledFunction{
Instructions: append([]byte{}, o.Instructions...),
NumLocals: o.NumLocals,
NumParameters: o.NumParameters,
VarArgs: o.VarArgs,
Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers
}
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *CompiledFunction) Equals(_ Object) bool {
return false
}
// SourcePos returns the source position of the instruction at ip.
func (o *CompiledFunction) SourcePos(ip int) parser.Pos {
for ip >= 0 {
if p, ok := o.SourceMap[ip]; ok {
return p
}
ip--
}
return parser.NoPos
}
// CanCall returns whether the Object can be Called.
func (o *CompiledFunction) CanCall() bool {
return true
}
// Error represents an error value.
type Error struct {
ObjectImpl
Value Object
}
// TypeName returns the name of the type.
func (o *Error) TypeName() string {
return "error"
}
func (o *Error) String() string {
if o.Value != nil {
return fmt.Sprintf("error: %s", o.Value.String())
}
return "error"
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Error) IsFalsy() bool {
return true // error is always false.
}
// Copy returns a copy of the type.
func (o *Error) Copy() Object {
return &Error{Value: o.Value.Copy()}
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Error) Equals(x Object) bool {
return o == x // pointer equality
}
// IndexGet returns an element at a given index.
func (o *Error) IndexGet(index Object) (res Object, err error) {
if strIdx, _ := ToString(index); strIdx != "value" {
err = ErrInvalidIndexOnError
return
}
res = o.Value
return
}
// Float represents a floating point number value.
type Float struct {
ObjectImpl
Value float64
}
func (o *Float) String() string {
return strconv.FormatFloat(o.Value, 'f', -1, 64)
}
// TypeName returns the name of the type.
func (o *Float) TypeName() string {
return "float"
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *Float) BinaryOp(op token.Token, rhs Object) (Object, error) {
switch rhs := rhs.(type) {
case *Float:
switch op {
case token.Add:
r := o.Value + rhs.Value
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Sub:
r := o.Value - rhs.Value
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Mul:
r := o.Value * rhs.Value
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Quo:
r := o.Value / rhs.Value
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Less:
if o.Value < rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.Greater:
if o.Value > rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.LessEq:
if o.Value <= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
case token.GreaterEq:
if o.Value >= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
case *Int:
switch op {
case token.Add:
r := o.Value + float64(rhs.Value)
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Sub:
r := o.Value - float64(rhs.Value)
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Mul:
r := o.Value * float64(rhs.Value)
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Quo:
r := o.Value / float64(rhs.Value)
if r == o.Value {
return o, nil
}
return &Float{Value: r}, nil
case token.Less:
if o.Value < float64(rhs.Value) {
return TrueValue, nil
}
return FalseValue, nil
case token.Greater:
if o.Value > float64(rhs.Value) {
return TrueValue, nil
}
return FalseValue, nil
case token.LessEq:
if o.Value <= float64(rhs.Value) {
return TrueValue, nil
}
return FalseValue, nil
case token.GreaterEq:
if o.Value >= float64(rhs.Value) {
return TrueValue, nil
}
return FalseValue, nil
}
}
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Float) Copy() Object {
return &Float{Value: o.Value}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Float) IsFalsy() bool {
return math.IsNaN(o.Value)
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Float) Equals(x Object) bool {
t, ok := x.(*Float)
if !ok {
return false
}
return o.Value == t.Value
}
// ImmutableArray represents an immutable array of objects.
type ImmutableArray struct {
ObjectImpl
Value []Object
}
// TypeName returns the name of the type.
func (o *ImmutableArray) TypeName() string {
return "immutable-array"
}
func (o *ImmutableArray) String() string {
var elements []string
for _, e := range o.Value {
elements = append(elements, e.String())
}
return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
}
// BinaryOp returns another object that is the result of a given binary
// operator and a right-hand side object.
func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error) {
if rhs, ok := rhs.(*ImmutableArray); ok {
switch op {
case token.Add:
return &Array{Value: append(o.Value, rhs.Value...)}, nil
}
}
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *ImmutableArray) Copy() Object {
var c []Object
for _, elem := range o.Value {
c = append(c, elem.Copy())
}
return &Array{Value: c}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *ImmutableArray) IsFalsy() bool {
return len(o.Value) == 0
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *ImmutableArray) Equals(x Object) bool {
var xVal []Object
switch x := x.(type) {
case *Array:
xVal = x.Value
case *ImmutableArray:
xVal = x.Value
default:
return false
}
if len(o.Value) != len(xVal) {
return false
}
for i, e := range o.Value {
if !e.Equals(xVal[i]) {
return false
}
}
return true
}
// IndexGet returns an element at a given index.
func (o *ImmutableArray) IndexGet(index Object) (res Object, err error) {
intIdx, ok := index.(*Int)
if !ok {
err = ErrInvalidIndexType
return
}
idxVal := int(intIdx.Value)
if idxVal < 0 || idxVal >= len(o.Value) {
res = UndefinedValue
return
}
res = o.Value[idxVal]
return
}
// Iterate creates an array iterator.
func (o *ImmutableArray) Iterate() Iterator {
return &ArrayIterator{
v: o.Value,
l: len(o.Value),
}
}
// CanIterate returns whether the Object can be Iterated.
func (o *ImmutableArray) CanIterate() bool {
return true
}
// ImmutableMap represents an immutable map object.
type ImmutableMap struct {
ObjectImpl
Value map[string]Object
}
// TypeName returns the name of the type.
func (o *ImmutableMap) TypeName() string {
return "immutable-map"
}
func (o *ImmutableMap) String() string {
var pairs []string
for k, v := range o.Value {
pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
}
return fmt.Sprintf("{%s}", strings.Join(pairs, ", "))
}
// Copy returns a copy of the type.
func (o *ImmutableMap) Copy() Object {
c := make(map[string]Object)
for k, v := range o.Value {
c[k] = v.Copy()
}
return &Map{Value: c}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *ImmutableMap) IsFalsy() bool {
return len(o.Value) == 0
}
// IndexGet returns the value for the given key.
func (o *ImmutableMap) IndexGet(index Object) (res Object, err error) {
strIdx, ok := ToString(index)
if !ok {
err = ErrInvalidIndexType
return
}
res, ok = o.Value[strIdx]
if !ok {
res = UndefinedValue
}
return
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *ImmutableMap) Equals(x Object) bool {
var xVal map[string]Object
switch x := x.(type) {
case *Map:
xVal = x.Value
case *ImmutableMap:
xVal = x.Value
default:
return false
}
if len(o.Value) != len(xVal) {
return false
}
for k, v := range o.Value {
tv := xVal[k]
if !v.Equals(tv) {
return false
}
}
return true
}
// Iterate creates an immutable map iterator.
func (o *ImmutableMap) Iterate() Iterator {
var keys []string
for k := range o.Value {
keys = append(keys, k)
}
return &MapIterator{
v: o.Value,
k: keys,
l: len(keys),
}
}
// CanIterate returns whether the Object can be Iterated.
func (o *ImmutableMap) CanIterate() bool {
return true
}
// Int represents an integer value.
type Int struct {
ObjectImpl
Value int64
}
func (o *Int) String() string {
return strconv.FormatInt(o.Value, 10)
}
// TypeName returns the name of the type.