-
Notifications
You must be signed in to change notification settings - Fork 305
/
iterator.go
209 lines (170 loc) · 4.51 KB
/
iterator.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
package tengo
// Iterator represents an iterator for underlying data type.
type Iterator interface {
Object
// Next returns true if there are more elements to iterate.
Next() bool
// Key returns the key or index value of the current element.
Key() Object
// Value returns the value of the current element.
Value() Object
}
// ArrayIterator is an iterator for an array.
type ArrayIterator struct {
ObjectImpl
v []Object
i int
l int
}
// TypeName returns the name of the type.
func (i *ArrayIterator) TypeName() string {
return "array-iterator"
}
func (i *ArrayIterator) String() string {
return "<array-iterator>"
}
// IsFalsy returns true if the value of the type is falsy.
func (i *ArrayIterator) IsFalsy() bool {
return true
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (i *ArrayIterator) Equals(Object) bool {
return false
}
// Copy returns a copy of the type.
func (i *ArrayIterator) Copy() Object {
return &ArrayIterator{v: i.v, i: i.i, l: i.l}
}
// Next returns true if there are more elements to iterate.
func (i *ArrayIterator) Next() bool {
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
func (i *ArrayIterator) Key() Object {
return &Int{Value: int64(i.i - 1)}
}
// Value returns the value of the current element.
func (i *ArrayIterator) Value() Object {
return i.v[i.i-1]
}
// BytesIterator represents an iterator for a string.
type BytesIterator struct {
ObjectImpl
v []byte
i int
l int
}
// TypeName returns the name of the type.
func (i *BytesIterator) TypeName() string {
return "bytes-iterator"
}
func (i *BytesIterator) String() string {
return "<bytes-iterator>"
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (i *BytesIterator) Equals(Object) bool {
return false
}
// Copy returns a copy of the type.
func (i *BytesIterator) Copy() Object {
return &BytesIterator{v: i.v, i: i.i, l: i.l}
}
// Next returns true if there are more elements to iterate.
func (i *BytesIterator) Next() bool {
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
func (i *BytesIterator) Key() Object {
return &Int{Value: int64(i.i - 1)}
}
// Value returns the value of the current element.
func (i *BytesIterator) Value() Object {
return &Int{Value: int64(i.v[i.i-1])}
}
// MapIterator represents an iterator for the map.
type MapIterator struct {
ObjectImpl
v map[string]Object
k []string
i int
l int
}
// TypeName returns the name of the type.
func (i *MapIterator) TypeName() string {
return "map-iterator"
}
func (i *MapIterator) String() string {
return "<map-iterator>"
}
// IsFalsy returns true if the value of the type is falsy.
func (i *MapIterator) IsFalsy() bool {
return true
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (i *MapIterator) Equals(Object) bool {
return false
}
// Copy returns a copy of the type.
func (i *MapIterator) Copy() Object {
return &MapIterator{v: i.v, k: i.k, i: i.i, l: i.l}
}
// Next returns true if there are more elements to iterate.
func (i *MapIterator) Next() bool {
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
func (i *MapIterator) Key() Object {
k := i.k[i.i-1]
return &String{Value: k}
}
// Value returns the value of the current element.
func (i *MapIterator) Value() Object {
k := i.k[i.i-1]
return i.v[k]
}
// StringIterator represents an iterator for a string.
type StringIterator struct {
ObjectImpl
v []rune
i int
l int
}
// TypeName returns the name of the type.
func (i *StringIterator) TypeName() string {
return "string-iterator"
}
func (i *StringIterator) String() string {
return "<string-iterator>"
}
// IsFalsy returns true if the value of the type is falsy.
func (i *StringIterator) IsFalsy() bool {
return true
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (i *StringIterator) Equals(Object) bool {
return false
}
// Copy returns a copy of the type.
func (i *StringIterator) Copy() Object {
return &StringIterator{v: i.v, i: i.i, l: i.l}
}
// Next returns true if there are more elements to iterate.
func (i *StringIterator) Next() bool {
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
func (i *StringIterator) Key() Object {
return &Int{Value: int64(i.i - 1)}
}
// Value returns the value of the current element.
func (i *StringIterator) Value() Object {
return &Char{Value: i.v[i.i-1]}
}