Skip to content

Commit

Permalink
native int variant of postcircumfix:<[ ]>
Browse files Browse the repository at this point in the history
Contains a patch to Perl6::Actions by jnthn++
  • Loading branch information
moritz committed Oct 24, 2011
1 parent b9f048b commit 41441b8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Perl6/Actions.pm
Expand Up @@ -3248,7 +3248,10 @@ class Perl6::Actions is HLL::Actions {

method postcircumfix:sym<[ ]>($/) {
my $past := PAST::Op.new( :name('postcircumfix:<[ ]>'), :pasttype('callmethod'), :node($/) );
if $<semilist><statement> { $past.push($<semilist>.ast); }
if $<semilist><statement> {
my $slast := $<semilist>.ast;
$past.push(+@($slast) == 1 && $slast[0]<boxable_native> ?? $slast[0][2] !! $slast);
}
make $past;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Perl6/Optimizer.pm
Expand Up @@ -280,7 +280,7 @@ class Perl6::Optimizer {
my $i := 0;
while $i < +@($node) {
my $visit := $node[$i];
unless pir::isa($visit, 'String') || pir::isa($visit, 'Integer') {
unless pir::isa($visit, 'String') || pir::isa($visit, 'Integer') || pir::isa($visit, 'Float') {
if $visit.isa(PAST::Op) {
$node[$i] := self.visit_op($visit)
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/Any.pm
Expand Up @@ -112,6 +112,10 @@ my class Any {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
self.at_pos($pos)
}
multi method postcircumfix:<[ ]>(int $pos) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
self.at_pos($pos)
}
multi method postcircumfix:<[ ]>(Positional $pos) is rw {
my $list = $pos.flat;
$list.gimme(*);
Expand Down
8 changes: 7 additions & 1 deletion src/core/Array.pm
Expand Up @@ -7,13 +7,19 @@ class Array {
nqp::p6list($args, self.WHAT, Bool::True);
}

method at_pos($pos is copy) is rw {
multi method at_pos($pos is copy) is rw {
$pos = $pos.Int;
self.exists($pos)
?? nqp::atpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos))
!! pir::setattribute__0PPsP(my $v, Scalar, '$!whence',
-> { nqp::bindpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos), $v) } )
}
multi method at_pos(int $pos ) is rw {
self.exists($pos)
?? nqp::atpos(nqp::getattr(self, List, '$!items'), $pos)
!! pir::setattribute__0PPsP(my $v, Scalar, '$!whence',
-> { nqp::bindpos(nqp::getattr(self, List, '$!items'), $pos, $v) } )
}

method flattens() { 1 }

Expand Down
8 changes: 7 additions & 1 deletion src/core/List.pm
Expand Up @@ -47,12 +47,18 @@ my class List does Positional {
nqp::p6parcel($rpa, Any);
}

method at_pos($pos is copy) is rw {
proto method at_pos($) { * }
multi method at_pos($pos is copy) is rw {
$pos = $pos.Int;
self.exists($pos)
?? nqp::atpos($!items, nqp::unbox_i($pos))
!! Nil
}
multi method at_pos(int $pos) is rw {
self.exists($pos)
?? nqp::atpos($!items, $pos)
!! Nil;
}

method eager() { self.gimme(*); self }

Expand Down

0 comments on commit 41441b8

Please sign in to comment.