Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rubyish slurpy hash params
  • Loading branch information
dwarring committed Oct 31, 2013
1 parent 4f69419 commit 1d2a068
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
47 changes: 31 additions & 16 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -82,8 +82,8 @@ grammar Rubyish::Grammar is HLL::Grammar {

rule signature {
:my $*IN_PARENS := 1;
<param>* % <comma>
[ ',' '*' <slurpy=.param> ]?
<param>* % <comma>
[ ',' '*' <slurpy=.param> ]?
','?
}

Expand Down Expand Up @@ -123,7 +123,11 @@ grammar Rubyish::Grammar is HLL::Grammar {
\% w <?before [.]> <quote_EXPR: ':q', ':w'>
}

token call-args {:s<hs> <EXPR>+ % <comma> }
token call-args {:s<hs> [<arg=.hash-args>||<arg=.EXPR>]+ % ',' }

token hash-args {:s <hash-arg>+ % ',' }
token hash-arg {:s <EXPR> '=>' <EXPR> }

token paren-args {:my $*IN_PARENS := 1; <call-args> }

token operation {<ident>[\!|\?]?}
Expand Down Expand Up @@ -320,7 +324,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
token do-block { <do> ~ 'end' <stmtlist> }

token do {:s<hs>
<separator> 'do'? | 'do' | <?before <tmpl-unesc>>
<separator> 'do'? | 'do' | <?before <tmpl-unesc>>
}

token term:sym<code> {
Expand Down Expand Up @@ -434,10 +438,21 @@ class Rubyish::Actions is HLL::Actions {

method call-args($/) {
my @args;
@args.push($_.ast) for $<EXPR>;
@args.push($_.ast) for $<arg>;
make @args;
}

method hash-args($/) {
my $args := QAST::Op.new( :op<hash> );

for $<hash-arg> {
$args.push( $_.ast )
for $_<EXPR>
}

make $args;
}

method paren-args($/) {
make $<call-args>.ast;
}
Expand Down Expand Up @@ -574,7 +589,7 @@ class Rubyish::Actions is HLL::Actions {
));
}

@params.push(QAST::Var.new(
@params.push(QAST::Var.new(
:name(~$<slurpy>), :scope('lexical'), :decl('param'), :slurpy<1>
)) if $<slurpy>;

Expand Down Expand Up @@ -693,15 +708,15 @@ class Rubyish::Actions is HLL::Actions {
# todo: proper type objects
my %call-tab;
sub call-tab-init() {
%call-tab := nqp::hash(
'call', 'call',
'nil?', 'isnull'
)
%call-tab := nqp::hash(
'call', 'call',
'nil?', 'isnull'
)
}

method postfix:sym<.>($/) {
call-tab-init() unless %call-tab<call>;
my $op := %call-tab{ ~$<operation> };
call-tab-init() unless %call-tab<call>;
my $op := %call-tab{ ~$<operation> };
my $meth_call := $op
?? QAST::Op.new( :op($op) )
!! QAST::Op.new( :op('callmethod'), :name(~$<operation>) );
Expand Down Expand Up @@ -731,7 +746,7 @@ class Rubyish::Actions is HLL::Actions {

method stmt:sym<cond>($/) {
my $ast := $<xblock>.ast;
$ast.op( ~$<op> );
$ast.op( ~$<op> );
$ast.push( $<else>.ast )
if $<else>;

Expand All @@ -740,7 +755,7 @@ class Rubyish::Actions is HLL::Actions {

method elsif($/) {
my $ast := $<xblock>.ast;
$ast.op( 'if' );
$ast.op( 'if' );
$ast.push( $<else>.ast )
if $<else>;

Expand All @@ -760,13 +775,13 @@ class Rubyish::Actions is HLL::Actions {
my $block := QAST::Block.new(
QAST::Var.new( :name(~$<ident>), :scope('lexical'), :decl('param')),
$<do-block>.ast,
);
);

make QAST::Op.new( $<EXPR>.ast, $block, :op('for'), :node($/) );
}

method do-block($/) {
make $<stmtlist>.ast
make $<stmtlist>.ast
}

method term:sym<code>($/) {
Expand Down
6 changes: 3 additions & 3 deletions examples/rubyish/t/arrays.t
Expand Up @@ -16,12 +16,12 @@ end
puts "#{%w{nok ok nok}[1]} 9 - quote words"
puts "#{['nok',['nok','nok','ok']][1][2]} 10 - 2d array"

def slurpy_arg_tests(x, *arg_arr)
def test_array_args(x, *arg_arr)
puts "ok #{x} - fixed arg"
for t in arg_arr do
puts "ok #{t} - slurpy arg array"
puts "ok #{t} - array slurpy arg"
end
end

slurpy_arg_tests(11, 12, 13)
test_array_args(11, 12, 13)

11 changes: 10 additions & 1 deletion examples/rubyish/t/hashs.t
@@ -1,4 +1,4 @@
puts "1..8"
puts "1..11"
h = {'a' =>10,'b' => 18+2 , 'c' => 30}
h{'d'} = 100
puts "#{h{'a'} == 10? 'ok' : 'nok'} 1 - h{'a'}"
Expand All @@ -15,3 +15,12 @@ end

h<d> = ['nok','ok']
puts "#{ h<d>[1] } 8 - HoA"

def test_hash_args(x, h_args)
puts "ok #{ x } - fixed arg"
for k in h_args do
puts "ok #{ h_args{k} } - hash slurpy arg (#{k})"
end
end

test_hash_args 9, 'z' => 10, 'y' => 11

0 comments on commit 1d2a068

Please sign in to comment.