From 4ae0840eb16a0e07f0cdc393e86ae4ccf3abfad3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= <inform@tiker.net>
Date: Thu, 6 Jul 2017 12:58:35 -0400
Subject: [PATCH] Address op precedence concerns in #70

---
 loopy/target/python.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/loopy/target/python.py b/loopy/target/python.py
index 99ec42f44..ddfd1e81c 100644
--- a/loopy/target/python.py
+++ b/loopy/target/python.py
@@ -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)
 
-- 
GitLab