From 04a918d37c9807416f8ce1c81d88f8ad29b97df5 Mon Sep 17 00:00:00 2001 From: pmichaud Date: Tue, 6 Jul 2010 00:15:19 -0500 Subject: [PATCH] Move postcircumfix:<{ }> into Any. Add hash autovivification. --- src/core/Any-list.pm | 28 ++++++++++++++++++++++++++++ src/core/Associative.pm | 17 ----------------- src/core/Hash.pm | 10 ++++------ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/core/Any-list.pm b/src/core/Any-list.pm index 80126455e20..2c8dd0c8ce3 100644 --- a/src/core/Any-list.pm +++ b/src/core/Any-list.pm @@ -1,3 +1,4 @@ +role Hash { ... } class Range { ... } augment class Any { @@ -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); } diff --git a/src/core/Associative.pm b/src/core/Associative.pm index 7de54759313..c03effcae01 100644 --- a/src/core/Associative.pm +++ b/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 diff --git a/src/core/Hash.pm b/src/core/Hash.pm index c4b1159600f..0365dfc4f95 100644 --- a/src/core/Hash.pm +++ b/src/core/Hash.pm @@ -1,5 +1,8 @@ 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' @@ -7,12 +10,7 @@ role Hash is EnumMap { $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: } }