codegen-2 2.10.0__tar.gz → 2.11.0__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.0}/PKG-INFO +1 -1
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/__init__.py +2 -1
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/ast.py +14 -1
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/expr.py +11 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/program.py +14 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/statement.py +16 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/pyproject.toml +1 -1
- {codegen_2-2.10.0 → codegen_2-2.11.0}/LICENSE +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/README.md +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/__init__.py +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/codegen/models/types.py +0 -0
- {codegen_2-2.10.0 → codegen_2-2.11.0}/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)
|
|
@@ -314,6 +314,22 @@ class IfStatement(Statement):
|
|
|
314
314
|
return f"if {self.cond.to_python()}:"
|
|
315
315
|
|
|
316
316
|
|
|
317
|
+
@dataclass
|
|
318
|
+
class TryStatement(Statement):
|
|
319
|
+
def to_python(self):
|
|
320
|
+
return "try:"
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
@dataclass
|
|
324
|
+
class CatchStatement(Statement):
|
|
325
|
+
match: Optional[Expr] = None
|
|
326
|
+
|
|
327
|
+
def to_python(self):
|
|
328
|
+
if self.match is None:
|
|
329
|
+
return "except:"
|
|
330
|
+
return f"except {self.match.to_python()}:"
|
|
331
|
+
|
|
332
|
+
|
|
317
333
|
@dataclass
|
|
318
334
|
class ElseStatement(Statement):
|
|
319
335
|
def to_python(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|