From ed22e7a30c995c75bc747fa7de239e3350031206 Mon Sep 17 00:00:00 2001 From: Xinlong Hu Date: Tue, 16 Dec 2025 18:32:04 -0600 Subject: [PATCH] 3.14 Loop changes --- .../templates/Loop.py | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/Loop.py b/pylingual/control_flow_reconstruction/templates/Loop.py index 9cb9620..5ef3364 100644 --- a/pylingual/control_flow_reconstruction/templates/Loop.py +++ b/pylingual/control_flow_reconstruction/templates/Loop.py @@ -12,6 +12,7 @@ from ..utils import ( is_not_type, no_back_edges, versions_below, + versions_except, versions_from, ending_instructions, has_no_lines, @@ -29,7 +30,7 @@ if TYPE_CHECKING: -@register_template(0, 1) +@register_template(0, 1, *versions_below(3, 14)) class ForLoop(ControlFlowTemplate): template = T( for_iter=~N("for_body", "tail"), @@ -46,6 +47,23 @@ class ForLoop(ControlFlowTemplate): {for_body} """ +@register_template(0, 1, *versions_from(3, 14)) +class ForLoop3_14(ControlFlowTemplate): + template = T( + for_iter=~N("for_body", "tail").with_cond(with_top_level_instructions("FOR_ITER")), + for_body=~N("for_iter").with_in_deg(1), + tail=N.tail().with_cond(is_not_type(LoopElse)), + ) + + try_match = make_try_match({EdgeKind.Fall: "tail"}, "for_iter", "for_body") + + @to_indented_source + def to_indented_source(): + """ + {for_iter} + {for_body} + """ + @register_template(0, 1) class ForElseLoop(ControlFlowTemplate): @@ -85,7 +103,7 @@ class LoopedReturn(ControlFlowTemplate): {for_body} """ -@register_template(0, 2, *versions_below(3, 10)) +@register_template(0, 2, *versions_except((3, 10), (3, 11), (3, 12), (3, 13))) class SelfLoop3_6(ControlFlowTemplate): template = T( loop_body=~N("loop_body", None) @@ -244,6 +262,49 @@ class WhileIfElseLoop(ControlFlowTemplate): """ +@register_template(1, 39, *versions_from(3, 14)) +class WhileLoop3_14(ControlFlowTemplate): + template = T( + if_header=~N("if_body", "else_body").with_cond(without_top_level_instructions("WITH_EXCEPT_START", "CHECK_EXC_MATCH", "FOR_ITER", "NOP")), + else_body=~N("tail.").with_cond(without_top_level_instructions("RERAISE", "END_FINALLY")).with_in_deg(1), + if_body=~N("if_header").with_in_deg(1), + tail=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "tail"}, "if_header", "if_body", "else_body") + + @to_indented_source + def to_indented_source(): + """ + {if_header} + {if_body} + {else_body?else:} + {else_body} + """ + + +@register_template(1, 39, *versions_from(3, 14)) +class WhileTrueLoop3_14(ControlFlowTemplate): + template = T( + if_header=~N("if_body", "else_body").with_cond(without_top_level_instructions("WITH_EXCEPT_START", "CHECK_EXC_MATCH", "FOR_ITER")).with_cond(starting_instructions("NOP")), + else_body=~N("tail.").with_cond(without_top_level_instructions("RERAISE", "END_FINALLY")).with_in_deg(1), + if_body=~N("if_header").with_in_deg(1), + tail=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "tail"}, "if_header", "if_body", "else_body") + + @to_indented_source + def to_indented_source(): + """ + while True: + {if_header} + {else_body} + {if_body?else:} + {if_body} + """ + + @register_template(0, 3) class InlinedComprehensionTemplate(ControlFlowTemplate): template = T(