Skip to content

Commit

Permalink
Move postcircumfix:<{ }> into Any. Add hash autovivification.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Jul 6, 2010
1 parent f07a656 commit 04a918d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
28 changes: 28 additions & 0 deletions src/core/Any-list.pm
@@ -1,3 +1,4 @@
role Hash { ... }
class Range { ... }

augment class Any {
Expand Down Expand Up @@ -310,6 +311,33 @@ augment class Any {
);
}
our multi method postcircumfix:<{ }>() {
self.values()
}
our multi method postcircumfix:<{ }>(@keys) {
my $result = pir::new__ps('ResizablePMCArray');
for @keys {
pir::push($result, self{$_})
}
Q:PIR {
$P0 = find_lex '$result'
%r = '&infix:<,>'($P0 :flat)
}
}
our multi method postcircumfix:<{ }>($key) { self.at_key($key) }
method at_key($key) {
fail "postcircumfix:<{ }> not defined for type {self.WHAT}"
if self.defined;
my $z = Any!butWHENCE(
{ self.defined || &infix:<=>(self, Hash.new);
pir::set__vQsP($!storage, $key, $z);
}
);
}
method !butWHENCE(&by) {
pir::setprop__0PsP(pir::clone__PP(pir::descalarref__PP(self)), 'WHENCE', &by);
}
Expand Down
17 changes: 0 additions & 17 deletions src/core/Associative.pm
@@ -1,21 +1,4 @@
role Associative[::T = Mu] {
our multi method postcircumfix:<{ }>() {
self.values()
}

our multi method postcircumfix:<{ }>(@keys) {
my $result = pir::new__ps('ResizablePMCArray');
for @keys {
pir::push($result, self{$_})
}
Q:PIR {
$P0 = find_lex '$result'
%r = '&infix:<,>'($P0 :flat)
}
}
our multi method postcircumfix:<{ }>($key) { self.at_key($key) }
method at_key($key) {
Q:PIR {
.local pmc self, key
Expand Down
10 changes: 4 additions & 6 deletions src/core/Hash.pm
@@ -1,18 +1,16 @@
role Hash is EnumMap {
method at_key($key) {
my $z = Any!butWHENCE(
{ pir::set__vQsP($!storage, $key, $z); }
);
Q:PIR {
.local pmc self
self = find_lex 'self'
$P0 = getattribute self, '$!storage'
$P1 = find_lex '$key'
%r = $P0[$P1]
unless null %r goto done
%r = new ['Proxy']
setattribute %r, '$!base', $P0
setattribute %r, '$!key', $P1
$P2 = get_hll_global ['Bool'], 'True'
setprop %r, 'scalar', $P2
setprop %r, 'rw', $P2
%r = find_lex '$z'
done:
}
}
Expand Down

0 comments on commit 04a918d

Please sign in to comment.