Skip to content

Commit

Permalink
Get prefix:<|> working in argument lists. All of foo(|@A), foo(|%h) a…
Browse files Browse the repository at this point in the history
…nd foo($c) work - the last of those never worked properly in alpha (it now passes along both positional and named parts of the capture).
  • Loading branch information
jnthn committed Feb 16, 2010
1 parent 0afe11b commit a6e4c1f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/Perl6/Actions.pm
Expand Up @@ -1470,15 +1470,47 @@ method args($/) {
method semiarglist($/) { make $<arglist>.ast; }

method arglist($/) {
my $past := PAST::Op.new( :pasttype('call'), :node($/) );
# Build up argument list, hanlding nameds as we go.
my $past := PAST::Op.new( );
if $<EXPR> {
my $expr := $<EXPR>.ast;
if $expr.name eq '&infix:<,>' {
for $expr.list { $past.push(handle_named_parameter($_)); }
}
else { $past.push(handle_named_parameter($expr)); }
}
make $past;

# See if we have any uses of prefix:<|>; if we have, then we take it and
# evaluate it once. We then stick it in a register, and pull out an RPA
# and a Hash that Parrot knows what to do with.
my $result := PAST::Op.new( :pasttype('call'), :node($/) );
for @($past) {
if $_.isa(PAST::Op) && $_.name() eq '&prefix:<|>' {
my $reg_name := $past.unique('flatten_tmp_');
my $steps := PAST::Stmts.new(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name($reg_name), :scope('register'), :isdecl(1) ),
$_
),
PAST::Op.new(
:pasttype('callmethod'), :name('!PARROT_POSITIONALS'),
PAST::Var.new( :name($reg_name), :scope('register') )
)
);
$steps.flat(1);
$result.push($steps);
$result.push(PAST::Op.new(
:flat(1), :named(1),
:pasttype('callmethod'), :name('!PARROT_NAMEDS'),
PAST::Var.new( :name($reg_name), :scope('register') )
));
}
else {
$result.push($_);
}
}

make $result;
}

sub handle_named_parameter($arg) {
Expand Down
24 changes: 24 additions & 0 deletions src/builtins/Capture.pir
Expand Up @@ -90,6 +90,30 @@ XXX Returns Parrot Hash, not Perl 6 Hash.
.return ($P0)
.end


=item !PARROT_POSITIONALS

Gets a Parrot RPA that we can use :flat on.

=cut

.sub '!PARROT_POSITIONALS' :method
$P0 = getattribute self, '$!pos'
.return ($P0)
.end


=item !PARROT_NAMEDS

Gets a Parrot Hash that we can use :flat :named on.

=cut

.sub '!PARROT_NAMEDS' :method
$P0 = getattribute self, '$!named'
.return ($P0)
.end

=back

=head2 Functions
Expand Down
5 changes: 5 additions & 0 deletions src/core/operators.pm
Expand Up @@ -159,6 +159,11 @@ our multi infix:<!=>($a, $b) {
pir::isne__INN(+$a, +$b) ?? True !! False
}

our multi prefix:<|>(@a) { @a.Capture }
our multi prefix:<|>(%h) { %h.Capture }
our multi prefix:<|>(Capture $c) { $c }
our multi prefix:<|>(Mu $fail) { die 'Cannot use prefix:<|> with a ' ~ $fail.WHAT; }

# XXX Wants to be a macro when we have them.
our sub WHAT(\$x) {
$x.WHAT
Expand Down

0 comments on commit a6e4c1f

Please sign in to comment.