Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor binding exceptions
  • Loading branch information
moritz committed May 27, 2012
1 parent decb0a5 commit 5f81da3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Perl6/Actions.pm
Expand Up @@ -3806,7 +3806,7 @@ class Perl6::Actions is HLL::Actions {
$was_lexical := 1;
}
unless $was_lexical {
$*W.throw($/, ['X', 'Bind', 'WrongLHS']);
$*W.throw($/, ['X', 'Bind', 'Comp']);
}
}

Expand All @@ -3831,7 +3831,7 @@ class Perl6::Actions is HLL::Actions {
}
# XXX Several more cases to do...
else {
$*W.throw($/, ['X', 'Bind', 'WrongLHS']);
$*W.throw($/, ['X', 'Bind', 'Comp']);
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/core/Any.pm
@@ -1,6 +1,7 @@
my class MapIter { ... }
my class Whatever { ... }
my class Range { ... }
my class X::Bind::Slice { ... }
my class X::Bind::ZenSlice { ... }

my class Any {
Expand Down Expand Up @@ -125,7 +126,9 @@ my class Any {

proto method postcircumfix:<[ ]>(|$) { * }
multi method postcircumfix:<[ ]>() { self.list }
multi method postcircumfix:<[ ]>(:$BIND!) { die(X::Bind::ZenSlice.new()) }
multi method postcircumfix:<[ ]>(:$BIND!) {
X::Bind::ZenSlice.new(type => self.WHAT).throw
}
multi method postcircumfix:<[ ]>(\$self: $pos) is rw {
fail "Cannot use negative index $pos on {$self.WHAT.perl}" if $pos < 0;
$self.at_pos($pos)
Expand Down Expand Up @@ -154,19 +157,19 @@ my class Any {
!! { $self[$_] }).eager.Parcel;
}
multi method postcircumfix:<[ ]>(Positional $pos, :$BIND!) is rw {
die "Cannot bind to an array slice"
X::Bind::Slice.new(type => self.WHAT).throw;
}
multi method postcircumfix:<[ ]>(\$self: Callable $block) is rw {
$self[$block(|($self.elems xx $block.count))]
}
multi method postcircumfix:<[ ]>(Callable $block, :$BIND!) is rw {
die "Cannot bind to a callable array slice"; # WhateverCode?
X::Bind::Slice.new(type => self.WHAT).throw;
}
multi method postcircumfix:<[ ]>(\$self: Whatever) is rw {
$self[^$self.elems]
}
multi method postcircumfix:<[ ]>(Whatever, :$BIND!) is rw {
die "Cannot bind to a whatever array slice"
X::Bind::Slice.new(type => self.WHAT).throw;
}

proto method at_pos(|$) {*}
Expand Down Expand Up @@ -194,7 +197,9 @@ my class Any {
########
proto method postcircumfix:<{ }>(|$) { * }
multi method postcircumfix:<{ }>() { self }
multi method postcircumfix:<{ }>(:$BIND!) { die(X::Bind::ZenSlice.new(:what<hash>)) }
multi method postcircumfix:<{ }>(:$BIND!) {
X::Bind::ZenSlice.new(type => self.WHAT).throw
}
multi method postcircumfix:<{ }>(\$self: $key) is rw {
$self.at_key($key)
}
Expand All @@ -207,13 +212,13 @@ my class Any {
!! $key.map({ $self{$_} }).eager.Parcel
}
multi method postcircumfix:<{ }>(Positional $key, :$BIND!) is rw {
die "Cannot bind to a hash slice"
X::Bind::Slice.new(type => self.WHAT).throw
}
multi method postcircumfix:<{ }>(\$self: Whatever) is rw {
$self{$self.keys}
}
multi method postcircumfix:<{ }>(Whatever, :$BIND!) is rw {
die "Cannot bind to a whatever hash slice"
X::Bind::Slice.new(type => self.WHAT).throw
}

proto method at_key(|$) { * }
Expand Down
23 changes: 17 additions & 6 deletions src/core/Exception.pm
Expand Up @@ -419,18 +419,29 @@ my class X::Method::Private::Unqualified does X::Comp {
}
}

my class X::Bind::WrongLHS does X::Comp {
method message() { 'Cannot use bind operator with this left-hand side' }
my class X::Bind::Comp does X::Comp {
has $.target;
method message() {
$.target.defined
?? "Cannot bind to $.target"
!! 'Cannot use bind operator with this left-hand side'
}
}
my class X::Bind::NativeType does X::Comp {
method message() {
'Cannot bind to a natively typed variable; use assignment instead'
}
}
my class X::Bind::ZenSlice is Exception {
has Str $.what = 'array';

method message() { "Cannot bind to a zen $.what slice." }
my class X::Bind::Slice is Exception {
has $.type;
method message() {
"Cannot bind to {$.type.^name} slice";
}
}
my class X::Bind::ZenSlice is X::Bind::Slice {
method message() {
"Cannot bind to {$.type.^name} zen slice";
}
}

my class X::Value::Dynamic does X::Comp {
Expand Down

0 comments on commit 5f81da3

Please sign in to comment.