Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Basic implementation of CATCH and a test for try and catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
tene committed Nov 25, 2009
1 parent b42c475 commit ef7eef0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
70 changes: 56 additions & 14 deletions src/NQP/Actions.pm
Expand Up @@ -13,7 +13,7 @@ sub xblock_immediate($xblock) {

sub block_immediate($block) {
$block.blocktype('immediate');
unless $block.symtable() {
unless $block.symtable() || $block.handlers() {
my $stmts := PAST::Stmts.new( :node($block) );
for $block.list { $stmts.push($_); }
$block := $stmts;
Expand Down Expand Up @@ -162,6 +162,46 @@ method statement_control:sym<return>($/) {
make PAST::Op.new( $<EXPR>.ast, :pasttype('return'), :node($/) );
}

method statement_control:sym<CATCH>($/) {
unless @BLOCK[0].handlers() {
@BLOCK[0].handlers([]);
}
my $block := $<block>.ast;
unless $block.arity {
$block.unshift(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :scope('lexical'), :name('$!'), :isdecl(1) ),
PAST::Var.new( :scope('lexical'), :name('$_')),
),
);
$block.unshift( PAST::Var.new( :name('$_'), :scope('parameter') ) );
$block.symbol('$_', :scope('lexical') );
$block.symbol('$!', :scope('lexical') );
$block.arity(1);
}
$block.blocktype('declaration');
@BLOCK[0].handlers.unshift(
PAST::Control.new(
:handle_types_except('CONTROL'),
:node($/),
PAST::Stmts.new(
PAST::Op.new( :pasttype('call'),
$block,
PAST::Var.new( :scope('register'), :name('exception')),
),
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :scope('keyed'),
PAST::Var.new( :scope('register'), :name('exception')),
'handled'
),
1
)
),
)
);
make PAST::Stmts.new(:node($/));
}

method statement_prefix:sym<INIT>($/) {
@BLOCK[0].loadinit.push($<blorst>.ast);
make PAST::Stmts.new(:node($/));
Expand All @@ -172,19 +212,21 @@ method statement_prefix:sym<try>($/) {
if $past.WHAT ne 'PAST::Block()' {
$past := PAST::Block.new($past, :blocktype('immediate'), :node($/));
}
my $default := PAST::Control.new(
:handle_types_except('CONTROL'),
PAST::Stmts.new(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :scope('keyed'),
PAST::Var.new( :scope('register'), :name('exception')),
'handled'
),
1
)
)
);
$past.handlers([$default]);
unless $past.handlers() {
$past.handlers([PAST::Control.new(
:handle_types_except('CONTROL'),
PAST::Stmts.new(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :scope('keyed'),
PAST::Var.new( :scope('register'), :name('exception')),
'handled'
),
1
)
)
)]
);
}
make $past;
}

Expand Down
5 changes: 5 additions & 0 deletions src/NQP/Grammar.pm
Expand Up @@ -177,6 +177,11 @@ token statement_control:sym<for> {
<xblock>
}

token statement_control:sym<CATCH> {
<sym> \s :s
<block>
}

proto token statement_prefix { <...> }
token statement_prefix:sym<INIT> { <sym> <blorst> }

Expand Down
69 changes: 69 additions & 0 deletions t/nqp/44-try-catch.t
@@ -0,0 +1,69 @@
#! nqp

# Tests for try and catch

plan(7);

sub oops($msg = "oops!") { # throw an exception
my $ex := Q:PIR { %r = new ['Exception'] };
$ex<message> := $msg;
pir::throw($ex);
}

my $ok := 1;
try {
oops();
$ok := 0;
}

ok($ok, "exceptions exit a try block");

sub foo() {
try {
return 1;
}
return 0;
}

ok(foo(), "control exceptions are not caught by a try block");

ok(try oops(), "statement prefix form of try works");

{
CATCH { ok(1, "CATCH blocks are invoked when an exception occurs"); }
oops();
}


$ok := 1;
sub bar() {
CATCH { $ok := 0; }
return;
}
bar();
ok($ok, "CATCH blocks ignore control exceptions");

$ok := 1;
{
{
{
oops();
CATCH { $ok := $ok * 2; pir::rethrow($!); }
}
CATCH { $ok := $ok * 2; pir::rethrow($!); }
}
CATCH { $ok := $ok * 2; pir::rethrow($!); }
CATCH { ok($ok == 8, "rethrow and multiple exception handlers work") }
}

$ok := 1;

{
for 1, 2, 3, 4 {
$ok := $ok * 2;
oops();
}
CATCH { my &c := $!<resume>; &c(); }
}

ok($ok == 16, "resuming from resumable exceptions works");

0 comments on commit ef7eef0

Please sign in to comment.