-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbase.py
81 lines (70 loc) · 2.71 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import abc
import typing
from collections.abc import Sequence
import mypy.nodes
from puya.awst.nodes import Expression
from puya.awst.txn_fields import TxnField
from puya.errors import CodeError
from puya.parse import SourceLocation
from puyapy.awst_build import pytypes
from puyapy.awst_build.eb import _expect as expect
from puyapy.awst_build.eb._base import FunctionBuilder, NotIterableInstanceExpressionBuilder
from puyapy.awst_build.eb._utils import constant_bool_and_error
from puyapy.awst_build.eb.interface import InstanceBuilder, NodeBuilder
from puyapy.awst_build.eb.transaction.txn_fields import PYTHON_TXN_FIELDS, PythonTxnField
class BaseTransactionExpressionBuilder(NotIterableInstanceExpressionBuilder, abc.ABC):
@typing.override
@typing.final
def to_bytes(self, location: SourceLocation) -> Expression:
raise CodeError("cannot serialize transaction type", location)
@abc.abstractmethod
def get_field_value(
self, field: TxnField, typ: pytypes.RuntimeType, location: SourceLocation
) -> InstanceBuilder: ...
@abc.abstractmethod
def get_array_field_value(
self,
field: TxnField,
typ: pytypes.RuntimeType,
index: InstanceBuilder,
location: SourceLocation,
) -> InstanceBuilder: ...
@typing.override
def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
field_data = PYTHON_TXN_FIELDS.get(name)
if field_data is None:
return super().member_access(name, location)
if field_data.field.is_array:
return _ArrayItem(base=self, field_data=field_data, location=location)
else:
return self.get_field_value(field_data.field, field_data.type, location)
@typing.override
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> InstanceBuilder:
return constant_bool_and_error(value=True, location=location, negate=negate)
class _ArrayItem(FunctionBuilder):
def __init__(
self,
base: BaseTransactionExpressionBuilder,
field_data: PythonTxnField,
location: SourceLocation,
):
super().__init__(location)
self.base = base
self.field_data = field_data
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
arg = expect.exactly_one_arg_of_type_else_dummy(
args,
pytypes.UInt64Type,
location,
resolve_literal=True,
)
return self.base.get_array_field_value(
self.field_data.field, self.field_data.type, arg, location
)