117def simplifyExpr(expr, add=None, sub=None):
119 :param expr: string containing an expression to simplify
120 :param add: string containing an expression to add
121 :param sub: string containing an expression to substract
122 :return: simplified expression
123 E.g. simplifyExpr('1+1+I+JI-I') => '2+JI'
124 Note: only additions and substractions are considered
125 addition and subtraction within parentheses are forbidden
129 if re.search(
r'\([^()]*[+-][^()]*\)', expr):
130 raise NotImplementedError(
"Expression cannot (yet) contain + or - sign inside " +
131 f
"parenthesis: {expr}")
136 :return: a list of (sign, abs(value))
139 splt = re.split(
'([+-])', expr.replace(
' ',
'').upper())
143 if len(splt) % 2 == 1:
147 splt = [(splt[2 * i], splt[2 * i + 1])
for i
in range(len(splt) // 2)]
154 splt += [(
'-' if sign ==
'+' else '+', elem)
for (sign, elem)
in split(sub)]
156 for sign, elem
in splt.copy():
157 if (
'+', elem)
in splt
and (
'-', elem)
in splt:
158 splt.remove((
'+', elem))
159 splt.remove((
'-', elem))
162 for i, (sign, elem)
in enumerate(splt.copy()):
167 result = str((1
if splt[found][0] ==
'+' else -1) * int(splt[found][1]) +
168 (1
if sign ==
'+' else -1) * int(elem))
169 splt[found] = split(str(result))[0]
172 splt.sort(key=
''.join)
177 result =
' '.join(s[0] +
' ' + s[1]
for s
in splt)
178 if result.startswith(
'+'):
180 return result.lstrip(
' ')
184def createArrayBounds(lowerBoundstr, upperBoundstr, context):
186 Return a lower-bound and upper-bound node
187 :param lowerBoundstr: string for the fortran lower bound of an array
188 :param upperBoundstr: string for the fortran upper bound of an array
189 :param context: 'DO' for DO loops
190 'DOCONCURRENT' for DO CONCURRENT loops
193 lowerBound = createElem(
'lower-bound')
194 lowerBound.insert(0, createExprPart(lowerBoundstr))
195 upperBound = createElem(
'upper-bound')
196 upperBound.insert(0, createExprPart(upperBoundstr))
198 lowerBound.tail =
', '
199 elif context
in (
'DOCONCURRENT',
'ARRAY'):
200 lowerBound.tail =
':'
202 raise PYFTError(f
'Context unknown in createArrayBounds: {context}')
203 return lowerBound, upperBound