From 195fe1379fb99776d77ce6e88a709bb7b60404ef Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Fri, 13 Jun 2025 13:46:48 -0500 Subject: [PATCH 1/2] With statement 3.12/3.13 All With statement 3.12 TC completed and most of With statement 3.13 TC completed --- .../templates/With.py | 64 ++++++++++++------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 05eff75..091327a 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -2,48 +2,50 @@ from typing import override from ..cft import ControlFlowTemplate, EdgeKind, register_template from ..utils import T, N, exact_instructions, starting_instructions, to_indented_source, make_try_match, versions_from - -class WithCleanup3_12(ControlFlowTemplate): +class WithCleanup3_11(ControlFlowTemplate): template = T( start=N("reraise", "poptop", "exc").with_cond( - 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 + starting_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START"), # 3.11 and later ), reraise=N(None, None, "exc").with_cond(exact_instructions("RERAISE")).with_in_deg(1), - poptop=N("tail", None, "exc").with_cond(exact_instructions("POP_TOP")).with_in_deg(1), + poptop=N("pop_exc", None, "exc").with_cond(exact_instructions("POP_TOP")).with_in_deg(1), exc=+N().with_cond(exact_instructions("COPY", "POP_EXCEPT", "RERAISE")).with_in_deg(3), - tail=~N.tail().with_cond(starting_instructions("POP_EXCEPT", "POP_TOP", "POP_TOP")).with_in_deg(1), + pop_exc=~N("tail.", None).with_cond(starting_instructions("POP_EXCEPT", "POP_TOP")).with_in_deg(1), + tail=N.tail(), ) - try_match = make_try_match({}, "start", "reraise", "poptop", "exc", "tail") + try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop", "exc", "pop_exc") - @override + @to_indented_source def to_indented_source(self, source): - return [] + """ + {pop_exc} + """ - -@register_template(0, 10, *versions_from(3, 12)) -class With3_12(ControlFlowTemplate): +@register_template(0, 10, (3, 11), (3, 12), (3, 13)) +class With3_11(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_12).with_in_deg(1), - normal_cleanup=~N.tail(), + with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), + exc_cleanup=~N("tail.", None).of_subtemplate(WithCleanup3_11).with_in_deg(1), + normal_cleanup=~N("tail.", None).with_in_deg(1), + tail=N.tail(), ) - try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "exc_cleanup") + try_match = make_try_match({EdgeKind.Fall: "tail"}, "setup_with", "with_body", "exc_cleanup", "normal_cleanup") @to_indented_source def to_indented_source(): """ {setup_with} {with_body} + {exc_cleanup} """ -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), @@ -52,18 +54,18 @@ class WithCleanup3_10(ControlFlowTemplate): try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop") - @override + @to_indented_source def to_indented_source(self, source): """ {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 +77,22 @@ class With3_10(ControlFlowTemplate): {setup_with} {with_body} {exc_cleanup} + """ + +@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("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", "buffer_block") + + @to_indented_source + def to_indented_source(): + """ + {setup_with} + {with_body} """ \ No newline at end of file From 8993af3fd7f9a9e6ddfb2c3bf4b34fdc33321360 Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Fri, 13 Jun 2025 13:48:39 -0500 Subject: [PATCH 2/2] Await With Edgecase Not sure if it can be optimized into one Await template --- .../templates/Generator.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pylingual/control_flow_reconstruction/templates/Generator.py b/pylingual/control_flow_reconstruction/templates/Generator.py index c6d17e8..c7d21fe 100644 --- a/pylingual/control_flow_reconstruction/templates/Generator.py +++ b/pylingual/control_flow_reconstruction/templates/Generator.py @@ -19,6 +19,19 @@ class Await3_12(ControlFlowTemplate): to_indented_source = defer_source_to("awaited") +@register_template(0, 0) +class AwaitWith3_12(ControlFlowTemplate): + template = T( + awaited=~N("SEND", None).with_cond(no_back_edges), + SEND=~N("YIELD_VALUE", "CLEANUP_THROW").with_in_deg(2).with_cond(exact_instructions("SEND")), + YIELD_VALUE=N("JUMP_BACK_NO_INT", None, "CLEANUP_THROW").with_in_deg(1).with_cond(exact_instructions("YIELD_VALUE")), + JUMP_BACK_NO_INT=~N("SEND", None).with_cond(exact_instructions("JUMP_BACKWARD_NO_INTERRUPT")), + CLEANUP_THROW=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "CLEANUP_THROW"}, "awaited", "SEND", "YIELD_VALUE", "JUMP_BACK_NO_INT") + + to_indented_source = defer_source_to("awaited") @register_template(0, 0) class Generator3_12(ControlFlowTemplate):