-
Notifications
You must be signed in to change notification settings - Fork 138
/
arrayiterator.pmc
691 lines (432 loc) · 12.5 KB
/
arrayiterator.pmc
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
/*
Copyright (C) 2001-2011, Parrot Foundation.
=head1 NAME
src/pmc/arrayiterator.pmc - Implementation of Iterator for Arrays.
=head1 DESCRIPTION
Generic iterator for traversing arrays.
=head1 SYNOPSIS
=head2 default usage
.local pmc iterator, array, entry
iterator = iter array
iter_loop:
unless iterator, iter_end # while (more values)
entry = shift iterator # get an entry
...
goto iter_loop
iter_end:
=head2 iterate from the end, for arrays
.local pmc iterator, array, entry
iterator = iter array
iterator = .ITERATE_FROM_END
iter_loop:
unless iterator, iter_end # while (more values)
entry = pop iterator # get an entry
...
goto iter_loop
iter_end:
=head2 Warning!
NB: for different direction you have to use different ops!
TODO: Discuss idea of having separate get_iter/get_reverse_iter VTABLEs
to avoid this caveat.
=head1 Vtable functions
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_DOES_NOT_RETURN
static void out_of_bounds(PARROT_INTERP)
__attribute__nonnull__(1);
#define ASSERT_ARGS_out_of_bounds __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
pmclass ArrayIterator extends Iterator provides iterator no_ro auto_attrs {
ATTR PMC *array; /* the array which this Iterator iterates */
ATTR INTVAL pos; /* Current position of iterator for forward iterator */
/* Previous position of iterator for reverse iterator */
ATTR INTVAL length; /* Length of C<array> */
ATTR INTVAL reverse; /* Direction of iteration. 1 - for reverse iteration */
/*
=item C<void init()>
Raises an exception. Use C<init_pmc()>.
=cut
*/
VTABLE void init() {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
"ArrayIterator init without aggregate");
}
/*
=item C<void init_pmc(PMC *initializer)>
Initializes the iterator with an aggregate PMC.
Defaults iteration mode to iterate from start.
=cut
*/
VTABLE void init_pmc(PMC *array) {
SET_ATTR_array(INTERP, SELF, array);
PObj_custom_mark_SET(SELF);
/* by default, iterate from start */
STATICSELF.set_integer_native(ITERATE_FROM_START);
}
/*
=item C<void mark()>
Marks the current idx/key and the aggregate as live.
=cut
*/
VTABLE void mark() {
PMC *array;
GET_ATTR_array(INTERP, SELF, array);
Parrot_gc_mark_PMC_alive(INTERP, array);
}
/*
=item C<PMC *clone()>
=cut
*/
VTABLE PMC* clone() {
INTVAL pos, reverse;
PMC *array;
PMC *clone;
GET_ATTR_array(INTERP, SELF, array);
GET_ATTR_pos(INTERP, SELF, pos);
GET_ATTR_reverse(INTERP, SELF, reverse);
clone = Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, array);
SET_ATTR_pos(INTERP, clone, pos);
SET_ATTR_reverse(INTERP, clone, reverse);
return clone;
}
/*
=item C<INTVAL get_bool()>
Returns true if there is more elements to iterate over.
=cut
*/
VTABLE INTVAL get_bool() {
return STATICSELF.elements() > 0;
}
/*
=item C<INTVAL elements()>
Returns the number of remaining elements in the array.
=cut
*/
VTABLE INTVAL elements() {
INTVAL reverse;
GET_ATTR_reverse(INTERP, SELF, reverse);
if (reverse) {
INTVAL pos;
GET_ATTR_pos(INTERP, SELF, pos);
return pos;
}
else {
INTVAL pos, length;
GET_ATTR_length(INTERP, SELF, length);
GET_ATTR_pos(INTERP, SELF, pos);
return length - pos;
}
}
VTABLE INTVAL get_integer() {
return STATICSELF.elements();
}
/*
=item C<void set_integer_native(INTVAL value)>
Reset the Iterator. C<value> must be one of
ITERATE_FROM_START ... Iterate from start
ITERATE_FROM_END ... Iterate from end
=cut
*/
VTABLE void set_integer_native(INTVAL value) {
PMC *array;
INTVAL element;
GET_ATTR_array(INTERP, SELF, array);
element = VTABLE_elements(INTERP, array);
switch (value) {
case ITERATE_FROM_START:
SET_ATTR_reverse(INTERP, SELF, 0);
SET_ATTR_pos(INTERP, SELF, 0);
SET_ATTR_length(INTERP, SELF, element);
break;
case ITERATE_FROM_END:
SET_ATTR_reverse(INTERP, SELF, 1);
SET_ATTR_length(INTERP, SELF, element);
SET_ATTR_pos(INTERP, SELF, element);
break;
default:
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
"Wrong direction for ArrayIterator");
}
}
/*
=item C<PMC *get_pmc()>
Returns this Iterator's array.
=cut
*/
VTABLE PMC *get_pmc() {
PMC *array;
GET_ATTR_array(INTERP, SELF, array);
return array ? array : PMCNULL;
}
/*
=item C<INTVAL shift_integer()>
Returns the element for the current idx and sets the idx to
the next one.
=cut
*/
VTABLE INTVAL shift_integer() {
INTVAL pos, length;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
GET_ATTR_length(INTERP, SELF, length);
if (pos >= length)
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, pos+1);
return VTABLE_get_integer_keyed_int(INTERP, array, pos);
}
/*
=item C<FLOATVAL shift_float()>
=cut
*/
VTABLE FLOATVAL shift_float() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, pos+1);
return VTABLE_get_number_keyed_int(INTERP, array, pos);
}
/*
=item C<STRING *shift_string()>
=cut
*/
VTABLE STRING *shift_string() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, pos+1);
return VTABLE_get_string_keyed_int(INTERP, array, pos);
}
/*
=item C<PMC *shift_pmc()>
Returns the element for the current idx/key and sets the idx/key to
the next one.
=cut
*/
VTABLE PMC *shift_pmc() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, pos+1);
return VTABLE_get_pmc_keyed_int(INTERP, array, pos);
}
/*
=item C<INTVAL pop_integer()>
Returns the element for the current idx and sets the idx to
the next one.
=cut
*/
VTABLE INTVAL pop_integer() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, --pos);
return VTABLE_get_integer_keyed_int(INTERP, array, pos);
}
/*
=item C<FLOATVAL pop_float()>
=cut
*/
VTABLE FLOATVAL pop_float() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, --pos);
return VTABLE_get_number_keyed_int(INTERP, array, pos);
}
/*
=item C<STRING *pop_string()>
=cut
*/
VTABLE STRING *pop_string() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, --pos);
return VTABLE_get_string_keyed_int(INTERP, array, pos);
}
/*
=item C<PMC *pop_pmc()>
Returns the element for the current idx/key and sets the idx/key to
the next one.
=cut
*/
VTABLE PMC *pop_pmc() {
INTVAL pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
if (!STATICSELF.get_bool())
out_of_bounds(INTERP);
GET_ATTR_array(INTERP, SELF, array);
SET_ATTR_pos(INTERP, SELF, --pos);
return VTABLE_get_pmc_keyed_int(INTERP, array, pos);
}
/*
=item C<PMC *get_pmc_keyed(PMC *key)>
Returns the element for C<*key>.
=cut
*/
VTABLE PMC *get_pmc_keyed(PMC *key) {
return STATICSELF.get_pmc_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<PMC *get_pmc_keyed_int(INTVAL key)>
Returns the element for C<key>.
=cut
*/
VTABLE PMC *get_pmc_keyed_int(INTVAL idx) {
INTVAL pos;
GET_ATTR_pos(INTERP, SELF, pos);
return VTABLE_get_pmc_keyed_int(INTERP, STATICSELF.get_pmc(),
pos + idx);
}
/*
=item C<INTVAL get_integer_keyed(PMC *key)>
=cut
*/
VTABLE INTVAL get_integer_keyed(PMC *key) {
return STATICSELF.get_integer_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<INTVAL get_integer_keyed_int(INTVAL idx)>
Get integer value of current position plus idx.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL idx) {
INTVAL pos;
GET_ATTR_pos(INTERP, SELF, pos);
return VTABLE_get_integer_keyed_int(INTERP, STATICSELF.get_pmc(),
pos + idx);
}
/*
=item C<FLOATVAL get_number_keyed(PMC *key)>
=cut
*/
VTABLE FLOATVAL get_number_keyed(PMC *key) {
return STATICSELF.get_number_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<FLOATVAL get_number_keyed_int(INTVAL idx)>
Get number value of current position plus idx.
=cut
*/
VTABLE FLOATVAL get_number_keyed_int(INTVAL idx) {
INTVAL pos;
GET_ATTR_pos(INTERP, SELF, pos);
return VTABLE_get_number_keyed_int(INTERP, STATICSELF.get_pmc(),
pos + idx);
}
/*
=item C<STRING *get_string_keyed(PMC *key)>
=cut
*/
VTABLE STRING *get_string_keyed(PMC *key) {
return STATICSELF.get_string_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<STRING *get_string_keyed_int(INTVAL idx)>
Get string value of current position plus idx.
=cut
*/
VTABLE STRING *get_string_keyed_int(INTVAL idx) {
INTVAL pos;
GET_ATTR_pos(INTERP, SELF, pos);
return VTABLE_get_string_keyed_int(INTERP, STATICSELF.get_pmc(),
pos + idx);
}
/*
=item C<INTVAL exists_keyed(PMC *key)>
Returns whether an element for C<*key> exists in the array.
=cut
*/
VTABLE INTVAL exists_keyed(PMC *key) {
return STATICSELF.exists_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<INTVAL exists_keyed_int(INTVAL idx)>
Returns whether an element for C<idx> exists in the aggregate.
=cut
*/
VTABLE INTVAL exists_keyed_int(INTVAL idx) {
INTVAL pos, reverse, final_pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
GET_ATTR_reverse(INTERP, SELF, reverse);
GET_ATTR_array(INTERP, SELF, array);
/* Cheat! */
final_pos = pos + idx - reverse;
return VTABLE_exists_keyed_int(INTERP, array, final_pos);
}
/*
=item C<INTVAL defined_keyed(PMC *key)>
=cut
*/
VTABLE INTVAL defined_keyed(PMC *key) {
return STATICSELF.defined_keyed_int(VTABLE_get_integer(INTERP, key));
}
/*
=item C<INTVAL defined_keyed_int(INTVAL key)>
Returns the result of calling C<defined_keyed(key)> on the aggregate.
=cut
*/
VTABLE INTVAL defined_keyed_int(INTVAL idx) {
INTVAL pos, reverse, final_pos;
PMC *array;
GET_ATTR_pos(INTERP, SELF, pos);
GET_ATTR_reverse(INTERP, SELF, reverse);
GET_ATTR_array(INTERP, SELF, array);
/* Cheat! */
final_pos = pos + idx - reverse;
return VTABLE_defined_keyed_int(INTERP, array, final_pos);
}
}
/*
=back
=head1 Auxiliar functions
=over 4
=item C<static void out_of_bounds(PARROT_INTERP)>
Throw out-of-bounds exception.
=cut
*/
PARROT_DOES_NOT_RETURN
static void
out_of_bounds(PARROT_INTERP)
{
ASSERT_ARGS(out_of_bounds)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"StopIteration");
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/