-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
369 lines (300 loc) · 6.94 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
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
package charlang
import (
"sync"
)
// Iterator wraps the methods required to iterate Objects in VM.
type Iterator interface {
// 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
}
// iteratorObject is used in VM to make an iterable Object.
type iteratorObject struct {
ObjectImpl
Iterator
}
var _ Object = (*iteratorObject)(nil)
// ArrayIterator represents an iterator for the array.
type ArrayIterator struct {
V Array
i int
}
var _ Iterator = (*ArrayIterator)(nil)
// Next implements Iterator interface.
func (it *ArrayIterator) Next() bool {
it.i++
return it.i-1 < len(it.V)
}
// Key implements Iterator interface.
func (it *ArrayIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *ArrayIterator) Value() Object {
i := it.i - 1
if i > -1 && i < len(it.V) {
return it.V[i]
}
return Undefined
}
// BytesIterator represents an iterator for the bytes.
type BytesIterator struct {
V Bytes
i int
}
var _ Iterator = (*BytesIterator)(nil)
// Next implements Iterator interface.
func (it *BytesIterator) Next() bool {
it.i++
return it.i-1 < len(it.V)
}
// Key implements Iterator interface.
func (it *BytesIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *BytesIterator) Value() Object {
i := it.i - 1
if i > -1 && i < len(it.V) {
return Byte(it.V[i])
}
return Undefined
}
// CharsIterator represents an iterator for the chars.
type CharsIterator struct {
V Chars
i int
}
var _ Iterator = (*CharsIterator)(nil)
// Next implements Iterator interface.
func (it *CharsIterator) Next() bool {
it.i++
return it.i-1 < len(it.V)
}
// Key implements Iterator interface.
func (it *CharsIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *CharsIterator) Value() Object {
i := it.i - 1
if i > -1 && i < len(it.V) {
return Char(it.V[i])
}
return Undefined
}
// MapIterator represents an iterator for the map.
type MapIterator struct {
V Map
keys []string
i int
}
var _ Iterator = (*MapIterator)(nil)
// Next implements Iterator interface.
func (it *MapIterator) Next() bool {
it.i++
return it.i-1 < len(it.keys)
}
// Key implements Iterator interface.
func (it *MapIterator) Key() Object {
return ToStringObject(it.keys[it.i-1])
}
// Value implements Iterator interface.
func (it *MapIterator) Value() Object {
v, ok := it.V[it.keys[it.i-1]]
if !ok {
return Undefined
}
return v
}
// OrderedMapIterator represents an iterator for the map.
type OrderedMapIterator struct {
V *OrderedMap
keys []string
i int
}
var _ Iterator = (*OrderedMapIterator)(nil)
// Next implements Iterator interface.
func (it *OrderedMapIterator) Next() bool {
it.i++
return it.i-1 < len(it.keys)
}
// Key implements Iterator interface.
func (it *OrderedMapIterator) Key() Object {
return ToStringObject(it.keys[it.i-1])
}
// Value implements Iterator interface.
func (it *OrderedMapIterator) Value() Object {
v, ok := it.V.Value.Get(it.keys[it.i-1])
if !ok {
return Undefined
}
return v.(Object)
}
// SyncIterator represents an iterator for the SyncMap.
type SyncIterator struct {
mu sync.Mutex
Iterator
}
// Next implements Iterator interface.
func (it *SyncIterator) Next() bool {
it.mu.Lock()
defer it.mu.Unlock()
return it.Iterator.Next()
}
// Key implements Iterator interface.
func (it *SyncIterator) Key() Object {
it.mu.Lock()
defer it.mu.Unlock()
return it.Iterator.Key()
}
// Value implements Iterator interface.
func (it *SyncIterator) Value() Object {
it.mu.Lock()
defer it.mu.Unlock()
return it.Iterator.Value()
}
// StringIterator represents an iterator for the string.
type StringIterator struct {
V String
i int
// k int
// r rune
}
var _ Iterator = (*StringIterator)(nil)
// Next implements Iterator interface.
func (it *StringIterator) Next() bool {
if it.i > len(it.V.Value)-1 {
return false
}
// r, s := utf8.DecodeRuneInString(it.V.Value[it.i:])
// if r == utf8.RuneError || s == 0 {
// return false
// }
// it.k = it.i
// it.r = r
// it.i += s
it.i++
return true
}
// Key implements Iterator interface.
func (it *StringIterator) Key() Object {
// return Int(it.k)
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *StringIterator) Value() Object {
// return Char(it.r)
i := it.i - 1
if i > -1 && i < len(it.V.Value) {
return Byte(it.V.Value[i])
}
return Undefined
}
// MutableStringIterator represents an iterator for the string.
type MutableStringIterator struct {
V *MutableString
i int
// k int
// r rune
}
var _ Iterator = (*MutableStringIterator)(nil)
// Next implements Iterator interface.
func (it *MutableStringIterator) Next() bool {
if it.i > len(it.V.Value)-1 {
return false
}
// r, s := utf8.DecodeRuneInString(it.V.Value[it.i:])
// if r == utf8.RuneError || s == 0 {
// return false
// }
// it.k = it.i
// it.r = r
// it.i += s
it.i++
return true
}
// Key implements Iterator interface.
func (it *MutableStringIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *MutableStringIterator) Value() Object {
i := it.i - 1
if i > -1 && i < len(it.V.Value) {
return Byte(it.V.Value[i])
}
return Undefined
}
// IntIterator represents an iterator for the Int.
type IntIterator struct {
V Int
i int
}
var _ Iterator = (*IntIterator)(nil)
// Next implements Iterator interface.
func (it *IntIterator) Next() bool {
it.i++
return it.i-1 < int(it.V)
}
// Key implements Iterator interface.
func (it *IntIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *IntIterator) Value() Object {
i := it.i - 1
if i > -1 && i < int(it.V) {
return Int(i)
}
return Undefined
}
// StackIterator represents an iterator for the Stack.
type StackIterator struct {
V *Stack
i int
}
var _ Iterator = (*StackIterator)(nil)
// Next implements Iterator interface.
func (it *StackIterator) Next() bool {
it.i++
return it.i-1 < it.V.Value.Size()
}
// Key implements Iterator interface.
func (it *StackIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *StackIterator) Value() Object {
i := it.i - 1
if i > -1 && i < it.V.Value.Size() {
return it.V.Value.PeekLayer(i).(Object)
}
return Undefined
}
// QueueIterator represents an iterator for the Queue.
type QueueIterator struct {
V *Queue
i int
}
var _ Iterator = (*QueueIterator)(nil)
// Next implements Iterator interface.
func (it *QueueIterator) Next() bool {
it.i++
return it.i-1 < it.V.Value.Size()
}
// Key implements Iterator interface.
func (it *QueueIterator) Key() Object {
return Int(it.i - 1)
}
// Value implements Iterator interface.
func (it *QueueIterator) Value() Object {
i := it.i - 1
if i > -1 && i < it.V.Value.Size() {
return it.V.Value.Get(i).(Object)
}
return Undefined
}