Skip to content
Snippets Groups Projects
Commit 4ae0840e authored by Andreas Klöckner's avatar Andreas Klöckner
Browse files

Address op precedence concerns in #70

parent 4ff0aef2
No related branches found
No related tags found
1 merge request!122Resolve "Enclosing precedences are wrong in Python code generation"
Pipeline #
......@@ -133,15 +133,17 @@ class ExpressionToPythonMapper(StringifyMapper):
def map_if(self, expr, enclosing_prec):
# Synthesize PREC_IFTHENELSE, make sure it is in the right place in the
# operator precedence hierarchy (right above "or").
from pymbolic.mapper.stringifier import PREC_LOGICAL_OR, PREC_NONE
from pymbolic.mapper.stringifier import PREC_LOGICAL_OR
PREC_IFTHENELSE = PREC_LOGICAL_OR - 1 # noqa
return self.parenthesize_if_needed(
"{then} if {cond} else {else_}".format(
then=self.rec(expr.then, PREC_IFTHENELSE),
cond=self.rec(expr.condition, PREC_IFTHENELSE),
else_=self.rec(expr.else_, PREC_IFTHENELSE)),
enclosing_prec, PREC_NONE)
# "1 if 0 if 1 else 2 else 3" is not valid Python.
# So force parens by using an artificially higher precedence.
then=self.rec(expr.then, PREC_LOGICAL_OR),
cond=self.rec(expr.condition, PREC_LOGICAL_OR),
else_=self.rec(expr.else_, PREC_LOGICAL_OR)),
enclosing_prec, PREC_IFTHENELSE)
# }}}
......@@ -265,7 +267,7 @@ class PythonASTBuilderBase(ASTBuilderBase):
"range(%s, %s + 1)"
% (
ecm(lbound, PREC_NONE, "i"),
ecm(ubound, PREC_NONE, "i"),
ecm(ubound, PREC_SUM, "i"),
),
inner)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment