File tree 3 files changed +34
-0
lines changed
3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,21 @@ func IndexInt(a Object) int {
67
67
return intI
68
68
}
69
69
70
+ // As IndexInt but if index is -ve addresses it from the end
71
+ //
72
+ // If index is out of range throws IndexError
73
+ func IndexIntCheck (a Object , max int ) int {
74
+ i := IndexInt (a )
75
+ if i < 0 {
76
+ i += max
77
+ }
78
+ if i < 0 || i >= max {
79
+ // FIXME IndexError
80
+ panic ("IndexError: list index out of range" )
81
+ }
82
+ return i
83
+ }
84
+
70
85
// Return the result of not a
71
86
func Not (a Object ) Object {
72
87
switch MakeBool (a ) {
Original file line number Diff line number Diff line change @@ -35,9 +35,22 @@ func (t List) M__iter__() Object {
35
35
return NewIterator (t )
36
36
}
37
37
38
+ func (t List ) M__getitem__ (key Object ) Object {
39
+ i := IndexIntCheck (key , len (t ))
40
+ return t [i ]
41
+ }
42
+
43
+ func (t List ) M__setitem__ (key , value Object ) Object {
44
+ i := IndexIntCheck (key , len (t ))
45
+ t [i ] = value
46
+ return None
47
+ }
48
+
38
49
// Check interface is satisfied
39
50
var _ I__len__ = List (nil )
40
51
var _ I__bool__ = List (nil )
41
52
var _ I__iter__ = List (nil )
53
+ var _ I__getitem__ = List (nil )
54
+ var _ I__setitem__ = List (nil )
42
55
43
56
// var _ richComparison = List(nil)
Original file line number Diff line number Diff line change @@ -34,9 +34,15 @@ func (t Tuple) M__iter__() Object {
34
34
return NewIterator (t )
35
35
}
36
36
37
+ func (t Tuple ) M__getitem__ (key Object ) Object {
38
+ i := IndexIntCheck (key , len (t ))
39
+ return t [i ]
40
+ }
41
+
37
42
// Check interface is satisfied
38
43
var _ I__len__ = Tuple (nil )
39
44
var _ I__bool__ = Tuple (nil )
40
45
var _ I__iter__ = Tuple (nil )
46
+ var _ I__getitem__ = Tuple (nil )
41
47
42
48
// var _ richComparison = Tuple(nil)
You can’t perform that action at this time.
0 commit comments