Skip to content

Commit

Permalink
make throw() a method in Perl6::World
Browse files Browse the repository at this point in the history
Also run a test for typed exceptions
  • Loading branch information
moritz committed Dec 17, 2011
1 parent 22d23a8 commit b57f60e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/Perl6/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,6 @@ class Perl6::Actions is HLL::Actions {
nqp::box_s($s, $*W.find_symbol(['Str']));
}

our sub throw($/, $ex_type, *%opts) {
# TODO: provide context
my $type_found := 1;
my $ex := try {
CATCH { $type_found := 0 };
$*W.find_symbol($ex_type);
};
if $type_found {
my $file := pir::find_caller_lex__ps('$?FILES');
%opts<line> := nqp::box_i(
HLL::Compiler.lineof($/.orig, $/.from),
$*W.find_symbol(['Int'])
);
%opts<filename> := p6box_s(
pir::isnull($file) ?? '<unknown file>' !! $file,
);
$ex.new(|%opts).throw;
} else {
my @err := ['Error while compiling, type ', nqp::join('::', $ex_type), "\n"];
for %opts -> $key {
@err.push: ' ';
@err.push: $key;
@err.push: ': ';
@err.push: %opts{$key};
@err.push: "\n";
}
$/.CURSOR.panic(nqp::join('', @err));
}
}

method ints_to_string($ints) {
if pir::does($ints, 'array') {
my $result := '';
Expand Down Expand Up @@ -608,7 +578,7 @@ class Perl6::Actions is HLL::Actions {
my @params;
my $block := $<blockoid>.ast;
if $block<placeholder_sig> && $<signature> {
throw($/, ['X', 'Signature', 'Placeholder']);
$*W.throw($/, ['X', 'Signature', 'Placeholder']);
}
elsif $block<placeholder_sig> {
@params := $block<placeholder_sig>;
Expand Down Expand Up @@ -1137,7 +1107,7 @@ class Perl6::Actions is HLL::Actions {
$past := box_native_if_needed($past, $attr.type);
}
else {
throw($/, ['X', 'Attribute', 'Undeclared'],
$*W.throw($/, ['X', 'Attribute', 'Undeclared'],
name => p6box_s($name),
package-type => p6box_s($*PKGDECL),
package-name => p6box_s($*PACKAGE.HOW.name($*PACKAGE)),
Expand Down Expand Up @@ -1489,7 +1459,7 @@ class Perl6::Actions is HLL::Actions {
# Obtain parameters, create signature object and generate code to
# call binder.
if $block<placeholder_sig> && $<multisig> {
self.throw($/, ['X', 'Signature', 'Placeholder']);
$*W.throw($/, ['X', 'Signature', 'Placeholder']);
}
my @params :=
$<multisig> ?? $<multisig>[0].ast !!
Expand Down
32 changes: 32 additions & 0 deletions src/Perl6/World.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1836,4 +1836,36 @@ class Perl6::World is HLL::World {
$fix
);
}

# throws a typed exception
method throw($/, $ex_type, *%opts) {
# TODO: provide context
my $type_found := 1;
my $ex := try {
CATCH { $type_found := 0 };
self.find_symbol($ex_type);
};
if $type_found {
my $file := pir::find_caller_lex__ps('$?FILES');
%opts<line> := nqp::box_i(
HLL::Compiler.lineof($/.orig, $/.from),
$*W.find_symbol(['Int'])
);
%opts<filename> := nqp::box_s(
pir::isnull($file) ?? '<unknown file>' !! $file,
self.find_symbol(['Str'])
);
$ex.new(|%opts).throw;
} else {
my @err := ['Error while compiling, type ', nqp::join('::', $ex_type), "\n"];
for %opts -> $key {
@err.push: ' ';
@err.push: $key;
@err.push: ': ';
@err.push: %opts{$key};
@err.push: "\n";
}
$/.CURSOR.panic(nqp::join('', @err));
}
}
}
1 change: 1 addition & 0 deletions t/spectest.data
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ S32-array/splice.t
S32-array/unshift.t
S32-basics/warn.t
S32-container/zip.t
S32-exceptions/misc.t
S32-hash/exists.t
# S32-hash/delete.t # err: Too many positional parameters passed
S32-hash/invert.t
Expand Down

0 comments on commit b57f60e

Please sign in to comment.