Permalink
Browse files

Make []:(kv|p|k|v) work according to spec

  • Loading branch information...
1 parent 2b77d6d commit 3e586874aba547f638a279d4866352507bbc0425 @lizmat lizmat committed Jun 27, 2013
Showing with 124 additions and 52 deletions.
  1. +124 −52 src/core/Any.pm
View
@@ -148,120 +148,192 @@ my class Any {
proto method postcircumfix:<[ ]>(|) { * }
multi method postcircumfix:<[ ]>() { self.list }
- multi method postcircumfix:<[ ]>(:$p!) { self.pairs }
multi method postcircumfix:<[ ]>(:$kv!) { self.kv }
+ multi method postcircumfix:<[ ]>(:$p!) { self.pairs }
multi method postcircumfix:<[ ]>(:$k!) { self.keys }
multi method postcircumfix:<[ ]>(:$BIND!) {
X::Bind::ZenSlice.new(type => self.WHAT).throw
}
+
+ # @a[$x]
multi method postcircumfix:<[ ]>(\SELF: $pos) is rw {
fail "Cannot use negative index $pos on {SELF.WHAT.perl}" if $pos < 0;
- SELF.at_pos($pos)
+ SELF.at_pos($pos);
}
multi method postcircumfix:<[ ]>($pos, Mu :$BIND! is parcel) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- self.bind_pos($pos, $BIND)
+ self.bind_pos($pos, $BIND);
}
- multi method postcircumfix:<[ ]>($pos, :$p!) is rw {
+ multi method postcircumfix:<[ ]>($pos, :$kv!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $p ?? RWPAIR($pos, self.at_pos($pos)) !! self.at_pos($pos)
+ $kv & !self.exists($pos) ?? () !! ($pos, self.at_pos($pos));
}
- multi method postcircumfix:<[ ]>($pos, :$kv!) is rw {
+ multi method postcircumfix:<[ ]>($pos, :$p!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $kv ?? ($pos, self.at_pos($pos)) !! self.at_pos($pos)
+ $p & !self.exists($pos) ?? () !! RWPAIR($pos, self.at_pos($pos));
}
multi method postcircumfix:<[ ]>($pos, :$k!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $k ?? $pos !! self.at_pos($pos)
+ $k & !self.exists($pos) ?? () !! $pos;
}
+ multi method postcircumfix:<[ ]>($pos, :$v!) is rw {
+ fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
+ $v & !self.exists($pos) ?? () !! self.at_pos($pos);
+ }
+
+ # @a[1]
multi method postcircumfix:<[ ]>(\SELF: int $pos) is rw {
fail "Cannot use negative index $pos on {SELF.WHAT.perl}" if $pos < 0;
- SELF.at_pos($pos)
+ SELF.at_pos($pos);
}
multi method postcircumfix:<[ ]>(int $pos, Mu :$BIND! is parcel) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- self.bind_pos($pos, $BIND)
+ self.bind_pos($pos, $BIND);
}
- multi method postcircumfix:<[ ]>(int $pos, :$p!) is rw {
+ multi method postcircumfix:<[ ]>(int $pos, :$kv!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $p ?? RWPAIR($pos, self.at_pos($pos)) !! self.at_pos($pos)
+ $kv & !self.exists($pos) ?? () !! ($pos, self.at_pos($pos));
}
- multi method postcircumfix:<[ ]>(int $pos, :$kv!) is rw {
+ multi method postcircumfix:<[ ]>(int $pos, :$p!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $kv ?? ($pos, self.at_pos($pos)) !! self.at_pos($pos)
+ $p & !self.exists($pos) ?? () !! RWPAIR($pos, self.at_pos($pos));
}
multi method postcircumfix:<[ ]>(int $pos, :$k!) is rw {
fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
- $k ?? $pos !! self.at_pos($pos)
+ $k & !self.exists($pos) ?? () !! $pos;
}
+ multi method postcircumfix:<[ ]>(int $pos, :$v!) is rw {
+ fail "Cannot use negative index $pos on {self.WHAT.perl}" if $pos < 0;
+ $v & !self.exists($pos) ?? () !! self.at_pos($pos);
+ }
+
+ # @a[@i]
multi method postcircumfix:<[ ]>(\SELF: Positional \pos) is rw {
if nqp::iscont(pos) {
- fail "Cannot use negative index {pos} on {SELF.WHAT.perl}" if pos < 0;
- return SELF.at_pos(pos)
+ SELF.at_pos(pos);
+ }
+ else {
+ my $positions = pos.flat;
+ $positions.gimme(*);
+ if $positions.infinite {
+ my $list = SELF.list;
+ $positions.map( {
+ last if $_ >= $list.gimme($_ + 1);
+ SELF[$_];
+ } ).eager.Parcel;
+ }
+ else {
+ $positions.map( { SELF[$_] } ).eager.Parcel;
+ }
}
- my $list = pos.flat;
- $list.gimme(*);
- $list.map($list.infinite
- ?? { last if $_ >= SELF.list.gimme($_ + 1); SELF[$_] }
- !! { SELF[$_] }).eager.Parcel;
+ }
+ multi method postcircumfix:<[ ]>(Positional $pos, :$BIND!) is rw {
+ X::Bind::Slice.new(type => self.WHAT).throw;
}
multi method postcircumfix:<[ ]>(\SELF: Positional \pos, :$kv!) is rw {
if nqp::iscont(pos) {
- fail "Cannot use negative index {pos} on {SELF.WHAT.perl}" if pos < 0;
- return (pos, SELF.at_pos(pos))
+ SELF[pos]:$kv;
+ }
+ else {
+ my $positions = pos.flat;
+ $positions.gimme(*);
+ if $positions.infinite {
+ my $list = SELF.list;
+ $positions.map( {
+ last if $_ >= $list.gimme($_ + 1);
+ SELF[$_]:$kv;
+ } ).eager.Parcel;
+ }
+ else {
+ $positions.map( { SELF[$_]:$kv } ).eager.Parcel;
+ }
}
- my $list = pos.flat;
- $list.gimme(*);
- $list.map({ last if $_ >= SELF.list.gimme($_ + 1); ($_, SELF[$_]) }).eager.Parcel;
}
multi method postcircumfix:<[ ]>(\SELF: Positional \pos, :$p!) is rw {
if nqp::iscont(pos) {
- fail "Cannot use negative index {pos} on {SELF.WHAT.perl}" if pos < 0;
- return RWPAIR(pos, SELF.at_pos(pos))
+ SELF[pos]:$p;
+ }
+ else {
+ my $positions = pos.flat;
+ $positions.gimme(*);
+ if $positions.infinite {
+ my $list = SELF.list;
+ $positions.map( {
+ last if $_ >= $list.gimme($_ + 1);
+ SELF[$_]:$p;
+ } ).eager.Parcel;
+ }
+ else {
+ $positions.map( { SELF[$_]:$p } ).eager.Parcel;
+ }
}
- my $list = pos.flat;
- $list.gimme(*);
- $list.map({ last if $_ >= SELF.list.gimme($_ + 1); RWPAIR($_, SELF[$_]) }).eager.Parcel;
}
multi method postcircumfix:<[ ]>(\SELF: Positional \pos, :$k!) is rw {
if nqp::iscont(pos) {
- fail "Cannot use negative index {pos} on {SELF.WHAT.perl}" if pos < 0;
- pos
+ SELF[pos]:$k;
+ }
+ else {
+ my $positions = pos.flat;
+ $positions.gimme(*);
+ if $positions.infinite {
+ my $list = SELF.list;
+ $positions.map( {
+ last if $_ >= $list.gimme($_ + 1);
+ SELF[$_]:$k;
+ } ).eager.Parcel;
+ }
+ else {
+ $positions.map( { SELF[$_]:$k } ).eager.Parcel;
+ }
}
- my $list = pos.flat;
- $list.gimme(*);
- $list.map({ last if $_ >= SELF.list.gimme($_ + 1); $_ }).eager.Parcel;
}
multi method postcircumfix:<[ ]>(\SELF: Positional \pos, :$v!) is rw {
if nqp::iscont(pos) {
- fail "Cannot use negative index {pos} on {SELF.WHAT.perl}" if pos < 0;
- SELF.at_pos(pos)
+ SELF[pos]:$v;
+ }
+ else {
+ my $positions = pos.flat;
+ $positions.gimme(*);
+ if $positions.infinite {
+ my $list = SELF.list;
+ $positions.map( {
+ last if $_ >= $list.gimme($_ + 1);
+ SELF[$_]:$v;
+ } ).eager.Parcel;
+ }
+ else {
+ $positions.map( { SELF[$_]:$v } ).eager.Parcel;
+ }
}
- my $list = pos.flat;
- $list.gimme(*);
- $list.map({ last if $_ >= SELF.list.gimme($_ + 1); SELF[$_] }).eager.Parcel;
- }
- multi method postcircumfix:<[ ]>(Positional $pos, :$BIND!) is rw {
- X::Bind::Slice.new(type => self.WHAT).throw;
}
+
+ # @a[->{}]
multi method postcircumfix:<[ ]>(\SELF: Callable $block) is rw {
SELF[$block(|(SELF.elems xx $block.count))]
}
+ multi method postcircumfix:<[ ]>(Callable $block, :$BIND!) is rw {
+ X::Bind::Slice.new(type => self.WHAT).throw;
+ }
multi method postcircumfix:<[ ]>(\SELF: Callable $block, :$kv!) is rw {
- SELF[$block(|(SELF.elems xx $block.count))]:kv
+ SELF[$block(|(SELF.elems xx $block.count))]:$kv
}
multi method postcircumfix:<[ ]>(\SELF: Callable $block, :$p!) is rw {
- SELF[$block(|(SELF.elems xx $block.count))]:p
+ SELF[$block(|(SELF.elems xx $block.count))]:$p
}
multi method postcircumfix:<[ ]>(\SELF: Callable $block, :$k!) is rw {
- SELF[$block(|(SELF.elems xx $block.count))]:k
+ SELF[$block(|(SELF.elems xx $block.count))]:$k
}
- multi method postcircumfix:<[ ]>(Callable $block, :$BIND!) is rw {
- X::Bind::Slice.new(type => self.WHAT).throw;
+ multi method postcircumfix:<[ ]>(\SELF: Callable $block, :$v!) is rw {
+ SELF[$block(|(SELF.elems xx $block.count))]:$v
}
+
+ # @a[*]
multi method postcircumfix:<[ ]>(\SELF: Whatever) is rw {
SELF[^SELF.elems]
}
+ multi method postcircumfix:<[ ]>(Whatever, :$BIND!) is rw {
+ X::Bind::Slice.new(type => self.WHAT).throw;
+ }
multi method postcircumfix:<[ ]>(\SELF: Whatever, :$kv!) is rw {
SELF[^SELF.elems]:$kv
}
@@ -271,8 +343,8 @@ my class Any {
multi method postcircumfix:<[ ]>(\SELF: Whatever, :$k!) is rw {
SELF[^SELF.elems]:$k
}
- multi method postcircumfix:<[ ]>(Whatever, :$BIND!) is rw {
- X::Bind::Slice.new(type => self.WHAT).throw;
+ multi method postcircumfix:<[ ]>(\SELF: Whatever, :$v!) is rw {
+ SELF[^SELF.elems]:$v
}
proto method at_pos(|) {*}

0 comments on commit 3e58687

Please sign in to comment.