/
iterator.pmc
229 lines (148 loc) · 3.86 KB
/
iterator.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
/*
Copyright (C) 2001-2009, Parrot Foundation.
=head1 NAME
src/pmc/iterator.pmc - Iterator PMC
=head1 DESCRIPTION
These are the vtable functions for the Iterator base class. Iterators are
used in combination with other classes(mainly aggregates) to visit all entries
in that aggregate.
=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 iterate over a hash
.local pmc iterator, hash, key, entry
iterator = iter hash
iter_loop:
unless iterator, iter_end # while (more values)
key = shift iterator # get the key..
entry = hash[key]
...
goto iter_loop
iter_end:
=head1 Methods
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass Iterator no_ro provides iterator {
/*
=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,
"Iterator 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 *aggregate) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
"Direct creation of Iterator");
}
/*
=item C<INTVAL get_integer_keyed(PMC *key)>
=cut
*/
VTABLE INTVAL get_integer_keyed(PMC *key) {
return VTABLE_get_integer_keyed(INTERP, SELF.get_pmc(), key);
}
/*
=item C<FLOATVAL get_number_keyed(PMC *key)>
=cut
*/
VTABLE FLOATVAL get_number_keyed(PMC *key) {
return VTABLE_get_number_keyed(INTERP, SELF.get_pmc(), key);
}
/*
=item C<STRING *get_string_keyed(PMC *key)>
=cut
*/
VTABLE STRING *get_string_keyed(PMC *key) {
return VTABLE_get_string_keyed(INTERP, SELF.get_pmc(), key);
}
/*
=item C<PMC *get_pmc_keyed(PMC *key)>
Returns the element for C<*key>.
=cut
*/
VTABLE PMC *get_pmc_keyed(PMC *key) {
return VTABLE_get_pmc_keyed(INTERP, SELF.get_pmc(), key);
}
/*
=item C<INTVAL exists_keyed(PMC *key)>
Returns whether an element for C<*key> exists in the aggregate.
=cut
*/
VTABLE INTVAL exists_keyed(PMC *key) {
return VTABLE_exists_keyed(INTERP, SELF.get_pmc(), key);
}
/*
=item C<INTVAL defined()>
Returns whether the iterator contains an aggregate.
=cut
*/
VTABLE INTVAL defined() {
return (INTVAL)(SELF.get_pmc() != PMCNULL);
}
/*
=item C<INTVAL defined_keyed(PMC *key)>
=cut
*/
VTABLE INTVAL defined_keyed(PMC *key) {
return VTABLE_defined_keyed(INTERP, SELF.get_pmc(), key);
}
VTABLE PMC *get_iter() {
UNUSED(INTERP);
return SELF;
}
/* Make the shift_pmc vtable function available as
* a method named "next" */
METHOD next() {
PMC * const next = VTABLE_shift_pmc(INTERP, SELF);
RETURN(PMC* next);
}
/*
=item C<void set_integer_native(INTVAL value)>
Restart iterator
=cut
*/
VTABLE void set_integer_native(INTVAL value) {
PARROT_FAILURE("Iterator: implementation have to override this method");
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
"Iterator: unimplemented method");
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/