-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow using the throwaway ('_') var
- Loading branch information
1 parent
db043f2
commit df7e7bf
Showing
24 changed files
with
941 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import typing | ||
from collections.abc import Sequence | ||
|
||
import mypy.nodes | ||
|
||
from puya.awst import wtypes | ||
from puya.awst.nodes import AssignmentExpression, Expression, Literal, Lvalue, VarExpression | ||
from puya.awst_build.eb.base import ExpressionBuilder | ||
from puya.errors import CodeError, InternalError | ||
from puya.parse import SourceLocation | ||
|
||
|
||
class ThrowawayExpressionBuilder(ExpressionBuilder): | ||
wtype = wtypes.throwaway_type | ||
|
||
def __init__(self, expr: Expression): | ||
super().__init__(expr.source_location) | ||
match expr: | ||
case VarExpression() as target: | ||
pass | ||
case AssignmentExpression(target=target): | ||
# this is an odd use case... | ||
pass | ||
case _: | ||
raise InternalError("Expected a VarExpression", expr.source_location) | ||
self._expr = target | ||
|
||
def lvalue(self) -> Lvalue: | ||
return self._expr | ||
|
||
def rvalue(self) -> typing.Never: | ||
self._raise_error(self.source_location) | ||
|
||
def delete(self, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def index(self, index: ExpressionBuilder | Literal, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def call( | ||
self, | ||
args: Sequence[ExpressionBuilder | Literal], | ||
arg_kinds: list[mypy.nodes.ArgKind], | ||
arg_names: list[str | None], | ||
location: SourceLocation, | ||
) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def member_access(self, name: str, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def iterate(self) -> typing.Never: | ||
self._raise_error(self.source_location) | ||
|
||
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def unary_plus(self, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def unary_minus(self, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def bitwise_invert(self, location: SourceLocation) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def contains( | ||
self, item: ExpressionBuilder | Literal, location: SourceLocation | ||
) -> typing.Never: | ||
self._raise_error(location) | ||
|
||
def _raise_error(self, location: SourceLocation) -> typing.Never: | ||
raise CodeError("'_' variables can only be assigned to", location) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from puyapy import Account, Bytes, Contract, Global, Txn, UInt64, subroutine | ||
|
||
|
||
class Throwaway(Contract): | ||
def approval_program(self) -> bool: | ||
tup = get_tuple() | ||
args, sender, _ = tup | ||
_, _, approval = tup | ||
assert sender == Global.creator_address | ||
assert args == 0 | ||
assert approval | ||
return True | ||
|
||
def clear_state_program(self) -> bool: | ||
return True | ||
|
||
|
||
@subroutine | ||
def get_tuple() -> tuple[UInt64, Account, Bytes]: | ||
return Txn.num_app_args, Txn.sender, Txn.approval_program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Op // Op Description Stack (out) X stack Source code Source line | ||
|
||
#pragma version 10 | ||
|
||
// test_cases.throwaway.contract.Throwaway.approval_program() -> uint64: | ||
main_block@0: | ||
callsub get_tuple // {get_tuple}.0,{get_tuple}.1,{get_tuple}.2 get_tuple() throwaway/contract.py:6 | ||
cover 2 // store tup.2#0 to l-stack (no copy) tup.2#0,{get_tuple}.0,{get_tuple}.1 tup = get_tuple() throwaway/contract.py:6 | ||
// virtual: store tup.1#0 to l-stack (no copy) tup.2#0,tup.1#0,{get_tuple}.0 tup = get_tuple() throwaway/contract.py:6 | ||
// virtual: store tup.0#0 to l-stack (no copy) tup.2#0,tup.0#0,tup.1#0 tup = get_tuple() throwaway/contract.py:6 | ||
global CreatorAddress // tup.2#0,tup.0#0,tup.1#0,{global} Global.creator_address throwaway/contract.py:9 | ||
// virtual: store tmp%0#0 to l-stack (no copy) tup.2#0,tup.0#0,tup.1#0,tmp%0#0 Global.creator_address throwaway/contract.py:9 | ||
// virtual: load tup.1#0 from l-stack (no copy) tup.2#0,tup.0#0,tmp%0#0,tup.1#0 sender == Global.creator_address throwaway/contract.py:9 | ||
// virtual: load tmp%0#0 from l-stack (no copy) tup.2#0,tup.0#0,tup.1#0,tmp%0#0 sender == Global.creator_address throwaway/contract.py:9 | ||
== // tup.2#0,tup.0#0,{==} sender == Global.creator_address throwaway/contract.py:9 | ||
// virtual: store tmp%1#0 to l-stack (no copy) tup.2#0,tup.0#0,tmp%1#0 sender == Global.creator_address throwaway/contract.py:9 | ||
// virtual: load tmp%1#0 from l-stack (no copy) tup.2#0,tup.0#0,tmp%1#0 assert sender == Global.creator_address throwaway/contract.py:9 | ||
assert // tup.2#0,tup.0#0 assert sender == Global.creator_address throwaway/contract.py:9 | ||
// virtual: load tup.0#0 from l-stack (no copy) tup.2#0,tup.0#0 args == 0 throwaway/contract.py:10 | ||
! // tup.2#0,{!} args == 0 throwaway/contract.py:10 | ||
// virtual: store tmp%2#0 to l-stack (no copy) tup.2#0,tmp%2#0 args == 0 throwaway/contract.py:10 | ||
// virtual: load tmp%2#0 from l-stack (no copy) tup.2#0,tmp%2#0 assert args == 0 throwaway/contract.py:10 | ||
assert // tup.2#0 assert args == 0 throwaway/contract.py:10 | ||
// virtual: load tup.2#0 from l-stack (no copy) tup.2#0 approval throwaway/contract.py:11 | ||
len // {len} approval throwaway/contract.py:11 | ||
// virtual: store tmp%3#0 to l-stack (no copy) tmp%3#0 approval throwaway/contract.py:11 | ||
// virtual: load tmp%3#0 from l-stack (no copy) tmp%3#0 assert approval throwaway/contract.py:11 | ||
assert // assert approval throwaway/contract.py:11 | ||
int 1 // 1 True throwaway/contract.py:12 | ||
return // return True throwaway/contract.py:12 | ||
|
||
|
||
// test_cases.throwaway.contract.get_tuple() -> uint64, bytes, bytes: | ||
get_tuple: | ||
proto 0 3 // @subroutine\ndef get_tuple() -> tuple[UInt64, Account, Bytes]: throwaway/contract.py:18-19 | ||
|
||
get_tuple_block@0: | ||
txn NumAppArgs // {txn} Txn.num_app_args throwaway/contract.py:20 | ||
// virtual: store tmp%0#0 to l-stack (no copy) tmp%0#0 Txn.num_app_args throwaway/contract.py:20 | ||
txn Sender // tmp%0#0,{txn} Txn.sender throwaway/contract.py:20 | ||
// virtual: store tmp%1#0 to l-stack (no copy) tmp%0#0,tmp%1#0 Txn.sender throwaway/contract.py:20 | ||
txn ApprovalProgram // tmp%0#0,tmp%1#0,{txn} Txn.approval_program throwaway/contract.py:20 | ||
// virtual: store tmp%2#0 to l-stack (no copy) tmp%0#0,tmp%1#0,tmp%2#0 Txn.approval_program throwaway/contract.py:20 | ||
uncover 2 // load tmp%0#0 from l-stack (no copy) tmp%1#0,tmp%2#0,tmp%0#0 return Txn.num_app_args, Txn.sender, Txn.approval_program throwaway/contract.py:20 | ||
uncover 2 // load tmp%1#0 from l-stack (no copy) tmp%2#0,tmp%0#0,tmp%1#0 return Txn.num_app_args, Txn.sender, Txn.approval_program throwaway/contract.py:20 | ||
uncover 2 // load tmp%2#0 from l-stack (no copy) tmp%0#0,tmp%1#0,tmp%2#0 return Txn.num_app_args, Txn.sender, Txn.approval_program throwaway/contract.py:20 | ||
retsub // tmp%0#0,tmp%1#0,tmp%2#0 return Txn.num_app_args, Txn.sender, Txn.approval_program throwaway/contract.py:20 | ||
|
Oops, something went wrong.