Skip to content

Commit

Permalink
Refactor Positional to follow the model developed for Associative.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Jun 16, 2010
1 parent d982db1 commit 7e58a35
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
18 changes: 9 additions & 9 deletions src/builtins/Positional.pir
Expand Up @@ -24,26 +24,26 @@ use the one in Positional.
.param pmc invocant
.param pmc args :optional
.param int has_args :opt_flag

$I0 = can invocant, 'postcircumfix:<[ ]>'
if $I0 goto object_method
$I0 = isa invocant, 'Mu'
if $I0 goto object_method
foreign:
# XXX not a good idea, this relies on the method being in the namespace
$P0 = get_hll_global ['Positional[::T]'], 'postcircumfix:<[ ]>'
if has_args goto foreign_args
$P1 = invocant.$P0()
.return ($P1)
foreign_args:
unless has_args goto foreign_zen
$P1 = invocant.$P0(args)
.return ($P1)
object_method:
if has_args goto object_args
$P1 = invocant.'postcircumfix:<[ ]>'()
foreign_zen:
$P1 = invocant.$P0()
.return ($P1)
object_args:
object_method:
unless has_args goto object_zen
$P1 = invocant.'postcircumfix:<[ ]>'(args)
.return ($P1)
object_zen:
$P1 = invocant.'postcircumfix:<[ ]>'()
.return ($P1)
.end


Expand Down
28 changes: 14 additions & 14 deletions src/core/Array.pm
Expand Up @@ -6,24 +6,24 @@ augment class Array {
[?&] map { self[$^a] !~~ Proxy }, @indices;
}

our multi method postcircumfix:<[ ]> (Int $index) {
fail "Cannot use negative index $index on {self.WHO}" if $index < 0;
#XXX: .exists calls postcircumfix<[ ]>, so can't perl6ify this for now...
return Q:PIR{
.local pmc self, i, values
method at_pos($pos) {
Q:PIR {
.local pmc self, items
.local int pos
self = find_lex 'self'
i = find_lex '$index'
$I0 = i
inc $I0
values = self.'!fill'($I0)
%r = values[i]
unless null %r goto have_elem
$P0 = find_lex '$pos'
pos = $P0
$I0 = pos + 1
items = self.'!fill'($I0)
%r = items[pos]
unless null %r goto done
%r = new ['Proxy']
setattribute %r, '$!base', values
setattribute %r, '$!key', i
have_elem:
setattribute %r, '$!base', self
$P0 = box pos
setattribute %r, '$!key', $P0
$P0 = get_hll_global ['Bool'], 'True'
setprop %r, 'rw', $P0
done:
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/Associative.pm
Expand Up @@ -10,7 +10,7 @@ role Associative[::T = Mu] {
}
Q:PIR {
$P0 = find_lex '$result'
.tailcall '&infix:<,>'($P0 :flat)
%r = '&infix:<,>'($P0 :flat)
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/core/List.pm
Expand Up @@ -108,18 +108,17 @@ augment class List does Positional {
!$tseq;
}

our multi method postcircumfix:<[ ]>(Int $index) {
fail "Cannot use negative index $index on {self.WHO}" if $index < 0;
method at_pos($pos) {
Q:PIR {
.local pmc self, items
.local pmc self, pos, items
self = find_lex 'self'
$P0 = find_lex '$index'
$I0 = $P0
$I1 = $I0 + 1
pos = find_lex '$pos'
$I0 = pos
$I1 = $I0 + 1
items = self.'!fill'($I1)
%r = items[$I0]
%r = items[$I0]
unless null %r goto done
%r = new ['Perl6Scalar']
%r = new ['Perl6Scalar']
done:
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/Parcel.pm
Expand Up @@ -6,8 +6,7 @@ augment class Parcel does Positional {
# Need this method here to avoid ResizablePMCArray.sort from Parrot.
method sort(&by = &infix:<cmp>) { self.list.sort(&by) }

multi method postcircumfix:<[ ]>($index) { self.flat.[$index] }
multi method postcircumfix:<[ ]>(@index) { self.flat.[@index] }
method at_pos($pos) { self.flat.[$pos] }

multi method ACCEPTS($x) {
self.elems == 0
Expand Down
36 changes: 22 additions & 14 deletions src/core/Positional.pm
Expand Up @@ -3,24 +3,32 @@ role Positional[::T = Mu] {

our multi method postcircumfix:<[ ]>(&block) { self[&block(self.elems)]; }

our multi method postcircumfix:<[ ]>($x) { self[$x.Int]; }
our multi method postcircumfix:<[ ]>(@pos) {
my $result = pir::new__ps('ResizablePMCArray');
for @pos {
pir::push($result, self{$_})
}
Q:PIR {
$P0 = find_lex '$result'
%r = '&infix:<,>'($P0 :flat)
}
}
our multi method postcircumfix:<[ ]>($pos) {
fail "Cannot use negative index $pos on {self.WHO}" if $pos < 0;
self.at_pos($pos)
}
our multi method postcircumfix:<[ ]>(@index) {
method at_pos($pos) {
Q:PIR {
.local pmc result, self, flat
result = root_new ['parrot';'ResizablePMCArray']
.local pmc self, pos
self = find_lex 'self'
$P0 = find_lex '@index'
$P0 = $P0.'flat'()
flat = $P0.'eager'()
loop:
unless flat goto done
$P0 = shift flat
$P0 = '!postcircumfix:<[ ]>'(self, $P0)
push result, $P0
goto loop
pos = find_lex '$pos'
$I0 = pos
%r = self[$I0]
unless null %r goto done
%r = new ['Perl6Scalar']
done:
%r = '&infix:<,>'(result :flat)
}
}

Expand Down

0 comments on commit 7e58a35

Please sign in to comment.