Skip to content

Commit

Permalink
Made smartmatching with something other than a regex on the RHS retur…
Browse files Browse the repository at this point in the history
…n a Bool.
  • Loading branch information
Kodi Arfer authored and Kodi Arfer committed Jan 14, 2011
1 parent a350ff4 commit 7fd3dea
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/Perl6/Actions.pm
Expand Up @@ -2402,7 +2402,7 @@ sub make_smartmatch($/, $negated) {
my $rhs := $/[1].ast;
my $old_topic_var := $lhs.unique('old_topic');
my $result_var := $lhs.unique('sm_result');
my $past := PAST::Op.new(
PAST::Op.new(
:pasttype('stmts'),

# Stash original $_.
Expand All @@ -2421,9 +2421,12 @@ sub make_smartmatch($/, $negated) {
# return value to a result variable.
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name($result_var), :scope('lexical'), :isdecl(1) ),
PAST::Op.new( :pasttype('callmethod'), :name('ACCEPTS'),
$rhs,
PAST::Var.new( :name('$_'), :scope('lexical') )
PAST::Op.new( :pasttype('call'), :name('&coerce-smartmatch-result'),
PAST::Op.new( :pasttype('callmethod'), :name('ACCEPTS'),
$rhs,
PAST::Var.new( :name('$_'), :scope('lexical') )
),
$negated
)
),

Expand All @@ -2436,14 +2439,6 @@ sub make_smartmatch($/, $negated) {
# And finally evaluate to the smart-match result.
PAST::Var.new( :name($result_var), :scope('lexical') )
);
if $negated {
$past := PAST::Op.new(
:pasttype('call'),
:name('&prefix:<!>'),
$past
);
}
$past;
}

method prefixish($/) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Any-list.pm
Expand Up @@ -214,10 +214,10 @@ augment class Any {
self.roll(Inf);
}

multi method classify($test) {
multi method classify(&test) {
my %result;
for @.list {
my $k = $_ ~~ $test;
my $k = test $_;
%result{$k} //= [];
%result{$k}.push: $_;
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/Match.pm
Expand Up @@ -210,4 +210,11 @@ multi sub infix:<eqv>(Match $a, Match $b) {
&& $a.hash eqv $b.hash
}

# A helper function used by Perl6/Actions.pm. The real point is to
# ensure that Parrot objects returned by .ACCEPTS are converted to
# Bools.
our multi coerce-smartmatch-result($x, 1) { ! $x }
our multi coerce-smartmatch-result(Mu $x, 0) { ? $x }
our multi coerce-smartmatch-result(Match $x, 0) { $x }

# vim: ft=perl6
2 changes: 1 addition & 1 deletion src/core/operators.pm
@@ -1,5 +1,5 @@
our multi infix:<~~>(Mu $topic, Mu $matcher) {
$matcher.ACCEPTS($topic)
? $matcher.ACCEPTS($topic)
}

our multi infix:<~~>(Mu $topic, Regex $matcher) {
Expand Down

0 comments on commit 7fd3dea

Please sign in to comment.