lumos-language 1.1.2 → 2.0.2
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/.github/FUNDING.yml +1 -0
- package/.npmrc.ci-backup +3 -0
- package/LICENSE +0 -29
- package/Lumos.png +0 -0
- package/README.md +284 -126
- package/STRUCTURE.md +216 -0
- package/examples/hello.lumos +5 -0
- package/index.cjs +125 -125
- package/index.html +120 -274
- package/package.json +20 -10
- package/src/backends/assembly/arm.js +39 -0
- package/src/backends/assembly/wasm.js +39 -0
- package/src/backends/assembly/x86.js +39 -0
- package/src/backends/compiled/c.js +39 -0
- package/src/backends/compiled/cpp.js +39 -0
- package/src/backends/compiled/csharp.js +39 -0
- package/src/backends/compiled/go.js +39 -0
- package/src/backends/compiled/java.js +39 -0
- package/src/backends/compiled/rust.js +39 -0
- package/src/backends/compiled/swift.js +39 -0
- package/src/backends/database/mongodb.js +39 -0
- package/src/backends/database/mysql.js +39 -0
- package/src/backends/database/postgresql.js +39 -0
- package/src/backends/database/sql.js +39 -0
- package/src/backends/database/sqlite.js +39 -0
- package/src/backends/functional/clojure.js +39 -0
- package/src/backends/functional/elixir.js +39 -0
- package/src/backends/functional/erlang.js +39 -0
- package/src/backends/functional/fsharp.js +39 -0
- package/src/backends/functional/haskell.js +39 -0
- package/src/backends/functional/scala.js +39 -0
- package/src/backends/interpreted/lua.js +39 -0
- package/src/backends/interpreted/perl.js +39 -0
- package/src/backends/interpreted/php.js +39 -0
- package/src/backends/interpreted/python.js +356 -0
- package/src/backends/interpreted/ruby.js +222 -0
- package/src/backends/scripting/bash.js +39 -0
- package/src/backends/scripting/javascript.js +39 -0
- package/src/backends/scripting/powershell.js +39 -0
- package/src/backends/scripting/typescript.js +39 -0
- package/src/backends/scripting/vbscript.js +39 -0
- package/src/backends/specialized/ada.js +39 -0
- package/src/backends/specialized/cobol.js +39 -0
- package/src/backends/specialized/fortran.js +39 -0
- package/src/backends/specialized/lisp.js +39 -0
- package/src/backends/specialized/mlang.js +39 -0
- package/src/backends/specialized/prolog.js +39 -0
- package/src/backends/web/css.js +39 -0
- package/src/backends/web/html.js +39 -0
- package/src/backends/web/jsx.js +39 -0
- package/src/backends/web/vue.js +39 -0
- package/src/cli/fileRunner.js +82 -0
- package/src/cli/repl.js +244 -0
- package/src/compiler/core/compiler.js +1350 -0
- package/src/compiler/framework-integrator.js +846 -0
- package/src/compiler/generators/dynamic-languages.js +620 -0
- package/src/compiler/generators/system-languages.js +1184 -0
- package/src/core/compiler.js +181 -0
- package/src/core/evaluator.js +408 -0
- package/src/core/lexer.js +251 -0
- package/src/core/parser.js +452 -0
- package/src/core/runtime.js +173 -0
- package/tests/run-tests.js +243 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
class JavaScriptGenerator {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.indentLevel = 0;
|
|
4
|
+
this.indentString = ' ';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
generate(ir) {
|
|
8
|
+
const code = [];
|
|
9
|
+
code.push('"use strict";');
|
|
10
|
+
code.push('');
|
|
11
|
+
|
|
12
|
+
for (const inst of ir) {
|
|
13
|
+
const line = this.generateInstruction(inst);
|
|
14
|
+
if (line) {
|
|
15
|
+
code.push(this.indent() + line);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return code.join('\n');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
generateInstruction(inst) {
|
|
23
|
+
switch (inst.op) {
|
|
24
|
+
case 'function':
|
|
25
|
+
this.indentLevel--;
|
|
26
|
+
const funcDecl = `function ${inst.arg1}() {`;
|
|
27
|
+
this.indentLevel++;
|
|
28
|
+
return funcDecl;
|
|
29
|
+
|
|
30
|
+
case 'end_function':
|
|
31
|
+
this.indentLevel--;
|
|
32
|
+
return '}';
|
|
33
|
+
|
|
34
|
+
case 'param':
|
|
35
|
+
return ``;
|
|
36
|
+
|
|
37
|
+
case 'assign':
|
|
38
|
+
if (typeof inst.arg1 === 'string' && inst.arg1.startsWith('t')) {
|
|
39
|
+
return `const ${inst.result} = ${inst.arg1};`;
|
|
40
|
+
}
|
|
41
|
+
return `let ${inst.result} = ${this.formatValue(inst.arg1)};`;
|
|
42
|
+
|
|
43
|
+
case '+':
|
|
44
|
+
return `const ${inst.result} = ${this.formatValue(inst.arg1)} + ${this.formatValue(inst.arg2)};`;
|
|
45
|
+
|
|
46
|
+
case '-':
|
|
47
|
+
return `const ${inst.result} = ${this.formatValue(inst.arg1)} - ${this.formatValue(inst.arg2)};`;
|
|
48
|
+
|
|
49
|
+
case '*':
|
|
50
|
+
return `const ${inst.result} = ${this.formatValue(inst.arg1)} * ${this.formatValue(inst.arg2)};`;
|
|
51
|
+
|
|
52
|
+
case '/':
|
|
53
|
+
return `const ${inst.result} = ${this.formatValue(inst.arg1)} / ${this.formatValue(inst.arg2)};`;
|
|
54
|
+
|
|
55
|
+
case '%':
|
|
56
|
+
return `const ${inst.result} = ${this.formatValue(inst.arg1)} % ${this.formatValue(inst.arg2)};`;
|
|
57
|
+
|
|
58
|
+
case 'call':
|
|
59
|
+
return `const ${inst.result} = ${inst.arg1}();`;
|
|
60
|
+
|
|
61
|
+
case 'push':
|
|
62
|
+
return ``;
|
|
63
|
+
|
|
64
|
+
case 'return':
|
|
65
|
+
return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return;';
|
|
66
|
+
|
|
67
|
+
case 'if_false':
|
|
68
|
+
return `if (!${this.formatValue(inst.arg1)}) { goto ${inst.result}; }`;
|
|
69
|
+
|
|
70
|
+
case 'goto':
|
|
71
|
+
return ``;
|
|
72
|
+
|
|
73
|
+
case 'label':
|
|
74
|
+
this.indentLevel--;
|
|
75
|
+
const label = `${inst.arg1}:`;
|
|
76
|
+
this.indentLevel++;
|
|
77
|
+
return label;
|
|
78
|
+
|
|
79
|
+
default:
|
|
80
|
+
return ``;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
formatValue(value) {
|
|
85
|
+
if (typeof value === 'string') {
|
|
86
|
+
if (value.startsWith('t') || value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
return `"${value}"`;
|
|
90
|
+
}
|
|
91
|
+
return String(value);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
indent() {
|
|
95
|
+
return this.indentString.repeat(Math.max(0, this.indentLevel));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class TypeScriptGenerator extends JavaScriptGenerator {
|
|
100
|
+
generate(ir) {
|
|
101
|
+
const code = [];
|
|
102
|
+
code.push('');
|
|
103
|
+
|
|
104
|
+
const functions = this.groupByFunctions(ir);
|
|
105
|
+
|
|
106
|
+
for (const func of functions) {
|
|
107
|
+
code.push(this.generateFunction(func));
|
|
108
|
+
code.push('');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return code.join('\n');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
groupByFunctions(ir) {
|
|
115
|
+
const functions = [];
|
|
116
|
+
let current = { name: 'main', params: [], body: [] };
|
|
117
|
+
|
|
118
|
+
for (const inst of ir) {
|
|
119
|
+
if (inst.op === 'function') {
|
|
120
|
+
if (current.body.length > 0) {
|
|
121
|
+
functions.push(current);
|
|
122
|
+
}
|
|
123
|
+
current = { name: inst.arg1, params: [], body: [] };
|
|
124
|
+
} else if (inst.op === 'param') {
|
|
125
|
+
current.params.push(inst.arg1);
|
|
126
|
+
} else if (inst.op === 'end_function') {
|
|
127
|
+
functions.push(current);
|
|
128
|
+
current = { name: 'main', params: [], body: [] };
|
|
129
|
+
} else {
|
|
130
|
+
current.body.push(inst);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (current.body.length > 0) {
|
|
135
|
+
functions.push(current);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return functions;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
generateFunction(func) {
|
|
142
|
+
const lines = [];
|
|
143
|
+
const params = func.params.map(p => `${p}: any`).join(', ');
|
|
144
|
+
|
|
145
|
+
lines.push(`function ${func.name}(${params}): any {`);
|
|
146
|
+
this.indentLevel++;
|
|
147
|
+
|
|
148
|
+
for (const inst of func.body) {
|
|
149
|
+
const line = this.generateInstruction(inst);
|
|
150
|
+
if (line) {
|
|
151
|
+
lines.push(this.indent() + line);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
this.indentLevel--;
|
|
156
|
+
lines.push('}');
|
|
157
|
+
|
|
158
|
+
return lines.join('\n');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
generateInstruction(inst) {
|
|
162
|
+
switch (inst.op) {
|
|
163
|
+
case 'assign':
|
|
164
|
+
if (typeof inst.arg1 === 'string' && inst.arg1.startsWith('t')) {
|
|
165
|
+
return `const ${inst.result}: any = ${inst.arg1};`;
|
|
166
|
+
}
|
|
167
|
+
return `let ${inst.result}: any = ${this.formatValue(inst.arg1)};`;
|
|
168
|
+
|
|
169
|
+
case '+':
|
|
170
|
+
case '-':
|
|
171
|
+
case '*':
|
|
172
|
+
case '/':
|
|
173
|
+
case '%':
|
|
174
|
+
return `const ${inst.result}: number = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
|
|
175
|
+
|
|
176
|
+
default:
|
|
177
|
+
return super.generateInstruction(inst);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class PythonGenerator {
|
|
183
|
+
constructor() {
|
|
184
|
+
this.indentLevel = 0;
|
|
185
|
+
this.indentString = ' ';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
generate(ir) {
|
|
189
|
+
const code = [];
|
|
190
|
+
|
|
191
|
+
const functions = this.groupByFunctions(ir);
|
|
192
|
+
|
|
193
|
+
for (const func of functions) {
|
|
194
|
+
code.push(this.generateFunction(func));
|
|
195
|
+
code.push('');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return code.join('\n');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
groupByFunctions(ir) {
|
|
202
|
+
const functions = [];
|
|
203
|
+
let current = { name: 'main', params: [], body: [] };
|
|
204
|
+
|
|
205
|
+
for (const inst of ir) {
|
|
206
|
+
if (inst.op === 'function') {
|
|
207
|
+
if (current.body.length > 0) {
|
|
208
|
+
functions.push(current);
|
|
209
|
+
}
|
|
210
|
+
current = { name: inst.arg1, params: [], body: [] };
|
|
211
|
+
} else if (inst.op === 'param') {
|
|
212
|
+
current.params.push(inst.arg1);
|
|
213
|
+
} else if (inst.op === 'end_function') {
|
|
214
|
+
functions.push(current);
|
|
215
|
+
current = { name: 'main', params: [], body: [] };
|
|
216
|
+
} else {
|
|
217
|
+
current.body.push(inst);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (current.body.length > 0) {
|
|
222
|
+
functions.push(current);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return functions;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
generateFunction(func) {
|
|
229
|
+
const lines = [];
|
|
230
|
+
const params = func.params.join(', ');
|
|
231
|
+
|
|
232
|
+
lines.push(`def ${func.name}(${params}):`);
|
|
233
|
+
this.indentLevel++;
|
|
234
|
+
|
|
235
|
+
if (func.body.length === 0) {
|
|
236
|
+
lines.push(this.indent() + 'pass');
|
|
237
|
+
} else {
|
|
238
|
+
for (const inst of func.body) {
|
|
239
|
+
const line = this.generateInstruction(inst);
|
|
240
|
+
if (line) {
|
|
241
|
+
lines.push(this.indent() + line);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.indentLevel--;
|
|
247
|
+
|
|
248
|
+
return lines.join('\n');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
generateInstruction(inst) {
|
|
252
|
+
switch (inst.op) {
|
|
253
|
+
case 'assign':
|
|
254
|
+
return `${inst.result} = ${this.formatValue(inst.arg1)}`;
|
|
255
|
+
|
|
256
|
+
case '+':
|
|
257
|
+
case '-':
|
|
258
|
+
case '*':
|
|
259
|
+
case '/':
|
|
260
|
+
case '%':
|
|
261
|
+
return `${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)}`;
|
|
262
|
+
|
|
263
|
+
case 'call':
|
|
264
|
+
return `${inst.result} = ${inst.arg1}()`;
|
|
265
|
+
|
|
266
|
+
case 'return':
|
|
267
|
+
return inst.arg1 ? `return ${this.formatValue(inst.arg1)}` : 'return';
|
|
268
|
+
|
|
269
|
+
case 'if_false':
|
|
270
|
+
return `if not ${this.formatValue(inst.arg1)}:`;
|
|
271
|
+
|
|
272
|
+
case 'label':
|
|
273
|
+
return ``;
|
|
274
|
+
|
|
275
|
+
default:
|
|
276
|
+
return '';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
formatValue(value) {
|
|
281
|
+
if (typeof value === 'string') {
|
|
282
|
+
if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
return `"${value}"`;
|
|
286
|
+
}
|
|
287
|
+
return String(value);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
indent() {
|
|
291
|
+
return this.indentString.repeat(this.indentLevel);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
class PHPGenerator {
|
|
296
|
+
constructor() {
|
|
297
|
+
this.indentLevel = 0;
|
|
298
|
+
this.indentString = ' ';
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
generate(ir) {
|
|
302
|
+
const code = [];
|
|
303
|
+
code.push('<?php');
|
|
304
|
+
code.push('');
|
|
305
|
+
|
|
306
|
+
const functions = this.groupByFunctions(ir);
|
|
307
|
+
|
|
308
|
+
for (const func of functions) {
|
|
309
|
+
code.push(this.generateFunction(func));
|
|
310
|
+
code.push('');
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return code.join('\n');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
groupByFunctions(ir) {
|
|
317
|
+
const functions = [];
|
|
318
|
+
let current = { name: 'main', params: [], body: [] };
|
|
319
|
+
|
|
320
|
+
for (const inst of ir) {
|
|
321
|
+
if (inst.op === 'function') {
|
|
322
|
+
if (current.body.length > 0) {
|
|
323
|
+
functions.push(current);
|
|
324
|
+
}
|
|
325
|
+
current = { name: inst.arg1, params: [], body: [] };
|
|
326
|
+
} else if (inst.op === 'param') {
|
|
327
|
+
current.params.push(inst.arg1);
|
|
328
|
+
} else if (inst.op === 'end_function') {
|
|
329
|
+
functions.push(current);
|
|
330
|
+
current = { name: 'main', params: [], body: [] };
|
|
331
|
+
} else {
|
|
332
|
+
current.body.push(inst);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (current.body.length > 0) {
|
|
337
|
+
functions.push(current);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return functions;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
generateFunction(func) {
|
|
344
|
+
const lines = [];
|
|
345
|
+
const params = func.params.map(p => `$${p}`).join(', ');
|
|
346
|
+
|
|
347
|
+
lines.push(`function ${func.name}(${params}) {`);
|
|
348
|
+
this.indentLevel++;
|
|
349
|
+
|
|
350
|
+
for (const inst of func.body) {
|
|
351
|
+
const line = this.generateInstruction(inst);
|
|
352
|
+
if (line) {
|
|
353
|
+
lines.push(this.indent() + line);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
this.indentLevel--;
|
|
358
|
+
lines.push('}');
|
|
359
|
+
|
|
360
|
+
return lines.join('\n');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
generateInstruction(inst) {
|
|
364
|
+
switch (inst.op) {
|
|
365
|
+
case 'assign':
|
|
366
|
+
return `$${inst.result} = ${this.formatValue(inst.arg1)};`;
|
|
367
|
+
|
|
368
|
+
case '+':
|
|
369
|
+
case '-':
|
|
370
|
+
case '*':
|
|
371
|
+
case '/':
|
|
372
|
+
case '%':
|
|
373
|
+
return `$${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
|
|
374
|
+
|
|
375
|
+
case 'call':
|
|
376
|
+
return `$${inst.result} = ${inst.arg1}();`;
|
|
377
|
+
|
|
378
|
+
case 'return':
|
|
379
|
+
return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return;';
|
|
380
|
+
|
|
381
|
+
default:
|
|
382
|
+
return '';
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
formatValue(value) {
|
|
387
|
+
if (typeof value === 'string') {
|
|
388
|
+
if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
389
|
+
return `$${value}`;
|
|
390
|
+
}
|
|
391
|
+
return `"${value}"`;
|
|
392
|
+
}
|
|
393
|
+
return String(value);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
indent() {
|
|
397
|
+
return this.indentString.repeat(this.indentLevel);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
class RubyGenerator {
|
|
402
|
+
constructor() {
|
|
403
|
+
this.indentLevel = 0;
|
|
404
|
+
this.indentString = ' ';
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
generate(ir) {
|
|
408
|
+
const code = [];
|
|
409
|
+
|
|
410
|
+
const functions = this.groupByFunctions(ir);
|
|
411
|
+
|
|
412
|
+
for (const func of functions) {
|
|
413
|
+
code.push(this.generateFunction(func));
|
|
414
|
+
code.push('');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return code.join('\n');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
groupByFunctions(ir) {
|
|
421
|
+
const functions = [];
|
|
422
|
+
let current = { name: 'main', params: [], body: [] };
|
|
423
|
+
|
|
424
|
+
for (const inst of ir) {
|
|
425
|
+
if (inst.op === 'function') {
|
|
426
|
+
if (current.body.length > 0) {
|
|
427
|
+
functions.push(current);
|
|
428
|
+
}
|
|
429
|
+
current = { name: inst.arg1, params: [], body: [] };
|
|
430
|
+
} else if (inst.op === 'param') {
|
|
431
|
+
current.params.push(inst.arg1);
|
|
432
|
+
} else if (inst.op === 'end_function') {
|
|
433
|
+
functions.push(current);
|
|
434
|
+
current = { name: 'main', params: [], body: [] };
|
|
435
|
+
} else {
|
|
436
|
+
current.body.push(inst);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (current.body.length > 0) {
|
|
441
|
+
functions.push(current);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return functions;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
generateFunction(func) {
|
|
448
|
+
const lines = [];
|
|
449
|
+
const params = func.params.join(', ');
|
|
450
|
+
|
|
451
|
+
lines.push(`def ${func.name}(${params})`);
|
|
452
|
+
this.indentLevel++;
|
|
453
|
+
|
|
454
|
+
for (const inst of func.body) {
|
|
455
|
+
const line = this.generateInstruction(inst);
|
|
456
|
+
if (line) {
|
|
457
|
+
lines.push(this.indent() + line);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
this.indentLevel--;
|
|
462
|
+
lines.push('end');
|
|
463
|
+
|
|
464
|
+
return lines.join('\n');
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
generateInstruction(inst) {
|
|
468
|
+
switch (inst.op) {
|
|
469
|
+
case 'assign':
|
|
470
|
+
return `${inst.result} = ${this.formatValue(inst.arg1)}`;
|
|
471
|
+
|
|
472
|
+
case '+':
|
|
473
|
+
case '-':
|
|
474
|
+
case '*':
|
|
475
|
+
case '/':
|
|
476
|
+
case '%':
|
|
477
|
+
return `${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)}`;
|
|
478
|
+
|
|
479
|
+
case 'call':
|
|
480
|
+
return `${inst.result} = ${inst.arg1}()`;
|
|
481
|
+
|
|
482
|
+
case 'return':
|
|
483
|
+
return inst.arg1 ? this.formatValue(inst.arg1) : 'nil';
|
|
484
|
+
|
|
485
|
+
default:
|
|
486
|
+
return '';
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
formatValue(value) {
|
|
491
|
+
if (typeof value === 'string') {
|
|
492
|
+
if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
493
|
+
return value;
|
|
494
|
+
}
|
|
495
|
+
return `"${value}"`;
|
|
496
|
+
}
|
|
497
|
+
return String(value);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
indent() {
|
|
501
|
+
return this.indentString.repeat(this.indentLevel);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
class GoGenerator {
|
|
506
|
+
constructor() {
|
|
507
|
+
this.indentLevel = 0;
|
|
508
|
+
this.indentString = '\t';
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
generate(ir) {
|
|
512
|
+
const code = [];
|
|
513
|
+
code.push('package main');
|
|
514
|
+
code.push('');
|
|
515
|
+
code.push('import "fmt"');
|
|
516
|
+
code.push('');
|
|
517
|
+
|
|
518
|
+
const functions = this.groupByFunctions(ir);
|
|
519
|
+
|
|
520
|
+
for (const func of functions) {
|
|
521
|
+
code.push(this.generateFunction(func));
|
|
522
|
+
code.push('');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return code.join('\n');
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
groupByFunctions(ir) {
|
|
529
|
+
const functions = [];
|
|
530
|
+
let current = { name: 'main', params: [], body: [] };
|
|
531
|
+
|
|
532
|
+
for (const inst of ir) {
|
|
533
|
+
if (inst.op === 'function') {
|
|
534
|
+
if (current.body.length > 0) {
|
|
535
|
+
functions.push(current);
|
|
536
|
+
}
|
|
537
|
+
current = { name: inst.arg1, params: [], body: [] };
|
|
538
|
+
} else if (inst.op === 'param') {
|
|
539
|
+
current.params.push(inst.arg1);
|
|
540
|
+
} else if (inst.op === 'end_function') {
|
|
541
|
+
functions.push(current);
|
|
542
|
+
current = { name: 'main', params: [], body: [] };
|
|
543
|
+
} else {
|
|
544
|
+
current.body.push(inst);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (current.body.length > 0) {
|
|
549
|
+
functions.push(current);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return functions;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
generateFunction(func) {
|
|
556
|
+
const lines = [];
|
|
557
|
+
const params = func.params.map(p => `${p} interface{}`).join(', ');
|
|
558
|
+
|
|
559
|
+
lines.push(`func ${func.name}(${params}) interface{} {`);
|
|
560
|
+
this.indentLevel++;
|
|
561
|
+
|
|
562
|
+
for (const inst of func.body) {
|
|
563
|
+
const line = this.generateInstruction(inst);
|
|
564
|
+
if (line) {
|
|
565
|
+
lines.push(this.indent() + line);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
this.indentLevel--;
|
|
570
|
+
lines.push('}');
|
|
571
|
+
|
|
572
|
+
return lines.join('\n');
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
generateInstruction(inst) {
|
|
576
|
+
switch (inst.op) {
|
|
577
|
+
case 'assign':
|
|
578
|
+
return `${inst.result} := ${this.formatValue(inst.arg1)}`;
|
|
579
|
+
|
|
580
|
+
case '+':
|
|
581
|
+
case '-':
|
|
582
|
+
case '*':
|
|
583
|
+
case '/':
|
|
584
|
+
case '%':
|
|
585
|
+
return `${inst.result} := ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)}`;
|
|
586
|
+
|
|
587
|
+
case 'call':
|
|
588
|
+
return `${inst.result} := ${inst.arg1}()`;
|
|
589
|
+
|
|
590
|
+
case 'return':
|
|
591
|
+
return inst.arg1 ? `return ${this.formatValue(inst.arg1)}` : 'return nil';
|
|
592
|
+
|
|
593
|
+
default:
|
|
594
|
+
return '';
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
formatValue(value) {
|
|
599
|
+
if (typeof value === 'string') {
|
|
600
|
+
if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
601
|
+
return value;
|
|
602
|
+
}
|
|
603
|
+
return `"${value}"`;
|
|
604
|
+
}
|
|
605
|
+
return String(value);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
indent() {
|
|
609
|
+
return this.indentString.repeat(this.indentLevel);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
module.exports = {
|
|
614
|
+
JavaScriptGenerator,
|
|
615
|
+
TypeScriptGenerator,
|
|
616
|
+
PythonGenerator,
|
|
617
|
+
PHPGenerator,
|
|
618
|
+
RubyGenerator,
|
|
619
|
+
GoGenerator
|
|
620
|
+
};
|