diff --git a/src/NQP/Actions.pm b/src/NQP/Actions.pm index 8c70bc9..15f1e3c 100644 --- a/src/NQP/Actions.pm +++ b/src/NQP/Actions.pm @@ -258,11 +258,34 @@ sub push_block_handler($/, $block) { } method statement_control:sym($/) { - make PAST::Stmts.new(); + my $past := $.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($/) { - make PAST::Stmts.new(); + my $past := xblock_immediate( $.ast ); + my $block := $past[1]; + my $expr := $past[0]; + + $past[0] := PAST::Op.new( :pasttype, :name, :node($/), + $expr, + PAST::Var.new(:name<$_>, :scope) ); + $block.push( control( $/, 'CONTROL_LOOP_LAST' ) ); + + make $past; } method statement_prefix:sym($/) { @@ -306,8 +329,8 @@ method statement_mod_cond:sym($/) { make $.ast; } method statement_mod_cond:sym($/) { $ := "if"; make PAST::Op.new( :pasttype, :name, :node($/), - PAST::Var.new(:name<$_>, :scope), - $.ast ); + $.ast, + PAST::Var.new(:name<$_>, :scope) ); } method statement_mod_loop:sym($/) { make $.ast; } diff --git a/t/nqp/52-given-when.t b/t/nqp/52-given-when.t new file mode 100644 index 0000000..7f3606d --- /dev/null +++ b/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');