codegen-2 2.7.1__tar.gz → 2.8.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.7.1
3
+ Version: 2.8.0
4
4
  Summary: Meta-programming
5
5
  License: MIT
6
6
  Author: Binh Vu
@@ -100,7 +100,7 @@ class DefFuncStatement(Statement):
100
100
  sig = "async " + sig
101
101
  sig = sig + ":"
102
102
  if self.is_static:
103
- sig = "static " + sig
103
+ sig = "@staticmethod\n" + sig
104
104
  if self.comment != "":
105
105
  sig = (
106
106
  sig
@@ -185,6 +185,8 @@ class DefClassVarStatement(Statement):
185
185
  type: Optional[str]
186
186
  # value of the variable
187
187
  value: Optional[Expr] = None
188
+ # whether this variable is static
189
+ is_static: bool = False
188
190
 
189
191
  def to_python(self):
190
192
  if self.type is None:
@@ -196,10 +198,20 @@ class DefClassVarStatement(Statement):
196
198
  return f"{self.name}: {self.type} = {self.value.to_python()}"
197
199
 
198
200
  def to_typescript(self):
199
- assert self.type is not None
201
+ mod = ""
202
+ if self.is_static:
203
+ mod = "static "
204
+ else:
205
+ # static variable don't require type
206
+ assert self.type is not None
207
+ if self.type is not None:
208
+ type = f": {self.type}"
209
+ else:
210
+ type = ""
211
+
200
212
  if self.value is None:
201
- return f"{self.name}: {self.type};"
202
- return f"{self.name}: {self.type} = {self.value.to_typescript()};"
213
+ return f"{mod}{self.name}{type};"
214
+ return f"{mod}{self.name}{type} = {self.value.to_typescript()};"
203
215
 
204
216
 
205
217
  @dataclass
@@ -241,6 +253,24 @@ class ExceptionStatement(Statement):
241
253
  return "raise " + self.expr.to_python()
242
254
 
243
255
 
256
+ @dataclass
257
+ class AssertionStatement(Statement):
258
+ expr: Expr
259
+ error_msg: Optional[Expr] = None
260
+
261
+ def to_python(self):
262
+ if self.error_msg:
263
+ return f"assert {self.expr.to_python()}, {self.error_msg.to_python()}"
264
+ return f"assert {self.expr.to_python()}"
265
+
266
+ def to_typescript(self):
267
+ if self.error_msg:
268
+ return f"if (!({self.expr.to_typescript()})) throw new Error({self.error_msg.to_typescript()});"
269
+ return (
270
+ f"if (!({self.expr.to_typescript()})) throw new Error('Assertion failed');"
271
+ )
272
+
273
+
244
274
  @dataclass
245
275
  class ForLoopStatement(Statement):
246
276
  item: Var
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "codegen-2"
3
- version = "2.7.1"
3
+ version = "2.8.0"
4
4
  description = "Meta-programming"
5
5
  authors = ["Binh Vu <binh@toan2.com>"]
6
6
  license = "MIT"
File without changes
File without changes
File without changes