Merge pull request #36 from XinlongCS/32-with-statement-control-flow-template-310

With Statement Control Flow Template: 3.10 #32
This commit is contained in:
Joel-Flores123
2025-06-11 10:36:10 -05:00
committed by GitHub
@@ -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),
@@ -39,3 +40,58 @@ class With3_12(ControlFlowTemplate):
{setup_with}
{with_body}
"""
class WithCleanup3_9(ControlFlowTemplate):
template = T(
start=~N("reraise", "poptop").with_cond(
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),
tail=N.tail(),
)
try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop")
@to_indented_source
def to_indented_source(self, source):
"""
{poptop}
"""
@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_9).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}
"""
@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}
"""