Skip to content

Commit

Permalink
Implement control operators next, redo, last, return
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Sep 15, 2010
1 parent aad86a4 commit ae2e557
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Body.pm
Expand Up @@ -156,7 +156,7 @@ use CgOp ();
CgOp::sink($self->do->cgop($self)),
CgOp::rawsccall('Kernel.Take',
CgOp::scopedlex('EMPTY'))));
} elsif ($self->returnable) {
} elsif ($self->returnable && defined($self->signature)) {
$self->cgoptree(CgOp::prog(@enter,
CgOp::return(CgOp::span("rstart", "rend",
$self->do->cgop($self))),
Expand Down
2 changes: 1 addition & 1 deletion src/Niecza/Actions.pm
Expand Up @@ -1560,7 +1560,7 @@ sub param_var { my ($cl, $M) = @_;
}
my $twigil = $M->{twigil}[0] ? $M->{twigil}[0]->Str : '';
my $sigil = $M->{sigil}->Str;
if ($twigil || ($sigil ne '$' && $sigil ne '@' && $sigil ne '%')) {
if ($twigil || ($sigil ne '$' && $sigil ne '@' && $sigil ne '%' && $sigil ne '&')) {
$M->sorry('Non bare scalar targets NYI');
return;
}
Expand Down
35 changes: 35 additions & 0 deletions test2.pl
@@ -1,4 +1,39 @@
# vim: ft=perl6
use Test;

sub flow-ok($fn, $flw, $msg) {
my $log = '';
$fn(-> $i { $log ~= $i });
is $log, $flw, $msg;
}

# XXX multi dispatch
sub next {
Q:CgOp { (rawsccall Kernel.SearchForHandler (int 1) (null Frame) (int -1) (null String) (null Variable)) }
}
sub last {
Q:CgOp { (rawsccall Kernel.SearchForHandler (int 2) (null Frame) (int -1) (null String) (null Variable)) }
}
sub redo {
Q:CgOp { (rawsccall Kernel.SearchForHandler (int 3) (null Frame) (int -1) (null String) (null Variable)) }
}
sub return is rawcall {
Q:CgOp { (rawsccall Kernel.SearchForHandler (int 4) (null Frame) (int -1) (null String) (pos 0)) }
}

flow-ok -> &l { my $i = 0; while $i < 2 { $i++; l(1); next; l(2) } }, '11',
"next skips second half of while loop";
flow-ok -> &l { my $i = 0; while $i < 2 { $i++; l(1); last; l(2) } }, '1',
"last skips everything";
flow-ok -> &l { my $i = 0; while True { l($i++); last if $i == 3 } }, '012',
"last can leave inf loop";
flow-ok -> &l { my $i = 3; while $i == 3 { l($i--); redo if $i } }, '321',
"redo reenters loops";
sub foo { return 2; }
is foo(), 2, "return values work";
my $cont = False;
sub foo3 { return 2; $cont = True; }
foo3;
ok !$cont, "return exits function";

done-testing;

0 comments on commit ae2e557

Please sign in to comment.