summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <leonerd@leonerd.org.uk>2021-02-09 19:11:41 +0000
committerPaul "LeoNerd" Evans <leonerd@leonerd.org.uk>2021-02-14 13:40:50 +0000
commit683e0651b057a7be4b2765ceb3d9f6617cd4c464 (patch)
treebcba3118910358d19a593ec51046897bf65be831
parentb086613286770c19d8327e3f4d9d58110a08c2c9 (diff)
downloadperl-683e0651b057a7be4b2765ceb3d9f6617cd4c464.tar.gz
Add B::Deparse support for try/catch syntax
-rw-r--r--lib/B/Deparse.pm25
-rw-r--r--lib/B/Deparse.t9
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/B/Deparse.pm b/lib/B/Deparse.pm
index 96d569acb9..67147f12dd 100644
--- a/lib/B/Deparse.pm
+++ b/lib/B/Deparse.pm
@@ -4057,6 +4057,31 @@ sub pp_leavetry {
return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
}
+sub pp_leavetrycatch {
+ my $self = shift;
+ my ($op) = @_;
+
+ # Expect that the first three kids should be (entertrycatch, poptry, catch)
+ my $entertrycatch = $op->first;
+ $entertrycatch->name eq "entertrycatch" or die "Expected entertrycatch as first child of leavetrycatch";
+
+ my $tryblock = $entertrycatch->sibling;
+ $tryblock->name eq "poptry" or die "Expected poptry as second child of leavetrycatch";
+
+ my $catch = $tryblock->sibling;
+ $catch->name eq "catch" or die "Expected catch as third child of leavetrycatch";
+
+ my $catchblock = $catch->first->sibling;
+ $catchblock->name eq "scope" or die "Expected scope as second child of catch";
+
+ my $trycode = scopeop(0, $self, $tryblock);
+ my $catchvar = $self->padname($catch->targ);
+ my $catchcode = scopeop(0, $self, $catchblock);
+
+ return "try {\n\t$trycode\n\b}\n" .
+ "catch($catchvar) {\n\t$catchcode\n\b}\cK";
+}
+
sub _op_is_or_was {
my ($op, $expect_type) = @_;
my $type = $op->type;
diff --git a/lib/B/Deparse.t b/lib/B/Deparse.t
index fb3a7fc912..24eb445041 100644
--- a/lib/B/Deparse.t
+++ b/lib/B/Deparse.t
@@ -3162,3 +3162,12 @@ $a = int($c == $d != $e);
$a = $b < ($c == $d != $e);
$a = $b == ($c == $d != $e);
$a = $b & $c == $d != $e;
+####
+# try/catch
+# CONTEXT use feature 'try'; no warnings 'experimental::try';
+try {
+ FIRST();
+}
+catch($var) {
+ SECOND();
+}