Merge branch 'with-statement-control-flow-template-312' into cflow-refactor

This commit is contained in:
Xinlong Hu
2025-06-13 13:50:01 -05:00
2 changed files with 30 additions and 16 deletions
@@ -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):
@@ -2,43 +2,44 @@ 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_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
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, (3, 11), (3, 12), (3, 13))
class With3_12(ControlFlowTemplate):
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_9(ControlFlowTemplate):