@terminals-tech/py2bend 0.1.0
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.
- package/LICENSE +21 -0
- package/dist/index.cjs +1328 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +236 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.js +1295 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/** Python AST node types we support (Phase 1 subset) */
|
|
2
|
+
type PyNodeType = "Module" | "FunctionDef" | "Return" | "Assign" | "AugAssign" | "If" | "For" | "While" | "Expr" | "BinOp" | "UnaryOp" | "Compare" | "BoolOp" | "Call" | "Name" | "Constant" | "Attribute" | "Subscript" | "List" | "Dict" | "Lambda" | "ClassDef" | "Try";
|
|
3
|
+
interface PyNode {
|
|
4
|
+
type: PyNodeType;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
interface PyModule extends PyNode {
|
|
8
|
+
type: "Module";
|
|
9
|
+
body: PyNode[];
|
|
10
|
+
}
|
|
11
|
+
interface PyFunctionDef extends PyNode {
|
|
12
|
+
type: "FunctionDef";
|
|
13
|
+
name: string;
|
|
14
|
+
params: string[];
|
|
15
|
+
body: PyNode[];
|
|
16
|
+
decorators: string[];
|
|
17
|
+
}
|
|
18
|
+
interface PyReturn extends PyNode {
|
|
19
|
+
type: "Return";
|
|
20
|
+
value: PyNode | null;
|
|
21
|
+
}
|
|
22
|
+
interface PyAssign extends PyNode {
|
|
23
|
+
type: "Assign";
|
|
24
|
+
target: string;
|
|
25
|
+
value: PyNode;
|
|
26
|
+
}
|
|
27
|
+
interface PyAugAssign extends PyNode {
|
|
28
|
+
type: "AugAssign";
|
|
29
|
+
target: string;
|
|
30
|
+
op: string;
|
|
31
|
+
value: PyNode;
|
|
32
|
+
}
|
|
33
|
+
interface PyIf extends PyNode {
|
|
34
|
+
type: "If";
|
|
35
|
+
test: PyNode;
|
|
36
|
+
body: PyNode[];
|
|
37
|
+
orelse: PyNode[];
|
|
38
|
+
}
|
|
39
|
+
interface PyFor extends PyNode {
|
|
40
|
+
type: "For";
|
|
41
|
+
target: string;
|
|
42
|
+
iter: PyNode;
|
|
43
|
+
body: PyNode[];
|
|
44
|
+
}
|
|
45
|
+
interface PyWhile extends PyNode {
|
|
46
|
+
type: "While";
|
|
47
|
+
test: PyNode;
|
|
48
|
+
body: PyNode[];
|
|
49
|
+
}
|
|
50
|
+
interface PyExpr extends PyNode {
|
|
51
|
+
type: "Expr";
|
|
52
|
+
value: PyNode;
|
|
53
|
+
}
|
|
54
|
+
interface PyBinOp extends PyNode {
|
|
55
|
+
type: "BinOp";
|
|
56
|
+
left: PyNode;
|
|
57
|
+
op: string;
|
|
58
|
+
right: PyNode;
|
|
59
|
+
}
|
|
60
|
+
interface PyUnaryOp extends PyNode {
|
|
61
|
+
type: "UnaryOp";
|
|
62
|
+
op: string;
|
|
63
|
+
operand: PyNode;
|
|
64
|
+
}
|
|
65
|
+
interface PyCompare extends PyNode {
|
|
66
|
+
type: "Compare";
|
|
67
|
+
left: PyNode;
|
|
68
|
+
ops: string[];
|
|
69
|
+
comparators: PyNode[];
|
|
70
|
+
}
|
|
71
|
+
interface PyBoolOp extends PyNode {
|
|
72
|
+
type: "BoolOp";
|
|
73
|
+
op: "and" | "or";
|
|
74
|
+
values: PyNode[];
|
|
75
|
+
}
|
|
76
|
+
interface PyCall extends PyNode {
|
|
77
|
+
type: "Call";
|
|
78
|
+
func: PyNode;
|
|
79
|
+
args: PyNode[];
|
|
80
|
+
kwargs: Array<{
|
|
81
|
+
key: string;
|
|
82
|
+
value: PyNode;
|
|
83
|
+
}>;
|
|
84
|
+
}
|
|
85
|
+
interface PyName extends PyNode {
|
|
86
|
+
type: "Name";
|
|
87
|
+
id: string;
|
|
88
|
+
}
|
|
89
|
+
interface PyConstant extends PyNode {
|
|
90
|
+
type: "Constant";
|
|
91
|
+
value: string | number | boolean | null;
|
|
92
|
+
kind: "str" | "int" | "float" | "bool" | "none";
|
|
93
|
+
}
|
|
94
|
+
interface PyAttribute extends PyNode {
|
|
95
|
+
type: "Attribute";
|
|
96
|
+
value: PyNode;
|
|
97
|
+
attr: string;
|
|
98
|
+
}
|
|
99
|
+
interface PySubscript extends PyNode {
|
|
100
|
+
type: "Subscript";
|
|
101
|
+
value: PyNode;
|
|
102
|
+
slice: PyNode;
|
|
103
|
+
}
|
|
104
|
+
interface PyList extends PyNode {
|
|
105
|
+
type: "List";
|
|
106
|
+
elts: PyNode[];
|
|
107
|
+
}
|
|
108
|
+
interface PyDict extends PyNode {
|
|
109
|
+
type: "Dict";
|
|
110
|
+
keys: PyNode[];
|
|
111
|
+
values: PyNode[];
|
|
112
|
+
}
|
|
113
|
+
interface PyLambda extends PyNode {
|
|
114
|
+
type: "Lambda";
|
|
115
|
+
params: string[];
|
|
116
|
+
body: PyNode;
|
|
117
|
+
}
|
|
118
|
+
interface PyClassDef extends PyNode {
|
|
119
|
+
type: "ClassDef";
|
|
120
|
+
name: string;
|
|
121
|
+
bases: string[];
|
|
122
|
+
body: PyNode[];
|
|
123
|
+
}
|
|
124
|
+
interface PyTry extends PyNode {
|
|
125
|
+
type: "Try";
|
|
126
|
+
body: PyNode[];
|
|
127
|
+
handler: {
|
|
128
|
+
name: string | null;
|
|
129
|
+
type: string | null;
|
|
130
|
+
body: PyNode[];
|
|
131
|
+
} | null;
|
|
132
|
+
}
|
|
133
|
+
/** Compilation result */
|
|
134
|
+
interface CompileResult {
|
|
135
|
+
bend: string;
|
|
136
|
+
errors: CompileError[];
|
|
137
|
+
warnings: string[];
|
|
138
|
+
}
|
|
139
|
+
interface CompileError {
|
|
140
|
+
message: string;
|
|
141
|
+
line?: number;
|
|
142
|
+
column?: number;
|
|
143
|
+
severity: "error" | "warning";
|
|
144
|
+
}
|
|
145
|
+
type BendNodeType = "BendModule" | "BendFunctionDef" | "BendFold" | "BendLoop" | "BendObject" | "BendWith" | "BendPassthrough";
|
|
146
|
+
interface BendNode {
|
|
147
|
+
type: BendNodeType;
|
|
148
|
+
/** Original Python AST node this was derived from */
|
|
149
|
+
source: PyNodeType;
|
|
150
|
+
[key: string]: unknown;
|
|
151
|
+
}
|
|
152
|
+
/** Top-level Bend module */
|
|
153
|
+
interface BendModule extends BendNode {
|
|
154
|
+
type: "BendModule";
|
|
155
|
+
source: "Module";
|
|
156
|
+
body: BendNode[];
|
|
157
|
+
}
|
|
158
|
+
/** Bend function definition — 1:1 from PyFunctionDef, body recursively transformed */
|
|
159
|
+
interface BendFunctionDef extends BendNode {
|
|
160
|
+
type: "BendFunctionDef";
|
|
161
|
+
source: "FunctionDef";
|
|
162
|
+
name: string;
|
|
163
|
+
params: string[];
|
|
164
|
+
body: BendNode[];
|
|
165
|
+
decorators: string[];
|
|
166
|
+
}
|
|
167
|
+
/** for loop -> fold over list */
|
|
168
|
+
interface BendFold extends BendNode {
|
|
169
|
+
type: "BendFold";
|
|
170
|
+
source: "For";
|
|
171
|
+
target: string;
|
|
172
|
+
iter: PyNode;
|
|
173
|
+
body: BendNode[];
|
|
174
|
+
accumulator: string | null;
|
|
175
|
+
}
|
|
176
|
+
/** while loop -> bend/fork recursive generation */
|
|
177
|
+
interface BendLoop extends BendNode {
|
|
178
|
+
type: "BendLoop";
|
|
179
|
+
source: "While";
|
|
180
|
+
test: PyNode;
|
|
181
|
+
body: BendNode[];
|
|
182
|
+
stateVars: string[];
|
|
183
|
+
}
|
|
184
|
+
/** class -> Bend ADT (algebraic data type) */
|
|
185
|
+
interface BendObject extends BendNode {
|
|
186
|
+
type: "BendObject";
|
|
187
|
+
source: "ClassDef";
|
|
188
|
+
name: string;
|
|
189
|
+
bases: string[];
|
|
190
|
+
fields: string[];
|
|
191
|
+
methods: BendFunctionDef[];
|
|
192
|
+
}
|
|
193
|
+
/** try/except -> with Result: monadic error handling */
|
|
194
|
+
interface BendWith extends BendNode {
|
|
195
|
+
type: "BendWith";
|
|
196
|
+
source: "Try";
|
|
197
|
+
body: BendNode[];
|
|
198
|
+
handler: {
|
|
199
|
+
name: string | null;
|
|
200
|
+
type: string | null;
|
|
201
|
+
body: BendNode[];
|
|
202
|
+
} | null;
|
|
203
|
+
}
|
|
204
|
+
/** Passthrough — nodes that don't need structural transformation */
|
|
205
|
+
interface BendPassthrough extends BendNode {
|
|
206
|
+
type: "BendPassthrough";
|
|
207
|
+
node: PyNode;
|
|
208
|
+
}
|
|
209
|
+
/** Parser result */
|
|
210
|
+
interface ParseResult {
|
|
211
|
+
ast: PyModule | null;
|
|
212
|
+
errors: CompileError[];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
declare function parsePython(source: string): ParseResult;
|
|
216
|
+
|
|
217
|
+
/** Transform a parsed Python module AST into Bend IR */
|
|
218
|
+
declare function transformModule(ast: PyModule): BendModule;
|
|
219
|
+
|
|
220
|
+
/** Generate Bend source code from a BendModule IR */
|
|
221
|
+
declare function generateBend(module: BendModule): string;
|
|
222
|
+
|
|
223
|
+
/** Python builtin functions -> Bend stdlib equivalents */
|
|
224
|
+
declare const BUILTIN_MAP: Record<string, string>;
|
|
225
|
+
/** Python binary/unary/boolean operators -> Bend operators */
|
|
226
|
+
declare const OP_MAP: Record<string, string>;
|
|
227
|
+
/** Python method calls -> Bend function equivalents */
|
|
228
|
+
declare const METHOD_MAP: Record<string, string>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Compile Python source to Bend source code.
|
|
232
|
+
* Chains: parse -> transform -> generate
|
|
233
|
+
*/
|
|
234
|
+
declare function compile(pythonSource: string): CompileResult;
|
|
235
|
+
|
|
236
|
+
export { BUILTIN_MAP, type BendFold, type BendFunctionDef, type BendLoop, type BendModule, type BendNode, type BendObject, type BendPassthrough, type BendWith, type CompileError, type CompileResult, METHOD_MAP, OP_MAP, type ParseResult, type PyAssign, type PyAttribute, type PyAugAssign, type PyBinOp, type PyBoolOp, type PyCall, type PyClassDef, type PyCompare, type PyConstant, type PyDict, type PyExpr, type PyFor, type PyFunctionDef, type PyIf, type PyLambda, type PyList, type PyModule, type PyName, type PyNode, type PyReturn, type PySubscript, type PyTry, type PyUnaryOp, type PyWhile, compile, generateBend, parsePython, transformModule };
|