codegen-2 2.1.3__tar.gz → 2.5.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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: codegen-2
3
- Version: 2.1.3
3
+ Version: 2.5.1
4
4
  Summary: Meta-programming
5
5
  License: MIT
6
6
  Author: Binh Vu
@@ -10,6 +10,7 @@ Classifier: License :: OSI Approved :: MIT License
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Classifier: Programming Language :: Python :: 3.11
12
12
  Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
13
14
  Description-Content-Type: text/markdown
14
15
 
15
16
  # codegen
@@ -1,14 +1,17 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from dataclasses import dataclass, field
4
- from typing import TYPE_CHECKING, Any, Callable, Optional
4
+ from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence
5
5
 
6
6
  from codegen.models.expr import ExceptionExpr, Expr
7
7
  from codegen.models.statement import (
8
8
  AssignStatement,
9
9
  BlockStatement,
10
10
  Comment,
11
+ DefClassStatement,
12
+ DefClassVarStatement,
11
13
  DefFuncStatement,
14
+ DefInterfaceStatement,
12
15
  ElseStatement,
13
16
  ExceptionStatement,
14
17
  ForLoopStatement,
@@ -40,10 +43,16 @@ class AST:
40
43
 
41
44
  @staticmethod
42
45
  def root(prog: Program):
43
- return AST(tuple(), prog, BlockStatement())
46
+ return AST(tuple(), prog, BlockStatement(has_owned_env=False))
47
+
48
+ def is_root(self) -> bool:
49
+ """Check if this is the root AST"""
50
+ return len(self.id) == 0
44
51
 
45
52
  def __call__(
46
- self, *args: Callable[[AST], Any] | Statement, return_self: bool = False
53
+ self,
54
+ *args: Callable[[AST], Any] | Optional[Statement],
55
+ return_self: bool = False,
47
56
  ) -> Optional[AST]:
48
57
  """Allow to build the graph hierarchically"""
49
58
  assert isinstance(return_self, bool)
@@ -53,9 +62,10 @@ class AST:
53
62
  for fn in args:
54
63
  if isinstance(fn, Statement):
55
64
  self._add_stmt(fn)
56
- else:
57
- assert callable(fn)
65
+ elif callable(fn):
58
66
  return_val = fn(self)
67
+ else:
68
+ assert fn is None
59
69
 
60
70
  if return_self:
61
71
  assert return_val is None, "Trying to return multiple asts at the same time"
@@ -67,8 +77,8 @@ class AST:
67
77
  for child in self.children:
68
78
  child.freeze()
69
79
 
70
- def import_(self, module: str, is_import_attr: bool):
71
- self._add_stmt(ImportStatement(module, is_import_attr))
80
+ def import_(self, module: str, is_import_attr: bool, alias: Optional[str] = None):
81
+ self._add_stmt(ImportStatement(module, is_import_attr, alias))
72
82
 
73
83
  def return_(self, expr: Expr):
74
84
  self._add_stmt(ReturnStatement(expr))
@@ -82,14 +92,52 @@ class AST:
82
92
  def comment(self, comment: str):
83
93
  self._add_stmt(Comment(comment))
84
94
 
85
- def func(self, name: str, vars: list[DeferredVar]):
95
+ def func(
96
+ self,
97
+ name: str,
98
+ vars: Sequence[DeferredVar | tuple[DeferredVar, Expr]],
99
+ return_type: Optional[Expr] = None,
100
+ is_async: bool = False,
101
+ is_static: bool = False,
102
+ comment: str = "",
103
+ ):
86
104
  """Define a function. The input variables are deferred vars as they are created for this function (i.e., must not be predefined prior to this function)"""
87
105
  grandchild_id = self.next_grandchild_id()
88
- for var in vars:
106
+ for vararg in vars:
107
+ if isinstance(vararg, tuple):
108
+ var = vararg[0]
109
+ else:
110
+ var = vararg
89
111
  var.set_var(
90
- self.prog.create_var(var.name, var.key, grandchild_id, var.force_name)
112
+ self.prog.create_var(
113
+ var.name, var.key, grandchild_id, var.force_name, var.type
114
+ )
115
+ )
116
+ return self._add_stmt(
117
+ DefFuncStatement(
118
+ name,
119
+ [
120
+ (
121
+ (var[0].get_var(), var[1])
122
+ if isinstance(var, tuple)
123
+ else var.get_var()
124
+ )
125
+ for var in vars
126
+ ],
127
+ return_type,
128
+ is_async,
129
+ is_static,
130
+ comment,
91
131
  )
92
- return self._add_stmt(DefFuncStatement(name, [var.get_var() for var in vars]))
132
+ )
133
+
134
+ def class_(self, name: str, parents: Optional[list[Expr]] = None):
135
+ """Define a class."""
136
+ return self._add_stmt(DefClassStatement(name, parents or []))
137
+
138
+ def interface(self, name: str, parents: Optional[list[Expr]] = None):
139
+ """Define a class."""
140
+ return self._add_stmt(DefInterfaceStatement(name, parents or []))
93
141
 
94
142
  def expr(self, expr: Expr):
95
143
  return self._add_stmt(SingleExprStatement(expr))
@@ -149,16 +197,6 @@ class AST:
149
197
  ast, context, stop = fn(ast, context)
150
198
  return ast
151
199
 
152
- def to_python(self, level: int = 0):
153
- """Convert the AST to python code"""
154
- if isinstance(self.stmt, BlockStatement):
155
- # there is no direct statement and the level does not increase after this statement
156
- return "\n".join([child.to_python(level) for child in self.children])
157
-
158
- prog = ["\t" * level + self.stmt.to_python()]
159
- prog.extend((child.to_python(level + 1) for child in self.children))
160
- return "\n".join(prog)
161
-
162
200
  def has_statement_between_ast(self, stmtcls: type[Statement], end_ast_id: AST_ID):
163
201
  """Check if there is a statement of the given type the current AST and the end_ast (exclusive)"""
164
202
  for ast in self.children:
@@ -218,3 +256,42 @@ class AST:
218
256
  ast = AST(self.next_child_id(), self.prog, stmt)
219
257
  self.children.append(ast)
220
258
  return ast
259
+
260
+ def to_python(self, level: int = 0):
261
+ """Convert the AST to python code"""
262
+ if isinstance(self.stmt, BlockStatement):
263
+ # there is no direct statement and the level does not increase after this statement
264
+ return "\n".join([child.to_python(level) for child in self.children])
265
+
266
+ prog = ["\t" * level + line for line in self.stmt.to_python().split("\n")]
267
+ prog.extend((child.to_python(level + 1) for child in self.children))
268
+ return "\n".join(prog)
269
+
270
+ def to_typescript(self, level: int = 0):
271
+ """Convert the AST to typescript code"""
272
+ if isinstance(self.stmt, BlockStatement):
273
+ if not self.stmt.has_owned_env:
274
+ # the level does not increase after this statement because we do not need to create a new scope
275
+ content = "\n".join(
276
+ [child.to_typescript(level) for child in self.children]
277
+ )
278
+ if self.is_root():
279
+ return content.lstrip("\n")
280
+ return content
281
+
282
+ if len(self.children) == 0:
283
+ return ""
284
+
285
+ return (
286
+ ("\t" * level + "{")
287
+ + "\n".join([child.to_typescript(level + 1) for child in self.children])
288
+ + "\n"
289
+ + ("\t" * level + "}")
290
+ )
291
+
292
+ prog = ["\t" * level + line for line in self.stmt.to_typescript().split("\n")]
293
+ if len(self.children) > 0:
294
+ prog.append("\t" * level + "{")
295
+ prog.extend((child.to_typescript(level + 1) for child in self.children))
296
+ prog.append("\t" * level + "}")
297
+ return "\n".join(prog)
@@ -0,0 +1,409 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from abc import ABC, abstractmethod
5
+ from dataclasses import dataclass
6
+ from typing import Any, Callable, Optional, Sequence
7
+
8
+ from codegen.models.var import Var
9
+
10
+
11
+ class Expr(ABC):
12
+ @abstractmethod
13
+ def to_python(self):
14
+ raise NotImplementedError(self.__class__)
15
+
16
+ def to_typescript(self):
17
+ raise NotImplementedError(self.__class__)
18
+
19
+ def to_wrapped_python(self):
20
+ if isinstance(self, (ExprVar, ExprConstant)):
21
+ return self.to_python()
22
+ return f"({self.to_python()})"
23
+
24
+
25
+ class ExceptionExpr(Expr): ...
26
+
27
+
28
+ @dataclass
29
+ class StandardExceptionExpr(ExceptionExpr):
30
+ cls: Expr
31
+ args: Sequence[Expr]
32
+
33
+ def to_python(self):
34
+ return f"{self.cls.to_python()}({', '.join([arg.to_python() for arg in self.args])})"
35
+
36
+
37
+ @dataclass
38
+ class ExprConstant(Expr):
39
+ constant: Any
40
+
41
+ def to_python(self):
42
+ return ExprConstant.constant_to_python(self.constant)
43
+
44
+ def to_typescript(self):
45
+ return ExprConstant.constant_to_typescript(self.constant)
46
+
47
+ @staticmethod
48
+ def constant_to_python(val: Any) -> str:
49
+ if isinstance(val, bool) or val is None:
50
+ return str(val)
51
+ if isinstance(val, (str, int, float)):
52
+ return json.dumps(val)
53
+ if isinstance(val, list):
54
+ return (
55
+ "[" + ", ".join([ExprConstant.constant_to_python(v) for v in val]) + "]"
56
+ )
57
+ if isinstance(val, dict):
58
+ return (
59
+ "{"
60
+ + ", ".join(
61
+ [
62
+ f"{ExprConstant.constant_to_python(k)}: {ExprConstant.constant_to_python(v)}"
63
+ for k, v in val.items()
64
+ ]
65
+ )
66
+ + "}"
67
+ )
68
+ if isinstance(val, set):
69
+ return (
70
+ "{"
71
+ + ", ".join(sorted((ExprConstant.constant_to_python(v) for v in val)))
72
+ + "}"
73
+ )
74
+ raise NotImplementedError()
75
+
76
+ @staticmethod
77
+ def constant_to_typescript(val: Any) -> str:
78
+ if isinstance(val, bool):
79
+ return str(val).lower()
80
+ if val == "undefined":
81
+ return "undefined"
82
+ if val is None:
83
+ return "null"
84
+ if isinstance(val, (str, int, float)):
85
+ return json.dumps(val)
86
+ if isinstance(val, list):
87
+ return (
88
+ "["
89
+ + ", ".join([ExprConstant.constant_to_typescript(v) for v in val])
90
+ + "]"
91
+ )
92
+ if isinstance(val, dict):
93
+ return (
94
+ "{"
95
+ + ", ".join(
96
+ [
97
+ f"{ExprConstant.constant_to_typescript(k)}: {ExprConstant.constant_to_typescript(v)}"
98
+ for k, v in val.items()
99
+ ]
100
+ )
101
+ + "}"
102
+ )
103
+ if isinstance(val, set):
104
+ return (
105
+ "new Set(["
106
+ + ", ".join([ExprConstant.constant_to_typescript(v) for v in val])
107
+ + "])"
108
+ )
109
+ raise NotImplementedError()
110
+
111
+
112
+ @dataclass
113
+ class ExprIdent(Expr):
114
+ ident: str
115
+
116
+ def to_python(self):
117
+ return self.ident
118
+
119
+ def to_typescript(self):
120
+ return self.ident
121
+
122
+
123
+ @dataclass
124
+ class ExprRawPython(Expr):
125
+ code: str
126
+
127
+ def to_python(self):
128
+ return self.code
129
+
130
+
131
+ @dataclass
132
+ class ExprVar(Expr): # a special identifier
133
+ var: Var
134
+
135
+ def to_python(self):
136
+ return self.var.get_name()
137
+
138
+
139
+ @dataclass
140
+ class ExprFuncCall(Expr):
141
+ func_name: Expr
142
+ args: Sequence[Expr]
143
+
144
+ def to_python(self):
145
+ return f"{self.func_name.to_python()}({', '.join([arg.to_python() for arg in self.args])})"
146
+
147
+ def to_typescript(self):
148
+ return f"{self.func_name.to_typescript()}({', '.join([arg.to_typescript() for arg in self.args])})"
149
+
150
+
151
+ @dataclass
152
+ class ExprNewInstance(Expr):
153
+ class_name: Expr
154
+ args: Sequence[Expr]
155
+
156
+ def to_python(self):
157
+ return f"{self.class_name.to_python()}({', '.join([arg.to_python() for arg in self.args])})"
158
+
159
+ def to_typescript(self):
160
+ return f"new {self.class_name.to_typescript()}({', '.join([arg.to_typescript() for arg in self.args])})"
161
+
162
+
163
+ @dataclass
164
+ class ExprMethodCall(Expr):
165
+ object: Expr
166
+ method: str
167
+ args: Sequence[Expr]
168
+
169
+ def to_python(self):
170
+ if self.method == "__contains__" and len(self.args) == 1:
171
+ return f"{self.args[0].to_python()} in {self.object.to_python()}"
172
+ return f"{self.object.to_python()}.{self.method}({', '.join([arg.to_python() for arg in self.args])})"
173
+
174
+ def to_typescript(self):
175
+ return f"{self.object.to_typescript()}.{self.method}({', '.join([arg.to_typescript() for arg in self.args])})"
176
+
177
+
178
+ @dataclass
179
+ class ExprNotEqual(Expr):
180
+ left: Expr
181
+ right: Expr
182
+
183
+ def to_python(self):
184
+ return f"{self.left.to_python()} != {self.right.to_python()}"
185
+
186
+
187
+ @dataclass
188
+ class ExprLessThanOrEqual(Expr):
189
+ left: Expr
190
+ right: Expr
191
+
192
+ def to_python(self):
193
+ return f"{self.left.to_python()} < {self.right.to_python()}"
194
+
195
+
196
+ @dataclass
197
+ class ExprEqual(Expr):
198
+ left: Expr
199
+ right: Expr
200
+
201
+ def to_python(self):
202
+ return f"{self.left.to_python()} == {self.right.to_python()}"
203
+
204
+ def to_typescript(self):
205
+ return f"{self.left.to_typescript()} === {self.right.to_typescript()}"
206
+
207
+
208
+ @dataclass
209
+ class ExprIs(Expr):
210
+ left: Expr
211
+ right: Expr
212
+
213
+ def to_python(self):
214
+ return f"{self.left.to_python()} is {self.right.to_python()}"
215
+
216
+
217
+ @dataclass
218
+ class ExprNegation(Expr):
219
+ expr: Expr
220
+
221
+ def to_python(self):
222
+ return f"not {self.expr.to_wrapped_python()}"
223
+
224
+
225
+ @dataclass
226
+ class ExprDivision(Expr):
227
+ left: Expr
228
+ right: Expr
229
+
230
+ def to_python(self):
231
+ return f"{self.left.to_python()} / {self.right.to_python()}"
232
+
233
+
234
+ class PredefinedFn:
235
+ @dataclass
236
+ class is_null(Expr):
237
+ expr: Expr
238
+
239
+ def to_python(self):
240
+ return f"{self.expr.to_python()} is None"
241
+
242
+ @dataclass
243
+ class tuple(Expr):
244
+ items: Sequence[Expr]
245
+
246
+ def to_python(self):
247
+ return f"({', '.join([item.to_python() for item in self.items])})"
248
+
249
+ @dataclass
250
+ class list(Expr):
251
+ items: Sequence[Expr]
252
+
253
+ def to_python(self):
254
+ return f"[{', '.join([item.to_python() for item in self.items])}]"
255
+
256
+ @dataclass
257
+ class dict(Expr):
258
+ items: Sequence[tuple[Expr, Expr]]
259
+
260
+ def to_python(self):
261
+ return (
262
+ "{"
263
+ + ", ".join(
264
+ [
265
+ f"{key.to_python()}: {value.to_python()}"
266
+ for key, value in self.items
267
+ ]
268
+ )
269
+ + "}"
270
+ )
271
+
272
+ def to_typescript(self):
273
+ return (
274
+ "{"
275
+ + ", ".join(
276
+ [
277
+ f"{key.to_typescript()}: {value.to_typescript()}"
278
+ for key, value in self.items
279
+ ]
280
+ )
281
+ + "}"
282
+ )
283
+
284
+ @dataclass
285
+ class attr_getter(Expr):
286
+ collection: Expr
287
+ attr: Expr
288
+
289
+ def to_python(self):
290
+ return f"{self.collection.to_python()}.{self.attr.to_python()}"
291
+
292
+ def to_typescript(self):
293
+ return f"{self.collection.to_typescript()}.{self.attr.to_typescript()}"
294
+
295
+ @dataclass
296
+ class attr_setter(Expr):
297
+ collection: Expr
298
+ attr: Expr
299
+ value: Expr
300
+
301
+ def to_python(self):
302
+ return f"{self.collection.to_python()}.{self.attr.to_python()} = {self.value.to_python()}"
303
+
304
+ def to_typescript(self):
305
+ return f"{self.collection.to_typescript()}.{self.attr.to_typescript()} = {self.value.to_typescript()};"
306
+
307
+ @dataclass
308
+ class item_getter(Expr):
309
+ collection: Expr
310
+ item: Expr
311
+
312
+ def to_python(self):
313
+ return f"{self.collection.to_python()}[{self.item.to_python()}]"
314
+
315
+ @dataclass
316
+ class item_setter(Expr):
317
+ collection: Expr
318
+ item: Expr
319
+ value: Expr
320
+
321
+ def to_python(self):
322
+ return f"{self.collection.to_python()}[{self.item.to_python()}] = {self.value.to_python()}"
323
+
324
+ @dataclass
325
+ class len(Expr):
326
+ collection: Expr
327
+
328
+ def to_python(self):
329
+ return f"len({self.collection.to_python()})"
330
+
331
+ @dataclass(init=False)
332
+ class map_list(Expr):
333
+ collection: Expr
334
+ func: Expr
335
+
336
+ def __init__(self, collection: Expr, func: Callable[[ExprIdent], Expr]):
337
+ self.collection = collection
338
+ self.func = func(ExprIdent("_x"))
339
+
340
+ def to_python(self):
341
+ return f"[{self.func.to_python()} for _x in {self.collection.to_python()}]"
342
+
343
+ def to_typescript(self):
344
+ return f"{self.collection.to_typescript()}.map((_x: any) => {self.func.to_typescript()})"
345
+
346
+ @dataclass
347
+ class range(Expr):
348
+ start: Expr
349
+ end: Expr
350
+ step: Optional[Expr] = None
351
+
352
+ def to_python(self):
353
+ if self.step is not None:
354
+ return f"range({self.start.to_python()}, {self.end.to_python()}, {self.step.to_python()})"
355
+ return f"range({self.start.to_python()}, {self.end.to_python()})"
356
+
357
+ @dataclass
358
+ class set_contains(Expr):
359
+ set_: Expr
360
+ item: Expr
361
+
362
+ def to_python(self):
363
+ return f"{self.item.to_wrapped_python()} in {self.set_.to_wrapped_python()}"
364
+
365
+ @dataclass
366
+ class list_append(Expr):
367
+ lst: Expr
368
+ item: Expr
369
+
370
+ def to_python(self):
371
+ return f"{self.lst.to_wrapped_python()}.append({self.item.to_python()})"
372
+
373
+ @dataclass
374
+ class has_item(Expr):
375
+ collection: Expr
376
+ item: Expr
377
+
378
+ def to_python(self):
379
+ return f"{self.item.to_wrapped_python()} in {self.collection.to_wrapped_python()}"
380
+
381
+ @dataclass
382
+ class not_has_item(Expr):
383
+ collection: Expr
384
+ item: Expr
385
+
386
+ def to_python(self):
387
+ return f"{self.item.to_wrapped_python()} not in {self.collection.to_wrapped_python()}"
388
+
389
+ @dataclass
390
+ class base_error(ExceptionExpr):
391
+ msg: str
392
+
393
+ def to_python(self):
394
+ return f"Exception('{self.msg}')"
395
+
396
+ @dataclass
397
+ class key_error(ExceptionExpr):
398
+ msg: str
399
+
400
+ def to_python(self):
401
+ return f"KeyError('{self.msg}')"
402
+
403
+ @dataclass
404
+ class keyword_assignment(Expr):
405
+ keyword: str
406
+ value: Expr
407
+
408
+ def to_python(self):
409
+ return f"{self.keyword}={self.value.to_python()}"
@@ -5,6 +5,7 @@ from dataclasses import dataclass, field
5
5
  from typing import Optional
6
6
 
7
7
  from codegen.models.ast import AST
8
+ from codegen.models.expr import ExprIdent # You may need to adjust this import path
8
9
  from codegen.models.statement import BlockStatement
9
10
  from codegen.models.types import AST_ID, KEY
10
11
  from codegen.models.var import Var, VarScope
@@ -15,14 +16,18 @@ class Program:
15
16
  root: AST
16
17
  vars: VarRegisters
17
18
  import_area: AST
19
+ imported_modules: set[str]
18
20
 
19
21
  def __init__(self):
20
22
  self.vars = VarRegisters(self)
21
23
  self.root = AST.root(self)
22
24
  self.import_area = self.root._add_stmt(BlockStatement(has_owned_env=False))
25
+ self.imported_modules = set()
23
26
 
24
- def import_(self, module: str, is_import_attr: bool):
25
- self.import_area.import_(module, is_import_attr)
27
+ def import_(self, module: str, is_import_attr: bool, alias: Optional[str] = None):
28
+ if module not in self.imported_modules:
29
+ self.import_area.import_(module, is_import_attr, alias)
30
+ self.imported_modules.add(module)
26
31
 
27
32
  def get_ast_by_id(self, id: AST_ID) -> AST:
28
33
  ast = self.root
@@ -48,15 +53,21 @@ class Program:
48
53
  )
