Skip to content

Commit

Permalink
Give methods on Array a Perl 6 signature and get the transformed into…
Browse files Browse the repository at this point in the history
… Perl6MultiSub, which is not only more correct but also resolves the issues with method calls on typed arrays. Note that we also bump the required Parrot version number, since I found and fixed a Parrot bug along the way.
  • Loading branch information
jnthn committed Apr 8, 2009
1 parent ccc12b3 commit 0fa3809
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
74 changes: 66 additions & 8 deletions src/classes/Array.pir
Expand Up @@ -14,7 +14,7 @@ src/classes/Array.pir - Perl 6 Array class and related functions
arrayproto.'!MUTABLE'()

$P0 = get_hll_namespace ['Perl6Array']
'!EXPORT'('delete,exists,pop,push,shift,unshift', 'from'=>$P0)
'!EXPORT'('delete,exists,pop,push,shift,unshift', 'from'=>$P0, 'to_p6_multi'=>1)
.end

=head2 Methods
Expand All @@ -28,7 +28,7 @@ Remove items from an array.
=cut

.namespace ['Perl6Array']
.sub 'delete' :method :multi(Perl6Array)
.sub 'delete' :method :multi() :subid('array_delete')
.param pmc indices :slurpy
.local pmc result
result = new 'List'
Expand Down Expand Up @@ -58,6 +58,16 @@ Remove items from an array.
indices_end:
.return (result)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_delete"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_param"("@indices", 1 :named("slurpy"))
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item exists(indices :slurpy)
Expand All @@ -66,7 +76,7 @@ Return true if the elements at C<indices> have been assigned to.

=cut

.sub 'exists' :method :multi(Perl6Array)
.sub 'exists' :method :multi() :subid('array_exists')
.param pmc indices :slurpy
.local int test

Expand All @@ -79,6 +89,16 @@ Return true if the elements at C<indices> have been assigned to.
indices_end:
.tailcall 'prefix:?'(test)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_exists"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_param"("@indices", 1 :named("slurpy"))
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item item()
Expand Down Expand Up @@ -111,8 +131,9 @@ Remove the last item from the array and return it.

=cut

.sub 'pop' :method :multi(Perl6Array)
.sub 'pop' :method :multi() :subid('array_pop')
.local pmc x
'!SIGNATURE_BIND'()
unless self goto empty
x = pop self
goto done
Expand All @@ -121,6 +142,15 @@ Remove the last item from the array and return it.
done:
.return (x)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_pop"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item push(args :slurpy)
Expand All @@ -129,13 +159,23 @@ Add C<args> to the end of the Array.

=cut

.sub 'push' :method :multi(Perl6Array)
.sub 'push' :method :multi() :subid('array_push')
.param pmc args :slurpy
args.'!flatten'()
$I0 = elements self
splice self, args, $I0, 0
.tailcall self.'elems'()
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_push"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_param"("@items", 1 :named("slurpy"))
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item shift()
Expand All @@ -144,7 +184,7 @@ Shift the first item off the array and return it.

=cut

.sub 'shift' :method :multi(Perl6Array)
.sub 'shift' :method :multi() :subid('array_shift')
.local pmc x
unless self goto empty
x = shift self
Expand All @@ -154,6 +194,15 @@ Shift the first item off the array and return it.
done:
.return (x)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_shift"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end


=item unshift(args :slurpy)
Expand All @@ -162,12 +211,22 @@ Adds C<args> to the beginning of the Array.

=cut

.sub 'unshift' :method :multi(Perl6Array)
.sub 'unshift' :method :multi() :subid('array_unshift')
.param pmc args :slurpy
args.'!flatten'()
splice self, args, 0, 0
.tailcall self.'elems'()
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "array_unshift"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_param"("@items", 1 :named("slurpy"))
$P0 = get_hll_global 'Array'
signature."!add_implicit_self"($P0)
.end

=item values()

Expand Down Expand Up @@ -262,7 +321,6 @@ Store things into an Array (e.g., upon assignment)
.return (self)
.end


=back

=cut
Expand Down
11 changes: 10 additions & 1 deletion src/classes/List.pir
Expand Up @@ -188,11 +188,20 @@ Return the number of elements in the list.
=cut

.namespace ['List']
.sub 'elems' :method :multi('ResizablePMCArray') :vtable('get_number')
.sub 'elems' :method :multi() :vtable('get_number') :subid('list_elems')
self.'!flatten'()
$I0 = elements self
.return ($I0)
.end
.sub '' :init :load
.local pmc block, signature
.const 'Sub' $P0 = "list_elems"
block = $P0
signature = new ["Signature"]
setprop block, "$!signature", signature
signature."!add_implicit_self"()
'!TOPERL6MULTISUB'(block)
.end


=back
Expand Down

0 comments on commit 0fa3809

Please sign in to comment.