Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfhm committed Oct 18, 2023
1 parent 2653b84 commit 7560abb
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions loki/transform/transform_scalar_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

from loki.expression import (

Check failure on line 8 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

W0611: Unused Product imported from loki.expression (unused-import)

Check failure on line 8 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

W0611: Unused Scalar imported from loki.expression (unused-import)
Sum, Product, IntLiteral, Scalar, Array, RangeIndex,
TypedSymbol, SubstituteExpressions
SubstituteExpressions
)
from loki.ir import CallStatement
from loki.visitors import FindNodes, Transformer
from loki.tools import as_tuple
from loki.types import BasicType
import pymbolic.primitives as pmbl

Check failure on line 16 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

C0411: third party import "import pymbolic.primitives as pmbl" should be placed before "from loki.expression import Sum, Product, IntLiteral, Scalar, Array, RangeIndex, SubstituteExpressions" (wrong-import-order)


__all__ = [
Expand All @@ -37,18 +38,53 @@ def check_if_scalar_syntax(arg, dummy):
return False


def single_sum(expr):
if isinstance(expr, pmbl.Sum):

Check failure on line 42 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return)
return expr
else:
return Sum((expr,))


def sum_ints(expr):
if isinstance(expr, pmbl.Sum):
n = 0
new_children = []
for c in expr.children:
if isinstance(c, IntLiteral):
n += c.value
elif (isinstance(c, pmbl.Product) and
all(isinstance(cc, IntLiteral) or isinstance(cc,int) for cc in c.children)):

Check failure on line 56 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

R1701: Consider merging these isinstance calls to isinstance(cc, (IntLiteral, int)) (consider-merging-isinstance)
m = 1
for cc in c.children:
if isinstance(cc, IntLiteral):
m = m*cc.value
else:
m = m*cc
n += m
else:
new_children += [c]

if n != 0:
new_children += [IntLiteral(n)]

expr.children = as_tuple(new_children)

Check failure on line 71 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

C0303: Trailing whitespace (trailing-whitespace)


def construct_range_index(lower, length):

if lower == IntLiteral(1):
new_high = length
elif isinstance(lower, IntLiteral) and isinstance(length, IntLiteral):
new_high = IntLiteral(value = length.value + lower.value - 1)
elif isinstance(lower, IntLiteral):
new_high = Sum((length,IntLiteral(value = lower.value - 1)))
new_high = single_sum(length) + IntLiteral(value = lower.value - 1)
elif isinstance(length, IntLiteral):
new_high = Sum((lower,IntLiteral(value = length.value - 1)))
new_high = single_sum(lower) + IntLiteral(value = length.value - 1)
else:
new_high = Sum((lower, length, Product((-1, IntLiteral(1)))))
new_high = single_sum(length) + lower - IntLiteral(1)

sum_ints(new_high)

return RangeIndex((lower, new_high))

Expand All @@ -58,13 +94,12 @@ def process_symbol(symbol, caller, call):
if isinstance(symbol, IntLiteral):

Check failure on line 94 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
return symbol

elif isinstance(symbol, Scalar):
elif not symbol.parents:
if symbol in call.routine.arguments:
return call.arg_map[symbol]

elif isinstance(symbol, TypedSymbol):
if symbol.parents[0] in call.routine.arguments:
return SubstituteExpressions(call.arg_map).visit(symbol)
elif symbol.parents[0] in call.routine.arguments:
return SubstituteExpressions(call.arg_map).visit(symbol.clone(scope=caller))

if call.routine in caller.members and symbol in caller.variables:
return symbol
Expand All @@ -80,11 +115,11 @@ def construct_length(xrange, routine, call):
if isinstance(new_start, IntLiteral) and isinstance(new_stop, IntLiteral):

Check failure on line 115 in loki/transform/transform_scalar_syntax.py

View workflow job for this annotation

GitHub Actions / code checks (3.11)

R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
return IntLiteral(value = new_stop.value - new_start.value + 1)
elif isinstance(new_start, IntLiteral):
return Sum((new_stop, Product((-1,(IntLiteral(value = new_start.value - 1))))))
return single_sum(new_stop) - IntLiteral(value = new_start.value - 1)
elif isinstance(new_stop, IntLiteral):
return Sum((IntLiteral(value = new_stop.value + 1), Product((-1,new_start))))
return single_sum(IntLiteral(value = new_stop.value + 1)) - new_start
else:
return Sum((new_stop, Product((-1,new_start)), IntLiteral(1)))
return single_sum(new_stop) - new_start + IntLiteral(1)


def fix_scalar_syntax(routine):
Expand Down

0 comments on commit 7560abb

Please sign in to comment.