Skip to content

Commit

Permalink
[Yapsi] added ENTER phaser
Browse files Browse the repository at this point in the history
And it works! Not bad.
  • Loading branch information
Carl Masak committed May 1, 2011
1 parent 79609ac commit a7d23db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/Yapsi.pm
Expand Up @@ -43,9 +43,10 @@ grammar Yapsi::Perl6::Grammar {
rule statement_control_until { 'until' <expression> <block> }

token expression { <assignment> || <binding> || <variable> || <literal>
|| <declaration> || <invocation> || <block>
|| <declaration> || <invocation> || <block> || <phaser>
|| <saycall> || <subcall> || <increment> || <decrement>
}
rule phaser { 'ENTER' <block> }
token lvalue { <declaration> || <variable> || <increment> }
token value { <variable> || <literal> || <declaration> || <saycall>
|| <increment> }
Expand Down Expand Up @@ -104,6 +105,7 @@ class FUTURE::Block is FUTURE::Node {
has $.name;
has @.vars is rw;
has $.immediate is rw;
has $.phaser is rw;

method info { [~] ' -- ', $.name,
(' [', @.vars»<name>.join(', '), ']' if @.vars) }
Expand Down Expand Up @@ -246,7 +248,12 @@ class Yapsi::Perl6::Actions {

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

method phaser($/) {
hoist $/, ('block', );
$/.ast.phaser = True;
}

method lvalue($/) {
Expand Down Expand Up @@ -501,10 +508,17 @@ class Yapsi::Compiler {
$register = unique-register;
push @blocks-to-serialize, $block
unless any(@already-serialized) eq $block;
push @blocksic,
"$register = closure-from-block '$block.name()'";
if $block.immediate {
push @blocksic, "call $register";
if $block.phaser {
unshift @blocksic,
"$register = closure-from-block '$block.name()'",
"call $register";
}
else {
push @blocksic,
"$register = closure-from-block '$block.name()'";
if $block.immediate {
push @blocksic, "call $register";
}
}
}

Expand Down
1 change: 1 addition & 0 deletions t/compiler.t
Expand Up @@ -43,6 +43,7 @@ my @programs-that-compile =
'our sub foo {}',
'sub foo {}; foo',
'sub foo {}; foo()',
'say 2; ENTER { say 1 }'
;

sub escape($string) { $string.subst("\n", "\\n", :g) }
Expand Down
1 change: 1 addition & 0 deletions t/runtime.t
Expand Up @@ -55,6 +55,7 @@ my @tests =
'foo(); sub foo { say 5 }', "5\n", 'call before declaration',
'sub foo { bar; sub bar { say 42 } }; foo',
"42\n", 'nested sub call',
'say 2; ENTER { say 1 }; say 3', "1\n2\n3\n", 'ENTER phaser',
;

for @tests -> $program, $expected, $message {
Expand Down

0 comments on commit a7d23db

Please sign in to comment.