Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
75) PR #3394. Added .NOT. support for sympy.

74) PR #3391 towards #3364. Update PSyclone to use fparser 0.2.2.

73) PR #3387. Update CI to use nvhpc 26.3.
Expand Down
19 changes: 18 additions & 1 deletion src/psyclone/psyir/backend/sympy_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from psyclone.psyir.nodes import (
ArrayOfStructuresReference, ArrayReference, BinaryOperation, Call,
DataNode, IntrinsicCall, Literal, Node,
Range, Reference, StructureReference, Schedule)
Range, Reference, StructureReference, Schedule, UnaryOperation)
from psyclone.psyir.symbols import (
ArrayType, RoutineSymbol, ScalarType, SymbolError, SymbolTable)

Expand Down Expand Up @@ -842,6 +842,23 @@ def binaryoperation_node(self, node: BinaryOperation) -> str:

return super().binaryoperation_node(node)

# ------------------------------------------------------------------------
def unaryoperation_node(self, node: UnaryOperation) -> str:
'''This function converts logical unary operations into SymPy format.
``NOT`` is mapped to ``Not(...)``. All other unary operations are
handled by the base class.

:param node: a UnaryOperation PSyIR node.

:returns: the SymPy representation as a string.

'''
if node.operator == UnaryOperation.Operator.NOT:
operand = self._visit(node.children[0])
return f"Not({operand})"

return super().unaryoperation_node(node)
Comment thread
hiker marked this conversation as resolved.

# ------------------------------------------------------------------------
def gen_indices(self,
indices: Iterable[Node],
Expand Down
4 changes: 4 additions & 0 deletions src/psyclone/psyir/frontend/sympy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class FortranPrinter(Printer):
not handle e.g. Fortran Array expressions (a(2:5)), so we specialise the
generic SymPy Printer and handle the necessary conversions.'''

def _print_Not(self, expr) -> str:
'''Called when converting a NOT expression.'''
return f"(.NOT.{self._print(expr.args[0])})"

def _print_And(self, expr) -> str:
'''Called when converting an AND expression.'''
return f"({'.AND.' .join(self._print(i) for i in expr.args)})"
Expand Down
4 changes: 3 additions & 1 deletion src/psyclone/tests/psyir/backend/sympy_writer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ def test_sympy_writer_user_types(fortran_reader, fortran_writer,


@pytest.mark.parametrize("fortran_expr,sympy_str",
[("a .and. b", "And(a, b)"),
[(".not. a", "Not(a)"),
("-1", "-1"),
("a .and. b", "And(a, b)"),
("a .or. b", "Or(a, b)"),
("a .eqv. b", "Equivalent(a, b)"),
("a .neqv. b", "Xor(a, b)"),
Expand Down
3 changes: 3 additions & 0 deletions src/psyclone/tests/psyir/frontend/sympy_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def test_sympy_reader_constructor():
("b(:5:2)", "b(:5:2)"),
("b(2:5:1)", "b(2:5)"),
("b(2:5:2)", "b(2:5:2)"),
(".not. i", ".NOT.i"),
(".not. (i .or. j)",
".NOT.(i .OR. j)"),
("i .and. j", "i .AND. j"),
("i .and. j .and. k",
"i .AND. j .AND. k"),
Expand Down
Loading