Skip to content

Commit

Permalink
Fix __neg__ for all MathBinExprs
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1797
  • Loading branch information
andrestt committed Oct 28, 2024
1 parent 8b77148 commit 4e18a65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rejection of variable declarations with type `Opaque` (eng/recordflux/RecordFlux#633)
- Fatal error caused by variable in case expression (eng/recordflux/RecordFlux#1800)
- Simplification of expressions with a unary minus operator (eng/recordflux/RecordFlux#1595, eng/recordflux/RecordFlux#1797)
- Evaluation of unary minus applied to binary expressions (eng/recordflux/RecordFlux#1797)

### Changed

Expand Down
18 changes: 17 additions & 1 deletion rflx/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ def _update_str(self) -> None:
f"{self.parenthesized(self.left)}{self.symbol}{self.parenthesized(self.right)}",
)

@abstractmethod
def __neg__(self) -> Expr:
return self.__class__(-self.left, self.right, location=self.location)
raise NotImplementedError

def __contains__(self, item: Expr) -> bool:
return item == self or item in (self.left, self.right)
Expand Down Expand Up @@ -831,6 +832,9 @@ def _check_type_subexpr(self) -> RecordFluxError:


class Sub(MathBinExpr):
def __neg__(self) -> Expr:
return self.__class__(self.right, self.left, location=self.location)

@property
def precedence(self) -> Precedence:
return Precedence.BINARY_ADDING_OPERATOR
Expand All @@ -852,6 +856,9 @@ def symbol(self) -> str:


class Div(MathBinExpr):
def __neg__(self) -> Expr:
return self.__class__(-self.left, self.right, location=self.location)

@property
def precedence(self) -> Precedence:
return Precedence.MULTIPLYING_OPERATOR
Expand Down Expand Up @@ -879,6 +886,9 @@ def symbol(self) -> str:


class Pow(MathBinExpr):
def __neg__(self) -> Expr:
return Neg(self)

@property
def precedence(self) -> Precedence:
return Precedence.HIGHEST_PRECEDENCE_OPERATOR
Expand All @@ -896,6 +906,9 @@ def symbol(self) -> str:


class Mod(MathBinExpr):
def __neg__(self) -> Expr:
return Neg(self)

@property
def precedence(self) -> Precedence:
return Precedence.MULTIPLYING_OPERATOR
Expand Down Expand Up @@ -925,6 +938,9 @@ def symbol(self) -> str:
class Rem(MathBinExpr):
"""Only used by code generator and therefore provides minimum functionality."""

def __neg__(self) -> Expr:
return Neg(self)

@property
def precedence(self) -> Precedence:
return Precedence.MULTIPLYING_OPERATOR
Expand Down

0 comments on commit 4e18a65

Please sign in to comment.