/
Array.pir
2087 lines (1599 loc) · 34.8 KB
/
Array.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# $Id$
=head1 NAME
src/classes/CardinalArray.pir - Cardinal CardinalArray class and related functions
Stolen from Rakudo
=head1 Methods
=over 4
=cut
.namespace ['CardinalArray']
.sub 'onload' :anon :load :init
.local pmc cardinalmeta, arrayproto, interp, core_type, hll_type
cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
arrayproto = cardinalmeta.'new_class'('CardinalArray', 'parent'=>'parrot;ResizablePMCArray CardinalObject')
#cardinalmeta.'register'('ResizablePMCArray', 'parent'=>'CardinalObject', 'protoobject'=>arrayproto)
core_type = get_class 'ResizablePMCArray'
hll_type = get_class 'CardinalArray'
interp = getinterp
interp.'hll_map'(core_type, hll_type)
.end
=item get_string() (vtable method)
Return the elements of the list concatenated.
=cut
.sub 'get_string' :vtable :method
$S0 = join ', ', self
$S0 = concat '[', $S0
$S0 = concat $S0, ']'
.return ($S0)
.end
.sub 'initialize' :method :multi(_)
noop
.end
.include "hllmacros.pir"
.sub 'initialize' :method :multi(_,CardinalInteger)
.param pmc size
.local pmc i
i = new 'CardinalInteger'
i = 0
$P0 = get_hll_global 'nil'
.DoWhile( {
self[i] = $P0
inc i
}, i < size)
.end
=item to_s() (method)
Return a CardinalString representing the Array.
=cut
.sub 'to_s' :method
$S0 = join '', self
$P0 = new 'CardinalString'
$P0 = $S0
.return($P0)
.end
=item clone() (vtable method)
Clones the list.
=cut
.sub 'clone' :vtable :method
$P0 = 'list'(self)
.return ($P0)
.end
=item clear() (method)
Removes all elements from the array.
=cut
.sub 'clear' :method
self = 0
.end
=item fill(value)
Fill C<self> with C<value>
Doesnt work, but it should be close...
=cut
.sub 'fill' :method
.param pmc value
.param int offset :optional
.param int length :optional
.local int i, end_index
unless offset goto set_offset
unless length goto set_length
goto do_fill
set_offset:
offset = 0
unless length goto set_length
goto do_fill
set_length:
length = elements self
length = length - offset
do_fill:
i = offset
end_index = offset + length
loop:
if i == end_index goto done
self[i] = value
inc i
goto loop
done:
.return (self)
.end
=item ACCEPTS(topic)
=cut
.sub 'ACCEPTS' :method
.param pmc topic
.local int i
.local string what
what = topic.'WHAT'()
if what == "CardinalArray" goto acc_list
goto no_match
acc_list:
# Smartmatch against another list. Smartmatch each
# element.
.local int count_1, count_2
count_1 = elements self
count_2 = elements topic
if count_1 != count_2 goto no_match
i = 0
list_cmp_loop:
if i >= count_1 goto list_cmp_loop_end
.local pmc elem_1, elem_2
elem_1 = self[i]
elem_2 = topic[i]
($I0) = elem_1.'ACCEPTS'(elem_2)
unless $I0 goto no_match
inc i
goto list_cmp_loop
list_cmp_loop_end:
goto match
no_match:
$P0 = get_hll_global ['Bool'], 'False'
.return($P0)
match:
$P0 = get_hll_global ['Bool'], 'True'
.return($P0)
.end
=item
Return the class name
=cut
.sub 'class' :method
.tailcall self.'WHAT'()
.end
=item sort()
Return a sorted copy of the list
=cut
.sub 'sort' :method
.param pmc by :optional :named('!BLOCK')
.param int has_by :opt_flag
if has_by goto have_by
by = get_hll_global 'infix:cmp'
have_by:
.local pmc fpa
.local int elems
elems = elements self
fpa = new 'FixedPMCArray'
fpa = elems
.local int i
i = 0
fpa_loop:
unless i < elems goto fpa_end
$P0 = self[i]
fpa[i] = $P0
inc i
goto fpa_loop
fpa_end:
fpa.'sort'(by)
.tailcall 'list'(fpa :flat)
.end
.sub 'sort!' :method
.param pmc by :optional
.param int has_by :opt_flag
if has_by goto have_by
by = get_hll_global 'infix:cmp'
have_by:
$P0 = self.'sort'()
self = 0
self.'concat'($P0)
.end
=item uniq(...)
=cut
.sub uniq :method
.param pmc block :optional :named("!BLOCK")
.param int block_flag :opt_flag
.local pmc uarray, hash, val, key
.local int i, len
uarray = new 'CardinalArray'
hash = new 'CardinalHash'
i = 0
len = elements self
loop:
if i == len goto done
key = self[i]
unless block_flag, finish
key = block(key)
finish:
$I0 = exists hash[key]
if $I0 goto skip
$P0 = self[i]
uarray.'push'($P0)
hash[key] = $P0
skip:
inc i
goto loop
done:
.return (uarray)
.end
.sub 'uniq!' :method
.param pmc block :optional :named("!BLOCK")
.param int block_flag :opt_flag
if block_flag goto with_block
$P0 = self.'uniq'()
goto done
with_block:
$P0 = self.'uniq'(block :named("!BLOCK"))
done:
$I0 = elements $P0
$I1 = elements self
if $I0 == $I1 goto no_change
self = 0
self.'concat'($P0)
.return (self)
no_change:
$P0 = get_hll_global 'nil'
.return ($P0)
.end
.sub reject :method
.param pmc block :named("!BLOCK")
.local pmc uarray, val
.local int i, len
uarray = new 'CardinalArray'
i = 0
len = elements self
loop:
if i == len goto done
val = self[i]
$P0 = block(val)
if $P0 goto skip
uarray.'push'(val)
skip:
inc i
goto loop
done:
.return (uarray)
.end
.sub 'reject!' :method
.param pmc block :named("!BLOCK")
$P0 = self.'reject'(block)
self = 0
self.'concat'($P0)
.end
.sub 'max' :method
$P0 = 'infix:max'(self)
.return($P0)
.end
.sub 'min' :method
$P0 = 'infix:min'(self)
.return($P0)
.end
=item include?(ELEMENT)
Return true if self contains ELEMENT
=cut
.sub 'include?' :method
.param pmc args
.local pmc it
it = iter self
iter_loop:
unless it goto done_f
$P0 = shift it
eq $P0, args, done_t
goto iter_loop
done_f:
$P0 = get_hll_global ['Bool'], 'False'
.return($P0)
done_t:
$P0 = get_hll_global ['Bool'], 'True'
.return($P0)
.end
=item index(ELEMENT)
Return index (or nil) of ELEMENT
=cut
.sub 'index' :method
.param pmc args
.local int len
len = elements self
$I0 = 0
loop:
if $I0 >= len goto done_notfound
$P0 = self[$I0]
eq $P0, args, done_found
inc $I0
goto loop
done_notfound:
$P0 = get_hll_global 'nil'
.return($P0)
done_found:
.return($I0)
.end
=item rindex(ELEMENT)
Return index (or nil) of ELEMENT, starting from the end
=cut
.sub 'rindex' :method
.param pmc args
$I0 = elements self
loop:
dec $I0
if $I0 < 0 goto done_notfound
$P0 = self[$I0]
eq $P0, args, done_found
goto loop
done_notfound:
$P0 = get_hll_global 'nil'
.return($P0)
done_found:
.return($I0)
.end
=item insert(INDEX, ELEMENT, ...)
Insert ELEMENT(s) at INDEX
=cut
.sub 'insert' :method
.param int index
.param pmc args :slurpy
.local int len
len = elements self
if index >= 0 goto skip_negative
index = index + 1
index = index + len
if index < 0 goto throw_index_exception
skip_negative:
splice self, args, index, 0
.return(self)
throw_index_exception:
.return(self)
.end
=item replace(ARRAY)
Replace current contents of array with ARRAY.
=cut
.sub 'replace' :method
.param pmc other_array
.local int len
len = elements self
splice self, other_array, 0, len
.return(self)
.end
=item
Return true is C<self> is of size 0
=cut
.sub 'empty?' :method
.local int len
len = elements self
if len == 0 goto empty
goto not_empty
empty:
$P0 = get_hll_global ['Bool'], 'True'
.return ($P0)
not_empty:
$P0 = get_hll_global ['Bool'], 'False'
.return ($P0)
.end
=item unshift(ELEMENTS)
Prepends ELEMENTS to the front of the list.
=cut
.sub 'unshift' :method
.param pmc args :slurpy
.local int narg
.local int i
narg = args
i = 0
.local pmc tmp
loop:
if i == narg goto done
pop tmp, args
unshift self, tmp
inc i
goto loop
done:
.end
=item keys()
Returns a CardinalArray containing the keys of the CardinalArray.
=cut
.sub 'keys' :method
.local pmc elem
.local pmc res
.local int len
.local int i
res = new 'CardinalArray'
len = elements self
i = 0
loop:
if i == len goto done
elem = new 'CardinalInteger'
elem = i
res.'push'(elem)
inc i
goto loop
done:
.return(res)
.end
=item values()
Returns a CardinalArray containing the values of the CardinalArray.
=cut
.sub 'values' :method
.local pmc elem
.local pmc res
.local int len
.local int i
res = new 'CardinalArray'
len = elements self
i = 0
loop:
if i == len goto done
elem = new 'CardinalInteger'
elem = self[i]
res.'push'(elem)
inc i
goto loop
done:
.return(res)
.end
=item shift()
Shifts the first item off the list and returns it.
=cut
.sub 'shift' :method
.local pmc x
x = shift self
.return (x)
.end
=item pop()
Treats the list as a stack, popping the last item off the list and returning it.
=cut
.sub 'pop' :method
.local pmc x
.local int len
len = elements self
if len == 0 goto empty
pop x, self
goto done
empty:
x = undef()
goto done
done:
.return (x)
.end
=item push(ELEMENTS)
Treats the list as a stack, pushing ELEMENTS onto the end of the list. Returns the new length of the list.
=cut
.sub 'push' :method
.param pmc args :slurpy
.local int len
.local pmc tmp
.local int i
len = args
i = 0
loop:
if i == len goto done
shift tmp, args
push self, tmp
inc i
goto loop
done:
len = elements self
.return (len)
.end
=item join(SEPARATOR)
Returns a string comprised of all of the list, separated by the string SEPARATOR. Given an empty list, join returns the empty string. SEPARATOR is an optional parameter
=cut
.sub 'join' :method
.param string sep :optional
.local string res
.local string tmp
.local int len
.local int i
res = ""
len = elements self
if len == 0 goto done
len = len - 1
i = 0
loop:
if i == len goto last
tmp = self[i]
concat res, tmp
concat res, sep
inc i
goto loop
last:
tmp = self[i]
concat res, tmp
done:
$S0 = res
.return(res)
.end
=item reverse()
Returns a list of the elements in revese order.
=cut
.sub 'reverse' :method
.local pmc res
.local int len
.local int i
res = new 'CardinalArray'
len = elements self
if len == 0 goto done
i = 0
.local pmc elem
loop:
if len == 0 goto done
dec len
elem = self[len]
res[i] = elem
inc i
goto loop
done:
.return(res)
.end
=item reverse!()
Reverses a list in place. Destructive update.
Returns self.
=cut
.sub 'reverse!' :method
.local int pos
.local int len
.local pmc tmp1
.local pmc tmp2
pos = elements self
len = pos
dec len
pos = pos / 2
loop:
if pos == 0 goto done
dec pos
tmp1 = self[pos]
$I0 = len-pos
tmp2 = self[$I0]
self[pos] = tmp2
self[$I0] = tmp1
goto loop
done:
.return(self)
.end
.sub 'compact' :method
.local pmc array
.local int i, len
array = new 'CardinalArray'
len = self
i = 0
loop:
if i == len goto done
$P0 = self[i]
$I0 = isa $P0, "NilClass"
if $I0 goto end_loop
array.'push'($P0)
end_loop:
inc i
goto loop
done:
.return (array)
.end
.sub 'compact!' :method
$P0 = self.'compact'()
self = 0
self.'concat'($P0)
.end
=item delete()
Deletes the given element from the CardinalArray, replacing them with Undef.
Returns the item if found, otherwise returns the result of running the block
if passed, otherwise returns nil.
=cut
.sub delete :method
.param pmc item
.param pmc block :optional :named('!BLOCK')
.param int block_flag :opt_flag
.local pmc nil
.local pmc elem
.local int len
.local int i
.local pmc return
.local pmc nil
nil = new 'NilClass'
return = nil
len = elements self
i = 0
loop:
if i == len goto done
elem = self[i]
$I0 = elem == item
if $I0, found
inc i
goto loop
found:
return = item
delete self[i]
dec len
goto loop
done:
$I0 = return == nil
if $I0, not_found
.return(return)
not_found:
if block_flag, have_block
.return(return)
have_block:
$P0 = block()
.return($P0)
.end
.sub delete_at :method
.param pmc index
.local pmc nil, result
nil = new 'NilClass'
result = nil
$I0 = exists self[index]
unless $I0 goto done
result = self[index]
delete self[index]
done:
.return (result)
.end
.sub delete_if :method
.param pmc block :named("!BLOCK")
.local int i, len
len = self
i = 0
loop:
if len == i goto done
$P0 = self[i]
$I0 = block($P0)
unless $I0 goto finish_loop
delete self[i]
finish_loop:
inc i
goto loop
done:
.end
=item exists(INDEX)
Checks to see if the specified index or indices have been assigned to. Returns a Bool value.
=cut
.sub exists :method
.param pmc indices :slurpy
.local int test
.local int len
.local pmc res
.local int ind
.local int i
test = 1
len = elements indices
i = 0
loop:
if i == len goto done
ind = indices[i]
test = exists self[ind]
if test == 0 goto done
inc i
goto loop
done:
.tailcall 'prefix:?'(test)
.end
=item kv()
=cut
.sub kv :method
.local pmc elem
.local pmc res
.local int len
.local int i
res = new 'CardinalArray'
len = elements self
i = 0
loop:
if i == len goto done
elem = new 'CardinalInteger'
elem = i
res.'push'(elem)
elem = self[i]
res.'push'(elem)
inc i
goto loop
done:
.return(res)
.end
=item pairs()
=cut
.sub pairs :method
.local pmc pair
.local pmc key
.local pmc val
.local pmc res
.local int len
.local int i
res = new 'CardinalArray'
len = elements self
i = 0
loop:
if i == len goto done
key = new 'CardinalInteger'
key = i
val = self[i]
pair = new 'Pair'
pair[key] = val
res.'push'(pair)
inc i
goto loop
done:
.return(res)
.end
=item grep(...)
=cut
.sub grep :method
.param pmc test
.param pmc block :named('!BLOCK')
.local pmc retv
.local pmc block_res
.local pmc block_arg
.local int narg
.local int i
retv = new 'CardinalArray'
narg = elements self
i = 0
loop:
if i == narg goto done
block_arg = self[i]
$P0 = 'infix:=~'(block_arg, test)
unless $P0 goto next
block_res = block(block_arg)
if block_res goto grepped
goto next
grepped:
retv.'push'(block_res)
goto next
next:
inc i
goto loop
done:
.return(retv)
.end
=item first(...)
=cut
.sub first :method :multi(CardinalArray,_)
.param int count
.local pmc newlist
.local pmc item
.local int elems
.local int i
newlist = new 'CardinalArray'
elems = elements self
le count, elems, sufficient_elements
count = elems
sufficient_elements:
i = 0
loop:
if i == count goto done
item = self[i]
item = clone item
push newlist, item
inc i
goto loop
done:
.return(newlist)
.end
.sub first :method :multi(CardinalArray)