Skip to content

Commit

Permalink
feat: support tuple equality comparisons with literal elements, suppo…
Browse files Browse the repository at this point in the history
…rt tuple repetition & concatenation, and support indexing/slicing literals that support it
  • Loading branch information
achidlow committed Jun 25, 2024
1 parent 8815be8 commit 0c8a745
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 101 deletions.
4 changes: 2 additions & 2 deletions src/puya/awst/wtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class WTuple(WType):
def __init__(self, types: Iterable[WType], source_location: SourceLocation | None):
types = tuple(types)
if not types:
raise CodeError("tuple needs types", source_location)
raise CodeError("empty tuples are not supported", source_location)
if void_wtype in types:
raise CodeError("tuple should not contain void types", source_location)
name = f"tuple<{','.join([t.name for t in types])}>"
Expand Down Expand Up @@ -285,7 +285,7 @@ class ARC4Tuple(ARC4Type):
def __init__(self, types: Iterable[WType], source_location: SourceLocation | None):
types = tuple(types)
if not types:
raise CodeError("ARC4 Tuple cannot be empty", source_location)
raise CodeError("empty tuples are not supported", source_location)
immutable = True
arc4_types = []
for typ_idx, typ in enumerate(types):
Expand Down
2 changes: 1 addition & 1 deletion src/puya/awst_build/eb/_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def pytype(self) -> pytypes.PyType:
def resolve(self) -> Expression:
if isinstance(self.value, bool):
return BoolConstant(value=self.value, source_location=self.source_location)
raise CodeError("A Python literal is not valid at this location", self.source_location)
raise CodeError("a Python literal is not valid at this location", self.source_location)

@typing.override
def resolve_literal(self, converter: LiteralConverter) -> InstanceBuilder:
Expand Down
12 changes: 5 additions & 7 deletions src/puya/awst_build/eb/arc4/abi_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
implicit_operand_conversion,
)
from puya.awst_build.eb.arc4.base import ARC4FromLogBuilder
from puya.awst_build.eb.factories import builder_for_instance
from puya.awst_build.eb.interface import InstanceBuilder, LiteralBuilder, NodeBuilder
from puya.awst_build.eb.subroutine import BaseClassSubroutineInvokerExpressionBuilder
from puya.awst_build.eb.transaction import InnerTransactionExpressionBuilder
from puya.awst_build.eb.transaction.fields import get_field_python_name
from puya.awst_build.eb.transaction.inner_params import get_field_expr
from puya.awst_build.eb.tuple import TupleExpressionBuilder
from puya.awst_build.eb.tuple import TupleLiteralBuilder
from puya.awst_build.utils import (
get_decorators_by_fullname,
require_instance_builder,
Expand Down Expand Up @@ -398,12 +399,9 @@ def append_ref_arg(ref_list: list[Expression], arg: InstanceBuilder) -> None:
else:
raise InternalError("Return type does not match signature type", location)

result_pytype = pytypes.GenericTupleType.parameterise(
[declared_result_pytype, itxn_result_pytype], location
)
tuple_expr = TupleExpression.from_items((abi_result, itxn_tmp), location)
assert tuple_expr.wtype == result_pytype.wtype # TODO: fixme
return TupleExpressionBuilder(tuple_expr, result_pytype)
abi_result_builder = builder_for_instance(declared_result_pytype, abi_result)
itxn_tmp_builder = builder_for_instance(itxn_result_pytype, itxn_tmp)
return TupleLiteralBuilder((abi_result_builder, itxn_tmp_builder), location)


def _add_array_exprs(
Expand Down
4 changes: 4 additions & 0 deletions src/puya/awst_build/eb/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from puya.awst_build.contract_data import AppStorageDeclaration
from puya.errors import CodeError
from puya.parse import SourceLocation
from puya.utils import invert_ordered_binary_op

if typing.TYPE_CHECKING:
from collections.abc import Collection, Sequence
Expand All @@ -28,6 +29,9 @@ class BuilderComparisonOp(enum.StrEnum):
gt = ">"
gte = ">="

def reversed(self) -> BuilderComparisonOp:
return BuilderComparisonOp(invert_ordered_binary_op(self.value))


@enum.unique
class BuilderUnaryOp(enum.StrEnum):
Expand Down
Loading

0 comments on commit 0c8a745

Please sign in to comment.