codegen-2 1.4.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/__init__.py ADDED
File without changes
@@ -0,0 +1,21 @@
1
+ import codegen.models.expr as expr
2
+ import codegen.models.statement as stmt
3
+ from codegen.models.ast import AST
4
+ from codegen.models.expr import PredefinedFn
5
+ from codegen.models.memory import Memory, Var, VarScope
6
+ from codegen.models.program import Program
7
+ from codegen.models.types import AST_ID, KEY, SCOPE
8
+
9
+ __all__ = [
10
+ "expr",
11
+ "AST",
12
+ "Memory",
13
+ "Var",
14
+ "VarScope",
15
+ "Program",
16
+ "stmt",
17
+ "PredefinedFn",
18
+ "AST_ID",
19
+ "KEY",
20
+ "SCOPE",
21
+ ]
codegen/models/ast.py ADDED
@@ -0,0 +1,181 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Any, Callable, Optional
5
+
6
+ from codegen.models.expr import ExceptionExpr, Expr
7
+ from codegen.models.memory import Memory, Var, VarScope
8
+ from codegen.models.statement import (
9
+ AssignStatement,
10
+ BlockStatement,
11
+ Comment,
12
+ ContinueStatement,
13
+ DefFuncStatement,
14
+ ElseStatement,
15
+ ExceptionStatement,
16
+ ForLoopStatement,
17
+ IfStatement,
18
+ ImportStatement,
19
+ LineBreak,
20
+ NoStatement,
21
+ ReturnStatement,
22
+ SingleExprStatement,
23
+ Statement,
24
+ )
25
+ from codegen.models.types import AST_ID
26
+
27
+
28
+ @dataclass
29
+ class AST:
30
+ id: AST_ID
31
+ stmt: Statement
32
+ children: list[AST] = field(default_factory=list)
33
+ _is_frozen: bool = (
34
+ False # whether to freeze the AST and disallow further modification
35
+ )
36
+
37
+ @staticmethod
38
+ def root():
39
+ return AST("root", NoStatement())
40
+
41
+ def __call__(
42
+ self, *args: Callable[[AST], Any] | Statement, return_self: bool = False
43
+ ) -> Optional[AST]:
44
+ """Allow to build the graph hierarchically"""
45
+ return_val = None
46
+
47
+ for fn in args:
48
+ if isinstance(fn, Statement):
49
+ self._add_stmt(fn)
50
+ else:
51
+ assert callable(fn)
52
+ return_val = fn(self)
53
+
54
+ if return_self:
55
+ assert return_val is None, "Trying to return multiple asts at the same time"
56
+ return self
57
+ return return_val
58
+
59
+ def freeze(self):
60
+ self._is_frozen = True
61
+ for child in self.children:
62
+ child.freeze()
63
+
64
+ def import_(self, module: str):
65
+ self._add_stmt(ImportStatement(module))
66
+
67
+ def return_(self, expr: Expr):
68
+ self._add_stmt(ReturnStatement(expr))
69
+
70
+ def linebreak(self):
71
+ self._add_stmt(LineBreak())
72
+
73
+ def comment(self, comment: str):
74
+ self._add_stmt(Comment(comment))
75
+
76
+ def func(self, name: str, vars: list[Var]):
77
+ return self._add_stmt(DefFuncStatement(name, vars))
78
+
79
+ def expr(self, expr: Expr):
80
+ return self._add_stmt(SingleExprStatement(expr))
81
+
82
+ def raise_exception(self, expr: ExceptionExpr):
83
+ return self._add_stmt(ExceptionStatement(expr))
84
+
85
+ def assign(self, mem: Memory, var: Var, expr: Expr):
86
+ scope = self.next_var_scope()
87
+ self._add_stmt(AssignStatement(var, expr))
88
+ var.set_scope(mem, scope)
89
+
90
+ def for_loop(self, mem: Memory, item: Var, iter: Expr):
91
+ assert item.scope is None, "The variable is already assigned to a scope"
92
+ ast = self._add_stmt(ForLoopStatement(item, iter))
93
+ scope = ast.next_var_scope()
94
+ item.set_scope(mem, scope)
95
+ return ast
96
+
97
+ def if_(self, condition: Expr):
98
+ return self._add_stmt(IfStatement(condition))
99
+
100
+ def else_(self):
101
+ assert len(self.children) > 0 and isinstance(self.children[-1].stmt, Statement)
102
+ return self._add_stmt(ElseStatement())
103
+
104
+ def update_recursively(
105
+ self, fn: Callable[[AST, Any], tuple[AST, Any, bool]], context: Any
106
+ ):
107
+ """Recursively updating the ast. It takes a function that works on the current tree and a context, returns a tuple of
108
+ (new_tree, new_context, stop). This function returns the last AST that is updated.
109
+ """
110
+ ast = self
111
+ stop = False
112
+ while not stop:
113
+ ast, context, stop = fn(ast, context)
114
+ return ast
115
+
116
+ def to_python(self, level: int = -1):
117
+ """Convert the AST to python code"""
118
+ if level == -1:
119
+ assert self.id == "root"
120
+ return "".join(
121
+ [
122
+ child.to_python(level + 1)
123
+ + ("\n" if ci < len(self.children) - 1 else "")
124
+ for ci, child in enumerate(self.children)
125
+ ]
126
+ )
127
+
128
+ if isinstance(self.stmt, BlockStatement):
129
+ prog = ["\t" * level + stmt.to_python() + "\n" for stmt in self.stmt.stmts]
130
+ else:
131
+ prog = ["\t" * level + self.stmt.to_python()]
132
+
133
+ for child in self.children:
134
+ prog.append("\n")
135
+ prog.append(child.to_python(level + 1))
136
+ return "".join(prog)
137
+
138
+ def next_var_scope(self) -> VarScope:
139
+ """Get a scope for the next variable that will be have if it is assigned to this AST"""
140
+ return VarScope(self.id, len(self.children))
141
+
142
+ def has_statement_between_ast(self, stmtcls: type[Statement], end_ast_id: AST_ID):
143
+ """Check if there is a statement of the given type the current AST and the end_ast (exclusive)"""
144
+ for ast in self.children:
145
+ for intermediate_ast in ast.find_ast_to(end_ast_id):
146
+ if (
147
+ isinstance(intermediate_ast.stmt, stmtcls)
148
+ and intermediate_ast.id != end_ast_id
149
+ ):
150
+ return True
151
+ return False
152
+
153
+ def find_ast_to(self, id: AST_ID):
154
+ """Iterate through ASTs that lead to the AST with the given id (inclusive)"""
155
+ if self.id == id:
156
+ yield self
157
+ else:
158
+ for ast in self.children:
159
+ if ast.find_ast(id) is not None:
160
+ yield from ast.find_ast_to(id)
161
+ break
162
+
163
+ def find_ast(self, id: AST_ID) -> Optional[AST]:
164
+ """Find the AST with the given id"""
165
+ if self.id == id:
166
+ return self
167
+ for child in self.children:
168
+ ast = child.find_ast(id)
169
+ if ast is not None:
170
+ return ast
171
+ return None
172
+
173
+ def _add_stmt(self, stmt: Statement):
174
+ if self._is_frozen:
175
+ raise Exception("The AST is frozen and cannot be modified")
176
+ ast = AST(self._next_child_id(), stmt)
177
+ self.children.append(ast)
178
+ return ast
179
+
180
+ def _next_child_id(self):
181
+ return self.id + "__" + str(len(self.children))
codegen/models/expr.py ADDED
@@ -0,0 +1,197 @@
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, Optional, Sequence
7
+
8
+ from codegen.models.memory import Var
9
+
10
+
11
+ class Expr(ABC):
12
+ @abstractmethod
13
+ def to_python(self):
14
+ raise NotImplementedError()
15
+
16
+
17
+ class ExceptionExpr(Expr):
18
+ pass
19
+
20
+
21
+ @dataclass
22
+ class ExprConstant(Expr):
23
+ constant: Any
24
+
25
+ def to_python(self):
26
+ return ExprConstant.constant_to_python(self.constant)
27
+
28
+ @staticmethod
29
+ def constant_to_python(val: Any):
30
+ if isinstance(val, bool) or val is None:
31
+ return str(val)
32
+ if isinstance(val, (str, int, float)):
33
+ return json.dumps(val)
34
+ if isinstance(val, list):
35
+ return (
36
+ "[" + ", ".join([ExprConstant.constant_to_python(v) for v in val]) + "]"
37
+ )
38
+ if isinstance(val, dict):
39
+ return (
40
+ "{"
41
+ + ", ".join(
42
+ [
43
+ f"{ExprConstant.constant_to_python(k)}: {ExprConstant.constant_to_python(v)}"
44
+ for k, v in val.items()
45
+ ]
46
+ )
47
+ + "}"
48
+ )
49
+ if isinstance(val, set):
50
+ return (
51
+ "{" + ", ".join([ExprConstant.constant_to_python(v) for v in val]) + "}"
52
+ )
53
+
54
+
55
+ @dataclass
56
+ class ExprIdent(Expr):
57
+ ident: str
58
+
59
+ def to_python(self):
60
+ return self.ident
61
+
62
+
63
+ @dataclass
64
+ class ExprVar(Expr): # a special identifier
65
+ var: Var
66
+
67
+ def to_python(self):
68
+ return self.var.get_name()
69
+
70
+
71
+ @dataclass
72
+ class ExprFuncCall(Expr):
73
+ func_name: Expr
74
+ args: list[Expr]
75
+
76
+ def to_python(self):
77
+ return f"{self.func_name.to_python()}({', '.join([arg.to_python() for arg in self.args])})"
78
+
79
+
80
+ @dataclass
81
+ class ExprMethodCall(Expr):
82
+ object: Expr
83
+ method: str
84
+ args: list[Expr]
85
+
86
+ def to_python(self):
87
+ if self.method == "__contains__" and len(self.args) == 1:
88
+ return f"{self.args[0].to_python()} in {self.object.to_python()}"
89
+ return f"{self.object.to_python()}.{self.method}({', '.join([arg.to_python() for arg in self.args])})"
90
+
91
+
92
+ @dataclass
93
+ class ExprNotEqual(Expr):
94
+ left: Expr
95
+ right: Expr
96
+
97
+ def to_python(self):
98
+ return f"{self.left.to_python()} != {self.right.to_python()}"
99
+
100
+
101
+ @dataclass
102
+ class ExprLessThanOrEqual(Expr):
103
+ left: Expr
104
+ right: Expr
105
+
106
+ def to_python(self):
107
+ return f"{self.left.to_python()} < {self.right.to_python()}"
108
+
109
+
110
+ @dataclass
111
+ class ExprEqual(Expr):
112
+ left: Expr
113
+ right: Expr
114
+
115
+ def to_python(self):
116
+ return f"{self.left.to_python()} == {self.right.to_python()}"
117
+
118
+
119
+ @dataclass
120
+ class ExprNegation(Expr):
121
+ expr: Expr
122
+
123
+ def to_python(self):
124
+ return f"not {self.expr.to_python()}"
125
+
126
+
127
+ class PredefinedFn:
128
+ @dataclass
129
+ class tuple(Expr):
130
+ items: Sequence[Expr]
131
+
132
+ def to_python(self):
133
+ return f"({', '.join([item.to_python() for item in self.items])})"
134
+
135
+ @dataclass
136
+ class item_getter(Expr):
137
+ collection: Expr
138
+ item: Expr
139
+
140
+ def to_python(self):
141
+ return f"{self.collection.to_python()}[{self.item.to_python()}]"
142
+
143
+ @dataclass
144
+ class len(Expr):
145
+ collection: Expr
146
+
147
+ def to_python(self):
148
+ return f"len({self.collection.to_python()})"
149
+
150
+ @dataclass
151
+ class range(Expr):
152
+ start: Expr
153
+ end: Expr
154
+ step: Optional[Expr] = None
155
+
156
+ def to_python(self):
157
+ if self.step is not None:
158
+ return f"range({self.start.to_python()}, {self.end.to_python()}, {self.step.to_python()})"
159
+ return f"range({self.start.to_python()}, {self.end.to_python()})"
160
+
161
+ @dataclass
162
+ class set_contains(Expr):
163
+ set_: Expr
164
+ item: Expr
165
+
166
+ def to_python(self):
167
+ return f"({self.item.to_python()}) in {self.set_.to_python()}"
168
+
169
+ @dataclass
170
+ class list_append(Expr):
171
+ lst: Expr
172
+ item: Expr
173
+
174
+ def to_python(self):
175
+ return f"{self.lst.to_python()}.append({self.item.to_python()})"
176
+
177
+ @dataclass
178
+ class has_item(Expr):
179
+ collection: Expr
180
+ item: Expr
181
+
182
+ def to_python(self):
183
+ return f"({self.item.to_python()}) in {self.collection.to_python()}"
184
+
185
+ @dataclass
186
+ class base_error(ExceptionExpr):
187
+ msg: str
188
+
189
+ def to_python(self):
190
+ return f"Exception('{self.msg}')"
191
+
192
+ @dataclass
193
+ class key_error(ExceptionExpr):
194
+ msg: str
195
+
196
+ def to_python(self):
197
+ return f"KeyError('{self.msg}')"
@@ -0,0 +1,199 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Literal, NamedTuple, Optional, cast
5
+
6
+ import pandas as pd
7
+ from codegen.models.types import (
8
+ AST_ID,
9
+ KEY,
10
+ NO_AST_ID,
11
+ NO_KEY,
12
+ NO_SCOPE,
13
+ REGISTER_TYPE,
14
+ SCOPE,
15
+ is_ast_same_or_ancestor_of,
16
+ )
17
+
18
+
19
+ @dataclass
20
+ class MemoryRecord:
21
+ id: int
22
+ name: str
23
+ key: KEY
24
+ scope: SCOPE
25
+ type: REGISTER_TYPE
26
+
27
+
28
+ @dataclass
29
+ class Memory:
30
+ counter: int = 0
31
+ registers: pd.DataFrame = field(
32
+ default_factory=lambda: pd.DataFrame(
33
+ columns=["id", "name", "key", "scope", "type"],
34
+ )
35
+ )
36
+
37
+ def register(
38
+ self,
39
+ name: str,
40
+ key: KEY,
41
+ scope: SCOPE,
42
+ type: REGISTER_TYPE,
43
+ strict: bool,
44
+ ):
45
+ """
46
+ Args:
47
+ strict: if True, raise an error if there is a register with the same name, key, and type.
48
+ """
49
+ if strict:
50
+ matched_records = self.find(name=name, key=key, type=type)
51
+ else:
52
+ matched_records = self.find(name=name, key=key, scope=scope, type=type)
53
+
54
+ if len(matched_records) > 0:
55
+ raise ValueError(
56
+ f"There is already a register with the same name={name}, key={key}, scope={scope} (if strict=True), and type={type}"
57
+ )
58
+
59
+ register_id = self.counter
60
+ self.counter += 1
61
+ self.registers.loc[register_id] = [register_id, name, key, scope, type]
62
+ return register_id
63
+
64
+ def remove_one(self, register_id: int) -> None:
65
+ self.registers = self.registers.drop(register_id)
66
+
67
+ def get_one(self, register_id: int) -> MemoryRecord:
68
+ reg = self.registers.loc[register_id]
69
+ return MemoryRecord(
70
+ reg["id"], reg["name"], reg["key"], reg["scope"], reg["type"]
71
+ )
72
+
73
+ def update_scope(self, register_id: int, scope: SCOPE):
74
+ self.registers.at[register_id, "scope"] = scope
75
+
76
+ def find(
77
+ self,
78
+ *,
79
+ name: Optional[str] = None,
80
+ key: Optional[KEY] = None,
81
+ scope: Optional[SCOPE] = None,
82
+ type: Optional[REGISTER_TYPE] = None,
83
+ ) -> list[MemoryRecord]:
84
+ conditions = []
85
+ if name is not None:
86
+ conditions.append(self.registers["name"] == name)
87
+ if key is not None:
88
+ conditions.append(self.registers["key"] == key)
89
+ if scope is not None:
90
+ conditions.append(self.registers["scope"] == scope)
91
+ if type is not None:
92
+ conditions.append(self.registers["type"] == type)
93
+
94
+ assert len(conditions) > 0
95
+ query = conditions[0]
96
+ for cond in conditions[1:]:
97
+ query = query & cond
98
+
99
+ res = self.registers[query]
100
+ out = []
101
+ for i in range(len(res)):
102
+ x = res.iloc[i]
103
+ out.append(
104
+ MemoryRecord(x["id"], x["name"], x["key"], x["scope"], x["type"])
105
+ )
106
+ return out
107
+
108
+ def find_one(
109
+ self,
110
+ *,
111
+ name: Optional[str] = None,
112
+ key: Optional[KEY] = None,
113
+ scope: Optional[SCOPE] = None,
114
+ type: Optional[REGISTER_TYPE] = None,
115
+ ) -> MemoryRecord:
116
+ res = self.find(name=name, key=key, scope=scope, type=type)
117
+ if len(res) == 0:
118
+ raise ValueError(
119
+ f"Cannot find a register matched name={name}, key={key}, scope={scope}, type={type}"
120
+ )
121
+ if len(res) > 1:
122
+ raise ValueError(
123
+ f"Found multiple registers matched name={name}, key={key}, scope={scope}, type={type}"
124
+ )
125
+ return res[0]
126
+
127
+
128
+ class VarScope(NamedTuple):
129
+ ast: AST_ID # the ast that has a child, which is an assign statement, that the first time the variable is defined
130
+ child_index: int # the index of the child (assign statement)
131
+
132
+ def is_subscope_of(self, scope: VarScope) -> bool:
133
+ if scope.ast == self.ast:
134
+ return scope.child_index <= self.child_index
135
+ return is_ast_same_or_ancestor_of(scope.ast, self.ast)
136
+
137
+
138
+ @dataclass
139
+ class Var: # variable
140
+ name: str
141
+ key: Optional[KEY]
142
+ register_id: int
143
+ scope: Optional[VarScope]
144
+
145
+ def get_name(self) -> str:
146
+ return f"{self.name}_{self.register_id}"
147
+
148
+ def set_scope(self, mem: Memory, scope: VarScope):
149
+ assert self.scope == None, "Cannot change the scope of a variable"
150
+ assert (
151
+ mem.get_one(self.register_id).scope == NO_SCOPE
152
+ ), "Cannot change the scope of a variable"
153
+
154
+ self.scope = scope
155
+ mem.update_scope(self.register_id, scope)
156
+
157
+ def delete(self, mem: Memory):
158
+ mem.remove_one(self.register_id)
159
+
160
+ @staticmethod
161
+ def create(
162
+ mem: Memory,
163
+ name: str,
164
+ key: KEY = NO_KEY,
165
+ scope: Optional[VarScope] = None,
166
+ strict: bool = True,
167
+ ):
168
+ return Var(
169
+ name,
170
+ key,
171
+ mem.register(name, key, scope or NO_SCOPE, "var", strict),
172
+ scope,
173
+ )
174
+
175
+ @staticmethod
176
+ def deref(
177
+ mem: Memory,
178
+ *,
179
+ name: Optional[str] = None,
180
+ key: Optional[KEY] = None,
181
+ scope: Optional[VarScope] = None,
182
+ ) -> Var:
183
+ reg = mem.find_one(name=name, key=key, scope=scope, type="var")
184
+ if reg.scope == NO_SCOPE:
185
+ scope = None
186
+ else:
187
+ scope = cast(VarScope, reg.scope)
188
+ return Var(reg.name, reg.key, reg.id, scope)
189
+
190
+ @staticmethod
191
+ def find_by_scope(mem: Memory, scope: VarScope) -> list[Var]:
192
+ """Find all variables that were defined in the given scope."""
193
+ vars = []
194
+ for reg in mem.find(type="var"):
195
+ if reg.scope != NO_SCOPE and cast(VarScope, reg.scope).is_subscope_of(
196
+ scope
197
+ ):
198
+ vars.append(Var(reg.name, reg.key, reg.id, cast(VarScope, reg.scope)))
199
+ return vars
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+
5
+ from codegen.models.ast import AST
6
+ from codegen.models.memory import Memory
7
+ from codegen.models.statement import BlockStatement, ImportStatement
8
+ from codegen.models.types import AST_ID
9
+
10
+
11
+ @dataclass(init=False)
12
+ class Program:
13
+ root: AST
14
+ memory: Memory
15
+ import_manager: ImportManager
16
+
17
+ def __init__(self):
18
+ self.import_manager = ImportManager()
19
+ self.memory = Memory()
20
+
21
+ self.root = AST.root()
22
+ self.root._add_stmt(self.import_manager)
23
+
24
+ def get_ast_for_id(self, id: AST_ID) -> AST:
25
+ ast = self.root.find_ast(id)
26
+ if ast is None:
27
+ raise KeyError(id)
28
+ return ast
29
+
30
+
31
+ @dataclass
32
+ class ImportManager(BlockStatement):
33
+
34
+ stmts: list[ImportStatement] = field(default_factory=list)
35
+
36
+ def import_(self, module: str):
37
+ if not any(stmt.module == module for stmt in self.stmts):
38
+ self.stmts.append(ImportStatement(module))
@@ -0,0 +1,135 @@
1
+ from __future__ import annotations
2
+
3
+ from abc import ABC, abstractmethod
4
+ from dataclasses import dataclass, field
5
+
6
+ from codegen.models.expr import ExceptionExpr, Expr
7
+ from codegen.models.memory import Var
8
+
9
+
10
+ class Statement(ABC):
11
+
12
+ @abstractmethod
13
+ def to_python(self):
14
+ raise NotImplementedError()
15
+
16
+
17
+ class NoStatement(Statement):
18
+ def __repr__(self):
19
+ return "NoStatement()"
20
+
21
+ def to_python(self):
22
+ return "pass"
23
+
24
+
25
+ @dataclass
26
+ class BlockStatement(Statement):
27
+ """A placeholder for a block of statements. It is used to group multiple same-level statements together."""
28
+
29
+ stmts: list[Statement] = field(default_factory=list)
30
+
31
+ def to_python(self):
32
+ raise Exception(
33
+ "BlockStatement won't provide the implementation. Users should invoke the implementation of each inner statement directly."
34
+ )
35
+
36
+
37
+ class LineBreak(Statement):
38
+ def to_python(self):
39
+ return ""
40
+
41
+
42
+ @dataclass
43
+ class ImportStatement(Statement):
44
+ module: str
45
+
46
+ def to_python(self):
47
+ if self.module.find(".") != -1:
48
+ module, attr = self.module.rsplit(".", 1)
49
+ return f"from {module} import {attr}"
50
+ return f"import {self.module}"
51
+
52
+
53
+ @dataclass
54
+ class DefFuncStatement(Statement):
55
+ name: str
56
+ args: list[Var] = field(default_factory=list)
57
+
58
+ def to_python(self):
59
+ return f"def {self.name}({', '.join([arg.get_name() for arg in self.args])}):"
60
+
61
+
62
+ @dataclass
63
+ class AssignStatement(Statement):
64
+ var: Var
65
+ expr: Expr
66
+
67
+ def to_python(self):
68
+ return f"{self.var.get_name()} = {self.expr.to_python()}"
69
+
70
+
71
+ @dataclass
72
+ class SingleExprStatement(Statement):
73
+ expr: Expr
74
+
75
+ def to_python(self):
76
+ return self.expr.to_python()
77
+
78
+
79
+ @dataclass
80
+ class ExceptionStatement(Statement):
81
+ expr: ExceptionExpr # we rely on special exception expr
82
+
83
+ def to_python(self):
84
+ return "raise " + self.expr.to_python()
85
+
86
+
87
+ @dataclass
88
+ class ForLoopStatement(Statement):
89
+ item: Var
90
+ iter: Expr
91
+
92
+ def to_python(self):
93
+ return f"for {self.item.get_name()} in {self.iter.to_python()}:"
94
+
95
+
96
+ @dataclass
97
+ class ContinueStatement(Statement):
98
+ def to_python(self):
99
+ return "continue"
100
+
101
+
102
+ @dataclass
103
+ class BreakStatement(Statement):
104
+ def to_python(self):
105
+ return "break"
106
+
107
+
108
+ @dataclass
109
+ class ReturnStatement(Statement):
110
+ expr: Expr
111
+
112
+ def to_python(self):
113
+ return f"return {self.expr.to_python()}"
114
+
115
+
116
+ @dataclass
117
+ class IfStatement(Statement):
118
+ cond: Expr
119
+
120
+ def to_python(self):
121
+ return f"if {self.cond.to_python()}:"
122
+
123
+
124
+ @dataclass
125
+ class ElseStatement(Statement):
126
+ def to_python(self):
127
+ return "else:"
128
+
129
+
130
+ @dataclass
131
+ class Comment(Statement):
132
+ comment: str
133
+
134
+ def to_python(self):
135
+ return f"# {self.comment}"
@@ -0,0 +1,18 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Literal, TypeAlias
4
+
5
+ KEY = str | tuple
6
+ NO_KEY = tuple()
7
+ REGISTER_TYPE = Literal["var"]
8
+
9
+ AST_ID: TypeAlias = str
10
+ NO_AST_ID: AST_ID = ""
11
+
12
+ SCOPE: TypeAlias = tuple
13
+ NO_SCOPE: SCOPE = tuple()
14
+
15
+
16
+ def is_ast_same_or_ancestor_of(ancestor: AST_ID, descendant: AST_ID):
17
+ """Check whether ast is an ancestor of self"""
18
+ return ancestor == descendant or descendant.startswith(ancestor)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Binh Vu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
2
+ Name: codegen-2
3
+ Version: 1.4.0
4
+ Summary: Meta-programming
5
+ License: MIT
6
+ Author: Binh Vu
7
+ Author-email: binh@toan2.com
8
+ Requires-Python: >=3.11,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Requires-Dist: pandas (>=2.2.0,<3.0.0)
14
+ Description-Content-Type: text/markdown
15
+
16
+ # codegen
17
+ Meta-programming
18
+
@@ -0,0 +1,12 @@
1
+ codegen/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ codegen/models/__init__.py,sha256=L8dzhX8x8j77ycM_rhaI-JobgLyBgeokhFtTiX8HUIw,470
3
+ codegen/models/ast.py,sha256=uhfMxCfZdMIa08cOcSr3971OfIGVdsKJL0cStgwcbV4,5839
4
+ codegen/models/expr.py,sha256=gBXVDAAdftPuMxOFZJZOtJnKDF4Ncra8i7yGjfyDeXQ,4585
5
+ codegen/models/memory.py,sha256=CUCSV6qNWpVbXCLPc9gLCeJUVup0g7XMHqwYrbJfx74,5858
6
+ codegen/models/program.py,sha256=Qi2IBZRKHrh_LlypUPmfytIH7fBG8_j4YmYpzykc10w,986
7
+ codegen/models/statement.py,sha256=rFsIEuGWsfP6k2Tr-jhj8Sqi4IOGoBDG92rzd9Zkq7M,2756
8
+ codegen/models/types.py,sha256=KxAY7aqMvOT4jWmXJm1EBb2ab_qMvzhq2NJKeck0YRg,433
9
+ codegen_2-1.4.0.dist-info/LICENSE,sha256=Ea6itrykT9pvPGd0Kgl6FbrqdDwKswPZJmXMFVjUBqw,1064
10
+ codegen_2-1.4.0.dist-info/METADATA,sha256=Qi0tK9sUiWG6rird99G3TnKue3IsF1akO5ywnpihReM,475
11
+ codegen_2-1.4.0.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
12
+ codegen_2-1.4.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.8.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any