From 4ae0840eb16a0e07f0cdc393e86ae4ccf3abfad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 6 Jul 2017 12:58:35 -0400 Subject: [PATCH 1/3] 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 From 4ab25cbdcec81b1d1d6eec69f30683b38efd7c52 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Mon, 10 Jul 2017 18:24:55 -0500 Subject: [PATCH 2/3] Fix missing import --- loopy/target/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loopy/target/python.py b/loopy/target/python.py index ddfd1e81c..11951abcf 100644 --- a/loopy/target/python.py +++ b/loopy/target/python.py @@ -259,7 +259,7 @@ class PythonASTBuilderBase(ASTBuilderBase): lbound, ubound, inner): ecm = codegen_state.expression_to_code_mapper - from pymbolic.mapper.stringifier import PREC_NONE + from pymbolic.mapper.stringifier import PREC_NONE, PREC_SUM from genpy import For return For( -- GitLab From be3ca1e51d9c29ab288b3bb87356afff8e7e3adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Mon, 10 Jul 2017 20:22:32 -0400 Subject: [PATCH 3/3] Bump version --- loopy/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loopy/version.py b/loopy/version.py index 8516ce006..02244f55d 100644 --- a/loopy/version.py +++ b/loopy/version.py @@ -32,4 +32,4 @@ except ImportError: else: _islpy_version = islpy.version.VERSION_TEXT -DATA_MODEL_VERSION = "v63-islpy%s" % _islpy_version +DATA_MODEL_VERSION = "v64-islpy%s" % _islpy_version -- GitLab