@xnoxs/flux-lang 4.0.9 → 4.1.1
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 +19 -0
- package/dist/flux-cli.js +7959 -4853
- package/dist/flux.cjs.js +5507 -3647
- package/dist/flux.esm.js +5505 -3650
- package/dist/flux.min.js +356 -95
- package/dist/transpiler.js +4 -0
- package/package.json +1 -1
- package/src/config.js +101 -86
- package/src/formatter.js +105 -100
- package/src/self/bundler.js +1 -197
- package/src/self/checker.js +0 -2
- package/src/self/cli.js +0 -2
- package/src/self/codegen.js +1 -811
- package/src/self/config.js +0 -2
- package/src/self/css-preprocessor.js +1 -3
- package/src/self/formatter.js +0 -2
- package/src/self/jsx.js +2 -4
- package/src/self/lexer.js +6 -8
- package/src/self/linter.js +0 -2
- package/src/self/mangler.js +0 -2
- package/src/self/parser.js +0 -2
- package/src/self/pkg.js +0 -2
- package/src/self/sourcemap.js +0 -2
- package/src/self/stdlib.js +0 -2
- package/src/self/test-runner.js +0 -2
- package/src/self/transpiler.js +0 -2
- package/src/self/type-checker.js +0 -2
- package/src/stdlib.js +731 -218
- package/src/self/index.flux +0 -87
package/src/self/codegen.js
CHANGED
|
@@ -1,811 +1 @@
|
|
|
1
|
-
/* compiled from src/self/codegen.flux by Flux Lang */
|
|
2
|
-
'use strict';
|
|
3
|
-
// ── Flux stdlib ──
|
|
4
|
-
|
|
5
|
-
function map(arr, fn) { return arr.map(fn); }
|
|
6
|
-
|
|
7
|
-
function join(arr, sep) { return arr.join(sep != null ? sep : ','); }
|
|
8
|
-
|
|
9
|
-
function round(n, decimals) {
|
|
10
|
-
if (decimals == null) return Math.round(n);
|
|
11
|
-
var f = Math.pow(10, decimals);
|
|
12
|
-
return Math.round(n * f) / f;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function trim(s) { return String(s).trim(); }
|
|
16
|
-
|
|
17
|
-
function trimStart(s) { return String(s).trimStart(); }
|
|
18
|
-
// ── end stdlib ──
|
|
19
|
-
|
|
20
|
-
// Generated by Flux Transpiler v3.2.0
|
|
21
|
-
"use strict";
|
|
22
|
-
|
|
23
|
-
const { Lexer, lexerize, T } = require("./lexer");
|
|
24
|
-
const { Parser, makeParser } = require("./parser");
|
|
25
|
-
function extractFormatSpec(raw) {
|
|
26
|
-
const ci = raw.lastIndexOf(":");
|
|
27
|
-
if ((ci < 1)) {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
const spec = raw.slice((ci + 1)).trim();
|
|
31
|
-
if ((((spec.length > 0) && /[.<>^,dbeEfFgGoOxXs%bcn]/.test(spec)) && /^([.<>^0\-+ #,]*[0-9]*\.?[0-9]*[dbeEfFgGoOxXs%bcn]?[,]?)$/.test(spec))) {
|
|
32
|
-
return { expr: raw.slice(0, ci).trim(), fmt: spec };
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
const FMT_HELPER = "function _fmt(v, s) {\n if (s === ',') return (+v).toLocaleString();\n if (s === '%') return ((+v)*100).toFixed(0)+'%';\n var m = s.match(/^([0-9,]*)?\\.([0-9]+)([fdgGeEb%x])$/);\n if (m) {\n var d=+m[2], t=m[3], comma=s[0]===',';\n if (t==='f'||t==='d') return comma ? (+v).toLocaleString(void 0,{minimumFractionDigits:d,maximumFractionDigits:d}) : (+v).toFixed(d);\n if (t==='e'||t==='E') return (+v).toExponential(d);\n if (t==='%') return ((+v)*100).toFixed(d)+'%';\n if (t==='b') return Math.round(+v).toString(2);\n if (t==='x') return Math.round(+v).toString(16);\n }\n var m2 = s.match(/^([0-9]*)d$/); if (m2) return Math.round(+v).toString();\n return String(v);\n}";
|
|
37
|
-
function buildClassRegistry(ast) {
|
|
38
|
-
const reg = { };
|
|
39
|
-
for (const node of ast.body) {
|
|
40
|
-
const n = ((node.type == "ExportDecl") ? node.decl : node);
|
|
41
|
-
if ((n.type == "ClassDecl")) {
|
|
42
|
-
reg[n.name] = { fields: n.fields, superClass: n.superClass };
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return reg;
|
|
46
|
-
}
|
|
47
|
-
function getAllFields(name, reg, visited) {
|
|
48
|
-
const vis = (visited ?? new Set([]));
|
|
49
|
-
if (((!name || !reg[name]) || vis.has(name))) {
|
|
50
|
-
return [];
|
|
51
|
-
}
|
|
52
|
-
vis.add(name);
|
|
53
|
-
const parent = getAllFields(reg[name].superClass, reg, vis);
|
|
54
|
-
return [...parent, ...reg[name].fields];
|
|
55
|
-
}
|
|
56
|
-
class CodeGenerator {
|
|
57
|
-
constructor(ind, level, lines, clsReg, smBuilder, _needsFmt, _loopDepth, _fnDepth, _version) {
|
|
58
|
-
this.ind = ind;
|
|
59
|
-
this.level = level;
|
|
60
|
-
this.lines = lines;
|
|
61
|
-
this.clsReg = clsReg;
|
|
62
|
-
this.smBuilder = smBuilder;
|
|
63
|
-
this._needsFmt = _needsFmt;
|
|
64
|
-
this._loopDepth = _loopDepth;
|
|
65
|
-
this._fnDepth = _fnDepth;
|
|
66
|
-
this._version = _version;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
i() {
|
|
70
|
-
let s = "";
|
|
71
|
-
let n = this.level;
|
|
72
|
-
while ((n > 0)) {
|
|
73
|
-
s += this.ind;
|
|
74
|
-
n = (n - 1);
|
|
75
|
-
}
|
|
76
|
-
return s;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
emit(s) {
|
|
80
|
-
this.lines.push((this.i() + s));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
emitRaw(s) {
|
|
84
|
-
this.lines.push(s);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
blank() {
|
|
88
|
-
this.lines.push("");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
indIn() {
|
|
92
|
-
this.level = (this.level + 1);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
indOut() {
|
|
96
|
-
if ((this.level > 0)) {
|
|
97
|
-
this.level = (this.level - 1);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
generate(ast) {
|
|
102
|
-
this.clsReg = buildClassRegistry(ast);
|
|
103
|
-
this.lines = [];
|
|
104
|
-
this.level = 0;
|
|
105
|
-
this._needsFmt = false;
|
|
106
|
-
this.emit((("// Generated by Flux Transpiler v" + (this._version ?? "3.5.3")) + " (self-hosted)"));
|
|
107
|
-
this.emit("\"use strict\";");
|
|
108
|
-
this.blank();
|
|
109
|
-
for (const node of ast.body) {
|
|
110
|
-
this.genStmt(node);
|
|
111
|
-
}
|
|
112
|
-
if (this._needsFmt) {
|
|
113
|
-
this.lines.splice(2, 0, FMT_HELPER);
|
|
114
|
-
}
|
|
115
|
-
return { code: this.lines.join("\n"), smBuilder: this.smBuilder };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
genStmt(node) {
|
|
119
|
-
if ((node.type == "VarDecl")) {
|
|
120
|
-
this.genVar(node);
|
|
121
|
-
}
|
|
122
|
-
else if ((node.type == "DestructureDecl")) {
|
|
123
|
-
this.genDestructure(node);
|
|
124
|
-
}
|
|
125
|
-
else if ((node.type == "FnDecl")) {
|
|
126
|
-
this.genFn(node, "");
|
|
127
|
-
}
|
|
128
|
-
else if ((node.type == "ClassDecl")) {
|
|
129
|
-
this.genClass(node);
|
|
130
|
-
}
|
|
131
|
-
else if ((node.type == "IfStmt")) {
|
|
132
|
-
this.genIf(node);
|
|
133
|
-
}
|
|
134
|
-
else if ((node.type == "ForInStmt")) {
|
|
135
|
-
this.genFor(node);
|
|
136
|
-
}
|
|
137
|
-
else if ((node.type == "WhileStmt")) {
|
|
138
|
-
this.genWhile(node);
|
|
139
|
-
}
|
|
140
|
-
else if ((node.type == "MatchStmt")) {
|
|
141
|
-
this.genMatch(node);
|
|
142
|
-
}
|
|
143
|
-
else if ((node.type == "ReturnStmt")) {
|
|
144
|
-
this.genReturn(node);
|
|
145
|
-
}
|
|
146
|
-
else if ((node.type == "TryCatchStmt")) {
|
|
147
|
-
this.genTryCatch(node);
|
|
148
|
-
}
|
|
149
|
-
else if ((node.type == "ThrowStmt")) {
|
|
150
|
-
this.genThrow(node);
|
|
151
|
-
}
|
|
152
|
-
else if ((node.type == "DoWhileStmt")) {
|
|
153
|
-
this.genDoWhile(node);
|
|
154
|
-
}
|
|
155
|
-
else if ((node.type == "BreakStmt")) {
|
|
156
|
-
this.emit("break;");
|
|
157
|
-
}
|
|
158
|
-
else if ((node.type == "ContinueStmt")) {
|
|
159
|
-
this.emit("continue;");
|
|
160
|
-
}
|
|
161
|
-
else if ((node.type == "ImportDecl")) {
|
|
162
|
-
this.genImport(node);
|
|
163
|
-
}
|
|
164
|
-
else if ((node.type == "ExportDecl")) {
|
|
165
|
-
this.genExport(node);
|
|
166
|
-
}
|
|
167
|
-
else if ((node.type == "TypeDecl")) {
|
|
168
|
-
this.genTypeDecl(node);
|
|
169
|
-
}
|
|
170
|
-
else if ((node.type == "InterfaceDecl")) {
|
|
171
|
-
this.genInterfaceDecl(node);
|
|
172
|
-
}
|
|
173
|
-
else if ((node.type == "EnumDecl")) {
|
|
174
|
-
this.genEnumDecl(node);
|
|
175
|
-
}
|
|
176
|
-
else if ((node.type == "DeclareDecl")) {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
else if ((node.type == "ExprStmt")) {
|
|
180
|
-
this.emit((this.genExpr(node.expr) + ";"));
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
throw new Error(`Unknown statement: ${node.type}`);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
genVar(node) {
|
|
188
|
-
const kw = ((node.kind == "val") ? "const" : "let");
|
|
189
|
-
const init = (node.init ? (" = " + this.genExpr(node.init)) : "");
|
|
190
|
-
this.emit(((((kw + " ") + node.name) + init) + ";"));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
genObjPair(p) {
|
|
194
|
-
if (p.spread) {
|
|
195
|
-
return ("..." + this.genExpr(p.value));
|
|
196
|
-
}
|
|
197
|
-
const isIdent = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p.key);
|
|
198
|
-
const keyStr = (isIdent ? p.key : (("\"" + p.key) + "\""));
|
|
199
|
-
if ((((isIdent && p.value) && (p.value.type == "Identifier")) && (p.value.name == p.key))) {
|
|
200
|
-
return p.key;
|
|
201
|
-
}
|
|
202
|
-
return ((keyStr + ": ") + this.genExpr(p.value));
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
genDestructProp(p) {
|
|
206
|
-
if (p.rest) {
|
|
207
|
-
return ("..." + p.key);
|
|
208
|
-
}
|
|
209
|
-
let s = ((p.alias != p.key) ? ((p.key + ": ") + p.alias) : p.key);
|
|
210
|
-
if (p.defaultVal) {
|
|
211
|
-
s = ((s + " = ") + this.genExpr(p.defaultVal));
|
|
212
|
-
}
|
|
213
|
-
return s;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
genDestructItem(p) {
|
|
217
|
-
if (!p) {
|
|
218
|
-
return "";
|
|
219
|
-
}
|
|
220
|
-
if (p.rest) {
|
|
221
|
-
return ("..." + p.name);
|
|
222
|
-
}
|
|
223
|
-
let s = p.name;
|
|
224
|
-
if (p.defaultVal) {
|
|
225
|
-
s = ((s + " = ") + this.genExpr(p.defaultVal));
|
|
226
|
-
}
|
|
227
|
-
return s;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
genDestructure(node) {
|
|
231
|
-
const kw = ((node.kind == "val") ? "const" : "let");
|
|
232
|
-
const init = this.genExpr(node.init);
|
|
233
|
-
if ((node.patternType == "object")) {
|
|
234
|
-
const props = node.pattern.map((p) => this.genDestructProp(p)).join(", ");
|
|
235
|
-
this.emit((((((kw + " { ") + props) + " } = ") + init) + ";"));
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
const items = node.pattern.map((p) => this.genDestructItem(p)).join(", ");
|
|
239
|
-
this.emit((((((kw + " [") + items) + "] = ") + init) + ";"));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
genFn(node, prefix) {
|
|
244
|
-
const asyncKw = (node.async ? "async " : "");
|
|
245
|
-
const name = (node.name ?? "");
|
|
246
|
-
const params = node.params.map((p) => (p.rest ? ("..." + p.name) : (p.defaultVal ? ((p.name + " = ") + this.genExpr(p.defaultVal)) : p.name))).join(", ");
|
|
247
|
-
if (node.inline) {
|
|
248
|
-
this.emit(((((((((prefix + asyncKw) + "function ") + name) + "(") + params) + ") { return ") + this.genExpr(node.body)) + "; }"));
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
this.emit(((((((prefix + asyncKw) + "function ") + name) + "(") + params) + ") {"));
|
|
252
|
-
this.indIn();
|
|
253
|
-
this._fnDepth = (this._fnDepth + 1);
|
|
254
|
-
for (const s of node.body) {
|
|
255
|
-
this.genStmt(s);
|
|
256
|
-
}
|
|
257
|
-
this._fnDepth = (this._fnDepth - 1);
|
|
258
|
-
this.indOut();
|
|
259
|
-
this.emit("}");
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
genClass(node) {
|
|
264
|
-
const ext = (node.superClass ? (" extends " + node.superClass) : "");
|
|
265
|
-
this.emit(((("class " + node.name) + ext) + " {"));
|
|
266
|
-
this.indIn();
|
|
267
|
-
const allFields = getAllFields(node.name, this.clsReg, null);
|
|
268
|
-
if ((allFields.length > 0)) {
|
|
269
|
-
const params = allFields.map((f) => f.name).join(", ");
|
|
270
|
-
this.emit((("constructor(" + params) + ") {"));
|
|
271
|
-
this.indIn();
|
|
272
|
-
if ((node.superClass && this.clsReg[node.superClass])) {
|
|
273
|
-
const parentFields = getAllFields(node.superClass, this.clsReg, null);
|
|
274
|
-
if ((parentFields.length > 0)) {
|
|
275
|
-
this.emit((("super(" + parentFields.map((f) => f.name).join(", ")) + ");"));
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
for (const f of node.fields) {
|
|
279
|
-
this.emit((((("this." + f.name) + " = ") + f.name) + ";"));
|
|
280
|
-
}
|
|
281
|
-
this.indOut();
|
|
282
|
-
this.emit("}");
|
|
283
|
-
this.blank();
|
|
284
|
-
}
|
|
285
|
-
for (const m of node.methods) {
|
|
286
|
-
const asyncKw = (m.async ? "async " : "");
|
|
287
|
-
const staticKw = ((m.modifiers && m.modifiers.has("static")) ? "static " : "");
|
|
288
|
-
const params = m.params.map((p) => (p.rest ? ("..." + p.name) : (p.defaultVal ? ((p.name + " = ") + this.genExpr(p.defaultVal)) : p.name))).join(", ");
|
|
289
|
-
if (m.inline) {
|
|
290
|
-
this.emit((((((((staticKw + asyncKw) + m.name) + "(") + params) + ") { return ") + this.genExpr(m.body)) + "; }"));
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
this.emit((((((staticKw + asyncKw) + m.name) + "(") + params) + ") {"));
|
|
294
|
-
this.indIn();
|
|
295
|
-
this._fnDepth = (this._fnDepth + 1);
|
|
296
|
-
for (const s of m.body) {
|
|
297
|
-
this.genStmt(s);
|
|
298
|
-
}
|
|
299
|
-
this._fnDepth = (this._fnDepth - 1);
|
|
300
|
-
this.indOut();
|
|
301
|
-
this.emit("}");
|
|
302
|
-
}
|
|
303
|
-
this.blank();
|
|
304
|
-
}
|
|
305
|
-
this.indOut();
|
|
306
|
-
this.emit("}");
|
|
307
|
-
this.blank();
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
genIf(node) {
|
|
311
|
-
this.emit((("if (" + this.genExpr(node.cond)) + ") {"));
|
|
312
|
-
this.indIn();
|
|
313
|
-
for (const s of node.then) {
|
|
314
|
-
this.genStmt(s);
|
|
315
|
-
}
|
|
316
|
-
this.indOut();
|
|
317
|
-
this.emit("}");
|
|
318
|
-
for (const ei of node.elseifs) {
|
|
319
|
-
this.emitRaw((((this.i() + "else if (") + this.genExpr(ei.cond)) + ") {"));
|
|
320
|
-
this.indIn();
|
|
321
|
-
for (const s of ei.body) {
|
|
322
|
-
this.genStmt(s);
|
|
323
|
-
}
|
|
324
|
-
this.indOut();
|
|
325
|
-
this.emit("}");
|
|
326
|
-
}
|
|
327
|
-
if (node.else_) {
|
|
328
|
-
this.emitRaw((this.i() + "else {"));
|
|
329
|
-
this.indIn();
|
|
330
|
-
for (const s of node.else_) {
|
|
331
|
-
this.genStmt(s);
|
|
332
|
-
}
|
|
333
|
-
this.indOut();
|
|
334
|
-
this.emit("}");
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
genFor(node) {
|
|
339
|
-
const varName = node["var"];
|
|
340
|
-
const iter = node.iter;
|
|
341
|
-
if ((iter.type == "RangeExpr")) {
|
|
342
|
-
const s = this.genExpr(iter.start);
|
|
343
|
-
const e = this.genExpr(iter.end);
|
|
344
|
-
this.emit((((((((((("for (let " + varName) + " = ") + s) + "; ") + varName) + " < ") + e) + "; ") + varName) + "++) {"));
|
|
345
|
-
}
|
|
346
|
-
else if (node.isAwait) {
|
|
347
|
-
this.emit((((("for await (const " + varName) + " of ") + this.genExpr(iter)) + ") {"));
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
this.emit((((("for (const " + varName) + " of ") + this.genExpr(iter)) + ") {"));
|
|
351
|
-
}
|
|
352
|
-
this._loopDepth = (this._loopDepth + 1);
|
|
353
|
-
this.indIn();
|
|
354
|
-
for (const s of node.body) {
|
|
355
|
-
this.genStmt(s);
|
|
356
|
-
}
|
|
357
|
-
this.indOut();
|
|
358
|
-
this._loopDepth = (this._loopDepth - 1);
|
|
359
|
-
this.emit("}");
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
genWhile(node) {
|
|
363
|
-
this.emit((("while (" + this.genExpr(node.cond)) + ") {"));
|
|
364
|
-
this._loopDepth = (this._loopDepth + 1);
|
|
365
|
-
this.indIn();
|
|
366
|
-
for (const s of node.body) {
|
|
367
|
-
this.genStmt(s);
|
|
368
|
-
}
|
|
369
|
-
this.indOut();
|
|
370
|
-
this._loopDepth = (this._loopDepth - 1);
|
|
371
|
-
this.emit("}");
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
genMatch(node) {
|
|
375
|
-
const subj = this.genExpr(node.subject);
|
|
376
|
-
const arms = node.arms;
|
|
377
|
-
let hasOpenIf = false;
|
|
378
|
-
for (const arm of arms) {
|
|
379
|
-
const pat = arm.pattern;
|
|
380
|
-
let cond = "";
|
|
381
|
-
const bindings = [];
|
|
382
|
-
if ((pat.type == "WildcardPat")) {
|
|
383
|
-
cond = "";
|
|
384
|
-
}
|
|
385
|
-
else if ((pat.type == "RangePat")) {
|
|
386
|
-
const lo = this.genExpr(pat.start);
|
|
387
|
-
const hi = this.genExpr(pat.end);
|
|
388
|
-
cond = ((((((subj + " >= ") + lo) + " && ") + subj) + " <= ") + hi);
|
|
389
|
-
}
|
|
390
|
-
else if ((pat.type == "VariantPat")) {
|
|
391
|
-
cond = (((subj + "?.__type === \"") + pat.variant) + "\"");
|
|
392
|
-
let bi = 0;
|
|
393
|
-
for (const b of pat.bindings) {
|
|
394
|
-
bindings.push((((((("const " + b) + " = ") + subj) + ".__args[") + bi) + "];"));
|
|
395
|
-
bi = (bi + 1);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
const patExpr = this.genExpr(pat.value);
|
|
400
|
-
if (((pat.value.type == "Identifier") && /^[A-Z]/.test(pat.value.name))) {
|
|
401
|
-
cond = (((((((("(" + subj) + " === ") + patExpr) + " || ") + subj) + "?.__type === \"") + pat.value.name) + "\")");
|
|
402
|
-
}
|
|
403
|
-
else {
|
|
404
|
-
cond = ((subj + " === ") + patExpr);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
if (arm.guard) {
|
|
408
|
-
const guardSrc = this.genExpr(arm.guard);
|
|
409
|
-
if ((bindings.length > 0)) {
|
|
410
|
-
const rebindings = bindings.map((bx) => bx.replace("const ", "var ")).join(" ");
|
|
411
|
-
const iife = (((("(function(){ " + rebindings) + " return (") + guardSrc) + "); }())");
|
|
412
|
-
cond = (cond ? ((("(" + cond) + ") && ") + iife) : iife);
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
cond = (cond ? (((("(" + cond) + ") && (") + guardSrc) + ")") : guardSrc);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
if ((!hasOpenIf && cond)) {
|
|
419
|
-
this.emit((("if (" + cond) + ") {"));
|
|
420
|
-
hasOpenIf = true;
|
|
421
|
-
}
|
|
422
|
-
else if ((!hasOpenIf && !cond)) {
|
|
423
|
-
this.emit("if (true) {");
|
|
424
|
-
hasOpenIf = true;
|
|
425
|
-
}
|
|
426
|
-
else if ((hasOpenIf && cond)) {
|
|
427
|
-
this.emitRaw((((this.i() + "else if (") + cond) + ") {"));
|
|
428
|
-
}
|
|
429
|
-
else {
|
|
430
|
-
this.emitRaw((this.i() + "else {"));
|
|
431
|
-
}
|
|
432
|
-
this.indIn();
|
|
433
|
-
for (const b of bindings) {
|
|
434
|
-
this.emit(b);
|
|
435
|
-
}
|
|
436
|
-
if (arm.inline) {
|
|
437
|
-
const exprSrc = this.genExpr(arm.body[0].expr);
|
|
438
|
-
if ((this._loopDepth > 0)) {
|
|
439
|
-
this.emit((exprSrc + ";"));
|
|
440
|
-
}
|
|
441
|
-
else if ((this._fnDepth > 0)) {
|
|
442
|
-
this.emit((("return " + exprSrc) + ";"));
|
|
443
|
-
}
|
|
444
|
-
else {
|
|
445
|
-
this.emit((exprSrc + ";"));
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
else {
|
|
449
|
-
for (const s of arm.body) {
|
|
450
|
-
this.genStmt(s);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
this.indOut();
|
|
454
|
-
this.emit("}");
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
genInterfaceDecl(node) {
|
|
459
|
-
this.blank();
|
|
460
|
-
this.emit(("// interface " + node.name));
|
|
461
|
-
this.blank();
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
genEnumDecl(node) {
|
|
465
|
-
this.blank();
|
|
466
|
-
const pairs = node.members.map((m) => ((m.name + ": ") + this.genExpr(m.value))).join(", ");
|
|
467
|
-
this.emit((((("const " + node.name) + " = Object.freeze({ ") + pairs) + " });"));
|
|
468
|
-
this.blank();
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
genTypeDecl(node) {
|
|
472
|
-
this.blank();
|
|
473
|
-
this.emit(("// ADT type: " + node.name));
|
|
474
|
-
for (const v of node.variants) {
|
|
475
|
-
if ((v.fields.length == 0)) {
|
|
476
|
-
this.emit((((("const " + v.name) + " = Object.freeze({ __type: \"") + v.name) + "\", __args: [] });"));
|
|
477
|
-
}
|
|
478
|
-
else {
|
|
479
|
-
const params = v.fields.join(", ");
|
|
480
|
-
const argsArr = (("[" + v.fields.join(", ")) + "]");
|
|
481
|
-
const fieldsObj = v.fields.map((f) => ((f + ": ") + f)).join(", ");
|
|
482
|
-
this.emit((((((((((("function " + v.name) + "(") + params) + ") { return Object.freeze({ __type: \"") + v.name) + "\", __args: ") + argsArr) + ", ") + fieldsObj) + " }); }"));
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
this.blank();
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
genReturn(node) {
|
|
489
|
-
if (node.value) {
|
|
490
|
-
this.emit((("return " + this.genExpr(node.value)) + ";"));
|
|
491
|
-
}
|
|
492
|
-
else {
|
|
493
|
-
this.emit("return;");
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
genTryCatch(node) {
|
|
498
|
-
this.emit("try {");
|
|
499
|
-
this.indIn();
|
|
500
|
-
for (const s of node.tryBody) {
|
|
501
|
-
this.genStmt(s);
|
|
502
|
-
}
|
|
503
|
-
this.indOut();
|
|
504
|
-
this.emit("}");
|
|
505
|
-
if (node.catchBody) {
|
|
506
|
-
const param = (node.catchParam ? (("(" + node.catchParam) + ")") : "(_err)");
|
|
507
|
-
this.emitRaw((((this.i() + "catch ") + param) + " {"));
|
|
508
|
-
this.indIn();
|
|
509
|
-
for (const s of node.catchBody) {
|
|
510
|
-
this.genStmt(s);
|
|
511
|
-
}
|
|
512
|
-
this.indOut();
|
|
513
|
-
this.emit("}");
|
|
514
|
-
}
|
|
515
|
-
if (node.finallyBody) {
|
|
516
|
-
this.emitRaw((this.i() + "finally {"));
|
|
517
|
-
this.indIn();
|
|
518
|
-
for (const s of node.finallyBody) {
|
|
519
|
-
this.genStmt(s);
|
|
520
|
-
}
|
|
521
|
-
this.indOut();
|
|
522
|
-
this.emit("}");
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
genThrow(node) {
|
|
527
|
-
this.emit((("throw " + this.genExpr(node.value)) + ";"));
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
genDoWhile(node) {
|
|
531
|
-
this.emit("do {");
|
|
532
|
-
this.indIn();
|
|
533
|
-
for (const s of node.body) {
|
|
534
|
-
this.genStmt(s);
|
|
535
|
-
}
|
|
536
|
-
this.indOut();
|
|
537
|
-
this.emit((("} while (" + this.genExpr(node.cond)) + ");"));
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
genImport(node) {
|
|
541
|
-
const src = String(node.source);
|
|
542
|
-
if (node.namespaceName) {
|
|
543
|
-
this.emit((((("const " + node.namespaceName) + " = require(\"") + src) + "\");"));
|
|
544
|
-
}
|
|
545
|
-
else if (node.defaultName) {
|
|
546
|
-
this.emit((((("const " + node.defaultName) + " = require(\"") + src) + "\");"));
|
|
547
|
-
}
|
|
548
|
-
else if (node.names.length) {
|
|
549
|
-
const parts = node.names.map((n) => ((typeof n == "string") ? n : ((n.name != n.alias) ? ((n.name + ": ") + n.alias) : n.name))).join(", ");
|
|
550
|
-
this.emit((((("const { " + parts) + " } = require(\"") + src) + "\");"));
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
genExport(node) {
|
|
555
|
-
if (node.isDefault) {
|
|
556
|
-
this.emit((("module.exports = " + this.genExpr(node.decl)) + ";"));
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
const decl = node.decl;
|
|
560
|
-
if ((decl.type == "FnDecl")) {
|
|
561
|
-
const asyncKw = (decl.async ? "async " : "");
|
|
562
|
-
const params = decl.params.map((p) => (p.rest ? ("..." + p.name) : (p.defaultVal ? ((p.name + " = ") + this.genExpr(p.defaultVal)) : p.name))).join(", ");
|
|
563
|
-
if (decl.inline) {
|
|
564
|
-
this.emit((((((((asyncKw + "function ") + decl.name) + "(") + params) + ") { return ") + this.genExpr(decl.body)) + "; }"));
|
|
565
|
-
}
|
|
566
|
-
else {
|
|
567
|
-
this.emit((((((asyncKw + "function ") + decl.name) + "(") + params) + ") {"));
|
|
568
|
-
this.indIn();
|
|
569
|
-
for (const s of decl.body) {
|
|
570
|
-
this.genStmt(s);
|
|
571
|
-
}
|
|
572
|
-
this.indOut();
|
|
573
|
-
this.emit("}");
|
|
574
|
-
}
|
|
575
|
-
this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";"));
|
|
576
|
-
}
|
|
577
|
-
else if ((decl.type == "ClassDecl")) {
|
|
578
|
-
this.genClass(decl);
|
|
579
|
-
this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";"));
|
|
580
|
-
}
|
|
581
|
-
else if ((decl.type == "TypeDecl")) {
|
|
582
|
-
this.genTypeDecl(decl);
|
|
583
|
-
for (const v of decl.variants) {
|
|
584
|
-
this.emit((((("module.exports." + v.name) + " = ") + v.name) + ";"));
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
else if ((decl.type == "InterfaceDecl")) {
|
|
588
|
-
this.genInterfaceDecl(decl);
|
|
589
|
-
}
|
|
590
|
-
else if ((decl.type == "EnumDecl")) {
|
|
591
|
-
this.genEnumDecl(decl);
|
|
592
|
-
this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";"));
|
|
593
|
-
}
|
|
594
|
-
else if ((decl.type == "VarDecl")) {
|
|
595
|
-
const kw = ((decl.kind == "val") ? "const" : "let");
|
|
596
|
-
this.emit((((((kw + " ") + decl.name) + " = ") + this.genExpr(decl.init)) + ";"));
|
|
597
|
-
this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";"));
|
|
598
|
-
}
|
|
599
|
-
else {
|
|
600
|
-
this.genStmt(decl);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
genExpr(node) {
|
|
605
|
-
if (!node) {
|
|
606
|
-
return "undefined";
|
|
607
|
-
}
|
|
608
|
-
if ((node.type == "NumberLit")) {
|
|
609
|
-
return String(node.value);
|
|
610
|
-
}
|
|
611
|
-
if ((node.type == "BoolLit")) {
|
|
612
|
-
return String(node.value);
|
|
613
|
-
}
|
|
614
|
-
if ((node.type == "NullLit")) {
|
|
615
|
-
return "null";
|
|
616
|
-
}
|
|
617
|
-
if ((node.type == "SelfExpr")) {
|
|
618
|
-
return "this";
|
|
619
|
-
}
|
|
620
|
-
if ((node.type == "Identifier")) {
|
|
621
|
-
return ((node.name == "print") ? "console.log" : node.name);
|
|
622
|
-
}
|
|
623
|
-
if ((node.type == "StringLit")) {
|
|
624
|
-
return JSON.stringify(node.value);
|
|
625
|
-
}
|
|
626
|
-
if ((node.type == "RegexLit")) {
|
|
627
|
-
return ((("/" + node.value.pattern) + "/") + node.value.flags);
|
|
628
|
-
}
|
|
629
|
-
if ((node.type == "TemplateLit")) {
|
|
630
|
-
return this.genTemplate(node.parts);
|
|
631
|
-
}
|
|
632
|
-
if ((node.type == "BinaryExpr")) {
|
|
633
|
-
return (((((("(" + this.genExpr(node.left)) + " ") + node.op) + " ") + this.genExpr(node.right)) + ")");
|
|
634
|
-
}
|
|
635
|
-
if ((node.type == "UnaryExpr")) {
|
|
636
|
-
return (node.op + this.genExpr(node.operand));
|
|
637
|
-
}
|
|
638
|
-
if ((node.type == "UpdateExpr")) {
|
|
639
|
-
if (node.prefix) {
|
|
640
|
-
return (node.op + this.genExpr(node.operand));
|
|
641
|
-
}
|
|
642
|
-
return (this.genExpr(node.operand) + node.op);
|
|
643
|
-
}
|
|
644
|
-
if ((node.type == "TernaryExpr")) {
|
|
645
|
-
return (((((("(" + this.genExpr(node.cond)) + " ? ") + this.genExpr(node.then)) + " : ") + this.genExpr(node.else_)) + ")");
|
|
646
|
-
}
|
|
647
|
-
if ((node.type == "AssignExpr")) {
|
|
648
|
-
return ((((this.genExpr(node.target) + " ") + node.op) + " ") + this.genExpr(node.value));
|
|
649
|
-
}
|
|
650
|
-
if ((node.type == "AwaitExpr")) {
|
|
651
|
-
return ("await " + this.genExpr(node.operand));
|
|
652
|
-
}
|
|
653
|
-
if ((node.type == "TypeofExpr")) {
|
|
654
|
-
return ("typeof " + this.genExpr(node.operand));
|
|
655
|
-
}
|
|
656
|
-
if ((node.type == "SpreadExpr")) {
|
|
657
|
-
return ("..." + this.genExpr(node.expr));
|
|
658
|
-
}
|
|
659
|
-
if ((node.type == "CallExpr")) {
|
|
660
|
-
const callee = this.genExpr(node.callee);
|
|
661
|
-
const args = node.args.map((a) => this.genExpr(a)).join(", ");
|
|
662
|
-
return (((callee + "(") + args) + ")");
|
|
663
|
-
}
|
|
664
|
-
if ((node.type == "MemberExpr")) {
|
|
665
|
-
const objSrc = this.genExpr(node.obj);
|
|
666
|
-
const wrapped = ((node.obj.type == "NumberLit") ? (("(" + objSrc) + ")") : objSrc);
|
|
667
|
-
return ((wrapped + ".") + node.prop);
|
|
668
|
-
}
|
|
669
|
-
if ((node.type == "OptMemberExpr")) {
|
|
670
|
-
return ((this.genExpr(node.obj) + "?.") + node.prop);
|
|
671
|
-
}
|
|
672
|
-
if ((node.type == "OptIndexExpr")) {
|
|
673
|
-
return (((this.genExpr(node.obj) + "?.[") + this.genExpr(node.idx)) + "]");
|
|
674
|
-
}
|
|
675
|
-
if ((node.type == "OptCallExpr")) {
|
|
676
|
-
const args = node.args.map((a) => this.genExpr(a)).join(", ");
|
|
677
|
-
return (((this.genExpr(node.callee) + "?.(") + args) + ")");
|
|
678
|
-
}
|
|
679
|
-
if ((node.type == "IndexExpr")) {
|
|
680
|
-
return (((this.genExpr(node.obj) + "[") + this.genExpr(node.idx)) + "]");
|
|
681
|
-
}
|
|
682
|
-
if ((node.type == "NewExpr")) {
|
|
683
|
-
const args = node.args.map((a) => this.genExpr(a)).join(", ");
|
|
684
|
-
return (((("new " + node.callee) + "(") + args) + ")");
|
|
685
|
-
}
|
|
686
|
-
if ((node.type == "LambdaExpr")) {
|
|
687
|
-
const params = node.params.map((p) => (p.rest ? ("..." + p.name) : p.name)).join(", ");
|
|
688
|
-
return ((("(" + params) + ") => ") + this.genExpr(node.body));
|
|
689
|
-
}
|
|
690
|
-
if ((node.type == "FnDecl")) {
|
|
691
|
-
const asyncKw = (node.async ? "async " : "");
|
|
692
|
-
const params = node.params.map((p) => (p.rest ? ("..." + p.name) : (p.defaultVal ? ((p.name + " = ") + this.genExpr(p.defaultVal)) : p.name))).join(", ");
|
|
693
|
-
if (node.inline) {
|
|
694
|
-
return (((((asyncKw + "function(") + params) + ") { return ") + this.genExpr(node.body)) + "; }");
|
|
695
|
-
}
|
|
696
|
-
const saved = this.lines.length;
|
|
697
|
-
const savedLevel = this.level;
|
|
698
|
-
this.emit((((asyncKw + "function(") + params) + ") {"));
|
|
699
|
-
this.indIn();
|
|
700
|
-
for (const s of node.body) {
|
|
701
|
-
this.genStmt(s);
|
|
702
|
-
}
|
|
703
|
-
this.indOut();
|
|
704
|
-
this.emit("}");
|
|
705
|
-
const block = this.lines.splice(saved).map((l) => l.trimStart()).join(" ");
|
|
706
|
-
this.level = savedLevel;
|
|
707
|
-
return block;
|
|
708
|
-
}
|
|
709
|
-
if ((node.type == "MatchStmt")) {
|
|
710
|
-
const saved = this.lines.length;
|
|
711
|
-
const savedLevel = this.level;
|
|
712
|
-
const savedLoop = this._loopDepth;
|
|
713
|
-
this._loopDepth = 0;
|
|
714
|
-
this._fnDepth = (this._fnDepth + 1);
|
|
715
|
-
this.emit("(() => {");
|
|
716
|
-
this.indIn();
|
|
717
|
-
this.genMatch(node);
|
|
718
|
-
this.indOut();
|
|
719
|
-
this.emit("})()");
|
|
720
|
-
this._fnDepth = (this._fnDepth - 1);
|
|
721
|
-
this._loopDepth = savedLoop;
|
|
722
|
-
const block = this.lines.splice(saved).map((l) => l.trimStart()).join(" ");
|
|
723
|
-
this.level = savedLevel;
|
|
724
|
-
return block;
|
|
725
|
-
}
|
|
726
|
-
if ((node.type == "ArrayExpr")) {
|
|
727
|
-
return (("[" + node.items.map((i) => this.genExpr(i)).join(", ")) + "]");
|
|
728
|
-
}
|
|
729
|
-
if ((node.type == "ObjectExpr")) {
|
|
730
|
-
const pairs = node.pairs.map((p) => this.genObjPair(p)).join(", ");
|
|
731
|
-
return (("{ " + pairs) + " }");
|
|
732
|
-
}
|
|
733
|
-
if ((node.type == "RangeExpr")) {
|
|
734
|
-
return (((((("Array.from({ length: " + this.genExpr(node.end)) + " - ") + this.genExpr(node.start)) + " }, (_, i) => i + ") + this.genExpr(node.start)) + ")");
|
|
735
|
-
}
|
|
736
|
-
if ((node.type == "PipeExpr")) {
|
|
737
|
-
return this.genPipe(node);
|
|
738
|
-
}
|
|
739
|
-
if ((node.type == "CastExpr")) {
|
|
740
|
-
return this.genExpr(node.expr);
|
|
741
|
-
}
|
|
742
|
-
if ((node.type == "AsConstExpr")) {
|
|
743
|
-
return (("Object.freeze(" + this.genExpr(node.expr)) + ")");
|
|
744
|
-
}
|
|
745
|
-
if ((node.type == "SatisfiesExpr")) {
|
|
746
|
-
return this.genExpr(node.expr);
|
|
747
|
-
}
|
|
748
|
-
if ((node.type == "IsExpr")) {
|
|
749
|
-
return this.genExpr(node.expr);
|
|
750
|
-
}
|
|
751
|
-
if ((node.type == "NonNullExpr")) {
|
|
752
|
-
return this.genExpr(node.expr);
|
|
753
|
-
}
|
|
754
|
-
throw new Error(`Unknown expression: ${node.type}`);
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
genTemplate(parts) {
|
|
758
|
-
let result = "`";
|
|
759
|
-
for (const p of parts) {
|
|
760
|
-
if ((p.type == "text")) {
|
|
761
|
-
result += p.value.replace(/`/g, "\\`").replace(/\$/g, "\\$");
|
|
762
|
-
}
|
|
763
|
-
else {
|
|
764
|
-
const fmtInfo = extractFormatSpec(p.value);
|
|
765
|
-
const exprSrc = (fmtInfo ? fmtInfo.expr : p.value);
|
|
766
|
-
const fmt = (fmtInfo ? fmtInfo.fmt : null);
|
|
767
|
-
try {
|
|
768
|
-
const tokens = lexerize(exprSrc).tokenize();
|
|
769
|
-
const expr = makeParser(tokens).parseExpr();
|
|
770
|
-
const gen = this.genExpr(expr);
|
|
771
|
-
if (fmt) {
|
|
772
|
-
this._needsFmt = true;
|
|
773
|
-
result += ((((("$" + "{_fmt(") + gen) + ", ") + JSON.stringify(fmt)) + ")}");
|
|
774
|
-
}
|
|
775
|
-
else {
|
|
776
|
-
result += ((("$" + "{") + gen) + "}");
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
catch (err) {
|
|
780
|
-
result += ((("$" + "{") + p.value) + "}");
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
result += "`";
|
|
785
|
-
return result;
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
genPipe(node) {
|
|
789
|
-
const left = this.genExpr(node.left);
|
|
790
|
-
const right = node.right;
|
|
791
|
-
if ((right.type == "Identifier")) {
|
|
792
|
-
return (((this.genExpr(right) + "(") + left) + ")");
|
|
793
|
-
}
|
|
794
|
-
if ((right.type == "CallExpr")) {
|
|
795
|
-
const fn_ = this.genExpr(right.callee);
|
|
796
|
-
const rest = right.args.map((a) => this.genExpr(a)).join(", ");
|
|
797
|
-
return (rest ? (((((fn_ + "(") + left) + ", ") + rest) + ")") : (((fn_ + "(") + left) + ")"));
|
|
798
|
-
}
|
|
799
|
-
return (((("(" + this.genExpr(right)) + ")(") + left) + ")");
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
module.exports.CodeGenerator = CodeGenerator;
|
|
805
|
-
function makeCodeGen(opts) {
|
|
806
|
-
const ind = (opts?.indent ?? " ");
|
|
807
|
-
const smb = (opts?.smBuilder ?? null);
|
|
808
|
-
const ver = (opts?.version ?? "3.5.3");
|
|
809
|
-
return new CodeGenerator(ind, 0, [], { }, smb, false, 0, ver);
|
|
810
|
-
}
|
|
811
|
-
module.exports.makeCodeGen = makeCodeGen;
|
|
1
|
+
function map(arr, fn) { return arr.map(fn); } function join(arr, sep) { return arr.join(sep != null ? sep : ','); } function round(n, decimals) { if (decimals == null) return Math.round(n); var f = Math.pow(10, decimals); return Math.round(n * f) / f; } function trim(s) { return String(s).trim(); } function trimStart(s) { return String(s).trimStart(); } "use strict"; const { Lexer, lexerize, T } = require("./lexer"); const { Parser, makeParser } = require("./parser"); function _a(_e) { const ci = _e.lastIndexOf(":"); if ((ci < 1)) { return null; } const spec = _e.slice((ci + 1)).trim(); if ((((spec.length > 0) && /[.<>^,dbeEfFgGoOxXs%bcn]/.test(spec)) && /^([.<>^0\-+ #,]*[0-9]*\.?[0-9]*[dbeEfFgGoOxXs%bcn]?[,]?)$/.test(spec))) { return { expr: _e.slice(0, ci).trim(), fmt: spec }; } return null; } const _b = "function _fmt(v, s) {\n if (s === ',') return (+v).toLocaleString();\n if (s === '%') return ((+v)*100).toFixed(0)+'%';\n var m = s.match(/^([0-9,]*)?\\.([0-9]+)([fdgGeEb%x])$/);\n if (m) {\n var d=+m[2], t=m[3], comma=s[0]===',';\n if (t==='f'||t==='d') return comma ? (+v).toLocaleString(void 0,{minimumFractionDigits:d,maximumFractionDigits:d}) : (+v).toFixed(d);\n if (t==='e'||t==='E') return (+v).toExponential(d);\n if (t==='%') return ((+v)*100).toFixed(d)+'%';\n if (t==='b') return Math.round(+v).toString(2);\n if (t==='x') return Math.round(+v).toString(16);\n }\n var m2 = s.match(/^([0-9]*)d$/); if (m2) return Math.round(+v).toString();\n return String(v);\n}"; function _c(_f) { const reg = { }; for (const _g of _f.body) { const n = ((_g.type == "ExportDecl") ? _g.decl : _g); if ((n.type == "ClassDecl")) { reg[n.name] = { fields: n.fields, superClass: n.superClass }; } } return reg; } function _d(_h, _i, _j) { const vis = (_j ?? new Set([])); if (((!_h || !_i[_h]) || vis.has(_h))) { return []; } vis.add(_h); const parent = _d(_i[_h].superClass, _i, vis); return [...parent, ..._i[_h].fields]; } class CodeGenerator { constructor(ind, level, lines, clsReg, smBuilder, _needsFmt, _loopDepth, _fnDepth, _version) { this.ind = ind; this.level = level; this.lines = lines; this.clsReg = clsReg; this.smBuilder = smBuilder; this._needsFmt = _needsFmt; this._loopDepth = _loopDepth; this._fnDepth = _fnDepth; this._version = _version; } i() { let s = ""; let n = this.level; while ((n > 0)) { s += this.ind; n = (n - 1); } return s; } emit(_k) { this.lines.push((this.i() + _k)); } emitRaw(_k) { this.lines.push(_k); } blank() { this.lines.push(""); } indIn() { this.level = (this.level + 1); } indOut() { if ((this.level > 0)) { this.level = (this.level - 1); } } generate(_f) { this.clsReg = _c(_f); this.lines = []; this.level = 0; this._needsFmt = false; this.emit((("// Generated by Flux Transpiler v" + (this._version ?? "3.5.3")) + " (self-hosted)")); this.emit("\"use strict\";"); this.blank(); for (const _g of _f.body) { this.genStmt(_g); } if (this._needsFmt) { this.lines.splice(2, 0, _b); } return { code: this.lines.join("\n"), smBuilder: this.smBuilder }; } genStmt(_g) { if ((_g.type == "VarDecl")) { this.genVar(_g); } else if ((_g.type == "DestructureDecl")) { this.genDestructure(_g); } else if ((_g.type == "FnDecl")) { this.genFn(_g, ""); } else if ((_g.type == "ClassDecl")) { this.genClass(_g); } else if ((_g.type == "IfStmt")) { this.genIf(_g); } else if ((_g.type == "ForInStmt")) { this.genFor(_g); } else if ((_g.type == "WhileStmt")) { this.genWhile(_g); } else if ((_g.type == "MatchStmt")) { this.genMatch(_g); } else if ((_g.type == "ReturnStmt")) { this.genReturn(_g); } else if ((_g.type == "TryCatchStmt")) { this.genTryCatch(_g); } else if ((_g.type == "ThrowStmt")) { this.genThrow(_g); } else if ((_g.type == "DoWhileStmt")) { this.genDoWhile(_g); } else if ((_g.type == "BreakStmt")) { this.emit("break;"); } else if ((_g.type == "ContinueStmt")) { this.emit("continue;"); } else if ((_g.type == "ImportDecl")) { this.genImport(_g); } else if ((_g.type == "ExportDecl")) { this.genExport(_g); } else if ((_g.type == "TypeDecl")) { this.genTypeDecl(_g); } else if ((_g.type == "InterfaceDecl")) { this.genInterfaceDecl(_g); } else if ((_g.type == "EnumDecl")) { this.genEnumDecl(_g); } else if ((_g.type == "DeclareDecl")) { return; } else if ((_g.type == "ExprStmt")) { this.emit((this.genExpr(_g.expr) + ";")); } else { throw new Error(`Unknown statement: ${_g.type}`); } } genVar(_g) { const kw = ((_g.kind == "val") ? "const" : "let"); const init = (_g.init ? (" = " + this.genExpr(_g.init)) : ""); this.emit(((((kw + " ") + _g.name) + init) + ";")); } genObjPair(_l) { if (_l.spread) { return ("..." + this.genExpr(_l.value)); } const isIdent = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(_l.key); const keyStr = (isIdent ? _l.key : (("\"" + _l.key) + "\"")); if ((((isIdent && _l.value) && (_l.value.type == "Identifier")) && (_l.value.name == _l.key))) { return _l.key; } return ((keyStr + ": ") + this.genExpr(_l.value)); } genDestructProp(_l) { if (_l.rest) { return ("..." + _l.key); } let _k = ((_l.alias != _l.key) ? ((_l.key + ": ") + _l.alias) : _l.key); if (_l.defaultVal) { _k = ((_k + " = ") + this.genExpr(_l.defaultVal)); } return _k; } genDestructItem(_l) { if (!_l) { return ""; } if (_l.rest) { return ("..." + _l.name); } let _k = _l.name; if (_l.defaultVal) { _k = ((_k + " = ") + this.genExpr(_l.defaultVal)); } return _k; } genDestructure(_g) { const kw = ((_g.kind == "val") ? "const" : "let"); const init = this.genExpr(_g.init); if ((_g.patternType == "object")) { const props = _g.pattern.map((_l) => this.genDestructProp(_l)).join(", "); this.emit((((((kw + " { ") + props) + " } = ") + init) + ";")); } else { const items = _g.pattern.map((_l) => this.genDestructItem(_l)).join(", "); this.emit((((((kw + " [") + items) + "] = ") + init) + ";")); } } genFn(_g, _m) { const asyncKw = (_g.async ? "async " : ""); const _h = (_g.name ?? ""); const params = _g.params.map((_l) => (_l.rest ? ("..." + _l.name) : (_l.defaultVal ? ((_l.name + " = ") + this.genExpr(_l.defaultVal)) : _l.name))).join(", "); if (_g.inline) { this.emit(((((((((_m + asyncKw) + "function ") + _h) + "(") + params) + ") { return ") + this.genExpr(_g.body)) + "; }")); } else { this.emit(((((((_m + asyncKw) + "function ") + _h) + "(") + params) + ") {")); this.indIn(); this._fnDepth = (this._fnDepth + 1); for (const _k of _g.body) { this.genStmt(_k); } this._fnDepth = (this._fnDepth - 1); this.indOut(); this.emit("}"); } } genClass(_g) { const ext = (_g.superClass ? (" extends " + _g.superClass) : ""); this.emit(((("class " + _g.name) + ext) + " {")); this.indIn(); const allFields = _d(_g.name, this.clsReg, null); if ((allFields.length > 0)) { const params = allFields.map((_n) => _n.name).join(", "); this.emit((("constructor(" + params) + ") {")); this.indIn(); if ((_g.superClass && this.clsReg[_g.superClass])) { const parentFields = _d(_g.superClass, this.clsReg, null); if ((parentFields.length > 0)) { this.emit((("super(" + parentFields.map((_n) => _n.name).join(", ")) + ");")); } } for (const _n of _g.fields) { this.emit((((("this." + _n.name) + " = ") + _n.name) + ";")); } this.indOut(); this.emit("}"); this.blank(); } for (const _o of _g.methods) { const asyncKw = (_o.async ? "async " : ""); const staticKw = ((_o.modifiers && _o.modifiers.has("static")) ? "static " : ""); const params = _o.params.map((_l) => (_l.rest ? ("..." + _l.name) : (_l.defaultVal ? ((_l.name + " = ") + this.genExpr(_l.defaultVal)) : _l.name))).join(", "); if (_o.inline) { this.emit((((((((staticKw + asyncKw) + _o.name) + "(") + params) + ") { return ") + this.genExpr(_o.body)) + "; }")); } else { this.emit((((((staticKw + asyncKw) + _o.name) + "(") + params) + ") {")); this.indIn(); this._fnDepth = (this._fnDepth + 1); for (const _k of _o.body) { this.genStmt(_k); } this._fnDepth = (this._fnDepth - 1); this.indOut(); this.emit("}"); } this.blank(); } this.indOut(); this.emit("}"); this.blank(); } genIf(_g) { this.emit((("if (" + this.genExpr(_g.cond)) + ") {")); this.indIn(); for (const _k of _g.then) { this.genStmt(_k); } this.indOut(); this.emit("}"); for (const _p of _g.elseifs) { this.emitRaw((((this.i() + "else if (") + this.genExpr(_p.cond)) + ") {")); this.indIn(); for (const _k of _p.body) { this.genStmt(_k); } this.indOut(); this.emit("}"); } if (_g.else_) { this.emitRaw((this.i() + "else {")); this.indIn(); for (const _k of _g.else_) { this.genStmt(_k); } this.indOut(); this.emit("}"); } } genFor(_g) { const varName = _g["var"]; const iter = _g.iter; if ((iter.type == "RangeExpr")) { const _k = this.genExpr(iter.start); const e = this.genExpr(iter.end); this.emit((((((((((("for (let " + varName) + " = ") + _k) + "; ") + varName) + " < ") + e) + "; ") + varName) + "++) {")); } else if (_g.isAwait) { this.emit((((("for await (const " + varName) + " of ") + this.genExpr(iter)) + ") {")); } else { this.emit((((("for (const " + varName) + " of ") + this.genExpr(iter)) + ") {")); } this._loopDepth = (this._loopDepth + 1); this.indIn(); for (const _k of _g.body) { this.genStmt(_k); } this.indOut(); this._loopDepth = (this._loopDepth - 1); this.emit("}"); } genWhile(_g) { this.emit((("while (" + this.genExpr(_g.cond)) + ") {")); this._loopDepth = (this._loopDepth + 1); this.indIn(); for (const _k of _g.body) { this.genStmt(_k); } this.indOut(); this._loopDepth = (this._loopDepth - 1); this.emit("}"); } genMatch(_g) { const subj = this.genExpr(_g.subject); const arms = _g.arms; let hasOpenIf = false; for (const _q of arms) { const pat = _q.pattern; let cond = ""; const bindings = []; if ((pat.type == "WildcardPat")) { cond = ""; } else if ((pat.type == "RangePat")) { const lo = this.genExpr(pat.start); const hi = this.genExpr(pat.end); cond = ((((((subj + " >= ") + lo) + " && ") + subj) + " <= ") + hi); } else if ((pat.type == "VariantPat")) { cond = (((subj + "?.__type === \"") + pat.variant) + "\""); let bi = 0; for (const _r of pat.bindings) { bindings.push((((((("const " + _r) + " = ") + subj) + ".__args[") + bi) + "];")); bi = (bi + 1); } } else { const patExpr = this.genExpr(pat.value); if (((pat.value.type == "Identifier") && /^[A-Z]/.test(pat.value.name))) { cond = (((((((("(" + subj) + " === ") + patExpr) + " || ") + subj) + "?.__type === \"") + pat.value.name) + "\")"); } else { cond = ((subj + " === ") + patExpr); } } if (_q.guard) { const guardSrc = this.genExpr(_q.guard); if ((bindings.length > 0)) { const rebindings = bindings.map((_s) => _s.replace("const ", "var ")).join(" "); const iife = (((("(function(){ " + rebindings) + " return (") + guardSrc) + "); }())"); cond = (cond ? ((("(" + cond) + ") && ") + iife) : iife); } else { cond = (cond ? (((("(" + cond) + ") && (") + guardSrc) + ")") : guardSrc); } } if ((!hasOpenIf && cond)) { this.emit((("if (" + cond) + ") {")); hasOpenIf = true; } else if ((!hasOpenIf && !cond)) { this.emit("if (true) {"); hasOpenIf = true; } else if ((hasOpenIf && cond)) { this.emitRaw((((this.i() + "else if (") + cond) + ") {")); } else { this.emitRaw((this.i() + "else {")); } this.indIn(); for (const _r of bindings) { this.emit(_r); } if (_q.inline) { const exprSrc = this.genExpr(_q.body[0].expr); if ((this._loopDepth > 0)) { this.emit((exprSrc + ";")); } else if ((this._fnDepth > 0)) { this.emit((("return " + exprSrc) + ";")); } else { this.emit((exprSrc + ";")); } } else { for (const _k of _q.body) { this.genStmt(_k); } } this.indOut(); this.emit("}"); } } genInterfaceDecl(_g) { this.blank(); this.emit(("// interface " + _g.name)); this.blank(); } genEnumDecl(_g) { this.blank(); const pairs = _g.members.map((_o) => ((_o.name + ": ") + this.genExpr(_o.value))).join(", "); this.emit((((("const " + _g.name) + " = Object.freeze({ ") + pairs) + " });")); this.blank(); } genTypeDecl(_g) { this.blank(); this.emit(("// ADT type: " + _g.name)); for (const _t of _g.variants) { if ((_t.fields.length == 0)) { this.emit((((("const " + _t.name) + " = Object.freeze({ __type: \"") + _t.name) + "\", __args: [] });")); } else { const params = _t.fields.join(", "); const argsArr = (("[" + _t.fields.join(", ")) + "]"); const fieldsObj = _t.fields.map((_n) => ((_n + ": ") + _n)).join(", "); this.emit((((((((((("function " + _t.name) + "(") + params) + ") { return Object.freeze({ __type: \"") + _t.name) + "\", __args: ") + argsArr) + ", ") + fieldsObj) + " }); }")); } } this.blank(); } genReturn(_g) { if (_g.value) { this.emit((("return " + this.genExpr(_g.value)) + ";")); } else { this.emit("return;"); } } genTryCatch(_g) { this.emit("try {"); this.indIn(); for (const _k of _g.tryBody) { this.genStmt(_k); } this.indOut(); this.emit("}"); if (_g.catchBody) { const param = (_g.catchParam ? (("(" + _g.catchParam) + ")") : "(_err)"); this.emitRaw((((this.i() + "catch ") + param) + " {")); this.indIn(); for (const _k of _g.catchBody) { this.genStmt(_k); } this.indOut(); this.emit("}"); } if (_g.finallyBody) { this.emitRaw((this.i() + "finally {")); this.indIn(); for (const _k of _g.finallyBody) { this.genStmt(_k); } this.indOut(); this.emit("}"); } } genThrow(_g) { this.emit((("throw " + this.genExpr(_g.value)) + ";")); } genDoWhile(_g) { this.emit("do {"); this.indIn(); for (const _k of _g.body) { this.genStmt(_k); } this.indOut(); this.emit((("} while (" + this.genExpr(_g.cond)) + ");")); } genImport(_g) { const src = String(_g.source); if (_g.namespaceName) { this.emit((((("const " + _g.namespaceName) + " = require(\"") + src) + "\");")); } else if (_g.defaultName) { this.emit((((("const " + _g.defaultName) + " = require(\"") + src) + "\");")); } else if (_g.names.length) { const parts = _g.names.map((_u) => ((typeof _u == "string") ? _u : ((_u.name != _u.alias) ? ((_u.name + ": ") + _u.alias) : _u.name))).join(", "); this.emit((((("const { " + parts) + " } = require(\"") + src) + "\");")); } } genExport(_g) { if (_g.isDefault) { this.emit((("module.exports = " + this.genExpr(_g.decl)) + ";")); return; } const decl = _g.decl; if ((decl.type == "FnDecl")) { const asyncKw = (decl.async ? "async " : ""); const params = decl.params.map((_l) => (_l.rest ? ("..." + _l.name) : (_l.defaultVal ? ((_l.name + " = ") + this.genExpr(_l.defaultVal)) : _l.name))).join(", "); if (decl.inline) { this.emit((((((((asyncKw + "function ") + decl.name) + "(") + params) + ") { return ") + this.genExpr(decl.body)) + "; }")); } else { this.emit((((((asyncKw + "function ") + decl.name) + "(") + params) + ") {")); this.indIn(); for (const _k of decl.body) { this.genStmt(_k); } this.indOut(); this.emit("}"); } this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";")); } else if ((decl.type == "ClassDecl")) { this.genClass(decl); this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";")); } else if ((decl.type == "TypeDecl")) { this.genTypeDecl(decl); for (const _t of decl.variants) { this.emit((((("module.exports." + _t.name) + " = ") + _t.name) + ";")); } } else if ((decl.type == "InterfaceDecl")) { this.genInterfaceDecl(decl); } else if ((decl.type == "EnumDecl")) { this.genEnumDecl(decl); this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";")); } else if ((decl.type == "VarDecl")) { const kw = ((decl.kind == "val") ? "const" : "let"); this.emit((((((kw + " ") + decl.name) + " = ") + this.genExpr(decl.init)) + ";")); this.emit((((("module.exports." + decl.name) + " = ") + decl.name) + ";")); } else { this.genStmt(decl); } } genExpr(_g) { if (!_g) { return "undefined"; } if ((_g.type == "NumberLit")) { return String(_g.value); } if ((_g.type == "BoolLit")) { return String(_g.value); } if ((_g.type == "NullLit")) { return "null"; } if ((_g.type == "SelfExpr")) { return "this"; } if ((_g.type == "Identifier")) { return ((_g.name == "print") ? "console.log" : _g.name); } if ((_g.type == "StringLit")) { return JSON.stringify(_g.value); } if ((_g.type == "RegexLit")) { return ((("/" + _g.value.pattern) + "/") + _g.value.flags); } if ((_g.type == "TemplateLit")) { return this.genTemplate(_g.parts); } if ((_g.type == "BinaryExpr")) { return (((((("(" + this.genExpr(_g.left)) + " ") + _g.op) + " ") + this.genExpr(_g.right)) + ")"); } if ((_g.type == "UnaryExpr")) { return (_g.op + this.genExpr(_g.operand)); } if ((_g.type == "UpdateExpr")) { if (_g.prefix) { return (_g.op + this.genExpr(_g.operand)); } return (this.genExpr(_g.operand) + _g.op); } if ((_g.type == "TernaryExpr")) { return (((((("(" + this.genExpr(_g.cond)) + " ? ") + this.genExpr(_g.then)) + " : ") + this.genExpr(_g.else_)) + ")"); } if ((_g.type == "AssignExpr")) { return ((((this.genExpr(_g.target) + " ") + _g.op) + " ") + this.genExpr(_g.value)); } if ((_g.type == "AwaitExpr")) { return ("await " + this.genExpr(_g.operand)); } if ((_g.type == "TypeofExpr")) { return ("typeof " + this.genExpr(_g.operand)); } if ((_g.type == "SpreadExpr")) { return ("..." + this.genExpr(_g.expr)); } if ((_g.type == "CallExpr")) { const callee = this.genExpr(_g.callee); const args = _g.args.map((_v) => this.genExpr(_v)).join(", "); return (((callee + "(") + args) + ")"); } if ((_g.type == "MemberExpr")) { const objSrc = this.genExpr(_g.obj); const wrapped = ((_g.obj.type == "NumberLit") ? (("(" + objSrc) + ")") : objSrc); return ((wrapped + ".") + _g.prop); } if ((_g.type == "OptMemberExpr")) { return ((this.genExpr(_g.obj) + "?.") + _g.prop); } if ((_g.type == "OptIndexExpr")) { return (((this.genExpr(_g.obj) + "?.[") + this.genExpr(_g.idx)) + "]"); } if ((_g.type == "OptCallExpr")) { const args = _g.args.map((_v) => this.genExpr(_v)).join(", "); return (((this.genExpr(_g.callee) + "?.(") + args) + ")"); } if ((_g.type == "IndexExpr")) { return (((this.genExpr(_g.obj) + "[") + this.genExpr(_g.idx)) + "]"); } if ((_g.type == "NewExpr")) { const args = _g.args.map((_v) => this.genExpr(_v)).join(", "); return (((("new " + _g.callee) + "(") + args) + ")"); } if ((_g.type == "LambdaExpr")) { const params = _g.params.map((_l) => (_l.rest ? ("..." + _l.name) : _l.name)).join(", "); return ((("(" + params) + ") => ") + this.genExpr(_g.body)); } if ((_g.type == "FnDecl")) { const asyncKw = (_g.async ? "async " : ""); const params = _g.params.map((_l) => (_l.rest ? ("..." + _l.name) : (_l.defaultVal ? ((_l.name + " = ") + this.genExpr(_l.defaultVal)) : _l.name))).join(", "); if (_g.inline) { return (((((asyncKw + "function(") + params) + ") { return ") + this.genExpr(_g.body)) + "; }"); } const saved = this.lines.length; const savedLevel = this.level; this.emit((((asyncKw + "function(") + params) + ") {")); this.indIn(); for (const _k of _g.body) { this.genStmt(_k); } this.indOut(); this.emit("}"); const block = this.lines.splice(saved).map((_w) => _w.trimStart()).join(" "); this.level = savedLevel; return block; } if ((_g.type == "MatchStmt")) { const saved = this.lines.length; const savedLevel = this.level; const savedLoop = this._loopDepth; this._loopDepth = 0; this._fnDepth = (this._fnDepth + 1); this.emit("(() => {"); this.indIn(); this.genMatch(_g); this.indOut(); this.emit("})()"); this._fnDepth = (this._fnDepth - 1); this._loopDepth = savedLoop; const block = this.lines.splice(saved).map((_w) => _w.trimStart()).join(" "); this.level = savedLevel; return block; } if ((_g.type == "ArrayExpr")) { return (("[" + _g.items.map((_x) => this.genExpr(_x)).join(", ")) + "]"); } if ((_g.type == "ObjectExpr")) { const pairs = _g.pairs.map((_l) => this.genObjPair(_l)).join(", "); return (("{ " + pairs) + " }"); } if ((_g.type == "RangeExpr")) { return (((((("Array.from({ length: " + this.genExpr(_g.end)) + " - ") + this.genExpr(_g.start)) + " }, (_, i) => i + ") + this.genExpr(_g.start)) + ")"); } if ((_g.type == "PipeExpr")) { return this.genPipe(_g); } if ((_g.type == "CastExpr")) { return this.genExpr(_g.expr); } if ((_g.type == "AsConstExpr")) { return (("Object.freeze(" + this.genExpr(_g.expr)) + ")"); } if ((_g.type == "SatisfiesExpr")) { return this.genExpr(_g.expr); } if ((_g.type == "IsExpr")) { return this.genExpr(_g.expr); } if ((_g.type == "NonNullExpr")) { return this.genExpr(_g.expr); } throw new Error(`Unknown expression: ${_g.type}`); } genTemplate(_y) { let result = "`"; for (const _l of _y) { if ((_l.type == "text")) { result += _l.value.replace(/`/g, "\\`").replace(/\$/g, "\\$"); } else { const fmtInfo = _a(_l.value); const exprSrc = (fmtInfo ? fmtInfo.expr : _l.value); const fmt = (fmtInfo ? fmtInfo.fmt : null); try { const tokens = lexerize(exprSrc).tokenize(); const expr = makeParser(tokens).parseExpr(); const gen = this.genExpr(expr); if (fmt) { this._needsFmt = true; result += ((((("$" + "{_fmt(") + gen) + ", ") + JSON.stringify(fmt)) + ")}"); } else { result += ((("$" + "{") + gen) + "}"); } } catch (_z) { result += ((("$" + "{") + _l.value) + "}"); } } } result += "`"; return result; } genPipe(_g) { const left = this.genExpr(_g.left); const right = _g.right; if ((right.type == "Identifier")) { return (((this.genExpr(right) + "(") + left) + ")"); } if ((right.type == "CallExpr")) { const fn_ = this.genExpr(right.callee); const rest = right.args.map((_v) => this.genExpr(_v)).join(", "); return (rest ? (((((fn_ + "(") + left) + ", ") + rest) + ")") : (((fn_ + "(") + left) + ")")); } return (((("(" + this.genExpr(right)) + ")(") + left) + ")"); } } module.exports.CodeGenerator = CodeGenerator; function makeCodeGen(_aa) { const ind = (_aa?.indent ?? " "); const smb = (_aa?.smBuilder ?? null); const ver = (_aa?.version ?? "3.5.3"); return new CodeGenerator(ind, 0, [], { }, smb, false, 0, ver); } module.exports.makeCodeGen = makeCodeGen;
|