Write the control flow lowering in native Python and let the transformation rewrite them in functional form. This is a no-op change that simplifies the intermediate code.
PiperOrigin-RevId: 307957130 Change-Id: I67646f91e49046f00bbd83658fce3e1945958d1b
This commit is contained in:
parent
8d72dbe4ea
commit
e9ec3a8b84
@ -53,7 +53,7 @@ class BreakTransformer(converter.Base):
|
||||
return block
|
||||
|
||||
template = """
|
||||
if ag__.not_(var_name):
|
||||
if not var_name:
|
||||
block
|
||||
"""
|
||||
node = templates.replace(
|
||||
@ -100,7 +100,7 @@ class BreakTransformer(converter.Base):
|
||||
|
||||
template = """
|
||||
var_name = False
|
||||
while ag__.and_(lambda: test, lambda: ag__.not_(var_name)):
|
||||
while not var_name and test:
|
||||
body
|
||||
orelse
|
||||
"""
|
||||
@ -150,7 +150,7 @@ class BreakTransformer(converter.Base):
|
||||
# break did not trigger).
|
||||
guarded_orelse = self._guard_if_present(node.orelse, break_var)
|
||||
extra_test = templates.replace_as_expression(
|
||||
'ag__.not_(var_name)', var_name=break_var)
|
||||
'not var_name', var_name=break_var)
|
||||
|
||||
# The extra test is hidden in the AST, which will confuse the static
|
||||
# analysis. To mitigate that, we insert a no-op statement that ensures
|
||||
|
@ -83,7 +83,7 @@ class ContinueCanonicalizationTransformer(converter.Base):
|
||||
block.create_guard_next = False
|
||||
if should_wrap_current:
|
||||
template = """
|
||||
if ag__.not_(var_name):
|
||||
if not var_name:
|
||||
original_node
|
||||
"""
|
||||
cond, = templates.replace(
|
||||
|
@ -256,7 +256,7 @@ class ReturnStatementsTransformer(converter.Base):
|
||||
state = self.state[_Block]
|
||||
if state.create_guard_now:
|
||||
template = """
|
||||
if ag__.not_(do_return_var_name):
|
||||
if not do_return_var_name:
|
||||
original_node
|
||||
"""
|
||||
cond, = templates.replace(
|
||||
@ -285,7 +285,7 @@ class ReturnStatementsTransformer(converter.Base):
|
||||
node.body = self._visit_statement_block(node, node.body)
|
||||
if self.state[_Block].return_used:
|
||||
node.test = templates.replace_as_expression(
|
||||
'ag__.and_(lambda: ag__.not_(control_var), lambda: test)',
|
||||
'not control_var and test',
|
||||
test=node.test,
|
||||
control_var=self.state[_Function].do_return_var_name)
|
||||
|
||||
@ -302,12 +302,12 @@ class ReturnStatementsTransformer(converter.Base):
|
||||
extra_test = anno.getanno(node, anno.Basic.EXTRA_LOOP_TEST, default=None)
|
||||
if extra_test is not None:
|
||||
extra_test = templates.replace_as_expression(
|
||||
'ag__.and_(lambda: ag__.not_(control_var), lambda: extra_test)',
|
||||
'not control_var and extra_test',
|
||||
extra_test=extra_test,
|
||||
control_var=self.state[_Function].do_return_var_name)
|
||||
else:
|
||||
extra_test = templates.replace_as_expression(
|
||||
'ag__.not_(control_var)',
|
||||
'not control_var',
|
||||
control_var=self.state[_Function].do_return_var_name)
|
||||
anno.setanno(node, anno.Basic.EXTRA_LOOP_TEST, extra_test)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user