Add 3.7 "testfalsel" for looping conditions

This commit is contained in:
rocky
2020-01-14 18:41:39 -05:00
parent e2cbf5f4bd
commit 5f230fa177
6 changed files with 22 additions and 3 deletions

View File

@@ -21,7 +21,6 @@ SKIP_TESTS=(
[test_context.py]=1
[test_coroutines.py]=1 # Parse error
[test_codecs.py]=1
[test_complex.py]=1 # Investigate
[test_crypt.py]=1 # Parse error
[test_ctypes.py]=1 # it fails on its own
[test_curses.py]=1 # Parse error

View File

@@ -641,7 +641,6 @@ class Python37Parser(Python37BaseParser):
expr ::= if_exp_37b
if_exp_37a ::= and_not expr JUMP_FORWARD come_froms expr COME_FROM
if_exp_37b ::= expr jmp_false expr POP_JUMP_IF_FALSE jump_forward_else expr
or ::= expr jmp_true expr
jmp_false_cf ::= POP_JUMP_IF_FALSE COME_FROM
comp_if ::= or jmp_false_cf comp_iter
"""
@@ -922,6 +921,13 @@ class Python37Parser(Python37BaseParser):
testfalse ::= testfalse_not_or
testfalse ::= testfalse_not_and
testfalse ::= or jmp_false COME_FROM
iflaststmtl ::= testexprl c_stmts JUMP_BACK
iflaststmtl ::= testexprl c_stmts JUMP_BACK COME_FROM_LOOP
iflaststmtl ::= testexprl c_stmts JUMP_BACK POP_BLOCK
testexprl ::= testfalsel
testfalsel ::= expr jmp_true
or ::= expr jmp_true expr
and ::= expr JUMP_IF_FALSE_OR_POP expr come_from_opt

View File

@@ -999,6 +999,7 @@ class Python37BaseParser(PythonParser):
"ifstmtl": ifstmt,
"or": or_check,
"testtrue": testtrue,
"testfalsel": testtrue,
"while1elsestmt": while1elsestmt,
"while1stmt": while1stmt,
"try_elsestmtl38": tryelsestmtl3,
@@ -1019,6 +1020,7 @@ class Python37BaseParser(PythonParser):
self.check_reduce["import_from37"] = "AST"
self.check_reduce["or"] = "AST"
self.check_reduce["testtrue"] = "tokens"
self.check_reduce["testfalsel"] = "tokens"
return
def custom_classfunc_rule(self, opname, token, customize, next_token):

View File

@@ -17,7 +17,11 @@ def or_check(self, lhs, n, rule, ast, tokens, first, last):
if load_global == "LOAD_GLOBAL" and load_global.attr == "AssertionError":
return True
first_offset = tokens[first].off2int()
jmp_true_target = ast[1][0].attr
if jmp_true_target < first_offset:
return False
jmp_false = tokens[last]
# If the jmp is backwards
if jmp_false == "POP_JUMP_IF_FALSE":

View File

@@ -12,4 +12,11 @@ def testtrue(self, lhs, n, rule, ast, tokens, first, last):
if pjit == "POP_JUMP_IF_TRUE" and tokens[first].off2int() > pjit.attr:
assert_next = tokens[min(last + 1, n - 1)]
return assert_next != "RAISE_VARARGS_1"
elif rule == ("testfalsel", ("expr", "jmp_true")):
pjit = tokens[min(last - 1, n - 2)]
# If we have a backwards (looping) jump then this is
# really a testtrue. But "asserts" work funny
if pjit == "POP_JUMP_IF_TRUE" and tokens[first].off2int() > pjit.attr:
assert_next = tokens[min(last + 1, n - 1)]
return assert_next == "RAISE_VARARGS_1"
return False

View File

@@ -148,6 +148,7 @@ def customize_for_version37(self, version):
"list_if37_not": (" if not %p%c", (0, 27), 1),
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
"testfalse_not_and": ("not (%c)", 0),
"testfalsel": ("not %c", (0, "expr")),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
"unmap_dict": ("{**%C}", (0, -1, ", **")),