-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathunsigned_builtins.py
303 lines (256 loc) · 9.74 KB
/
unsigned_builtins.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
import abc
import typing
from collections.abc import Sequence
import mypy.nodes
from puya import log
from puya.awst.nodes import (
BinaryBooleanOperator,
Enumeration,
Expression,
IntegerConstant,
Lvalue,
Range,
Reversed,
Statement,
UInt64Constant,
)
from puya.errors import CodeError, InternalError
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 GenericTypeBuilder
from puyapy.awst_build.eb.interface import (
BuilderBinaryOp,
BuilderComparisonOp,
BuilderUnaryOp,
InstanceBuilder,
NodeBuilder,
TypeBuilder,
)
from puyapy.awst_build.eb.uint64 import UInt64ExpressionBuilder
logger = log.get_logger(__name__)
class UnsignedRangeBuilder(TypeBuilder):
def __init__(self, location: SourceLocation):
super().__init__(pytypes.urangeType, location)
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
assert set(arg_names) == {None}
uint64_args = [
expect.argument_of_type_else_dummy(in_arg, pytypes.UInt64Type, resolve_literal=True)
for in_arg in args
]
first, rest = expect.at_least_one_arg(
uint64_args, location, default=expect.default_dummy_value(pytypes.UInt64Type)
)
match rest:
case []:
range_stop = first
range_start: InstanceBuilder = UInt64ExpressionBuilder(
UInt64Constant(value=0, source_location=location)
)
range_step: InstanceBuilder = UInt64ExpressionBuilder(
UInt64Constant(value=1, source_location=location)
)
case [range_stop]:
range_start = first
range_step = UInt64ExpressionBuilder(
UInt64Constant(value=1, source_location=location)
)
case [range_stop, range_step, *extra]:
range_start = first
if isinstance(range_step, IntegerConstant) and range_step.value == 0:
logger.error(
"urange step size cannot be zero", location=range_step.source_location
)
if extra:
logger.error(
f"expected at most 3 arguments, got {len(args)}", location=location
)
case _:
raise InternalError("UH OH SPAGHETTI-O's !!! 🤠🍝🌵️", location)
return _RangeIterBuilder(
source_location=location,
start=range_start,
stop=range_stop,
step=range_step,
)
class UnsignedEnumerateBuilder(GenericTypeBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
# note: we actually expect exactly 1, but want to provide special error message
# in case of extra params
sequence, extra = expect.at_least_one_arg(args, location, default=expect.default_raise)
if extra:
logger.error(
"unlike enumerate(), uenumerate() does not support a start parameter "
"(ie, start must always be zero)",
location=location,
)
return _EnumerateIterBuilder(sequence, location)
class ReversedFunctionExpressionBuilder(GenericTypeBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
sequence = expect.exactly_one_arg(args, location, default=expect.default_raise)
return _ReversedIterBuilder(sequence, location)
class _IterableOnlyBuilder(InstanceBuilder, abc.ABC):
@typing.override
def resolve_literal(self, converter: TypeBuilder) -> InstanceBuilder:
return self.try_resolve_literal(converter)
@typing.override
def try_resolve_literal(self, converter: TypeBuilder) -> InstanceBuilder:
return self
@typing.override
def to_bytes(self, location: SourceLocation) -> Expression:
return self._iterable_only(location)
@typing.override
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def resolve(self) -> Expression:
return self._iterable_only(self.source_location)
@typing.override
def resolve_lvalue(self) -> Lvalue:
return self._iterable_only(self.source_location)
@typing.override
def delete(self, location: SourceLocation) -> Statement:
return self._iterable_only(location)
@typing.override
def unary_op(self, op: BuilderUnaryOp, location: SourceLocation) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def contains(self, item: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def index(self, index: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def slice_index(
self,
begin_index: InstanceBuilder | None,
end_index: InstanceBuilder | None,
stride: InstanceBuilder | None,
location: SourceLocation,
) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
return self._iterable_only(location)
@typing.override
def compare(
self, other: InstanceBuilder, op: BuilderComparisonOp, location: SourceLocation
) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def binary_op(
self,
other: InstanceBuilder,
op: BuilderBinaryOp,
location: SourceLocation,
*,
reverse: bool,
) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def bool_binary_op(
self, other: InstanceBuilder, op: BinaryBooleanOperator, location: SourceLocation
) -> InstanceBuilder:
return self._iterable_only(location)
@typing.override
def augmented_assignment(
self, op: BuilderBinaryOp, rhs: InstanceBuilder, location: SourceLocation
) -> Statement:
return self._iterable_only(location)
def _iterable_only(self, location: SourceLocation) -> typing.Never:
raise CodeError("expression is only usable as the source of a for-loop", location)
class _RangeIterBuilder(_IterableOnlyBuilder):
def __init__(
self,
start: InstanceBuilder,
stop: InstanceBuilder,
step: InstanceBuilder,
source_location: SourceLocation,
):
super().__init__(source_location)
self._start = start
self._stop = stop
self._step = step
@typing.override
@property
def pytype(self) -> pytypes.PyType:
return pytypes.urangeType
@typing.override
def iterate(self) -> Expression:
return Range(
start=self._start.resolve(),
stop=self._stop.resolve(),
step=self._step.resolve(),
source_location=self.source_location,
)
@typing.override
def iterable_item_type(self) -> pytypes.PyType:
return pytypes.UInt64Type
@typing.override
def single_eval(self) -> InstanceBuilder:
return _RangeIterBuilder(
start=self._start.single_eval(),
stop=self._stop.single_eval(),
step=self._step.single_eval(),
source_location=self.source_location,
)
class _EnumerateIterBuilder(_IterableOnlyBuilder):
def __init__(self, sequence: InstanceBuilder, source_location: SourceLocation):
super().__init__(source_location)
self._sequence = sequence
@typing.override
@property
def pytype(self) -> pytypes.PyType:
# TODO: this should be parametrised using the sequence item pytype
return pytypes.uenumerateGenericType
@typing.override
def iterate(self) -> Expression:
return Enumeration(expr=self._sequence.iterate(), source_location=self.source_location)
@typing.override
def iterable_item_type(self) -> pytypes.PyType:
item_type = self._sequence.iterable_item_type()
return pytypes.GenericTupleType.parameterise(
[pytypes.UInt64Type, item_type], self.source_location
)
@typing.override
def single_eval(self) -> InstanceBuilder:
return _EnumerateIterBuilder(self._sequence.single_eval(), self.source_location)
class _ReversedIterBuilder(_IterableOnlyBuilder):
def __init__(self, sequence: InstanceBuilder, source_location: SourceLocation):
super().__init__(source_location)
self._sequence = sequence
@typing.override
@property
def pytype(self) -> pytypes.PyType:
# TODO: this should be parametrised using the sequence item pytype
return pytypes.reversedGenericType
@typing.override
def iterate(self) -> Expression:
return Reversed(expr=self._sequence.iterate(), source_location=self.source_location)
@typing.override
def iterable_item_type(self) -> pytypes.PyType:
return self._sequence.iterable_item_type()
@typing.override
def single_eval(self) -> InstanceBuilder:
return _ReversedIterBuilder(self._sequence.single_eval(), self.source_location)