-
Notifications
You must be signed in to change notification settings - Fork 9
/
string.py
175 lines (154 loc) · 5.86 KB
/
string.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
from __future__ import annotations
from typing import TYPE_CHECKING
import structlog
from puya.awst import wtypes
from puya.awst.nodes import (
ARC4Encode,
ArrayConcat,
ArrayExtend,
BytesComparisonExpression,
BytesConstant,
BytesEncoding,
EqualityComparison,
Expression,
ExpressionStatement,
Literal,
Statement,
)
from puya.awst_build.eb.arc4.base import (
ARC4ClassExpressionBuilder,
ARC4EncodedExpressionBuilder,
get_bytes_expr,
)
from puya.awst_build.eb.base import BuilderBinaryOp, BuilderComparisonOp, ExpressionBuilder
from puya.awst_build.eb.var_factory import var_expression
from puya.errors import CodeError
if TYPE_CHECKING:
from collections.abc import Sequence
import mypy.nodes
from puya.parse import SourceLocation
logger: structlog.types.FilteringBoundLogger = structlog.get_logger(__name__)
class StringClassExpressionBuilder(ARC4ClassExpressionBuilder):
def produces(self) -> wtypes.WType:
return wtypes.arc4_string_wtype
def call(
self,
args: Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> ExpressionBuilder:
match args:
case [Literal(value=str(str_val), source_location=loc)]:
return var_expression(
ARC4Encode(
value=BytesConstant(
value=str_val.encode("utf8"),
source_location=loc,
encoding=BytesEncoding.utf8,
),
source_location=location,
wtype=self.produces(),
)
)
case _:
raise CodeError("Invalid/unhandled arguments", location)
def arc4_encode_bytes(bytes_expr: Expression, source_location: SourceLocation) -> Expression:
return ARC4Encode(
value=bytes_expr, source_location=source_location, wtype=wtypes.arc4_string_wtype
)
def expect_string_or_bytes(expr: ExpressionBuilder | Literal) -> Expression:
match expr:
case Literal(value=str(string_literal), source_location=source_location):
return arc4_encode_bytes(
BytesConstant(
value=string_literal.encode("utf-8"),
encoding=BytesEncoding.utf8,
source_location=source_location,
),
source_location,
)
case Literal(value=bytes(bytes_literal), source_location=source_location):
return arc4_encode_bytes(
BytesConstant(
value=bytes_literal,
encoding=BytesEncoding.utf8,
source_location=source_location,
),
source_location,
)
case ExpressionBuilder() as eb:
rvalue = eb.rvalue()
if rvalue.wtype == wtypes.arc4_string_wtype:
return rvalue
if rvalue.wtype == wtypes.bytes_wtype:
return arc4_encode_bytes(rvalue, eb.source_location)
raise CodeError("Expected String or Bytes, or a str or bytes literal", expr.source_location)
class StringExpressionBuilder(ARC4EncodedExpressionBuilder):
wtype = wtypes.arc4_string_wtype
def augmented_assignment(
self, op: BuilderBinaryOp, rhs: ExpressionBuilder | Literal, location: SourceLocation
) -> Statement:
match op:
case BuilderBinaryOp.add:
return ExpressionStatement(
expr=ArrayExtend(
base=self.expr,
other=expect_string_or_bytes(rhs),
source_location=location,
wtype=wtypes.arc4_string_wtype,
)
)
case _:
return super().augmented_assignment(op, rhs, location)
def binary_op(
self,
other: ExpressionBuilder | Literal,
op: BuilderBinaryOp,
location: SourceLocation,
*,
reverse: bool,
) -> ExpressionBuilder:
match op:
case BuilderBinaryOp.add:
lhs = self.expr
rhs = expect_string_or_bytes(other)
if reverse:
(lhs, rhs) = (rhs, lhs)
return var_expression(
ArrayConcat(
left=lhs,
right=rhs,
source_location=location,
wtype=wtypes.arc4_string_wtype,
)
)
case _:
return super().binary_op(other, op, location, reverse=reverse)
def compare(
self, other: ExpressionBuilder | Literal, op: BuilderComparisonOp, location: SourceLocation
) -> ExpressionBuilder:
other_expr: Expression
match other:
case Literal(value=str(string_literal), source_location=source_location):
other_expr = arc4_encode_bytes(
BytesConstant(
value=string_literal.encode("utf-8"),
encoding=BytesEncoding.utf8,
source_location=source_location,
),
source_location,
)
case ExpressionBuilder() as eb if eb.rvalue().wtype == wtypes.arc4_string_wtype:
other_expr = eb.rvalue()
case _:
raise CodeError("Expected arc4.String or str literal")
return var_expression(
BytesComparisonExpression(
source_location=location,
lhs=get_bytes_expr(self.expr),
operator=EqualityComparison(op.value),
rhs=get_bytes_expr(other_expr),
)
)