Skip to content

Commit

Permalink
Make various other loop constructs sensitive to "last foo"
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jun 19, 2021
1 parent 8886a8d commit 02c554c
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions src/core.c/Rakudo/Iterator.pm6
Expand Up @@ -10,6 +10,8 @@

class Rakudo::Iterator {
my $empty := nqp::list; # an empty list for nqp::splice
sub always-IterationEnd(--> IterationEnd) { }
sub always-False(--> False) { }

#-------------------------------------------------------------------------------
# Roles that are used by iterators in the rest of the core settings, in
Expand Down Expand Up @@ -1438,7 +1440,16 @@ class Rakudo::Iterator {
($stopped = nqp::if(&!cond(),0,1))
),
'REDO', ($stopped = 0),
'LAST', ($result := IterationEnd)
'LAST', nqp::if(
nqp::isnull(
$result := nqp::getpayload(nqp::exception)
),
($result := IterationEnd),
nqp::stmts(
($!seen-first = 0),
(&!cond := &always-False)
)
)
)
),
:nohandler
Expand Down Expand Up @@ -2467,7 +2478,11 @@ class Rakudo::Iterator {
'LABELED', $!label,
'NEXT', ($stopped = 0),
'REDO', ($stopped = 0),
'LAST', ($result := IterationEnd)
'LAST', nqp::if(
nqp::isnull($result := nqp::getpayload(nqp::exception)),
($result := IterationEnd),
(&!body := &always-IterationEnd)
)
)
),
:nohandler
Expand Down Expand Up @@ -3682,14 +3697,14 @@ class Rakudo::Iterator {
# a condition. Takes a Callable to be considered the body of the loop,
# and a Callable for the condition..
my class RepeatLoop does Rakudo::SlippyIterator {
has $!body;
has $!cond;
has &!body;
has &!cond;
has $!label;
has int $!skip;

method !SET-SELF(\body,\cond,\label) {
$!body := body;
$!cond := cond;
&!body := body;
&!cond := cond;
$!label := nqp::decont(label);
$!skip = 1;
self
Expand All @@ -3706,25 +3721,31 @@ class Rakudo::Iterator {
}
else {
nqp::if(
$!skip || $!cond(),
$!skip || &!cond(),
nqp::stmts(
($!skip = 0),
nqp::until(
nqp::until( # XXX perhaps repeat_until?
(my int $stopped),
nqp::stmts(
($stopped = 1),
nqp::handle(
nqp::if(
nqp::istype(($result := $!body()),Slip),
nqp::istype(($result := &!body()),Slip),
($stopped = nqp::eqaddr(
($result := self.start-slip($result)),
IterationEnd
) && nqp::if($!cond(),0,1))
) && nqp::if(&!cond(),0,1))
),
'LABELED', $!label,
'NEXT', ($stopped = nqp::if($!cond(),0,1)),
'NEXT', ($stopped = nqp::if(&!cond(),0,1)),
'REDO', ($stopped = 0),
'LAST', ($result := IterationEnd)
'LAST', nqp::if(
nqp::isnull(
$result := nqp::getpayload(nqp::exception)
),
($result := IterationEnd),
(&!cond := &always-False)
)
)
),
:nohandler
Expand Down Expand Up @@ -4720,13 +4741,13 @@ class Rakudo::Iterator {
# a condition. Takes a Callable to be considered the body of the loop,
# and a Callable for the condition.
my class WhileLoop does Rakudo::SlippyIterator {
has $!body;
has $!cond;
has &!body;
has &!cond;
has $!label;

method !SET-SELF(\body,\cond,\label) {
$!body := body;
$!cond := cond;
&!body := body;
&!cond := cond;
$!label := nqp::decont(label);
self
}
Expand All @@ -4742,24 +4763,30 @@ class Rakudo::Iterator {
}
else {
nqp::if(
$!cond(),
&!cond(),
nqp::stmts(
nqp::until(
(my int $stopped),
nqp::stmts(
($stopped = 1),
nqp::handle(
nqp::if(
nqp::istype(($result := $!body()),Slip),
nqp::istype(($result := &!body()),Slip),
($stopped = nqp::eqaddr(
($result := self.start-slip($result)),
IterationEnd
) && nqp::if($!cond(),0,1))
) && nqp::if(&!cond(),0,1))
),
'LABELED', $!label,
'NEXT', ($stopped = nqp::if($!cond(),0,1)),
'NEXT', ($stopped = nqp::if(&!cond(),0,1)),
'REDO', ($stopped = 0),
'LAST', ($result := IterationEnd)
'LAST', nqp::if(
nqp::isnull(
$result := nqp::getpayload(nqp::exception)
),
($result := IterationEnd),
(&!cond := &always-False)
)
)
),
:nohandler
Expand Down

0 comments on commit 02c554c

Please sign in to comment.