Skip to content

Commit d37ab70

Browse files
hikerhbrunie
authored andcommitted
stfc#2923 Improved conversion by replacing loop with dictionary.
1 parent ede7da8 commit d37ab70

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/psyclone/psyir/backend/sympy_writer.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
'''
4040

4141
import keyword
42-
from typing import Dict, List, Optional, Set, Union
42+
from typing import Dict, Iterable, List, Optional, Set, Union
4343

4444
import sympy
4545
from sympy.parsing.sympy_parser import parse_expr
@@ -102,6 +102,14 @@ class SymPyWriter(FortranWriter):
102102
# This class attribute will get initialised in __init__:
103103
_RESERVED_NAMES: Set[str] = set()
104104

105+
# A mapping of PSyIR's logical binary operations to the required
106+
# SymPy format:
107+
_BINARY_OP_MAPPING: Dict[BinaryOperation.Operator, str] = \
108+
{BinaryOperation.Operator.AND: "And({lhs}, {rhs})",
109+
BinaryOperation.Operator.OR: "Or({lhs}, {rhs})",
110+
BinaryOperation.Operator.EQV: "Equivalent({lhs}, {rhs})",
111+
BinaryOperation.Operator.NEQV: "Xor({lhs}, {rhs})"}
112+
105113
def __init__(self):
106114
super().__init__()
107115

@@ -255,7 +263,7 @@ def _create_sympy_array_function(
255263

256264
# -------------------------------------------------------------------------
257265
def _create_type_map(self,
258-
list_of_expressions: List[Node],
266+
list_of_expressions: Iterable[Node],
259267
identical_variables: Optional[Dict[str, str]] = None,
260268
all_variables_positive: Optional[bool] = None):
261269
'''This function creates a dictionary mapping each Reference in any
@@ -399,7 +407,7 @@ def type_map(self) -> Dict[str, Union[sympy.core.symbol.Symbol,
399407
# -------------------------------------------------------------------------
400408
def _to_str(
401409
self,
402-
list_of_expressions: Union[Node, List[Node]],
410+
list_of_expressions: Union[Node, Iterable[Node]],
403411
identical_variables: Optional[Dict[str, str]] = None,
404412
all_variables_positive: Optional[bool] = False) -> Union[str,
405413
List[str]]:
@@ -423,14 +431,14 @@ def _to_str(
423431
:returns: the converted strings(s).
424432
425433
'''
426-
is_list = isinstance(list_of_expressions, (tuple, list))
434+
is_list = isinstance(list_of_expressions, (Iterable))
427435
if not is_list:
428436
# Make mypy happy:
429437
assert isinstance(list_of_expressions, Node)
430438
list_of_expressions = [list_of_expressions]
431439

432440
# Make mypy happy:
433-
assert isinstance(list_of_expressions, List)
441+
assert isinstance(list_of_expressions, Iterable)
434442
# Create the type map in `self._sympy_type_map`, which is required
435443
# when converting these strings to SymPy expressions
436444
self._create_type_map(list_of_expressions,
@@ -711,18 +719,11 @@ def binaryoperation_node(self, node: BinaryOperation) -> str:
711719
:param node: a Reference PSyIR BinaryOperation.
712720
713721
'''
714-
for psy_op, sympy_op in [(BinaryOperation.Operator.AND,
715-
"{lhs} & {rhs}"),
716-
(BinaryOperation.Operator.OR,
717-
"{lhs} | {rhs}"),
718-
(BinaryOperation.Operator.EQV,
719-
"Equivalent({lhs}, {rhs})"),
720-
(BinaryOperation.Operator.NEQV,
721-
"~Equivalent({lhs}, {rhs})")]:
722-
if node.operator == psy_op:
723-
lhs = self._visit(node.children[0])
724-
rhs = self._visit(node.children[1])
725-
return sympy_op.format(rhs=rhs, lhs=lhs)
722+
if node.operator in self._BINARY_OP_MAPPING:
723+
lhs = self._visit(node.children[0])
724+
rhs = self._visit(node.children[1])
725+
return self._BINARY_OP_MAPPING[node.operator].format(rhs=rhs,
726+
lhs=lhs)
726727

727728
return super().binaryoperation_node(node)
728729

0 commit comments

Comments
 (0)