49
54
 
50
55
  def create_var(
51
- self, name: str, key: KEY, ast: AST_ID, force_name: Optional[str] = None
56
+ self,
57
+ name: str,
58
+ key: KEY,
59
+ ast: AST_ID,
60
+ force_name: Optional[str] = None,
61
+ type: Optional[ExprIdent] = None,
52
62
  ) -> Var:
53
- reg = self.vars.register(name, key, ast, force_name)
63
+ reg = self.vars.register(name, key, ast, force_name, type)
54
64
  return Var(
55
65
  name=name,
56
66
  key=key,
57
67
  register_id=reg.id,
58
68
  scope=reg.scope,
59
69
  force_name=force_name,
70
+ type=type,
60
71
  )
61
72
 
62
73
  def get_var(
@@ -69,7 +80,9 @@ class Program:
69
80
  reg = self.vars.find(key, at)
70
81
  if reg is None:
71
82
  raise KeyError(f"Variable with key {key} is not found in the current scope")
72
- return Var(reg.name, key, reg.id, reg.scope)
83
+ return Var(
84
+ name=reg.name, key=key, register_id=reg.id, scope=reg.scope, type=reg.type
85
+ )
73
86
 
74
87
 
75
88
  @dataclass
@@ -79,6 +92,7 @@ class VarRegister:
79
92
  key: KEY
80
93
  scope: VarScope
81
94
  force_name: Optional[str] = None
95
+ type: Optional[ExprIdent] = None
82
96
 
83
97
 
84
98
  @dataclass
@@ -88,7 +102,12 @@ class VarRegisters:
88
102
  key2registers: dict[KEY, list[int]] = field(default_factory=dict)
89
103
 
90
104
  def register(
91
- self, name: str, key: KEY, ast: AST_ID, force_name: Optional[str] = None
105
+ self,
106
+ name: str,
107
+ key: KEY,
108
+ ast: AST_ID,
109
+ force_name: Optional[str] = None,
110
+ type: Optional[ExprIdent] = None,
92
111
  ) -> VarRegister:
93
112
  """Register a new variable with the given name, key, then return the register id.
94
113
 
@@ -110,6 +129,7 @@ class VarRegisters:
110
129
  key=key,
111
130
  scope=VarScope.from_ast_id(ast),
112
131
  force_name=force_name,
132
+ type=type,
113
133
  )
114
134
  self.registers.append(reg)
115
135
  self.key2registers.setdefault(key, []).append(reg.id)