5
src/classes/Array.pir - Perl 6 Array class and related functions
10
.sub '' :anon :load :init
11
.local pmc p6meta, arrayproto
12
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
13
arrayproto = p6meta.'new_class'('Perl6Array', 'parent'=>'List', 'name'=>'Array')
14
arrayproto.'!MUTABLE'()
16
$P0 = get_hll_namespace ['Perl6Array']
17
'!EXPORT'('delete,exists,pop,push,shift,unshift', 'from'=>$P0, 'to_p6_multi'=>1)
25
Remove items from an array.
29
.namespace ['Perl6Array']
30
.sub 'delete' :method :multi() :subid('array_delete')
31
.param pmc indices :slurpy
38
unless indices goto indices_end
48
if $I0 < 0 goto shorten_end
50
if null $P0 goto do_shorten
52
if $I1 goto shorten_end
64
.local pmc block, signature
65
.const 'Sub' $P0 = "array_delete"
67
signature = new ["Signature"]
68
setprop block, "$!signature", signature
69
signature."!add_param"("@indices", 1 :named("slurpy"))
70
$P0 = get_hll_global 'Array'
71
signature."!add_implicit_self"($P0)
75
=item exists(indices :slurpy)
77
Return true if the elements at C<indices> have been assigned to.
81
.sub 'exists' :method :multi() :subid('array_exists')
82
.param pmc indices :slurpy
87
unless indices goto indices_end
89
test = exists self[$I0]
90
if test goto indices_loop
92
.tailcall 'prefix:?'(test)
95
.local pmc block, signature
96
.const 'Sub' $P0 = "array_exists"
98
signature = new ["Signature"]
99
setprop block, "$!signature", signature
100
signature."!add_param"("@indices", 1 :named("slurpy"))
101
$P0 = get_hll_global 'Array'
102
signature."!add_implicit_self"($P0)
108
Return Array in item context (i.e., self)
112
.namespace ['Perl6Array']
120
Return invocant as a List.
124
.namespace ['Perl6Array']
125
.sub '' :method('list')
126
.tailcall self.'values'()
132
Remove the last item from the array and return it.
136
.sub 'pop' :method :multi() :subid('array_pop')
138
unless self goto empty
142
x = '!FAIL'('Undefined value popped from empty array')
147
.local pmc block, signature
148
.const 'Sub' $P0 = "array_pop"
150
signature = new ["Signature"]
151
setprop block, "$!signature", signature
152
$P0 = get_hll_global 'Array'
153
signature."!add_implicit_self"($P0)
157
=item push(args :slurpy)
159
Add C<args> to the end of the Array.
163
.sub 'push' :method :multi() :subid('array_push')
164
.param pmc args :slurpy
170
unless it goto it_loop_end
172
$I0 = type.'ACCEPTS'($P0)
173
unless $I0 goto type_error
177
splice self, args, $I0, 0
180
'die'('Type check failure in push')
183
.local pmc block, signature
184
.const 'Sub' $P0 = "array_push"
186
signature = new ["Signature"]
187
setprop block, "$!signature", signature
188
signature."!add_param"("@items", 1 :named("slurpy"))
189
$P0 = get_hll_global 'Array'
190
signature."!add_implicit_self"($P0)
196
Shift the first item off the array and return it.
200
.sub 'shift' :method :multi() :subid('array_shift')
202
unless self goto empty
206
x = '!FAIL'('Undefined value shifted from empty array')
211
.local pmc block, signature
212
.const 'Sub' $P0 = "array_shift"
214
signature = new ["Signature"]
215
setprop block, "$!signature", signature
216
$P0 = get_hll_global 'Array'
217
signature."!add_implicit_self"($P0)
221
=item unshift(args :slurpy)
223
Adds C<args> to the beginning of the Array.
227
.sub 'unshift' :method :multi() :subid('array_unshift')
228
.param pmc args :slurpy
234
unless it goto it_loop_end
236
$I0 = type.'ACCEPTS'($P0)
237
unless $I0 goto type_error
240
splice self, args, 0, 0
243
'die'('Type check failure in push')
246
.local pmc block, signature
247
.const 'Sub' $P0 = "array_unshift"
249
signature = new ["Signature"]
250
setprop block, "$!signature", signature
251
signature."!add_param"("@items", 1 :named("slurpy"))
252
$P0 = get_hll_global 'Array'
253
signature."!add_implicit_self"($P0)
258
Return Array as a List of its values.
262
.namespace ['Perl6Array']
263
.sub 'values' :method
265
splice $P0, self, 0, 0
283
.param pmc values :slurpy
284
.tailcall values.'Scalar'()
290
=head2 Coercion methods
298
.namespace ['Perl6Array']
306
=head2 Private Methods
312
Return self, as Arrays are already flattened.
316
.namespace ['Perl6Array']
317
.sub '!flatten' :method
323
Store things into an Array (e.g., upon assignment)
327
.namespace ['Perl6Array']
328
.sub '!STORE' :method
330
.local pmc array, it, type
332
## we create a new array here instead of emptying self in case
333
## the source argument contains self or elements of self.
334
array = root_new ['parrot';'ResizablePMCArray']
335
source = 'list'(source)
338
unless it goto array_done
340
$I0 = type.'ACCEPTS'($P0)
341
unless $I0 goto type_error
342
$P0 = '!CALLMETHOD'('Scalar',$P0)
344
.fixup_cloned_sub($P0, $P1)
345
setprop $P1, 'type', type
350
splice self, array, 0, $I0
353
$S0 = '!make_type_fail_message'('Array assignment', $P0, type)
365
# vim: expandtab shiftwidth=4 ft=pir: