/
Parcel.pir
292 lines (215 loc) Β· 5.22 KB
/
Parcel.pir
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
=head1 TITLE
Parcel - Perl 6 Parcel class
=head1 DESCRIPTION
This file implements Parcels, which holds a sequence of
elements and can be flattened into Captures or Lists.
=head2 Methods
=over 4
=cut
.namespace ['Parcel']
.sub 'onload' :anon :init :load
.local pmc p6meta, parcelproto, pos_role
p6meta = get_hll_global ['Mu'], '$!P6META'
parcelproto = p6meta.'new_class'('Parcel', 'parent'=>'parrot;ResizablePMCArray Iterable')
.end
.sub '' :vtable('get_string') :method
$S0 = self.'Str'()
.return ($S0)
.end
=item defined()
=cut
.sub 'defined' :method
$I0 = elements self
.tailcall '&prefix:<?>'($I0)
.end
.sub '' :vtable('defined')
$I0 = elements self
.return ($I0)
.end
=item item()
A Parcel in item context becomes a Seq.
=cut
.sub 'item' :method
.local pmc seq, flat, rest
seq = get_hll_global 'Seq'
seq = seq.'new'(self)
seq.'eager'()
.return (seq)
.end
=item hash()
=cut
.sub 'hash' :method
.tailcall 'hash'(self)
.end
=item iterator()
Construct an iterator for the Parcel.
=cut
.namespace ['Parcel']
.sub 'iterator' :method
.local pmc parceliter, rpa
parceliter = new ['ParcelIter']
rpa = root_new ['parrot';'ResizablePMCArray']
splice rpa, self, 0, 0
setattribute parceliter, '$!parcel', rpa
.return (parceliter)
.end
=item perl
=cut
.namespace ['Parcel']
.sub 'perl' :method
.local pmc self_it, perllist
self_it = iter self
perllist = new ['ResizablePMCArray']
self_loop:
unless self_it goto self_done
$P0 = shift self_it
$S0 = $P0.'perl'()
push perllist, $S0
goto self_loop
self_done:
$I0 = elements perllist
unless $I0 == 1 goto have_perllist
push perllist, ''
have_perllist:
$S0 = join ', ', perllist
$S0 = concat '(', $S0
$S0 = concat $S0, ')'
.return ($S0)
.end
=item Bool()
=cut
.sub 'Bool' :method
$I0 = istrue self
.tailcall '&prefix:<?>'($I0)
.end
=item Capture()
Coerce the Parcel into a capture.
=cut
.namespace ['Parcel']
.sub 'Capture' :method
.local pmc self_it, pos, named
self_it = iter self
pos = root_new ['parrot';'ResizablePMCArray']
named = root_new ['parrot';'Hash']
self_loop:
unless self_it goto self_done
$P0 = shift self_it
$I0 = isa $P0, 'Enum'
if $I0 goto to_named
push pos, $P0
goto self_loop
to_named:
$P1 = $P0.'key'()
$P2 = $P0.'value'()
named[$P1] = $P2
goto self_loop
self_done:
$P0 = get_hll_global 'Capture'
$P0 = $P0.'new'(pos :flat, named :flat :named)
.return ($P0)
.end
=item !FETCH()
=cut
.sub '!FETCH' :method
$P0 = self.'flat'()
$P0 = new ['Perl6Scalar'], $P0
.return ($P0)
.end
=back
=head2 Functions
=over 4
=item &eager
Return a Parcel containing the eager evaluation of all items
in a list.
=cut
.namespace []
.sub '&eager'
.param pmc args :slurpy
.local pmc parcel
parcel = new ['Parcel']
splice parcel, args, 0, 0
$P0 = parcel.'flat'()
$P0 = $P0.'eager'()
.return ($P0)
.end
=item &Nil
The canonical function for creating an empty Parcel.
=cut
.namespace []
.sub '&Nil'
.param pmc args :slurpy
$P0 = '&infix:<,>'()
.return ($P0)
.end
=back
=head2 Operators
=over 4
=item &infix:<,>
The canonical operator for creating a Parcel.
=cut
.namespace []
.sub '&infix:<,>'
.param pmc args :slurpy
# Recast the arguments into a Parcel object
.local pmc parcel
parcel = new ['Parcel']
transform_to_p6opaque parcel
splice parcel, args, 0, 0
# treat parcel itself as rw (for list assignment)
$P0 = get_hll_global ['Bool'], 'True'
setprop parcel, 'rw', $P0
.return (parcel)
.end
=back
=head2 Private methods
=over 4
=item !STORE(source)
Handle assignment to a Parcel (list assignment).
=cut
.namespace ['Parcel']
.sub '!STORE' :method
.param pmc source
# First, create a flattening Array from C<source>. This
# creates # copies of values in C<source>, in case any lvalue target
# containers in C<self> also happen to be listed as rvalues
# in C<source>. (Creating a copy of everything in C<source>
# likely isn't the most efficient approach to this, but it
# works for now.)
source = '&circumfix:<[ ]>'(source)
# Now, loop through targets of C<self>, storing the corresponding
# values from source and flattening any RPAs we encounter. If a
# target is an array or hash, then it will end up consuming all
# of the remaining elements of source.
.local pmc targets
targets = root_new ['parrot';'ResizablePMCArray']
splice targets, self, 0, 0
store_loop:
unless targets goto store_done
.local pmc cont
cont = shift targets
$I0 = isa cont, ['ResizablePMCArray']
if $I0 goto store_rpa
$P0 = getprop 'scalar', cont
if null $P0 goto store_array
unless $P0 goto store_array
store_scalar:
$P0 = source.'shift'()
cont.'!STORE'($P0)
goto store_loop
store_array:
store_hash:
cont.'!STORE'(source)
source = '&circumfix:<[ ]>'()
goto store_loop
store_rpa:
splice targets, cont, 0, 0
goto store_loop
store_done:
.return (self)
.end
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: