Skip to content

Commit

Permalink
refactor: make SingleEvaluation node work by structural equality rath…
Browse files Browse the repository at this point in the history
…er than instance equality, to allow for (future) AWST serialization formats without references
  • Loading branch information
achidlow committed Aug 8, 2024
1 parent 897a6f5 commit 63b14b9
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/puya/awst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,24 +803,31 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_box_value_expression(self)


@attrs.frozen(init=False, eq=False, hash=False) # use identity equality
@attrs.frozen
class SingleEvaluation(Expression):
"""caveat emptor"""
"""
This node wraps an underlying expression and effectively caches the result of that lowering,
such that regardless of how many times the SingleEvaluation object
(or any object comparing equal to it) appears in the AWST,
the underlying source expression will only be evaluated once.
"""

source: Expression
_id: int = attrs.field()
wtype: WType = attrs.field(init=False, eq=False)
source_location: SourceLocation = attrs.field(eq=False)

def __init__(self, source: Expression):
self.__attrs_init__(
source=source,
source_location=source.source_location,
wtype=source.wtype,
)
@_id.default
def _default_id(self) -> int:
return id(self)

def __eq__(self, other: object) -> bool:
return id(self) == id(other)
@wtype.default
def _wtype(self) -> WType:
return self.source.wtype

def __hash__(self) -> int:
return id(self)
@source_location.default
def _default_source_location(self) -> SourceLocation:
return self.source.source_location

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_single_evaluation(self)
Expand Down

0 comments on commit 63b14b9

Please sign in to comment.