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

Commit

Permalink
Basic loop control (next/last/redo terms)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed May 13, 2010
1 parent 458dbfe commit f5f582d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -765,6 +765,23 @@ method prefix:sym<make>($/) {
);
}

sub control($/, $id) {
make PAST::Op.new(
:node($/),
:pasttype('inline'),
:inline(
'.include "except_types.pasm"',
' %r = new "Exception"',
' %r["type"] = ' ~ $id,
' throw %r'
)
)
}

method term:sym<next>($/) { control($/, '.CONTROL_LOOP_NEXT') }
method term:sym<last>($/) { control($/, '.CONTROL_LOOP_LAST') }
method term:sym<redo>($/) { control($/, '.CONTROL_LOOP_REDO') }

method infix:sym<~~>($/) {
make PAST::Op.new( :pasttype<callmethod>, :name<ACCEPTS>, :node($/) );
}
Expand Down
3 changes: 3 additions & 0 deletions src/NQP/Grammar.pm
Expand Up @@ -526,6 +526,9 @@ token infix:sym<,> { <sym> <O('%comma, :pasttype<list>')> }

token prefix:sym<return> { <sym> \s <O('%list_prefix, :pasttype<return>')> }
token prefix:sym<make> { <sym> \s <O('%list_prefix')> }
token term:sym<last> { <sym> \s <O('%list_prefix')> }
token term:sym<next> { <sym> \s <O('%list_prefix')> }
token term:sym<redo> { <sym> \s <O('%list_prefix')> }

method smartmatch($/) {
# swap rhs into invocant position
Expand Down
32 changes: 32 additions & 0 deletions t/nqp/47-loop-control.t
@@ -0,0 +1,32 @@
#! nqp

plan(3);

my $runs := 0;

while $runs < 5 {
$runs++;
last if $runs == 3;
}

ok($runs == 3, "last works in while");

$runs := 0;
my $i := 0;
while $runs < 5 {
$runs++;
next if $runs % 2;
$i++;
}

ok($i == 2, "next works in while");

$runs := 0;
$i := 0;
while $i < 5 {
$runs++;
redo if $runs % 2;
$i++;
}

ok($runs == 10, "redo works in while");

0 comments on commit f5f582d

Please sign in to comment.