Skip to content

Commit 869f54b

Browse files
committed
Add B::Deparse support for try/catch syntax
1 parent b4bcc70 commit 869f54b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/B/Deparse.pm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,6 +4057,31 @@ sub pp_leavetry {
40574057
return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
40584058
}
40594059

4060+
sub pp_leavetrycatch {
4061+
my $self = shift;
4062+
my ($op) = @_;
4063+
4064+
# Expect that the first three kids should be (entertrycatch, poptry, catch)
4065+
my $entertrycatch = $op->first;
4066+
$entertrycatch->name eq "entertrycatch" or die "Expected entertrycatch as first child of leavetrycatch";
4067+
4068+
my $tryblock = $entertrycatch->sibling;
4069+
$tryblock->name eq "poptry" or die "Expected poptry as second child of leavetrycatch";
4070+
4071+
my $catch = $tryblock->sibling;
4072+
$catch->name eq "catch" or die "Expected catch as third child of leavetrycatch";
4073+
4074+
my $catchblock = $catch->first->sibling;
4075+
$catchblock->name eq "scope" or die "Expected scope as second child of catch";
4076+
4077+
my $trycode = scopeop(0, $self, $tryblock);
4078+
my $catchvar = $self->padname($catch->targ);
4079+
my $catchcode = scopeop(0, $self, $catchblock);
4080+
4081+
return "try {\n\t$trycode\n\b}\n" .
4082+
"catch($catchvar) {\n\t$catchcode\n\b}\cK";
4083+
}
4084+
40604085
sub _op_is_or_was {
40614086
my ($op, $expect_type) = @_;
40624087
my $type = $op->type;

lib/B/Deparse.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,3 +3162,12 @@ $a = int($c == $d != $e);
31623162
$a = $b < ($c == $d != $e);
31633163
$a = $b == ($c == $d != $e);
31643164
$a = $b & $c == $d != $e;
3165+
####
3166+
# try/catch
3167+
# CONTEXT use feature 'try'; no warnings 'experimental::try';
3168+
try {
3169+
FIRST();
3170+
}
3171+
catch($var) {
3172+
SECOND();
3173+
}

0 commit comments

Comments
 (0)