codegen-2 2.10.0__tar.gz → 2.11.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {codegen_2-2.10.0 → codegen_2-2.11.1}/PKG-INFO +1 -1
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/__init__.py +2 -1
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/ast.py +14 -1
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/expr.py +11 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/program.py +14 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/statement.py +21 -1
- {codegen_2-2.10.0 → codegen_2-2.11.1}/pyproject.toml +1 -1
- {codegen_2-2.10.0 → codegen_2-2.11.1}/LICENSE +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/README.md +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/__init__.py +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/types.py +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.1}/codegen/models/var.py +0 -0
|
@@ -2,7 +2,7 @@ import codegen.models.expr as expr
|
|
|
2
2
|
import codegen.models.statement as stmt
|
|
3
3
|
from codegen.models.ast import AST
|
|
4
4
|
from codegen.models.expr import PredefinedFn
|
|
5
|
-
from codegen.models.program import Program
|
|
5
|
+
from codegen.models.program import ImportHelper, Program
|
|
6
6
|
from codegen.models.types import AST_ID, KEY
|
|
7
7
|
from codegen.models.var import DeferredVar, Var, VarScope
|
|
8
8
|
|
|
@@ -17,4 +17,5 @@ __all__ = [
|
|
|
17
17
|
"PredefinedFn",
|
|
18
18
|
"AST_ID",
|
|
19
19
|
"KEY",
|
|
20
|
+
"ImportHelper",
|
|
20
21
|
]
|
|
@@ -7,6 +7,7 @@ from codegen.models.expr import ExceptionExpr, Expr
|
|
|
7
7
|
from codegen.models.statement import (
|
|
8
8
|
AssignStatement,
|
|
9
9
|
BlockStatement,
|
|
10
|
+
CatchStatement,
|
|
10
11
|
Comment,
|
|
11
12
|
DefClassLikeStatement,
|
|
12
13
|
DefClassStatement,
|
|
@@ -23,6 +24,7 @@ from codegen.models.statement import (
|
|
|
23
24
|
ReturnStatement,
|
|
24
25
|
SingleExprStatement,
|
|
25
26
|
Statement,
|
|
27
|
+
TryStatement,
|
|
26
28
|
)
|
|
27
29
|
from codegen.models.types import AST_ID
|
|
28
30
|
from codegen.models.var import DeferredVar, Var, VarScope
|
|
@@ -183,10 +185,21 @@ class AST:
|
|
|
183
185
|
|
|
184
186
|
def else_(self):
|
|
185
187
|
assert len(self.children) > 0 and isinstance(
|
|
186
|
-
self.children[-1].stmt, IfStatement
|
|
188
|
+
self.children[-1].stmt, (IfStatement, CatchStatement)
|
|
187
189
|
)
|
|
188
190
|
return self._add_stmt(ElseStatement())
|
|
189
191
|
|
|
192
|
+
def try_(self):
|
|
193
|
+
"""Start a try block. The next statement must be an exception statement."""
|
|
194
|
+
return self._add_stmt(TryStatement())
|
|
195
|
+
|
|
196
|
+
def catch(self, match: Optional[Expr] = None):
|
|
197
|
+
return self._add_stmt(
|
|
198
|
+
CatchStatement(
|
|
199
|
+
match,
|
|
200
|
+
)
|
|
201
|
+
)
|
|
202
|
+
|
|
190
203
|
def python_stmt(self, stmt: str) -> AST:
|
|
191
204
|
return self._add_stmt(PythonStatement(stmt))
|
|
192
205
|
|
|
@@ -159,6 +159,17 @@ class ExprFuncCall(Expr):
|
|
|
159
159
|
return f"{self.func_name.to_typescript()}({', '.join([arg.to_typescript() for arg in self.args])})"
|
|
160
160
|
|
|
161
161
|
|
|
162
|
+
@dataclass
|
|
163
|
+
class ExprAwait(Expr):
|
|
164
|
+
expr: Expr
|
|
165
|
+
|
|
166
|
+
def to_python(self):
|
|
167
|
+
return f"(await {self.expr.to_python()})"
|
|
168
|
+
|
|
169
|
+
def to_typescript(self):
|
|
170
|
+
return f"(await {self.expr.to_typescript()})"
|
|
171
|
+
|
|
172
|
+
|
|
162
173
|
@dataclass
|
|
163
174
|
class ExprNewInstance(Expr):
|
|
164
175
|
class_name: Expr
|
|
@@ -155,3 +155,17 @@ class VarRegisters:
|
|
|
155
155
|
if matched_regid is None:
|
|
156
156
|
return None
|
|
157
157
|
return self.registers[matched_regid]
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@dataclass
|
|
161
|
+
class ImportHelper:
|
|
162
|
+
"""Helper class to manage imports in the program."""
|
|
163
|
+
|
|
164
|
+
program: Program
|
|
165
|
+
# mapping from identifiers to their imported paths
|
|
166
|
+
idents: dict[str, str] = field(default_factory=dict)
|
|
167
|
+
|
|
168
|
+
def use(self, ident: str):
|
|
169
|
+
assert ident in self.idents
|
|
170
|
+
self.program.import_(self.idents[ident], True)
|
|
171
|
+
return ExprIdent(ident)
|
|
@@ -239,7 +239,11 @@ class SingleExprStatement(Statement):
|
|
|
239
239
|
expr: Expr
|
|
240
240
|
|
|
241
241
|
def to_python(self):
|
|
242
|
-
|
|
242
|
+
stmt = self.expr.to_python()
|
|
243
|
+
if stmt[0] == "(" and stmt[-1] == ")":
|
|
244
|
+
# remove parentheses if they are not needed
|
|
245
|
+
stmt = stmt[1:-1]
|
|
246
|
+
return stmt
|
|
243
247
|
|
|
244
248
|
def to_typescript(self):
|
|
245
249
|
return self.expr.to_typescript()
|
|
@@ -314,6 +318,22 @@ class IfStatement(Statement):
|
|
|
314
318
|
return f"if {self.cond.to_python()}:"
|
|
315
319
|
|
|
316
320
|
|
|
321
|
+
@dataclass
|
|
322
|
+
class TryStatement(Statement):
|
|
323
|
+
def to_python(self):
|
|
324
|
+
return "try:"
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@dataclass
|
|
328
|
+
class CatchStatement(Statement):
|
|
329
|
+
match: Optional[Expr] = None
|
|
330
|
+
|
|
331
|
+
def to_python(self):
|
|
332
|
+
if self.match is None:
|
|
333
|
+
return "except:"
|
|
334
|
+
return f"except {self.match.to_python()}:"
|
|
335
|
+
|
|
336
|
+
|
|
317
337
|
@dataclass
|
|
318
338
|
class ElseStatement(Statement):
|
|
319
339
|
def to_python(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|