/
Block.pm
52 lines (46 loc) · 1.69 KB
/
Block.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
49
50
51
52
my class Block { # declared in BOOTSTRAP
# class Block is Code {
# has Mu $!phasers;
method add_phaser(Str $name, &block) {
nqp::isnull($!phasers) &&
nqp::bindattr(self, Block, '$!phasers', nqp::hash());
nqp::existskey($!phasers, nqp::unbox_s($name)) ||
nqp::bindkey($!phasers, nqp::unbox_s($name), nqp::list());
if $name eq 'LEAVE' || $name eq 'KEEP' || $name eq 'UNDO' {
nqp::unshift(nqp::atkey($!phasers, nqp::unbox_s($name)), &block);
self.add_phaser('!LEAVE-ORDER', &block);
}
elsif $name eq 'NEXT' || $name eq '!LEAVE-ORDER' || $name eq 'POST' {
nqp::unshift(nqp::atkey($!phasers, nqp::unbox_s($name)), &block);
}
else {
nqp::push(nqp::atkey($!phasers, nqp::unbox_s($name)), &block);
}
}
method fire_phasers(str $name) {
if !nqp::isnull($!phasers) && nqp::existskey($!phasers, $name) {
my Mu $iter := nqp::iterator(nqp::atkey($!phasers, $name));
nqp::shift($iter).() while $iter;
}
}
method phasers(Str $name) {
unless nqp::isnull($!phasers) {
if nqp::existskey($!phasers, nqp::unbox_s($name)) {
return nqp::p6parcel(nqp::atkey($!phasers, nqp::unbox_s($name)), Mu);
}
}
()
}
method assuming(Block:D $b: |curried) {
anon sub CURRIED (|direct) {
$b(|curried, |direct)
}
}
multi method perl(Block:D:) {
my $perl = '-> ';
$perl ~= substr(self.signature().perl,1); # lose colon prefix
$perl ~= ' { #`(' ~ self.WHICH ~ ') ... }';
$perl
}
}
# vim: ft=perl6 expandtab sw=4