Skip to content

Commit

Permalink
Move most of the things in Pair to Enum, which Pair inherits from. Br…
Browse files Browse the repository at this point in the history
…ing in various things from master, including ACCEPTS, which has been corrected to meet the latest spec.
  • Loading branch information
jnthn committed Feb 6, 2010
1 parent bcd6689 commit 951c71c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 55 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -179,6 +179,7 @@ CORE_SOURCES = \
src/core/RangeIter.pm \
src/core/EnumMap.pm \
src/core/Hash.pm \
src/core/Enum.pm \
src/core/IO.pm \
src/core/Parameter.pm \
src/core/Block.pm \
Expand Down
12 changes: 0 additions & 12 deletions src/core/Pair.pm
@@ -1,18 +1,6 @@
augment class Pair {
method value() {
$!value
}

multi method perl() {
# $.key.perl ~ ' => ' ~ $.value.perl;
"Pair.new(:key({$.key.perl}), :value({$.value.perl}))";
}

# I don't see anything in the spec about what
# this should do. This is doing the same thing
# that ~Pair did in old Rakudo master, which didn't
# seem to have Pair.Str.
multi method Str() {
"$.key\t$.value";
}
}
95 changes: 52 additions & 43 deletions src/core/enum.pm
@@ -1,58 +1,67 @@
role Enum[$key, $value, $enumeration] {
method key() {
$key
}

method value() {
$value
augment class Enum {

=begin item ACCEPTS()
Called from smartmatches '$_ ~~ X'.
For C<$_ ~~ Mapping> tests if C<$_{X.key} ~~ X.value>
Else it delegates to a method call '.:Xkey(Xval)'
(TODO: should actually be .Xkey, not .:Xkey).
=end item

multi method ACCEPTS(EnumMap $topic) {
$topic{$.key} ~~ $.value;
}

method WHAT() {
$enumeration
multi method ACCEPTS($topic) {
my $meth_name = $.key;
return (?$topic."$meth_name"()) === (?$.value);
}
}

# XXX Just defining this here, but Rakudo will need it properly before we
# can actually use any of this code.
class EnumMap { }
=begin item fmt
our Str multi Pair::fmt ( Str $format = "%s\t%s" )
Returns the invocant pair formatted by an implicit call to C<sprintf> on
the key and value.
=end item
method fmt(Str $format = "%s\t%s") {
return sprintf($format, $.key, $.value);
}

class Enumeration {
has $!name;
has $.enums;
=begin item kv
method new(::UnderlyingType, $name, @values) {
# Create instance.
my $self = self.bless(*, :name($name));

# Create all of the enum values.
my %value_objects;
my UnderlyingType $current;
for @values -> $value {
my ($key, $val);
if $value ~~ Pair {
$key = $value.key;
$current = $val = $value.value;
}
else {
$key = $value;
$val = $current++;
}
%value_objects{$key} = $val but Enum[$key, $val, $self];
}
Return key and value as a 2-element List.
# Set enum map in place.
pir::setattribute__vPsP($self, '$!enums', EnumMap.new(%value_objects));
=end item
method kv() {
return list($.key, $.value);
}

method defined() {
0
=begin item pairs
=end item
method pairs() {
return self.list();
}

method value() {
$!value
}

method WHAT() {
self
multi method perl() {
# $.key.perl ~ ' => ' ~ $.value.perl;
"Pair.new(:key({$.key.perl}), :value({$.value.perl}))";
}

method Str() {
$!name;
# I don't see anything in the spec about what
# this should do. This is doing the same thing
# that ~Pair did in old Rakudo master, which didn't
# seem to have Pair.Str.
multi method Str() {
"$.key\t$.value";
}
}

0 comments on commit 951c71c

Please sign in to comment.