From 4c261c2c6e65ba62ff52cacd4ad12d60f557a300 Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:34:49 -0500 Subject: [PATCH 1/4] Update With.py --- .../templates/With.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 26f2276..05eff75 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -39,3 +39,40 @@ class With3_12(ControlFlowTemplate): {setup_with} {with_body} """ + +class WithCleanup3_10(ControlFlowTemplate): + template = T( + start=~N("reraise", "poptop").with_cond( + starting_instructions("WITH_EXCEPT_START"), # 3.10 + ), + reraise=+N().with_cond(exact_instructions("RERAISE")).with_in_deg(1), + poptop=~N("tail.", None).with_cond(starting_instructions("POP_TOP")).with_in_deg(1), + tail=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop") + + @override + def to_indented_source(self, source): + """ + {poptop} + """ + +@register_template(0, 10, *versions_from(3, 10)) +class With3_10(ControlFlowTemplate): + template = T( + setup_with=~N("with_body", None), + with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), + exc_cleanup=N.tail().of_subtemplate(WithCleanup3_10).with_in_deg(1), + normal_cleanup=~N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "exc_cleanup") + + @to_indented_source + def to_indented_source(): + """ + {setup_with} + {with_body} + {exc_cleanup} + """ \ No newline at end of file From 5f13e32e2d48047a41ab4573439edbd92d03a39c Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Mon, 9 Jun 2025 14:16:23 -0500 Subject: [PATCH 2/4] With 3.8 and 3.9 Refactoring for With 3.9 since its mostly the same as With 3.10 Implementation for With 3.8 --- .../templates/With.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 05eff75..c1676e9 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -40,10 +40,10 @@ class With3_12(ControlFlowTemplate): {with_body} """ -class WithCleanup3_10(ControlFlowTemplate): +class WithCleanup3_9(ControlFlowTemplate): template = T( start=~N("reraise", "poptop").with_cond( - starting_instructions("WITH_EXCEPT_START"), # 3.10 + starting_instructions("WITH_EXCEPT_START"), # 3.9 & 3.10 ), reraise=+N().with_cond(exact_instructions("RERAISE")).with_in_deg(1), poptop=~N("tail.", None).with_cond(starting_instructions("POP_TOP")).with_in_deg(1), @@ -58,12 +58,12 @@ class WithCleanup3_10(ControlFlowTemplate): {poptop} """ -@register_template(0, 10, *versions_from(3, 10)) -class With3_10(ControlFlowTemplate): +@register_template(0, 10, (3, 9), (3, 10)) +class With3_9(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), - exc_cleanup=N.tail().of_subtemplate(WithCleanup3_10).with_in_deg(1), + exc_cleanup=N.tail().of_subtemplate(WithCleanup3_9).with_in_deg(1), normal_cleanup=~N.tail(), ) @@ -75,4 +75,22 @@ class With3_10(ControlFlowTemplate): {setup_with} {with_body} {exc_cleanup} + """ + +@register_template(0, 10, (3, 8)) +class With3_8(ControlFlowTemplate): + template = T( + setup_with=~N("with_body", None), + with_body=N("begin_finally.", None, "normal_cleanup").with_in_deg(1), + begin_finally=~N("normal_cleanup.", None).with_in_deg(1), + normal_cleanup=~N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "begin_finally") + + @to_indented_source + def to_indented_source(): + """ + {setup_with} + {with_body} """ \ No newline at end of file From 133de2be4482fc096e0d31749e063ca807c30560 Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Mon, 9 Jun 2025 15:05:58 -0500 Subject: [PATCH 3/4] Update With.py Forgot to_indented_source decorator --- pylingual/control_flow_reconstruction/templates/With.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index c1676e9..ec801d2 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -52,7 +52,7 @@ class WithCleanup3_9(ControlFlowTemplate): try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop") - @override + @to_indented_source def to_indented_source(self, source): """ {poptop} From 263dcf9eca58e1b784881cd01b7bf20fc2de22d2 Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:43:57 -0500 Subject: [PATCH 4/4] Update With.py With 3.6 & 3.7 --- .../control_flow_reconstruction/templates/With.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index ec801d2..112f68b 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -6,6 +6,7 @@ from ..utils import T, N, exact_instructions, starting_instructions, to_indented class WithCleanup3_12(ControlFlowTemplate): template = T( start=N("reraise", "poptop", "exc").with_cond( + exact_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START", "POP_JUMP_FORWARD_IF_TRUE"), # 3.11 exact_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START", "POP_JUMP_IF_TRUE"), # 3.12 exact_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START", "TO_BOOL", "POP_JUMP_IF_TRUE"), # 3.13 ), @@ -22,7 +23,7 @@ class WithCleanup3_12(ControlFlowTemplate): return [] -@register_template(0, 10, *versions_from(3, 12)) +@register_template(0, 10, (3, 11), (3, 12), (3, 13)) class With3_12(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), @@ -77,16 +78,16 @@ class With3_9(ControlFlowTemplate): {exc_cleanup} """ -@register_template(0, 10, (3, 8)) -class With3_8(ControlFlowTemplate): +@register_template(0, 10, (3, 6), (3, 7), (3, 8)) +class With3_6(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), - with_body=N("begin_finally.", None, "normal_cleanup").with_in_deg(1), - begin_finally=~N("normal_cleanup.", None).with_in_deg(1), + with_body=N("buffer_block.", None, "normal_cleanup").with_in_deg(1), + buffer_block=~N("normal_cleanup.", None).with_in_deg(1), normal_cleanup=~N.tail(), ) - try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "begin_finally") + try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "buffer_block") @to_indented_source def to_indented_source():