codegen-2 2.5.2__py3-none-any.whl → 2.7.0__py3-none-any.whl

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/models/ast.py CHANGED
@@ -1,17 +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, Sequence
4
+ from typing import TYPE_CHECKING, Any, Callable, Literal, 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
+ DefClassLikeStatement,
11
12
  DefClassStatement,
12
13
  DefClassVarStatement,
13
14
  DefFuncStatement,
14
- DefInterfaceStatement,
15
15
  ElseStatement,
16
16
  ExceptionStatement,
17
17
  ForLoopStatement,
@@ -135,9 +135,14 @@ class AST:
135
135
  """Define a class."""
136
136
  return self._add_stmt(DefClassStatement(name, parents or []))
137
137
 
138
- def interface(self, name: str, parents: Optional[Sequence[Expr]] = None):
139
- """Define a class."""
140
- return self._add_stmt(DefInterfaceStatement(name, parents or []))
138
+ def class_like(
139
+ self,
140
+ keyword: Literal["interface", "enum"],
141
+ name: str,
142
+ parents: Optional[Sequence[Expr]] = None,
143
+ ):
144
+ """Define a class-like structure such as enum or interface in Typescript."""
145
+ return self._add_stmt(DefClassLikeStatement(keyword, name, parents or []))
141
146
 
142
147
  def expr(self, expr: Expr):
143
148
  return self._add_stmt(SingleExprStatement(expr))
codegen/models/expr.py CHANGED
@@ -231,6 +231,19 @@ class ExprDivision(Expr):
231
231
  return f"{self.left.to_python()} / {self.right.to_python()}"
232
232
 
233
233
 
234
+ @dataclass
235
+ class ExprTernary(Expr):
236
+ condition: Expr
237
+ true_expr: Expr
238
+ false_expr: Expr
239
+
240
+ def to_python(self):
241
+ return f"{self.true_expr.to_python()} if {self.condition.to_python()} else {self.false_expr.to_python()}"
242
+
243
+ def to_typescript(self):
244
+ return f"{self.true_expr.to_typescript()} ? {self.condition.to_typescript()} : {self.false_expr.to_typescript()}"
245
+
246
+
234
247
  class PredefinedFn:
235
248
  @dataclass
236
249
  class is_null(Expr):
@@ -3,7 +3,7 @@ from __future__ import annotations
3
3
  from abc import ABC, abstractmethod
4
4
  from dataclasses import dataclass, field
5
5
  from multiprocessing import Value
6
- from typing import Optional, Sequence
6
+ from typing import Literal, Optional, Sequence
7
7
 
8
8
  from codegen.models.expr import ExceptionExpr, Expr, ExprFuncCall
9
9
  from codegen.models.var import Var
@@ -156,7 +156,10 @@ class DefClassStatement(Statement):
156
156
 
157
157
 
158
158
  @dataclass
159
- class DefInterfaceStatement(Statement):
159
+ class DefClassLikeStatement(Statement):
160
+ """Statement to define a class or interface"""
161
+
162
+ keyword: Literal["interface", "enum"]
160
163
  name: str
161
164
  parents: Sequence[Expr] = field(default_factory=list)
162
165
 
@@ -169,7 +172,7 @@ class DefInterfaceStatement(Statement):
169
172
  extend = f" extends {' '.join(p.to_typescript() for p in self.parents)}"
170
173
  else:
171
174
  extend = ""
172
- return f"export interface {self.name}" + extend
175
+ return f"export {self.keyword} {self.name}" + extend
173
176
 
174
177
 
175
178
  @dataclass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: codegen-2
3
- Version: 2.5.2
3
+ Version: 2.7.0
4
4
  Summary: Meta-programming
5
5
  License: MIT
6
6
  Author: Binh Vu
@@ -0,0 +1,12 @@
1
+ codegen/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ codegen/models/__init__.py,sha256=j__FrDdGOILIo9jqEDElL3YP69h0xkL2SqrMxSqgpTc,457
3
+ codegen/models/ast.py,sha256=qq1xeWCMbTUXoOXszVYYPTaN-eiPJHdoTbsYbz5MKUo,10518
4
+ codegen/models/expr.py,sha256=pWZ3j6yH5DJ-gqEomznufUTP6uroqJebzffInsAqOos,10967
5
+ codegen/models/program.py,sha256=mrvtOp9WpaZ6B5KOGpFFLBXMtSCo-BpuGCEgipUuBV8,5062
6
+ codegen/models/statement.py,sha256=XWgjswJDZ3HpgE19d_HXyv6BGvr00qk4hQzLMDX3JHY,8558
7
+ codegen/models/types.py,sha256=Dl-wzGB1viA7EZj2CuTrZe1F133Q6ENVr_yRqxhde8M,178
8
+ codegen/models/var.py,sha256=sOzDrqJHkAgA2Tqhh6IelW3dDe2o7us6HPjaRBVKmCQ,2722
9
+ codegen_2-2.7.0.dist-info/LICENSE,sha256=Ea6itrykT9pvPGd0Kgl6FbrqdDwKswPZJmXMFVjUBqw,1064
10
+ codegen_2-2.7.0.dist-info/METADATA,sha256=H1tigBMx5WV9nJm9FzvpZWgrTXMI2m-Fiu_qkGtctio,487
11
+ codegen_2-2.7.0.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
12
+ codegen_2-2.7.0.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- codegen/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- codegen/models/__init__.py,sha256=j__FrDdGOILIo9jqEDElL3YP69h0xkL2SqrMxSqgpTc,457
3
- codegen/models/ast.py,sha256=74cLjaZqrC_Cpo4WQqcXvuTZMcpfvNGYmvOqACeYD48,10366
4
- codegen/models/expr.py,sha256=uFlJThzGSGFavC_9ExhTrLQ7Z5na7C6oXRFGIO_cas0,10576
5
- codegen/models/program.py,sha256=mrvtOp9WpaZ6B5KOGpFFLBXMtSCo-BpuGCEgipUuBV8,5062
6
- codegen/models/statement.py,sha256=50bl4-ahjHtBfds3gEMd_4KynheHa4i2EN0zvJ9PaiU,8450
7
- codegen/models/types.py,sha256=Dl-wzGB1viA7EZj2CuTrZe1F133Q6ENVr_yRqxhde8M,178
8
- codegen/models/var.py,sha256=sOzDrqJHkAgA2Tqhh6IelW3dDe2o7us6HPjaRBVKmCQ,2722
9
- codegen_2-2.5.2.dist-info/LICENSE,sha256=Ea6itrykT9pvPGd0Kgl6FbrqdDwKswPZJmXMFVjUBqw,1064
10
- codegen_2-2.5.2.dist-info/METADATA,sha256=NFWJKwFC5-1KMZ8-6Zb0TXHK-zIJFzEx7xssgBvwxHE,487
11
- codegen_2-2.5.2.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
12
- codegen_2-2.5.2.dist-info/RECORD,,