/
op.pir
429 lines (327 loc) Β· 7.11 KB
/
op.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
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
## $Id$
=head1 NAME
src/builtins/op.pir - Perl6 builtin operators
=head1 Functions
=over 4
=cut
.namespace []
## autoincrement
.sub 'postfix:++' :multi(_)
.param pmc a
$P0 = clone a
inc a
.return ($P0)
.end
.sub 'postfix:--' :multi(_)
.param pmc a
$P0 = clone a
dec a
.return ($P0)
.end
.sub 'prefix:++' :multi(_)
.param pmc a
inc a
.return (a)
.end
.sub 'prefix:--' :multi(_)
.param pmc a
dec a
.return (a)
.end
## exponentiation
.sub 'infix:**' :multi(_,_)
.param num base
.param num exp
$N0 = pow base, exp
.return ($N0)
.end
## symbolic unary
.sub 'prefix:!' :multi(_)
.param pmc a
if a goto a_true
$P0 = get_hll_global ['Bool'], 'True'
.return ($P0)
a_true:
$P0 = get_hll_global ['Bool'], 'False'
.return ($P0)
.end
.sub 'prefix:+' :multi(_)
.param num a
.return (a)
.end
.sub 'prefix:+' :multi('Integer')
.param int a
.return (a)
.end
.sub 'prefix:-' :multi(_)
.param num a
$N0 = neg a
.return ($N0)
.end
.sub 'prefix:-' :multi('Integer')
.param int a
$I0 = neg a
.return ($I0)
.end
.sub 'prefix:~' :multi(_)
.param string a
.return (a)
.end
.sub 'prefix:?' :multi(_)
.param pmc a
if a goto a_true
$P0 = get_hll_global ['Bool'], 'False'
.return ($P0)
a_true:
$P0 = get_hll_global ['Bool'], 'True'
.return ($P0)
.end
## TODO: prefix:= prefix:* prefix:** prefix:~^ prefix:+^
.sub 'prefix:?^' :multi(_)
.param pmc a
$I0 = isfalse a
.return ($I0)
.end
.sub 'prefix:^' :multi('P6Protoobject')
.param pmc proto
.return proto.'HOW'()
.end
.sub 'prefix:^' :multi('Integer')
.param int to
dec to
.return 'infix:..'(0, to)
.end
.sub 'prefix:^' :multi('Num')
.param num to
dec to
.return 'infix:..'(0, to)
.end
.sub 'prefix:^' :multi('List')
.param pmc list
# Iterate over the list and build a list of ranges upto the given subscripts.
.local pmc ranges, it
ranges = 'list'()
it = iter list
iter_loop:
unless it goto iter_loop_end
$P0 = shift it
$P0 = $P0 - 1
$P0 = 'infix:..'(0, $P0)
push ranges, $P0
goto iter_loop
iter_loop_end:
# Now just use cross operator to make all the permutations.
.return 'infix:X'(ranges)
.end
## multiplicative
.sub 'infix:*' :multi(_,_)
.param num a
.param num b
$N0 = a * b
.return ($N0)
.end
.sub 'infix:/' :multi(_,_)
.param num a
.param num b
$N0 = a / b
.return ($N0)
.end
.sub 'infix:%' :multi(_,_)
.param num a
.param num b
$N0 = mod a, b
.return ($N0)
.end
.sub 'infix:x' :multi(_,_)
.param string a
.param int b
$S0 = repeat a, b
.return ($S0)
.end
.sub 'infix:xx' :multi(_,_)
.param pmc a
.param int n
$P0 = 'list'()
loop:
unless n > 0 goto done
push $P0, a
dec n
goto loop
done:
.return ($P0)
.end
.sub 'infix:+&' :multi(_,_)
.param int a
.param int b
$I0 = band a, b
.return ($I0)
.end
.sub 'infix:+<' :multi(_,_)
.param int a
.param int b
$I0 = shl a, b
.return ($I0)
.end
.sub 'infix:+>' :multi(_,_)
.param int a
.param int b
$I0 = shr a, b
.return ($I0)
.end
.sub 'infix:~&' :multi(_,_)
.param string a
.param string b
$S0 = bands a, b
.return ($S0)
.end
## TODO: infix:~< infix:~>
## additive
.sub 'infix:+' :multi(_,_)
.param num a
.param num b
$N0 = a + b
.return ($N0)
.end
.sub 'infix:-' :multi(_,_)
.param num a
.param num b
$N0 = a - b
.return ($N0)
.end
.sub 'infix:~' :multi(_,_)
.param string a
.param string b
$S0 = concat a, b
.return ($S0)
.end
.sub 'infix:+|'
.param int a
.param int b
$I0 = bor a, b
.return ($I0)
.end
.sub 'infix:+^'
.param int a
.param int b
$I0 = bxor a, b
.return ($I0)
.end
.sub 'infix:~|'
.param string a
.param string b
$S0 = bors a, b
.return ($S0)
.end
.sub 'infix:~^'
.param string a
.param string b
$S0 = bxors a, b
.return ($S0)
.end
.sub 'infix:?&'
.param int a
.param int b
$I0 = band a, b
$I0 = isne $I0, 0
.return ($I0)
.end
.sub 'infix:?|'
.param int a
.param int b
$I0 = bor a, b
$I0 = isne $I0, 0
.return ($I0)
.end
.sub 'infix:?^'
.param int a
.param int b
$I0 = bxor a, b
$I0 = isne $I0, 0
.return ($I0)
.end
.sub 'true' :multi(_)
.param pmc a
.return 'prefix:?'(a)
.end
.sub 'not' :multi(_)
.param pmc a
.return 'prefix:!'(a)
.end
.sub 'infix:.?'
.param pmc invocant
.param string method_name
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# For now we won't worry about signature, just if a method exists.
$I0 = can invocant, method_name
if $I0 goto invoke
$P0 = get_hll_global 'Failure'
.return ($P0)
# If we do have a method, call it.
invoke:
.return invocant.method_name(pos_args :flat, named_args :named :flat)
.end
.sub 'infix:.*'
.param pmc invocant
.param string method_name
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# Return an empty list if no methods exist at all.
$I0 = can invocant, method_name
if $I0 goto invoke
.return 'list'()
# Now find all methods and call them - since we know there are methods,
# we just pass on to infix:.+.
invoke:
.return 'infix:.+'(invocant, method_name, pos_args :flat, named_args :named :flat)
.end
.sub 'infix:.+'
.param pmc invocant
.param string method_name
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# We need to find all methods we could call with the right name.
.local pmc p6meta, result_list, class, mro, it, cap_class, failure_class
result_list = 'list'()
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
class = invocant.'HOW'()
class = p6meta.get_parrotclass(class)
mro = inspect class, 'all_parents'
it = iter mro
cap_class = get_hll_global 'Capture'
failure_class = get_hll_global 'Failure'
mro_loop:
unless it goto mro_loop_end
.local pmc cur_class, meths, cur_meth
cur_class = shift it
meths = inspect cur_class, 'methods'
cur_meth = meths[method_name]
if null cur_meth goto mro_loop
# If we're here, found a method. Invoke it and add capture of the results
# to the result list.
.local pmc pos_res, named_res, cap
(pos_res :slurpy, named_res :named :slurpy) = cur_meth(invocant, pos_args :flat, named_args :named :flat)
cap = cap_class.'!create'(failure_class, pos_res :flat, named_res :flat :named)
push result_list, cap
goto mro_loop
mro_loop_end:
# Make sure we got some elements, or we have to die.
$I0 = elements result_list
if $I0 == 0 goto failure
.return (result_list)
failure:
$S0 = "Could not invoke method '"
concat $S0, method_name
concat $S0, "' on invocant of type '"
$S1 = invocant.WHAT()
concat $S0, $S1
concat $S0, "'"
'die'($S0)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: