conditional -> if_exp ...

to match Python IfExp AST
This commit is contained in:
rocky
2020-02-07 16:17:47 -05:00
parent 7fb50918cd
commit 278af38df6
19 changed files with 98 additions and 103 deletions

View File

@@ -22,7 +22,7 @@ assert i[0]('a') == True
assert i[0]('A') == False
# Issue #170. Bug is needing an "conditional_not_lambda" grammar rule
# in addition the the "if_expr_lambda" rule
# in addition the the "if_exp_lambda" rule
j = lambda a: False if not a else True
assert j(True) == True
assert j(False) == False

View File

@@ -8,7 +8,7 @@ list(x for x in range(10) if x % 2 if x % 3)
# expresion which evaluates True unconditionally,
# but leave dead code or junk around that we have to match on.
# Tests "if_expr_true" rule
# Tests "if_exp_true" rule
5 if 1 else 2
0 or max(5, 3) if 0 else 3

View File

@@ -1,6 +1,6 @@
# Bug found in 2.7 test_itertools.py
# Bug was erroneously using reduction to if_expr_true
# A proper fix would be to use if_expr_true only when we
# Bug was erroneously using reduction to if_exp_true
# A proper fix would be to use if_exp_true only when we
# can determine there is or was dead code.
from itertools import izip_longest
for args in [['abc', range(6)]]:

View File

@@ -570,7 +570,7 @@ class PythonParser(GenericASTBuilder):
_mklambda ::= mklambda
expr ::= conditional
expr ::= if_exp
ret_expr ::= expr
ret_expr ::= ret_and

View File

