Skip to content

Commit

Permalink
[Yapsi] implemented subroutine invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Masak committed Mar 6, 2011
1 parent d825429 commit 73785d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/Yapsi.pm
Expand Up @@ -44,7 +44,8 @@ grammar Yapsi::Perl6::Grammar {

token expression { <assignment> || <binding> || <variable> || <literal>
|| <declaration> || <invocation> || <block>
|| <saycall> || <increment> || <decrement> }
|| <saycall> || <subcall> || <increment> || <decrement>
}
token lvalue { <declaration> || <variable> || <increment> }
token value { <variable> || <literal> || <declaration> || <saycall>
|| <increment> }
Expand All @@ -59,6 +60,7 @@ grammar Yapsi::Perl6::Grammar {
rule assignment { <lvalue> '=' <expression> }
rule binding { <lvalue> ':=' <expression> }
rule saycall { 'say' <expression> } # very temporary solution
rule subcall { $<subname>=[\w+]'()'? }
rule increment { '++' <value> }
rule decrement { '--' <value> }
rule invocation { [<variable>||<block>]'()' }
Expand Down Expand Up @@ -240,8 +242,8 @@ class Yapsi::Perl6::Actions {
}

method expression($/) {
hoist $/, <assignment literal saycall variable declaration binding
increment decrement invocation block>;
hoist $/, <assignment literal saycall subcall variable declaration
binding increment decrement invocation block>;
}

method lvalue($/) {
Expand Down Expand Up @@ -305,6 +307,11 @@ class Yapsi::Perl6::Actions {
make FUTURE::Call.new(:name('&say'), :children($<expression>.ast));
}

method subcall($/) {
make FUTURE::Call.new(:name('&' ~ $<subname>),
:children());
}

method increment($/) {
make FUTURE::Call.new(:name('&prefix:<++>'), :children($<value>.ast));
}
Expand Down Expand Up @@ -400,7 +407,9 @@ class Yapsi::Compiler {
}

multi process(FUTURE::Call $call) {
process $call.children[0]; # a FUTURE::Expression
if $call.children.elems {
process $call.children[0]; # a FUTURE::Expression
}

given $call.name {
when '&say' {
Expand All @@ -425,7 +434,10 @@ class Yapsi::Compiler {
push @blocksic, "call $register";
}
default {
die "Don't know how to handle $call.name()";
# This is slightly cannibalistic, but still better than
# code duplication
process FUTURE::Var.new(:name($call.name));
push @blocksic, "call $register";
}
}
}
Expand Down Expand Up @@ -560,7 +572,7 @@ class Yapsi::Compiler {
}

multi process(Any $node) {
die "No multi defined for {$node.WHAT.perl}, sorry :/";
die "No multi 'process' defined for {$node.WHAT.perl}, sorry :/";
}

sub declutter(@instructions) {
Expand Down
3 changes: 3 additions & 0 deletions t/compiler.t
Expand Up @@ -41,6 +41,8 @@ my @programs-that-compile =
'sub foo {}',
'my sub foo {}',
'our sub foo {}',
'sub foo {}; foo',
'sub foo {}; foo()',
;

sub escape($string) { $string.subst("\n", "\\n", :g) }
Expand Down Expand Up @@ -75,6 +77,7 @@ my @programs-that-don't-compile = # '
'unless a {}' => 'could not parse',
'my $a = 1; { say $a; my $a = 2 }' => 'reference to outer variable',
'sub foo' => 'could not parse',
'foo' => 'undefined subroutine',
;

for @programs-that-don't-compile -> $pair { # '
Expand Down

0 comments on commit 73785d9

Please sign in to comment.