@hoox-sh/pynets 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/CHANGELOG.md +30 -0
- package/LICENSE +661 -0
- package/NOTICE +46 -0
- package/README.md +327 -0
- package/dist/browser.js +44 -0
- package/dist/index.d.ts +348 -0
- package/dist/index.js +41741 -0
- package/package.json +77 -0
- package/src/ast/builder.ts +1002 -0
- package/src/ast/dump.ts +43 -0
- package/src/ast/helper.ts +77 -0
- package/src/ast/nodes.ts +570 -0
- package/src/ast/unparse.ts +360 -0
- package/src/cli/rich.ts +378 -0
- package/src/cli.ts +627 -0
- package/src/generated/PinescriptLexer.interp +267 -0
- package/src/generated/PinescriptLexer.tokens +131 -0
- package/src/generated/PinescriptLexer.ts +673 -0
- package/src/generated/PinescriptLexerBase.ts +395 -0
- package/src/generated/PinescriptParser.interp +267 -0
- package/src/generated/PinescriptParser.tokens +131 -0
- package/src/generated/PinescriptParser.ts +8863 -0
- package/src/generated/PinescriptParserBase.ts +21 -0
- package/src/generated/PinescriptParserVisitor.ts +840 -0
- package/src/index.ts +55 -0
- package/src/parser/LexerBase.ts +395 -0
- package/src/parser/ParserBase.ts +21 -0
- package/src/parser/error.ts +33 -0
- package/src/runtime/array.ts +486 -0
- package/src/runtime/color.ts +164 -0
- package/src/runtime/drawings.ts +510 -0
- package/src/runtime/input.ts +102 -0
- package/src/runtime/interpret.ts +3897 -0
- package/src/runtime/library.ts +282 -0
- package/src/runtime/log.ts +69 -0
- package/src/runtime/map.ts +85 -0
- package/src/runtime/math.ts +231 -0
- package/src/runtime/matrix.ts +1154 -0
- package/src/runtime/provider.ts +232 -0
- package/src/runtime/request.ts +372 -0
- package/src/runtime/series.ts +44 -0
- package/src/runtime/str.ts +433 -0
- package/src/runtime/strategy.ts +1105 -0
- package/src/runtime/ta.ts +2093 -0
- package/src/runtime/ticker.ts +81 -0
- package/src/runtime/time.ts +439 -0
- package/src/runtime/timeframe.ts +85 -0
- package/src/runtime/udt.ts +119 -0
package/src/ast/dump.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (C) 2024-2026 jango_blockchained
|
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
4
|
+
*
|
|
5
|
+
* Python-style `ast.dump` text for ASDL-shaped nodes.
|
|
6
|
+
*/
|
|
7
|
+
import type { AST } from "./nodes.ts";
|
|
8
|
+
|
|
9
|
+
function dumpValue(value: unknown, annotate: boolean): string {
|
|
10
|
+
if (value == null) return "None";
|
|
11
|
+
if (typeof value === "boolean") return value ? "True" : "False";
|
|
12
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
13
|
+
if (typeof value === "number") return String(value);
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return `[${value.map((v) => dumpValue(v, annotate)).join(", ")}]`;
|
|
16
|
+
}
|
|
17
|
+
if (typeof value === "object" && value !== null && "kind" in (value as object)) {
|
|
18
|
+
return dump(value as AST, { annotate_fields: annotate });
|
|
19
|
+
}
|
|
20
|
+
return String(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const SKIP = new Set(["kind", "lineno", "col_offset", "end_lineno", "end_col_offset", "kind_lit"]);
|
|
24
|
+
|
|
25
|
+
export function dump(
|
|
26
|
+
node: AST,
|
|
27
|
+
options: { annotate_fields?: boolean; include_attributes?: boolean } = {},
|
|
28
|
+
): string {
|
|
29
|
+
const annotate = options.annotate_fields !== false;
|
|
30
|
+
const rec = node as unknown as Record<string, unknown>;
|
|
31
|
+
const fields: string[] = [];
|
|
32
|
+
for (const [key, value] of Object.entries(rec)) {
|
|
33
|
+
if (SKIP.has(key)) continue;
|
|
34
|
+
if (key === "annotations" && Array.isArray(value) && value.length === 0) continue;
|
|
35
|
+
if (value === undefined) continue;
|
|
36
|
+
const rendered = dumpValue(value, annotate);
|
|
37
|
+
fields.push(annotate ? `${key}=${rendered}` : rendered);
|
|
38
|
+
}
|
|
39
|
+
if (node.kind === "Constant" && rec.kind_lit) {
|
|
40
|
+
fields.push(annotate ? `kind=${dumpValue(rec.kind_lit, annotate)}` : dumpValue(rec.kind_lit, annotate));
|
|
41
|
+
}
|
|
42
|
+
return `${node.kind}(${fields.join(", ")})`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (C) 2024-2026 jango_blockchained
|
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
4
|
+
*
|
|
5
|
+
* Public parse / unparse / dump — mirrors pynescript.ast.helper.
|
|
6
|
+
*/
|
|
7
|
+
import { CharStreams, CommonTokenStream, Token } from "antlr4";
|
|
8
|
+
import { PinescriptASTBuilder } from "./builder.ts";
|
|
9
|
+
import { dump } from "./dump.ts";
|
|
10
|
+
import { unparse } from "./unparse.ts";
|
|
11
|
+
import type { AST, Script } from "./nodes.ts";
|
|
12
|
+
import PinescriptLexer from "../generated/PinescriptLexer.ts";
|
|
13
|
+
import PinescriptParser from "../generated/PinescriptParser.ts";
|
|
14
|
+
import { PinescriptErrorListener, PinescriptSyntaxError } from "../parser/error.ts";
|
|
15
|
+
|
|
16
|
+
export { dump, unparse };
|
|
17
|
+
export { PinescriptSyntaxError };
|
|
18
|
+
|
|
19
|
+
export type ParseMode = "exec" | "eval";
|
|
20
|
+
|
|
21
|
+
function attachAnnotations(tree: AST, tokens: CommonTokenStream): void {
|
|
22
|
+
if (tree.kind !== "Script") return;
|
|
23
|
+
const script = tree as Script;
|
|
24
|
+
tokens.fill();
|
|
25
|
+
const anns: string[] = [];
|
|
26
|
+
for (const tok of tokens.tokens) {
|
|
27
|
+
if (tok == null || tok.type !== PinescriptLexer.COMMENT) continue;
|
|
28
|
+
const text = (tok.text ?? "").trim();
|
|
29
|
+
if (text.startsWith("//@")) anns.push(text);
|
|
30
|
+
}
|
|
31
|
+
if (anns.length) script.annotations = anns;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function parse(
|
|
35
|
+
source: string,
|
|
36
|
+
_filename = "<unknown>",
|
|
37
|
+
mode: ParseMode = "exec",
|
|
38
|
+
): AST {
|
|
39
|
+
if (mode !== "exec" && mode !== "eval") {
|
|
40
|
+
throw new Error(`invalid argument mode: ${mode}`);
|
|
41
|
+
}
|
|
42
|
+
const stream = CharStreams.fromString(source);
|
|
43
|
+
const lexer = new PinescriptLexer(stream);
|
|
44
|
+
const tokens = new CommonTokenStream(lexer);
|
|
45
|
+
const parser = new PinescriptParser(tokens);
|
|
46
|
+
const listener = PinescriptErrorListener.INSTANCE;
|
|
47
|
+
|
|
48
|
+
lexer.removeErrorListeners();
|
|
49
|
+
parser.removeErrorListeners();
|
|
50
|
+
lexer.addErrorListener(listener as never);
|
|
51
|
+
parser.addErrorListener(listener as never);
|
|
52
|
+
|
|
53
|
+
const rule = mode === "exec" ? parser.start_script() : parser.start_expression();
|
|
54
|
+
const builder = new PinescriptASTBuilder();
|
|
55
|
+
const node = builder.visit(rule) as AST;
|
|
56
|
+
if (!node || typeof node !== "object" || !("kind" in node)) {
|
|
57
|
+
throw new PinescriptSyntaxError("parse produced no AST");
|
|
58
|
+
}
|
|
59
|
+
if (mode === "exec" && source.includes("@")) {
|
|
60
|
+
attachAnnotations(node, tokens);
|
|
61
|
+
}
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function tokenize(source: string): Token[] {
|
|
66
|
+
const stream = CharStreams.fromString(source);
|
|
67
|
+
const lexer = new PinescriptLexer(stream);
|
|
68
|
+
lexer.removeErrorListeners();
|
|
69
|
+
lexer.addErrorListener(PinescriptErrorListener.INSTANCE as never);
|
|
70
|
+
const tokens: Token[] = [];
|
|
71
|
+
for (;;) {
|
|
72
|
+
const tok = lexer.nextToken();
|
|
73
|
+
tokens.push(tok);
|
|
74
|
+
if (tok.type === Token.EOF) break;
|
|
75
|
+
}
|
|
76
|
+
return tokens;
|
|
77
|
+
}
|
package/src/ast/nodes.ts
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (C) 2024-2026 jango_blockchained
|
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
4
|
+
*
|
|
5
|
+
* Hand-written ASDL subset (Pinescript.asdl). Field names match Python.
|
|
6
|
+
*/
|
|
7
|
+
export type ASTKind =
|
|
8
|
+
| "Script"
|
|
9
|
+
| "Expression"
|
|
10
|
+
| "Expr"
|
|
11
|
+
| "Assign"
|
|
12
|
+
| "ReAssign"
|
|
13
|
+
| "FunctionDef"
|
|
14
|
+
| "TypeDef"
|
|
15
|
+
| "EnumDef"
|
|
16
|
+
| "Param"
|
|
17
|
+
| "Call"
|
|
18
|
+
| "Name"
|
|
19
|
+
| "Constant"
|
|
20
|
+
| "Arg"
|
|
21
|
+
| "Attribute"
|
|
22
|
+
| "Subscript"
|
|
23
|
+
| "BinOp"
|
|
24
|
+
| "UnaryOp"
|
|
25
|
+
| "Compare"
|
|
26
|
+
| "If"
|
|
27
|
+
| "Tuple"
|
|
28
|
+
| "Conditional"
|
|
29
|
+
| "ForTo"
|
|
30
|
+
| "ForIn"
|
|
31
|
+
| "While"
|
|
32
|
+
| "Switch"
|
|
33
|
+
| "Case"
|
|
34
|
+
| "BoolOp"
|
|
35
|
+
| "And"
|
|
36
|
+
| "Or"
|
|
37
|
+
| "Break"
|
|
38
|
+
| "Continue"
|
|
39
|
+
| "Import"
|
|
40
|
+
| "Load"
|
|
41
|
+
| "Store"
|
|
42
|
+
| "Eq"
|
|
43
|
+
| "NotEq"
|
|
44
|
+
| "Lt"
|
|
45
|
+
| "LtE"
|
|
46
|
+
| "Gt"
|
|
47
|
+
| "GtE"
|
|
48
|
+
| "Add"
|
|
49
|
+
| "Sub"
|
|
50
|
+
| "Mult"
|
|
51
|
+
| "Div"
|
|
52
|
+
| "Mod"
|
|
53
|
+
| "UAdd"
|
|
54
|
+
| "USub"
|
|
55
|
+
| "Not"
|
|
56
|
+
| "Invert"
|
|
57
|
+
| "Var"
|
|
58
|
+
| "VarIp";
|
|
59
|
+
|
|
60
|
+
export interface AST {
|
|
61
|
+
readonly kind: ASTKind;
|
|
62
|
+
lineno?: number;
|
|
63
|
+
col_offset?: number;
|
|
64
|
+
end_lineno?: number | null;
|
|
65
|
+
end_col_offset?: number | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface Script extends AST {
|
|
69
|
+
kind: "Script";
|
|
70
|
+
body: stmt[];
|
|
71
|
+
annotations: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface Expression extends AST {
|
|
75
|
+
kind: "Expression";
|
|
76
|
+
body: expr;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface Expr extends AST {
|
|
80
|
+
kind: "Expr";
|
|
81
|
+
value: expr;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface Assign extends AST {
|
|
85
|
+
kind: "Assign";
|
|
86
|
+
target: expr;
|
|
87
|
+
value: expr | null;
|
|
88
|
+
type: expr | null;
|
|
89
|
+
mode: Var | VarIp | null;
|
|
90
|
+
export: number | null;
|
|
91
|
+
annotations: string[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface ReAssign extends AST {
|
|
95
|
+
kind: "ReAssign";
|
|
96
|
+
target: expr;
|
|
97
|
+
value: expr;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface Var extends AST {
|
|
101
|
+
kind: "Var";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface VarIp extends AST {
|
|
105
|
+
kind: "VarIp";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type decl_mode = Var | VarIp;
|
|
109
|
+
|
|
110
|
+
export interface Import extends AST {
|
|
111
|
+
kind: "Import";
|
|
112
|
+
namespace: string;
|
|
113
|
+
name: string;
|
|
114
|
+
version: number;
|
|
115
|
+
alias: string | null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type stmt = Expr | Assign | ReAssign | FunctionDef | TypeDef | EnumDef | Import | Break | Continue;
|
|
119
|
+
|
|
120
|
+
export interface FunctionDef extends AST {
|
|
121
|
+
kind: "FunctionDef";
|
|
122
|
+
name: string;
|
|
123
|
+
args: Param[];
|
|
124
|
+
body: stmt[];
|
|
125
|
+
method: number | null;
|
|
126
|
+
export: number | null;
|
|
127
|
+
annotations: string[];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface TypeDef extends AST {
|
|
131
|
+
kind: "TypeDef";
|
|
132
|
+
name: string;
|
|
133
|
+
body: stmt[];
|
|
134
|
+
export: number | null;
|
|
135
|
+
annotations: string[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface EnumDef extends AST {
|
|
139
|
+
kind: "EnumDef";
|
|
140
|
+
name: string;
|
|
141
|
+
body: stmt[];
|
|
142
|
+
export: number | null;
|
|
143
|
+
annotations: string[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface Param extends AST {
|
|
147
|
+
kind: "Param";
|
|
148
|
+
name: string;
|
|
149
|
+
default: expr | null;
|
|
150
|
+
type: expr | null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface Call extends AST {
|
|
154
|
+
kind: "Call";
|
|
155
|
+
func: expr;
|
|
156
|
+
args: Arg[];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface Name extends AST {
|
|
160
|
+
kind: "Name";
|
|
161
|
+
id: string;
|
|
162
|
+
ctx: Load | Store;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface Constant extends AST {
|
|
166
|
+
kind: "Constant";
|
|
167
|
+
value: unknown;
|
|
168
|
+
kind_lit?: string | null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface Arg extends AST {
|
|
172
|
+
kind: "Arg";
|
|
173
|
+
value: expr;
|
|
174
|
+
name: string | null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface Attribute extends AST {
|
|
178
|
+
kind: "Attribute";
|
|
179
|
+
value: expr;
|
|
180
|
+
attr: string;
|
|
181
|
+
ctx: Load | Store;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface Subscript extends AST {
|
|
185
|
+
kind: "Subscript";
|
|
186
|
+
value: expr;
|
|
187
|
+
slice: expr | null;
|
|
188
|
+
ctx: Load | Store;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface BinOp extends AST {
|
|
192
|
+
kind: "BinOp";
|
|
193
|
+
left: expr;
|
|
194
|
+
op: operator;
|
|
195
|
+
right: expr;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface UnaryOp extends AST {
|
|
199
|
+
kind: "UnaryOp";
|
|
200
|
+
op: unary_op;
|
|
201
|
+
operand: expr;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export interface Compare extends AST {
|
|
205
|
+
kind: "Compare";
|
|
206
|
+
left: expr;
|
|
207
|
+
ops: compare_op[];
|
|
208
|
+
comparators: expr[];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface If extends AST {
|
|
212
|
+
kind: "If";
|
|
213
|
+
test: expr;
|
|
214
|
+
body: stmt[];
|
|
215
|
+
orelse: stmt[];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface Tuple extends AST {
|
|
219
|
+
kind: "Tuple";
|
|
220
|
+
elts: expr[];
|
|
221
|
+
ctx: Load | Store;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export interface Conditional extends AST {
|
|
225
|
+
kind: "Conditional";
|
|
226
|
+
test: expr;
|
|
227
|
+
body: expr;
|
|
228
|
+
orelse: expr;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface ForTo extends AST {
|
|
232
|
+
kind: "ForTo";
|
|
233
|
+
target: expr;
|
|
234
|
+
start: expr;
|
|
235
|
+
end: expr;
|
|
236
|
+
body: stmt[];
|
|
237
|
+
step: expr | null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export interface ForIn extends AST {
|
|
241
|
+
kind: "ForIn";
|
|
242
|
+
target: expr;
|
|
243
|
+
iter: expr;
|
|
244
|
+
body: stmt[];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface While extends AST {
|
|
248
|
+
kind: "While";
|
|
249
|
+
test: expr;
|
|
250
|
+
body: stmt[];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface Switch extends AST {
|
|
254
|
+
kind: "Switch";
|
|
255
|
+
cases: Case[];
|
|
256
|
+
subject: expr | null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface Case extends AST {
|
|
260
|
+
kind: "Case";
|
|
261
|
+
body: stmt[];
|
|
262
|
+
pattern: expr | null;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export interface BoolOp extends AST {
|
|
266
|
+
kind: "BoolOp";
|
|
267
|
+
op: bool_op;
|
|
268
|
+
values: expr[];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface Break extends AST {
|
|
272
|
+
kind: "Break";
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface Continue extends AST {
|
|
276
|
+
kind: "Continue";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export type expr =
|
|
280
|
+
| Call
|
|
281
|
+
| Name
|
|
282
|
+
| Constant
|
|
283
|
+
| Attribute
|
|
284
|
+
| Subscript
|
|
285
|
+
| BinOp
|
|
286
|
+
| UnaryOp
|
|
287
|
+
| Compare
|
|
288
|
+
| If
|
|
289
|
+
| Tuple
|
|
290
|
+
| Conditional
|
|
291
|
+
| ForTo
|
|
292
|
+
| ForIn
|
|
293
|
+
| While
|
|
294
|
+
| Switch
|
|
295
|
+
| BoolOp;
|
|
296
|
+
|
|
297
|
+
export interface Load extends AST {
|
|
298
|
+
kind: "Load";
|
|
299
|
+
}
|
|
300
|
+
export interface Store extends AST {
|
|
301
|
+
kind: "Store";
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface Add extends AST {
|
|
305
|
+
kind: "Add";
|
|
306
|
+
}
|
|
307
|
+
export interface Sub extends AST {
|
|
308
|
+
kind: "Sub";
|
|
309
|
+
}
|
|
310
|
+
export interface Mult extends AST {
|
|
311
|
+
kind: "Mult";
|
|
312
|
+
}
|
|
313
|
+
export interface Div extends AST {
|
|
314
|
+
kind: "Div";
|
|
315
|
+
}
|
|
316
|
+
export interface Mod extends AST {
|
|
317
|
+
kind: "Mod";
|
|
318
|
+
}
|
|
319
|
+
export type operator = Add | Sub | Mult | Div | Mod;
|
|
320
|
+
|
|
321
|
+
export interface UAdd extends AST {
|
|
322
|
+
kind: "UAdd";
|
|
323
|
+
}
|
|
324
|
+
export interface USub extends AST {
|
|
325
|
+
kind: "USub";
|
|
326
|
+
}
|
|
327
|
+
export interface Not extends AST {
|
|
328
|
+
kind: "Not";
|
|
329
|
+
}
|
|
330
|
+
export interface Invert extends AST {
|
|
331
|
+
kind: "Invert";
|
|
332
|
+
}
|
|
333
|
+
export type unary_op = UAdd | USub | Not | Invert;
|
|
334
|
+
|
|
335
|
+
export interface Eq extends AST {
|
|
336
|
+
kind: "Eq";
|
|
337
|
+
}
|
|
338
|
+
export interface NotEq extends AST {
|
|
339
|
+
kind: "NotEq";
|
|
340
|
+
}
|
|
341
|
+
export interface Lt extends AST {
|
|
342
|
+
kind: "Lt";
|
|
343
|
+
}
|
|
344
|
+
export interface LtE extends AST {
|
|
345
|
+
kind: "LtE";
|
|
346
|
+
}
|
|
347
|
+
export interface Gt extends AST {
|
|
348
|
+
kind: "Gt";
|
|
349
|
+
}
|
|
350
|
+
export interface GtE extends AST {
|
|
351
|
+
kind: "GtE";
|
|
352
|
+
}
|
|
353
|
+
export type compare_op = Eq | NotEq | Lt | LtE | Gt | GtE;
|
|
354
|
+
|
|
355
|
+
export interface And extends AST {
|
|
356
|
+
kind: "And";
|
|
357
|
+
}
|
|
358
|
+
export interface Or extends AST {
|
|
359
|
+
kind: "Or";
|
|
360
|
+
}
|
|
361
|
+
export type bool_op = And | Or;
|
|
362
|
+
|
|
363
|
+
export const Load: Load = { kind: "Load" };
|
|
364
|
+
export const Store: Store = { kind: "Store" };
|
|
365
|
+
export const Var: Var = { kind: "Var" };
|
|
366
|
+
export const VarIp: VarIp = { kind: "VarIp" };
|
|
367
|
+
export const Add: Add = { kind: "Add" };
|
|
368
|
+
export const Sub: Sub = { kind: "Sub" };
|
|
369
|
+
export const Mult: Mult = { kind: "Mult" };
|
|
370
|
+
export const Div: Div = { kind: "Div" };
|
|
371
|
+
export const Mod: Mod = { kind: "Mod" };
|
|
372
|
+
export const UAdd: UAdd = { kind: "UAdd" };
|
|
373
|
+
export const USub: USub = { kind: "USub" };
|
|
374
|
+
export const NotOp: Not = { kind: "Not" };
|
|
375
|
+
export const Invert: Invert = { kind: "Invert" };
|
|
376
|
+
export const Eq: Eq = { kind: "Eq" };
|
|
377
|
+
export const NotEq: NotEq = { kind: "NotEq" };
|
|
378
|
+
export const Lt: Lt = { kind: "Lt" };
|
|
379
|
+
export const LtE: LtE = { kind: "LtE" };
|
|
380
|
+
export const Gt: Gt = { kind: "Gt" };
|
|
381
|
+
export const GtE: GtE = { kind: "GtE" };
|
|
382
|
+
export const And: And = { kind: "And" };
|
|
383
|
+
export const Or: Or = { kind: "Or" };
|
|
384
|
+
|
|
385
|
+
export function script(body: stmt[] = [], annotations: string[] = []): Script {
|
|
386
|
+
return { kind: "Script", body, annotations };
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function expression(body: expr): Expression {
|
|
390
|
+
return { kind: "Expression", body };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function exprStmt(value: expr): Expr {
|
|
394
|
+
return { kind: "Expr", value };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function call(func: expr, args: Arg[] = []): Call {
|
|
398
|
+
return { kind: "Call", func, args };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function name(id: string, ctx: Load | Store = Load): Name {
|
|
402
|
+
return { kind: "Name", id, ctx };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export function constant(value: unknown, kind_lit: string | null = null): Constant {
|
|
406
|
+
return { kind: "Constant", value, kind_lit };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export function arg(value: expr, name: string | null = null): Arg {
|
|
410
|
+
return { kind: "Arg", value, name };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function attribute(value: expr, attrName: string, ctx: Load | Store = Load): Attribute {
|
|
414
|
+
return { kind: "Attribute", value, attr: attrName, ctx };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function subscript(value: expr, slice: expr | null = null, ctx: Load | Store = Load): Subscript {
|
|
418
|
+
return { kind: "Subscript", value, slice, ctx };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function binOp(left: expr, op: operator, right: expr): BinOp {
|
|
422
|
+
return { kind: "BinOp", left, op, right };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function unaryOp(op: unary_op, operand: expr): UnaryOp {
|
|
426
|
+
return { kind: "UnaryOp", op, operand };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export function assign(
|
|
430
|
+
target: expr,
|
|
431
|
+
value: expr | null,
|
|
432
|
+
type: expr | null = null,
|
|
433
|
+
mode: decl_mode | null = null,
|
|
434
|
+
exported: number | null = null,
|
|
435
|
+
annotations: string[] = [],
|
|
436
|
+
): Assign {
|
|
437
|
+
return {
|
|
438
|
+
kind: "Assign",
|
|
439
|
+
target,
|
|
440
|
+
value,
|
|
441
|
+
type,
|
|
442
|
+
mode,
|
|
443
|
+
export: exported,
|
|
444
|
+
annotations,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function reAssign(target: expr, value: expr): ReAssign {
|
|
449
|
+
return { kind: "ReAssign", target, value };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export function functionDef(
|
|
453
|
+
name: string,
|
|
454
|
+
args: Param[] = [],
|
|
455
|
+
body: stmt[] = [],
|
|
456
|
+
method: number | null = null,
|
|
457
|
+
exported: number | null = null,
|
|
458
|
+
annotations: string[] = [],
|
|
459
|
+
): FunctionDef {
|
|
460
|
+
return {
|
|
461
|
+
kind: "FunctionDef",
|
|
462
|
+
name,
|
|
463
|
+
args,
|
|
464
|
+
body,
|
|
465
|
+
method,
|
|
466
|
+
export: exported,
|
|
467
|
+
annotations,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function typeDef(
|
|
472
|
+
name: string,
|
|
473
|
+
body: stmt[] = [],
|
|
474
|
+
exported: number | null = null,
|
|
475
|
+
annotations: string[] = [],
|
|
476
|
+
): TypeDef {
|
|
477
|
+
return {
|
|
478
|
+
kind: "TypeDef",
|
|
479
|
+
name,
|
|
480
|
+
body,
|
|
481
|
+
export: exported,
|
|
482
|
+
annotations,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export function enumDef(
|
|
487
|
+
name: string,
|
|
488
|
+
body: stmt[] = [],
|
|
489
|
+
exported: number | null = null,
|
|
490
|
+
annotations: string[] = [],
|
|
491
|
+
): EnumDef {
|
|
492
|
+
return {
|
|
493
|
+
kind: "EnumDef",
|
|
494
|
+
name,
|
|
495
|
+
body,
|
|
496
|
+
export: exported,
|
|
497
|
+
annotations,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
export function param(
|
|
502
|
+
name: string,
|
|
503
|
+
defaultValue: expr | null = null,
|
|
504
|
+
type: expr | null = null,
|
|
505
|
+
): Param {
|
|
506
|
+
return { kind: "Param", name, default: defaultValue, type };
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export function compare(left: expr, ops: compare_op[], comparators: expr[]): Compare {
|
|
510
|
+
return { kind: "Compare", left, ops, comparators };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export function ifExpr(test: expr, body: stmt[] = [], orelse: stmt[] = []): If {
|
|
514
|
+
return { kind: "If", test, body, orelse };
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export function tupleExpr(elts: expr[], ctx: Load | Store = Store): Tuple {
|
|
518
|
+
return { kind: "Tuple", elts, ctx };
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export function conditional(test: expr, body: expr, orelse: expr): Conditional {
|
|
522
|
+
return { kind: "Conditional", test, body, orelse };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export function forTo(
|
|
526
|
+
target: expr,
|
|
527
|
+
start: expr,
|
|
528
|
+
end: expr,
|
|
529
|
+
body: stmt[],
|
|
530
|
+
step: expr | null = null,
|
|
531
|
+
): ForTo {
|
|
532
|
+
return { kind: "ForTo", target, start, end, body, step };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export function forIn(target: expr, iter: expr, body: stmt[]): ForIn {
|
|
536
|
+
return { kind: "ForIn", target, iter, body };
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function whileExpr(test: expr, body: stmt[] = []): While {
|
|
540
|
+
return { kind: "While", test, body };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export function switchExpr(cases: Case[] = [], subject: expr | null = null): Switch {
|
|
544
|
+
return { kind: "Switch", cases, subject };
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export function caseNode(body: stmt[] = [], pattern: expr | null = null): Case {
|
|
548
|
+
return { kind: "Case", body, pattern };
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export function boolOp(op: bool_op, values: expr[] = []): BoolOp {
|
|
552
|
+
return { kind: "BoolOp", op, values };
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export function breakStmt(): Break {
|
|
556
|
+
return { kind: "Break" };
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function continueStmt(): Continue {
|
|
560
|
+
return { kind: "Continue" };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export function importStmt(
|
|
564
|
+
namespace: string,
|
|
565
|
+
name: string,
|
|
566
|
+
version: number,
|
|
567
|
+
alias: string | null = null,
|
|
568
|
+
): Import {
|
|
569
|
+
return { kind: "Import", namespace, name, version, alias };
|
|
570
|
+
}
|