@@ -49,7 +49,7 @@ class Python23Parser(Python24Parser):
and2 ::= _jump jmp_false COME_FROM expr COME_FROM
alias ::= IMPORT_NAME attributes store
conditional ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM
if_exp ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM
'''
def customize_grammar_rules(self, tokens, customize):

View File

@@ -58,7 +58,7 @@ class Python24Parser(Python25Parser):
def remove_rules_24(self):
self.remove_rules("""
expr ::= conditional
expr ::= if_exp
""")

View File

@@ -81,12 +81,11 @@ class Python25Parser(Python26Parser):
return_stmt_lambda ::= ret_expr RETURN_VALUE_LAMBDA
setupwithas ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 setup_finally
stmt ::= classdefdeco
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
if_expr_lambda ::= expr jmp_false_then expr return_if_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
if_exp_lambda ::= expr jmp_false_then expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
conditional_not_lambda
::= expr jmp_true_then expr return_if_lambda
if_exp_not_lambda ::= expr jmp_true_then expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
""")
super(Python25Parser, self).customize_grammar_rules(tokens, customize)

View File

@@ -283,11 +283,11 @@ class Python26Parser(Python2Parser):
kvlist ::= kvlist kv3
# Note: preserve positions 0 2 and 4 for semantic actions
conditional_not ::= expr jmp_true expr jf_cf_pop expr COME_FROM
conditional ::= expr jmp_false expr jf_cf_pop expr come_from_opt
conditional ::= expr jmp_false expr ja_cf_pop expr
if_exp_not ::= expr jmp_true expr jf_cf_pop expr COME_FROM
if_exp ::= expr jmp_false expr jf_cf_pop expr come_from_opt
if_exp ::= expr jmp_false expr ja_cf_pop expr
expr ::= conditional_not
expr ::= if_exp_not
and ::= expr JUMP_IF_FALSE POP_TOP expr JUMP_IF_FALSE POP_TOP
@@ -311,27 +311,27 @@ class Python26Parser(Python2Parser):
compare_chained2 ::= expr COMPARE_OP return_lambda
return_if_lambda ::= RETURN_END_IF_LAMBDA POP_TOP
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
if_expr_lambda ::= expr jmp_false_then expr return_if_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
if_exp_lambda ::= expr jmp_false_then expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
conditional_not_lambda ::=
if_exp_not_lambda ::=
expr jmp_true_then expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
# if_expr_true are for conditions which always evaluate true
# if_exp_true are for conditions which always evaluate true
# There is dead or non-optional remnants of the condition code though,
# and we use that to match on to reconstruct the source more accurately
expr ::= if_expr_true
if_expr_true ::= expr jf_pop expr COME_FROM
expr ::= if_exp_true
if_exp_true ::= expr jf_pop expr COME_FROM
# This comes from
# 0 or max(5, 3) if 0 else 3
# where there seems to be an additional COME_FROM at the
# end. Not sure if this is appropriately named or
# is the best way to handle
expr ::= conditional_false
conditional_false ::= conditional COME_FROM
expr ::= if_exp_false
if_exp_false ::= if_exp COME_FROM
"""
@@ -366,10 +366,10 @@ class Python26Parser(Python2Parser):
if ast[1] is None:
return False
# For now, we won't let the 2nd 'expr' be a "conditional_not"
# For now, we won't let the 2nd 'expr' be a "if_exp_not"
# However in < 2.6 where we don't have if/else expression it *can*
# be.
if self.version >= 2.6 and ast[2][0] == 'conditional_not':
if self.version >= 2.6 and ast[2][0] == "if_exp_not":
return True
test_index = last

View File

@@ -116,17 +116,17 @@ class Python27Parser(Python2Parser):
compare_chained2 ::= expr COMPARE_OP return_lambda
compare_chained2 ::= expr COMPARE_OP return_lambda
# if_expr_true are for conditions which always evaluate true
# if_exp_true are for conditions which always evaluate true
# There is dead or non-optional remnants of the condition code though,
# and we use that to match on to reconstruct the source more accurately.
# FIXME: we should do analysis and reduce *only* if there is dead code?
# right now we check that expr is "or". Any other nodes types?
expr ::= if_expr_true
if_expr_true ::= expr JUMP_FORWARD expr COME_FROM
expr ::= if_exp_true
if_exp_true ::= expr JUMP_FORWARD expr COME_FROM
conditional ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM
conditional ::= expr jmp_false expr JUMP_ABSOLUTE expr
if_exp ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM
if_exp ::= expr jmp_false expr JUMP_ABSOLUTE expr
"""
def p_stmt27(self, args):
@@ -190,16 +190,15 @@ class Python27Parser(Python2Parser):
# Common with 2.6
return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
if_expr_lambda ::= expr jmp_false expr return_if_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
if_exp_lambda ::= expr jmp_false expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
conditional_not_lambda
::= expr jmp_true expr return_if_lambda
if_exp_not_lambda ::= expr jmp_true expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
expr ::= conditional_not
conditional_not ::= expr jmp_true expr _jump expr COME_FROM
expr ::= if_exp_not
if_exp_not ::= expr jmp_true expr _jump expr COME_FROM
kv3 ::= expr expr STORE_MAP
"""
@@ -229,7 +228,7 @@ class Python27Parser(Python2Parser):
}
self.check_reduce["and"] = "AST"
self.check_reduce["conditional"] = "AST"
self.check_reduce["if_exp"] = "AST"
self.check_reduce["except_handler"] = "tokens"
self.check_reduce["except_handler_else"] = "tokens"
@@ -241,7 +240,7 @@ class Python27Parser(Python2Parser):
self.check_reduce["list_if_not"] = "AST"
self.check_reduce["list_if"] = "AST"
self.check_reduce["comp_if"] = "AST"
self.check_reduce["if_expr_true"] = "tokens"
self.check_reduce["if_exp_true"] = "tokens"
self.check_reduce["whilestmt"] = "tokens"
return
@@ -266,7 +265,7 @@ class Python27Parser(Python2Parser):
return tokens[first].offset < jmp_false[0].attr < tokens[last].offset
pass
elif (rule[0], rule[1][0:5]) == (
"conditional",
"if_exp",
("expr", "jmp_false", "expr", "JUMP_ABSOLUTE", "expr")):
jmp_false = ast[1]
if jmp_false[0] == "POP_JUMP_IF_FALSE":
@@ -336,7 +335,7 @@ class Python27Parser(Python2Parser):
while (tokens[i] != "JUMP_BACK"):
i -= 1
return tokens[i].attr != tokens[i-1].attr
elif rule[0] == "if_expr_true":
elif rule[0] == "if_exp_true":
return (first) > 0 and tokens[first-1] == "POP_JUMP_IF_FALSE"
return False

View File

@@ -349,13 +349,12 @@ class Python3Parser(PythonParser):
def p_stmt3(self, args):
"""
stmt ::= if_expr_lambda
stmt ::= if_exp_lambda
stmt ::= conditional_not_lambda
if_expr_lambda ::= expr jmp_false expr return_if_lambda
stmt ::= if_exp_not_lambda
if_exp_lambda ::= expr jmp_false expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
conditional_not_lambda
::= expr jmp_true expr return_if_lambda
if_exp_not_lambda ::= expr jmp_true expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
return_stmt_lambda ::= ret_expr RETURN_VALUE_LAMBDA
@@ -469,18 +468,18 @@ class Python3Parser(PythonParser):
def p_expr3(self, args):
"""
expr ::= LOAD_STR
expr ::= conditionalnot
conditionalnot ::= expr jmp_true expr jump_forward_else expr COME_FROM
expr ::= if_exp_not
if_exp_not ::= expr jmp_true expr jump_forward_else expr COME_FROM
# a JUMP_FORWARD to another JUMP_FORWARD can get turned into
# a JUMP_ABSOLUTE with no COME_FROM
conditional ::= expr jmp_false expr jump_absolute_else expr
if_exp ::= expr jmp_false expr jump_absolute_else expr
# if_expr_true are for conditions which always evaluate true
# if_exp_true are for conditions which always evaluate true
# There is dead or non-optional remnants of the condition code though,
# and we use that to match on to reconstruct the source more accurately
expr ::= if_expr_true
if_expr_true ::= expr JUMP_FORWARD expr COME_FROM
expr ::= if_exp_true
if_exp_true ::= expr JUMP_FORWARD expr COME_FROM
"""
@staticmethod
@@ -705,12 +704,11 @@ class Python3Parser(PythonParser):
stmt ::= assign2_pypy
assign3_pypy ::= expr expr expr store store store
assign2_pypy ::= expr expr store store
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
if_expr_lambda ::= expr jmp_false expr return_if_lambda
return_lambda LAMBDA_MARKER
conditional_not_lambda
::= expr jmp_true expr return_if_lambda
if_exp_not_lambda ::= expr jmp_true expr return_if_lambda
return_lambda LAMBDA_MARKER
""",
nop_func,

View File

@@ -17,7 +17,7 @@ class Python32Parser(Python3Parser):
def p_32to35(self, args):
"""
conditional ::= expr jmp_false expr jump_forward_else expr COME_FROM
if_exp ::= expr jmp_false expr jump_forward_else expr COME_FROM
# compare_chained2 is used in a "chained_compare": x <= y <= z
# used exclusively in compare_chained

View File

@@ -65,7 +65,7 @@ class Python36Parser(Python35Parser):
jf_cf ::= JUMP_FORWARD COME_FROM
cf_jf_else ::= come_froms JUMP_FORWARD ELSE
conditional ::= expr jmp_false expr jf_cf expr COME_FROM
if_exp ::= expr jmp_false expr jf_cf expr COME_FROM
async_for_stmt ::= SETUP_LOOP expr
GET_AITER
@@ -158,8 +158,8 @@ class Python36Parser(Python35Parser):
# that and then we can remove this.
def p_37conditionals(self, args):
"""
expr ::= conditional37
conditional37 ::= expr expr jf_cfs expr COME_FROM
expr ::= if_exp37
if_exp37 ::= expr expr jf_cfs expr COME_FROM
jf_cfs ::= JUMP_FORWARD _come_froms
ifelsestmt ::= testexpr c_stmts_opt jf_cfs else_suite opt_come_from_except
"""

View File

@@ -186,7 +186,7 @@ class Python37Parser(Python37BaseParser):
_mklambda ::= mklambda
expr ::= conditional
expr ::= if_exp
ret_expr ::= expr
ret_expr ::= ret_and
@@ -406,7 +406,7 @@ class Python37Parser(Python37BaseParser):
def p_32on(self, args):
"""
conditional::= expr jmp_false expr jump_forward_else expr COME_FROM
if_exp::= expr jmp_false expr jump_forward_else expr COME_FROM
# compare_chained2 is used in a "chained_compare": x <= y <= z
# used exclusively in compare_chained
@@ -631,8 +631,8 @@ class Python37Parser(Python37BaseParser):
def p_37conditionals(self, args):
"""
expr ::= conditional37
conditional37 ::= expr expr jf_cfs expr COME_FROM
expr ::= if_exp37
if_exp37 ::= expr expr jf_cfs expr COME_FROM
jf_cfs ::= JUMP_FORWARD _come_froms
ifelsestmt ::= testexpr c_stmts_opt jf_cfs else_suite opt_come_from_except
@@ -713,18 +713,18 @@ class Python37Parser(Python37BaseParser):
def p_expr3(self, args):
"""
expr ::= conditionalnot
conditionalnot ::= expr jmp_true expr jump_forward_else expr COME_FROM
expr ::= if_exp_not
if_exp_not ::= expr jmp_true expr jump_forward_else expr COME_FROM
# a JUMP_FORWARD to another JUMP_FORWARD can get turned into
# a JUMP_ABSOLUTE with no COME_FROM
conditional ::= expr jmp_false expr jump_absolute_else expr
if_exp ::= expr jmp_false expr jump_absolute_else expr
# if_expr_true are for conditions which always evaluate true
# if_exp_true are for conditions which always evaluate true
# There is dead or non-optional remnants of the condition code though,
# and we use that to match on to reconstruct the source more accurately
expr ::= if_expr_true
if_expr_true ::= expr JUMP_FORWARD expr COME_FROM
expr ::= if_exp_true
if_exp_true ::= expr JUMP_FORWARD expr COME_FROM
"""
def p_generator_exp3(self, args):
@@ -967,15 +967,15 @@ class Python37Parser(Python37BaseParser):
def p_stmt3(self, args):
"""
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
# If statement inside a loop:
stmt ::= ifstmtl
if_expr_lambda ::= expr jmp_false expr return_if_lambda
if_exp_lambda ::= expr jmp_false expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
conditional_not_lambda
if_exp_not_lambda
::= expr jmp_true expr return_if_lambda
return_stmt_lambda LAMBDA_MARKER
@@ -1092,7 +1092,7 @@ class Python37Parser(Python37BaseParser):
jf_cf ::= JUMP_FORWARD COME_FROM
cf_jf_else ::= come_froms JUMP_FORWARD ELSE
conditional ::= expr jmp_false expr jf_cf expr COME_FROM
if_exp ::= expr jmp_false expr jf_cf expr COME_FROM
async_for_stmt ::= setup_loop expr
GET_AITER

View File

@@ -159,12 +159,11 @@ class Python37BaseParser(PythonParser):
stmt ::= assign2_pypy
assign3_pypy ::= expr expr expr store store store
assign2_pypy ::= expr expr store store
stmt ::= if_expr_lambda
stmt ::= conditional_not_lambda
if_expr_lambda ::= expr jmp_false expr return_if_lambda
stmt ::= if_exp_lambda
stmt ::= if_exp_not_lambda
if_exp_lambda ::= expr jmp_false expr return_if_lambda
return_lambda LAMBDA_MARKER
conditional_not_lambda
::= expr jmp_true expr return_if_lambda
if_exp_not_lambda ::= expr jmp_true expr return_if_lambda
return_lambda LAMBDA_MARKER
""",
nop_func,

View File

@@ -56,11 +56,11 @@ PRECEDENCE = {
'_mklambda': 30,
'conditional': 28, # Conditional expression
'conditional_lamdba': 28, # Lambda expression
'conditional_not_lamdba': 28, # Lambda expression
'conditionalnot': 28,
'if_expr_true': 28,
'if_exp': 28, # If_Exp expression
'if_exp_lamdba': 28, # Lambda expression
'if_exp_not_lamdba': 28, # Lambda expression
'if_exp_not': 28,
'if_exp_true': 28,
'ret_cond': 28,
'or': 26, # Boolean OR
@@ -308,17 +308,17 @@ TABLE_DIRECT = {
'and2': ( '%c', 3 ),
'or': ( '%c or %c', 0, 2 ),
'ret_or': ( '%c or %c', 0, 2 ),
'conditional': ( '%p if %c else %c',
'if_exp': ( '%p if %c else %c',
(2, 'expr', 27), 0, 4 ),
'if_expr_lambda': ( '%p if %c else %c',
'if_exp_lambda': ( '%p if %c else %c',
(2, 'expr', 27), (0, 'expr'), 4 ),
'if_expr_true': ( '%p if 1 else %c', (0, 'expr', 27), 2 ),
'if_exp_true': ( '%p if 1 else %c', (0, 'expr', 27), 2 ),
'ret_cond': ( '%p if %p else %p', (2, 27), (0, 27), (-1, 27) ),
'conditional_not': ( '%p if not %p else %p',
'if_exp_not': ( '%p if not %p else %p',
(2, 27),
(0, "expr", PRECEDENCE['unary_not']),
(4, 27) ),
'conditional_not_lambda':
'if_exp_not_lambda':
( '%p if not %c else %c',
(2, 'expr', 27), 0, 4 ),

View File

@@ -36,7 +36,7 @@ def customize_for_version3(self, version):
TABLE_DIRECT.update(
{
"comp_for": (" for %c in %c", (2, "store"), (0, "expr")),
"conditionalnot": (
"if_exp_not": (
"%c if not %c else %c",
(2, "expr"),
(0, "expr"),

View File

@@ -64,7 +64,7 @@ def customize_for_version36(self, version):
"call_ex": ("%c(%p)", (0, "expr"), (1, 100)),
# This comes from 3.7. Eventually we will rebase from 3.7
# and then this can go away
"conditional37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
"if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
"store_annotation": ("%[1]{pattr}: %c", 0),
"ann_assign_init_value": (
"%|%c = %p\n",

View File

@@ -115,7 +115,7 @@ def customize_for_version37(self, version):
(0, 19),
(6, 19),
),
'conditional37': ( '%p if %c else %c',
'if_exp37': ( '%p if %c else %c',
(1, 'expr', 27), 0, 3 ),
"except_return": ("%|except:\n%+%c%-", 3),

View File

@@ -51,7 +51,7 @@ def find_all_globals(node, globs):
# # print("XXX", n.kind, global_ops)
# if isinstance(n, SyntaxTree):
# # FIXME: do I need a caser for n.kind="mkfunc"?
# if n.kind in ("if_expr_lambda", "return_lambda"):
# if n.kind in ("if_exp_lambda", "return_lambda"):
# globs = find_globals(n, globs, mklambda_globals)
# else:
# globs = find_globals(n, globs, global_ops)