/
Hash.pm
48 lines (46 loc) · 1.38 KB
/
Hash.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
role Hash is EnumMap {
method postcircumfix:<{ }>($key) {
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, 'rw', $P2
done:
}
}
method !STORE(\$to_store) {
# We create a new storage hash, in case we are referenced in
# what is being stored.
pir::setattribute__vPsP(self, '$!storage', pir::new__Ps('Hash'));
# Work through the list, storing the things in it.
my $need_value = 0;
my $key;
for list($to_store) -> $cur {
if $need_value {
self{$key} = $cur;
$need_value = 0;
}
else {
given $cur {
# when Enum { ... }
# when EnumMap { ... }
default {
$key = $cur;
$need_value = 1;
}
}
}
}
if $need_value {
die('Odd number of elements found where hash expected');
}
self
}
}