Skip to content

Commit

Permalink
Get several more cases of smart-matching to work again.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Mar 13, 2010
1 parent 3ddd002 commit 5ec16a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/core/Any-str.pm
@@ -1,4 +1,8 @@
augment class Any {
method ACCEPTS($topic) {
self === $topic
}

our Int multi method bytes() is export {
pir::box__PI(pir::bytelength__IS(self))
}
Expand Down
7 changes: 6 additions & 1 deletion src/core/EnumMap.pm
Expand Up @@ -21,7 +21,12 @@ class EnumMap does Associative {
}

multi method ACCEPTS(Regex $topic) {
any(@.keys) ~~ $topic;
for @.keys -> $k {
if $topic.ACCEPTS($k) {
return True;
}
}
False
}

multi method ACCEPTS(%topic) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/Parcel.pm
Expand Up @@ -8,7 +8,7 @@ augment class Parcel {
if self.elems == 0 {
$x.notdef || ($x.does(::Positional) && $x == 0)
} else {
die "Don't know how to smart-match against a Parcel that doesn't happen to be empty";
self.Seq.ACCEPTS($x)
}
}
}
45 changes: 45 additions & 0 deletions src/core/Seq.pm
@@ -1,4 +1,49 @@
augment class Seq {
multi method ACCEPTS(@topic) {
my $self_it = self.iterator();
my $topic_it = @topic.iterator();
loop {
my $cur_self_elem = $self_it.get;
if $cur_self_elem ~~ EMPTY { last }
if $cur_self_elem ~~ Whatever {
# If we just have * left, we're done. Otherwise, we have a
# "target" to look for.
loop {
$cur_self_elem = $self_it.get;
if $cur_self_elem ~~ EMPTY { return True }
unless $cur_self_elem ~~ Whatever {
last;
}
}

# Need to find our target in the topic, if possible.
loop {
my $cur_topic_elem = $topic_it.get;
if $cur_topic_elem ~~ EMPTY {
# Ran out before finding what we wanted.
return False;
}
elsif $cur_topic_elem === $cur_self_elem {
last;
}
}
}
else {
my $cur_topic_elem = $topic_it.get;
if $cur_topic_elem ~~ EMPTY || $cur_topic_elem !=== $cur_self_elem {
return False;
}
}
}

# If we've nothing left to match, we're successful.
$topic_it.get ~~ EMPTY
}

multi method ACCEPTS($topic) {
self.ACCEPTS(@($topic))
}

method elems() { pir::set__IP(self!fill); }

method Str() {
Expand Down

0 comments on commit 5ec16a7

Please sign in to comment.