Skip to content

Commit

Permalink
!~~ now topicalizes too
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Aug 10, 2010
1 parent 77d4761 commit 9412433
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Perl6/Actions.pm
Expand Up @@ -2218,7 +2218,11 @@ method EXPR($/, $key?) {
return 1;
}
elsif $sym eq '~~' {
make make_smartmatch($/);
make make_smartmatch($/, 0);
return 1;
}
elsif $sym eq '!~~' {
make make_smartmatch($/, 1);
return 1;
}
unless $past {
Expand Down Expand Up @@ -2311,12 +2315,12 @@ sub make_feed($/) {
return $result;
}

sub make_smartmatch($/) {
sub make_smartmatch($/, $negated) {
my $lhs := $/[0].ast;
my $rhs := $/[1].ast;
my $old_topic_var := $lhs.unique('old_topic');
my $result_var := $lhs.unique('sm_result');
PAST::Op.new(
my $past := PAST::Op.new(
:pasttype('stmts'),

# Stash original $_.
Expand Down Expand Up @@ -2350,6 +2354,14 @@ sub make_smartmatch($/) {
# 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
1 change: 1 addition & 0 deletions src/Perl6/Grammar.pm
Expand Up @@ -1837,6 +1837,7 @@ token infix:sym<eqv> { <sym> <O('%chaining')> }
token infix:sym<before> { <sym> <O('%chaining')> }
token infix:sym<after> { <sym> <O('%chaining')> }
token infix:sym<~~> { <sym> <O('%chaining')> <!dumbsmart> }
token infix:sym<!~~> { <sym> <O('%chaining')> <!dumbsmart> }

token dumbsmart {
| <?before \h* 'Bool::'? 'True' »> <.panic("Smartmatch against True always matches; if you mean to test the topic for truthiness, use :so or *.so or ?* instead")>
Expand Down
5 changes: 5 additions & 0 deletions src/core/metaops.pm
Expand Up @@ -270,4 +270,9 @@ our multi sub infix:«+<»($x) { $x }
our multi sub infix+>»($x) { $x }
our multi sub infix:<~&>($x) { $x }

# negated smart-matching needs to be handled syntactically,
# but somebody might still call it by name.
our multi sub infix:<!~~>(Mu $a, Mu $b) { !($a ~~ $b) }
our multi sub infix:<!~~>(Mu $x?) { True }

# vim: ft=perl6

0 comments on commit 9412433

Please sign in to comment.