-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinterface.py
336 lines (267 loc) · 10.2 KB
/
interface.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from __future__ import annotations
import abc
import enum
import typing
from collections.abc import Sequence
import attrs
import mypy.nodes
import typing_extensions
from puya import log
from puya.awst.nodes import (
BinaryBooleanOperator,
ConstantValue,
Expression,
Lvalue,
Statement,
)
from puya.errors import CodeError
from puya.parse import SourceLocation
from puya.utils import invert_ordered_binary_op
from puyapy.awst_build import pytypes
logger = log.get_logger(__name__)
@enum.unique
class BuilderComparisonOp(enum.StrEnum):
eq = "=="
ne = "!="
lt = "<"
lte = "<="
gt = ">"
gte = ">="
def reversed(self) -> BuilderComparisonOp:
return BuilderComparisonOp(invert_ordered_binary_op(self.value))
@enum.unique
class BuilderUnaryOp(enum.StrEnum):
positive = "+"
negative = "-"
bit_invert = "~"
@enum.unique
class BuilderBinaryOp(enum.StrEnum):
add = "+"
sub = "-"
mult = "*"
div = "/"
floor_div = "//"
mod = "%"
pow = "**"
mat_mult = "@"
lshift = "<<"
rshift = ">>"
bit_or = "|"
bit_xor = "^"
bit_and = "&"
class NodeBuilder(abc.ABC):
def __init__(self, location: SourceLocation):
self.source_location: typing.Final = location
@property
@abc.abstractmethod
def pytype(self) -> pytypes.PyType | None: ...
@abc.abstractmethod
def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
"""Handle self.name"""
@abc.abstractmethod
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> InstanceBuilder:
"""Handle boolean-ness evaluation, possibly inverted (ie "not" unary operator)"""
class CallableBuilder(NodeBuilder, abc.ABC):
@abc.abstractmethod
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
"""Handle self(args...)"""
_TPyType_co = typing_extensions.TypeVar(
"_TPyType_co", bound=pytypes.PyType, default=pytypes.PyType, covariant=True
)
class InstanceBuilder(NodeBuilder, typing.Generic[_TPyType_co], abc.ABC):
@typing.override
@property
@abc.abstractmethod
def pytype(self) -> _TPyType_co: ...
@abc.abstractmethod
def resolve(self) -> Expression:
"""Produce an expression for use as an intermediary"""
@abc.abstractmethod
def resolve_literal(self, converter: TypeBuilder) -> InstanceBuilder:
"""For use prior to calling resolve(), when a known type is expected,
and implicit conversion is possible without breaking semantic compatibility.
Primarily, this would be as an argument to an algopy function or class, or as an
operand to a binary operator with an algopy-typed object.
When implementing, this function should return self if there are no literals to convert,
or if the literals are not expected to be homogenous (e.g. in the general case of a tuple
with literals)"""
@abc.abstractmethod
def try_resolve_literal(self, converter: TypeBuilder) -> InstanceBuilder | None:
"""Similar to resolve_literal, but in the case where a conversion is possible but the
literal values are the wrong type, don't produce any errors and return None instead.
If no conversion is necessary (ie no literals or it's not meaningful to convert literals
this way, such as a tuple with literals), just return the current instance.
"""
@abc.abstractmethod
def resolve_lvalue(self) -> Lvalue:
"""Produce an expression for the target of an assignment"""
@abc.abstractmethod
def delete(self, location: SourceLocation) -> Statement:
"""Handle del self"""
@abc.abstractmethod
def unary_op(self, op: BuilderUnaryOp, location: SourceLocation) -> InstanceBuilder:
"""Handle {op} self"""
@abc.abstractmethod
def compare(
self, other: InstanceBuilder, op: BuilderComparisonOp, location: SourceLocation
) -> InstanceBuilder:
"""Handle self {op} other"""
@abc.abstractmethod
def binary_op(
self,
other: InstanceBuilder,
op: BuilderBinaryOp,
location: SourceLocation,
*,
reverse: bool,
) -> InstanceBuilder:
"""Handle self {op} other"""
@abc.abstractmethod
def bool_binary_op(
self, other: InstanceBuilder, op: BinaryBooleanOperator, location: SourceLocation
) -> InstanceBuilder:
"""Handle self and/or other"""
from puyapy.awst_build.eb.binary_bool_op import BinaryBoolOpBuilder
return BinaryBoolOpBuilder(left=self, right=other, op=op, location=location)
@abc.abstractmethod
def augmented_assignment(
self, op: BuilderBinaryOp, rhs: InstanceBuilder, location: SourceLocation
) -> Statement:
"""Handle self {op}= rhs"""
@abc.abstractmethod
def contains(self, item: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
"""Handle item in self"""
from puyapy.awst_build.eb._utils import dummy_value
logger.error("expression is not iterable", location=self.source_location)
return dummy_value(pytypes.BoolType, location)
@abc.abstractmethod
def iterate(self) -> Expression:
"""Produce target of ForInLoop"""
raise CodeError("expression is not iterable", self.source_location)
@abc.abstractmethod
def iterable_item_type(self) -> pytypes.PyType:
"""The type of the item if this expression is iterable"""
raise CodeError("expression is not iterable", self.source_location)
@abc.abstractmethod
def index(self, index: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
"""Handle self[index]"""
raise CodeError("expression is not a collection", location)
@abc.abstractmethod
def slice_index(
self,
begin_index: InstanceBuilder | None,
end_index: InstanceBuilder | None,
stride: InstanceBuilder | None,
location: SourceLocation,
) -> InstanceBuilder:
"""Handle self[begin_index:end_index:stride]"""
raise CodeError("expression is not a collection", location)
@abc.abstractmethod
def to_bytes(self, location: SourceLocation) -> Expression:
"""Handle conversion/cast to bytes, if possible"""
@abc.abstractmethod
def single_eval(self) -> InstanceBuilder:
"""wrap any underlying expressions etc. (if applicable) to avoid multiple evaluations"""
class StaticSizedCollectionBuilder(NodeBuilder, abc.ABC):
@abc.abstractmethod
def iterate_static(self) -> Sequence[InstanceBuilder]: ...
class LiteralBuilder(InstanceBuilder, abc.ABC):
@property
@abc.abstractmethod
def value(self) -> ConstantValue: ...
@typing.override
@abc.abstractmethod
def unary_op(self, op: BuilderUnaryOp, location: SourceLocation) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def member_access(self, name: str, location: SourceLocation) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def compare(
self, other: InstanceBuilder, op: BuilderComparisonOp, location: SourceLocation
) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def binary_op(
self,
other: InstanceBuilder,
op: BuilderBinaryOp,
location: SourceLocation,
*,
reverse: bool,
) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def contains(self, item: InstanceBuilder, location: SourceLocation) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def index(self, index: InstanceBuilder, location: SourceLocation) -> LiteralBuilder: ...
@typing.override
@abc.abstractmethod
def slice_index(
self,
begin_index: InstanceBuilder | None,
end_index: InstanceBuilder | None,
stride: InstanceBuilder | None,
location: SourceLocation,
) -> LiteralBuilder: ...
# TODO: separate interface from implementation
class TypeBuilder(CallableBuilder, typing.Generic[_TPyType_co], abc.ABC):
def __init__(self, pytype: _TPyType_co, location: SourceLocation):
super().__init__(location)
self._pytype = pytype
@typing.final
@typing.override
@property
def pytype(self) -> pytypes.TypeType:
return pytypes.TypeType(self._pytype)
@typing.final
def produces(self) -> _TPyType_co:
return self._pytype
@typing.final
def convert_literal(
self, literal: LiteralBuilder, location: SourceLocation
) -> InstanceBuilder:
from puyapy.awst_build.eb._utils import dummy_value
result = self.try_convert_literal(literal, location)
if result is not None:
return result
logger.error(
f"can't covert literal to {self.produces()}", location=literal.source_location
)
return dummy_value(self.produces(), location)
def try_convert_literal(
self, literal: LiteralBuilder, location: SourceLocation
) -> InstanceBuilder | None:
"""
If the type of `literal.value` is correct, return a new instance, otherwise return `None`.
If the value is out of range or otherwise invalid, an error is logged,
but an instance is still returned.
"""
return None
@typing.override
@typing.final
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> InstanceBuilder:
from puyapy.awst_build.eb._utils import constant_bool_and_error
return constant_bool_and_error(value=True, location=location, negate=negate)
@typing.override
def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
raise CodeError(f"unrecognised member {name!r} of type '{self._pytype}'", location)
@attrs.frozen(kw_only=True)
class StorageProxyConstructorArgs:
key: Expression | None
key_arg_name: str
description: str | None
initial_value: InstanceBuilder | None = None
class StorageProxyConstructorResult(
InstanceBuilder[pytypes.StorageProxyType | pytypes.StorageMapProxyType], abc.ABC
):
@property
@abc.abstractmethod
def args(self) -> StorageProxyConstructorArgs: ...