Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Yapsi] implemented closure invocation syntax

You can now do $x(), as well as { say 42 }().
  • Loading branch information...
commit 706d5f6109e419aedae3e48f4cb9c5868a0efc92 1 parent dd03f58
Carl Mäsak authored November 14, 2010
8  lib/Yapsi.pm
@@ -23,7 +23,7 @@ grammar Yapsi::Perl6::Grammar {
23 23
                            }> \n
24 24
                            || <.ws> ';' }
25 25
     token expression { <assignment> || <binding> || <variable> || <literal>
26  
-                       || <declaration> || <block>
  26
+                       || <declaration> || <invocation> || <block>
27 27
                        || <saycall> || <increment> || <decrement> }
28 28
     token statement_control { <statement_control_if>
29 29
                               || <statement_control_while_until> 
@@ -43,6 +43,7 @@ grammar Yapsi::Perl6::Grammar {
43 43
     rule  saycall { 'say' <expression> }  # very temporary solution
44 44
     rule  increment { '++' <value> }
45 45
     rule  decrement { '--' <value> }
  46
+    rule  invocation { [<variable>||<block>]'()' }
46 47
     token block { <.ws> '{'
47 48
                   { push @blockstack, { :name(unique-block()) } }
48 49
                   <.ws> <statementlist> <.ws> '}' }
@@ -356,6 +357,11 @@ class Yapsi::Compiler {
356 357
                                         "store $locator, $register";
357 358
                         make [$register, $locator];
358 359
                     }
  360
+                    elsif $key eq 'invocation' {
  361
+                        my ($register) = $<variable> ?? $<variable>.ast.list
  362
+                                                     !! $<block>.ast<register>;
  363
+                        push @blocksic, "call $register";
  364
+                    }
359 365
                     elsif $key eq 'saycall' {
360 366
                         my ($register, $) = $<expression>.ast.list;
361 367
                         my $result = self.unique-register;
2  t/compiler.t
@@ -35,6 +35,8 @@ my @programs-that-compile =
35 35
     'my $a=0; unless $a { say $a }',
36 36
     'my $a=0; until $a { say 42; ++$a; }',
37 37
     'our $a',
  38
+    'my $a; $a()',
  39
+    '{ say 42 }()',
38 40
 ;
39 41
 
40 42
 sub escape($string) { $string.subst("\n", "\\n", :g) }
3  t/runtime.t
@@ -47,7 +47,8 @@ my @tests =
47 47
                                        "5\n",       'our lookup',
48 48
     'my $a = 0; my $b = 0; until $a { if $b { $a = 1 }; say ++$b }',
49 49
                                        "1\n2\n",    'nested jumps in SIC',
50  
-    'my $a = { say 42 }',              "",          'non-immediate block',
  50
+    'my $a = { say 42 }; say 1; $a()', "1\n42\n",   'non-immediate block',
  51
+    '{ say 42 }()',                    "42\n",      'call a block',
51 52
 ;
52 53
 
53 54
 for @tests -> $program, $expected, $message {

0 notes on commit 706d5f6

Please sign in to comment.
Something went wrong with that request. Please try again.