Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[nqp] Add named argument handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 28, 2009
1 parent f645528 commit ed8726a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -116,10 +116,19 @@ method statement_control:sym<return>($/) {

## Terms

method noun:sym<colonpair>($/) { make $<colonpair>.ast; }
method noun:sym<variable>($/) { make $<variable>.ast; }
method noun:sym<scope_declarator>($/) { make $<scope_declarator>.ast; }
method noun:sym<routine_declarator>($/) { make $<routine_declarator>.ast; }

method colonpair($/) {
my $past := $<circumfix>
?? $<circumfix>[0].ast
!! PAST::Val.new( :value( !$<not> ) );
$past.named( ~$<identifier> );
make $past;
}

method variable($/) {
my $past := PAST::Var.new( :name(~$/) );
if $<twigil>[0] eq '*' {
Expand Down
18 changes: 17 additions & 1 deletion src/NQP/Grammar.pm
Expand Up @@ -4,6 +4,13 @@ token TOP {
<comp_unit>
}

## Lexer stuff

token identifier { <ident> }


## Top-level rules

token comp_unit {
<.newpad>
<statementlist>
Expand Down Expand Up @@ -96,10 +103,19 @@ token statement_control:sym<return> {

## Terms

token noun:sym<colonpair> { <colonpair> }
token noun:sym<variable> { <variable> }
token noun:sym<scope_declarator> { <scope_declarator> }
token noun:sym<routine_declarator> { <routine_declarator> }

token colonpair {
':'
[
| $<not>='!' <identifier>
| <identifier> <circumfix>?
]
}

token variable {
<sigil> <twigil>? <desigilname=ident>
}
Expand Down Expand Up @@ -154,7 +170,7 @@ rule default_value { '=' <EXPR('i=')> }
proto token term { <...> }

token term:sym<identifier> {
<identifier=ident> <args>
<identifier> <args>
}

token args {
Expand Down
2 changes: 1 addition & 1 deletion src/cheats/hll-compiler.pir
Expand Up @@ -20,7 +20,7 @@
null parseactions
$S0 = options['target']
# if $S0 == 'parse' goto have_parseactions
if $S0 == 'parse' goto have_parseactions
parseactions = self.'parseactions'()
have_parseactions:
Expand Down
20 changes: 20 additions & 0 deletions t/nqp/23-named-args.t
@@ -0,0 +1,20 @@
#! nqp

# test named parameters and arguments

plan(4);

sub f1 ($x, :$y) { $x - $y; }
say('ok ', f1(2, :y(1)), ' # named args passable');

sub f2 ($x, :$y) { $x; }
say('ok ', f2(2), ' # named args ignorable');

sub f3 (:$x, :$y) { $x - $y; }
say('ok ', f3(:y(2), :x(5)), ' # args reorderable');

sub f4 ($w, $x, :$y, :$z) { $w + $x + $y + $z; }
say('ok ', f4(:z(2), -3, :y(1), 4), ' # named/positional intermixable');


# XXX: test mandatory named args are actually mandatory

0 comments on commit ed8726a

Please sign in to comment.