Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Implement given BLOCK and when BLOCK, make postifix when match as $_ ~~
Browse files Browse the repository at this point in the history
EXPR rather than EXPR ~~ $_. Added tests.
  • Loading branch information
arnsholt committed Jul 11, 2010
1 parent fbe85c9 commit 60f3383
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/NQP/Actions.pm
Expand Up @@ -258,11 +258,34 @@ sub push_block_handler($/, $block) {
}

method statement_control:sym<given>($/) {
make PAST::Stmts.new();
my $past := $<xblock>.ast;
my $block := $past[1];
my $expr := $past[0];

unless $block.arity {
$block[0].push( PAST::Var.new( :name('$_'), :scope('parameter') ) );
$block.symbol('$_', :scope('lexical') );
$block.arity(1);
}

$past.pasttype('repeat_while');
$past[0] := PAST::Val.new(:value(0));
$past[1] := PAST::Op.new( :pasttype('call'), $block, $expr );

make $past;
}

method statement_control:sym<when>($/) {
make PAST::Stmts.new();
my $past := xblock_immediate( $<xblock>.ast );
my $block := $past[1];
my $expr := $past[0];

$past[0] := PAST::Op.new( :pasttype<callmethod>, :name<ACCEPTS>, :node($/),
$expr,
PAST::Var.new(:name<$_>, :scope<lexical>) );
$block.push( control( $/, 'CONTROL_LOOP_LAST' ) );

make $past;
}

method statement_prefix:sym<INIT>($/) {
Expand Down Expand Up @@ -306,8 +329,8 @@ method statement_mod_cond:sym<unless>($/) { make $<cond>.ast; }
method statement_mod_cond:sym<when>($/) {
$<sym> := "if";
make PAST::Op.new( :pasttype<callmethod>, :name<ACCEPTS>, :node($/),
PAST::Var.new(:name<$_>, :scope<lexical>),
$<cond>.ast );
$<cond>.ast,
PAST::Var.new(:name<$_>, :scope<lexical>) );
}

method statement_mod_loop:sym<while>($/) { make $<cond>.ast; }
Expand Down
27 changes: 27 additions & 0 deletions t/nqp/52-given-when.t
@@ -0,0 +1,27 @@
#! nqp

say('1..6');

given 42 {
say('ok 1 - code inside given');
print('not ') if $_ != 42;
say('ok 2 - $_ is 42');
}

class Foo { }
class Bar { }

my $_ := Foo.new;
say('ok 3 - postfix when') when Foo;

given Foo.new {
say('ok 4 - code before match run');
when Bar {
say('not ok 5 - matches Foo');
}
when Foo {
say('ok 5 - matches Foo');
}
print('not ');
}
say('ok 6 - code after match not run');

0 comments on commit 60f3383

Please sign in to comment.