Skip to content

Commit

Permalink
Preserve 6.c Regex.Bool behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jan 28, 2019
1 parent 8ef7c15 commit 3d581c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/Perl6/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ my $abbrev-block := 'abbreviated';
my int $?BITS := nqp::isgt_i(nqp::add_i(2147483648, 1), 0) ?? 64 !! 32;

sub block_closure($code, :$regex) {
my $clone := $regex
?? QAST::Op.new(
:op('callmethod'), :name('clone'), $code,
QAST::Var.new( :name('$_'), :scope('lexical'), :named('topic') ),
QAST::Var.new( :name('$/'), :scope('lexical'), :named('slash') ),
)
!! QAST::Op.new( :op('callmethod'), :name('clone'), $code );
my $clone := QAST::Op.new( :op('callmethod'), :name('clone'), $code );
if $regex {
if $*W.lang-ver-before('d') {
my $marker := $*W.find_symbol(['Rakudo', 'Internals', 'RegexBoolification6cMarker']);
$clone.push(QAST::WVal.new( :value($marker), :named('topic') ));
}
else {
$clone.push(QAST::Var.new( :name('$_'), :scope('lexical'), :named('topic') ));
$clone.push(QAST::Var.new( :name('$/'), :scope('lexical'), :named('slash') ));
}
}
QAST::Op.new( :op('p6capturelex'), $clone ).annotate_self(
'past_block', $code.ann('past_block')
).annotate_self(
Expand Down
3 changes: 3 additions & 0 deletions src/core/Rakudo/Internals.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ my class Rakudo::Internals {
method dynamic() { False }
}

# Marker symbol for 6.c-mode regex boolification.
class RegexBoolification6cMarker { }

# rotate nqp list to another given list without using push/pop
method RotateListToList(\from,\n,\to) {
nqp::stmts(
Expand Down
30 changes: 27 additions & 3 deletions src/core/Regex.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,33 @@ my class Regex { # declared in BOOTSTRAP
}

multi method Bool(Regex:D:) {
nqp::isconcrete($!topic)
?? ($!slash = $!topic.match(self)).Bool
!! False
my Mu \topic = $!topic;
nqp::istype_nd(topic, Rakudo::Internals::RegexBoolification6cMarker)
?? self!Bool6c()
!! nqp::isconcrete(topic)
?? ($!slash = topic.match(self)).Bool
!! False
}

method !Bool6c() {
nqp::stmts(
(my $ctx := nqp::ctx),
nqp::until(
nqp::isnull($ctx := nqp::ctxcallerskipthunks($ctx))
|| nqp::isconcrete(
my $underscore := nqp::getlexrelcaller($ctx,'$_')
),
nqp::null
),
nqp::if(
nqp::isnull($ctx),
False,
nqp::stmts(
(my $slash := nqp::getlexrelcaller($ctx,'$/')),
($slash = $underscore.match(self)).Bool
)
)
)
}

multi method gist(Regex:D:) {
Expand Down

0 comments on commit 3d581c8

Please sign in to comment.