Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move most of the things in Pair to Enum, which Pair inherits from. Br…
…ing in various things from master, including ACCEPTS, which has been corrected to meet the latest spec.
- Loading branch information
Showing
3 changed files
with
53 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |