Skip to content

Commit

Permalink
Improve parenthesizing of arithmetic negations in generated code
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux!1474
  • Loading branch information
treiher committed Feb 14, 2024
1 parent 2b71b3a commit c4d1e92
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rflx/ada.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __neg__(self) -> Expr:
return self.expression

def _update_str(self) -> None:
self._str = intern(f"(-{self.parenthesized(self.expression)})")
self._str = intern(f"-{self.parenthesized(self.expression)}")

@property
def precedence(self) -> Precedence:
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/ada_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_not_rflx_expr() -> None:


def test_neg_str() -> None:
assert str(ada.Neg(ada.Variable("X"))) == "(-X)"
assert str(ada.Neg(ada.Neg(ada.Variable("X")))) == "(-((-X)))"
assert str(ada.Neg(ada.Variable("X"))) == "-X"
assert str(ada.Neg(ada.Neg(ada.Variable("X")))) == "-(-X)"


def test_neg_rflx_expr() -> None:
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_composite_relation_rflx_expr(relation: Callable[[ada.Expr, ada.Expr], a

def test_attribute_str() -> None:
assert str(ada.First("X")) == "X'First"
assert str(-ada.First("X")) == "(-X'First)"
assert str(-ada.First("X")) == "-X'First"


def test_literal_rflx_expr() -> None:
Expand All @@ -174,7 +174,7 @@ def test_attribute_expression_str() -> None:

def test_selected_str() -> None:
assert str(ada.Selected(ada.Variable("X"), "Y")) == "X.Y"
assert str(-ada.Selected(ada.Variable("X"), "Y")) == "(-X.Y)"
assert str(-ada.Selected(ada.Variable("X"), "Y")) == "-X.Y"


def test_selected_rflx_expr() -> None:
Expand All @@ -186,7 +186,7 @@ def test_selected_rflx_expr() -> None:

def test_indexed_str() -> None:
assert str(ada.Indexed(ada.Variable("X"), ada.Variable("Y"))) == "X (Y)"
assert str(-ada.Indexed(ada.Variable("X"), ada.Variable("Y"))) == "(-X (Y))"
assert str(-ada.Indexed(ada.Variable("X"), ada.Variable("Y"))) == "-X (Y)"


def test_indexed_rflx_expr() -> None:
Expand Down Expand Up @@ -585,7 +585,7 @@ def test_call_str() -> None:
)
== "A (B, C, D => 1, E => 2)"
)
assert str(-ada.Call("A", [])) == "(-A)"
assert str(-ada.Call("A", [])) == "-A"


def test_conversion_str() -> None:
Expand Down

0 comments on commit c4d1e92

Please sign in to comment.