Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Yapsi] implemented closure invocation syntax

You can now do $x(), as well as { say 42 }().
  • Loading branch information...
commit 706d5f6109e419aedae3e48f4cb9c5868a0efc92 1 parent dd03f58
@masak authored
Showing with 11 additions and 2 deletions.
  1. +7 −1 lib/Yapsi.pm
  2. +2 −0  t/compiler.t
  3. +2 −1  t/runtime.t
View
8 lib/Yapsi.pm
@@ -23,7 +23,7 @@ grammar Yapsi::Perl6::Grammar {
}> \n
|| <.ws> ';' }
token expression { <assignment> || <binding> || <variable> || <literal>
- || <declaration> || <block>
+ || <declaration> || <invocation> || <block>
|| <saycall> || <increment> || <decrement> }
token statement_control { <statement_control_if>
|| <statement_control_while_until>
@@ -43,6 +43,7 @@ grammar Yapsi::Perl6::Grammar {
rule saycall { 'say' <expression> } # very temporary solution
rule increment { '++' <value> }
rule decrement { '--' <value> }
+ rule invocation { [<variable>||<block>]'()' }
token block { <.ws> '{'
{ push @blockstack, { :name(unique-block()) } }
<.ws> <statementlist> <.ws> '}' }
@@ -356,6 +357,11 @@ class Yapsi::Compiler {
"store $locator, $register";
make [$register, $locator];
}
+ elsif $key eq 'invocation' {
+ my ($register) = $<variable> ?? $<variable>.ast.list
+ !! $<block>.ast<register>;
+ push @blocksic, "call $register";
+ }
elsif $key eq 'saycall' {
my ($register, $) = $<expression>.ast.list;
my $result = self.unique-register;
View
2  t/compiler.t
@@ -35,6 +35,8 @@ my @programs-that-compile =
'my $a=0; unless $a { say $a }',
'my $a=0; until $a { say 42; ++$a; }',
'our $a',
+ 'my $a; $a()',
+ '{ say 42 }()',
;
sub escape($string) { $string.subst("\n", "\\n", :g) }
View
3  t/runtime.t
@@ -47,7 +47,8 @@ my @tests =
"5\n", 'our lookup',
'my $a = 0; my $b = 0; until $a { if $b { $a = 1 }; say ++$b }',
"1\n2\n", 'nested jumps in SIC',
- 'my $a = { say 42 }', "", 'non-immediate block',
+ 'my $a = { say 42 }; say 1; $a()', "1\n42\n", 'non-immediate block',
+ '{ say 42 }()', "42\n", 'call a block',
;
for @tests -> $program, $expected, $message {
Please sign in to comment.
Something went wrong with that request. Please try again.