codegen-2 2.9.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: codegen-2
3
- Version: 2.9.0
3
+ Version: 2.11.0
4
4
  Summary: Meta-programming
5
5
  License: MIT
6
6
  Author: Binh Vu
@@ -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
@@ -235,6 +246,28 @@ class ExprNegation(Expr):
235
246
  return f"not {self.expr.to_wrapped_python()}"
236
247
 
237
248
 
249
+ @dataclass
250
+ class ExprLogicalAnd(Expr):
251
+ terms: Sequence[Expr]
252
+
253
+ def to_python(self):
254
+ return f" and ".join([term.to_python() for term in self.terms])
255
+
256
+ def to_typescript(self):
257
+ return f" && ".join([term.to_typescript() for term in self.terms])
258
+
259
+
260
+ @dataclass
261
+ class ExprLogicalOr(Expr):
262
+ terms: Sequence[Expr]
263
+
264
+ def to_python(self):
265
+ return f" or ".join([term.to_python() for term in self.terms])
266
+
267
+ def to_typescript(self):
268
+ return f" || ".join([term.to_typescript() for term in self.terms])
269
+
270
+
238
271
  @dataclass
239
272
  class ExprDivision(Expr):
240
273
  left: 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):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "codegen-2"
3
- version = "2.9.0"
3
+ version = "2.11.0"
4
4
  description = "Meta-programming"
5
5
  authors = ["Binh Vu <binh@toan2.com>"]
6
6
  license = "MIT"
File without changes
File without changes