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.
Files changed (63) hide show
  1. package/.github/FUNDING.yml +1 -0
  2. package/.npmrc.ci-backup +3 -0
  3. package/LICENSE +0 -29
  4. package/Lumos.png +0 -0
  5. package/README.md +284 -126
  6. package/STRUCTURE.md +216 -0
  7. package/examples/hello.lumos +5 -0
  8. package/index.cjs +125 -125
  9. package/index.html +120 -274
  10. package/package.json +20 -10
  11. package/src/backends/assembly/arm.js +39 -0
  12. package/src/backends/assembly/wasm.js +39 -0
  13. package/src/backends/assembly/x86.js +39 -0
  14. package/src/backends/compiled/c.js +39 -0
  15. package/src/backends/compiled/cpp.js +39 -0
  16. package/src/backends/compiled/csharp.js +39 -0
  17. package/src/backends/compiled/go.js +39 -0
  18. package/src/backends/compiled/java.js +39 -0
  19. package/src/backends/compiled/rust.js +39 -0
  20. package/src/backends/compiled/swift.js +39 -0
  21. package/src/backends/database/mongodb.js +39 -0
  22. package/src/backends/database/mysql.js +39 -0
  23. package/src/backends/database/postgresql.js +39 -0
  24. package/src/backends/database/sql.js +39 -0
  25. package/src/backends/database/sqlite.js +39 -0
  26. package/src/backends/functional/clojure.js +39 -0
  27. package/src/backends/functional/elixir.js +39 -0
  28. package/src/backends/functional/erlang.js +39 -0
  29. package/src/backends/functional/fsharp.js +39 -0
  30. package/src/backends/functional/haskell.js +39 -0
  31. package/src/backends/functional/scala.js +39 -0
  32. package/src/backends/interpreted/lua.js +39 -0
  33. package/src/backends/interpreted/perl.js +39 -0
  34. package/src/backends/interpreted/php.js +39 -0
  35. package/src/backends/interpreted/python.js +356 -0
  36. package/src/backends/interpreted/ruby.js +222 -0
  37. package/src/backends/scripting/bash.js +39 -0
  38. package/src/backends/scripting/javascript.js +39 -0
  39. package/src/backends/scripting/powershell.js +39 -0
  40. package/src/backends/scripting/typescript.js +39 -0
  41. package/src/backends/scripting/vbscript.js +39 -0
  42. package/src/backends/specialized/ada.js +39 -0
  43. package/src/backends/specialized/cobol.js +39 -0
  44. package/src/backends/specialized/fortran.js +39 -0
  45. package/src/backends/specialized/lisp.js +39 -0
  46. package/src/backends/specialized/mlang.js +39 -0
  47. package/src/backends/specialized/prolog.js +39 -0
  48. package/src/backends/web/css.js +39 -0
  49. package/src/backends/web/html.js +39 -0
  50. package/src/backends/web/jsx.js +39 -0
  51. package/src/backends/web/vue.js +39 -0
  52. package/src/cli/fileRunner.js +82 -0
  53. package/src/cli/repl.js +244 -0
  54. package/src/compiler/core/compiler.js +1350 -0
  55. package/src/compiler/framework-integrator.js +846 -0
  56. package/src/compiler/generators/dynamic-languages.js +620 -0
  57. package/src/compiler/generators/system-languages.js +1184 -0
  58. package/src/core/compiler.js +181 -0
  59. package/src/core/evaluator.js +408 -0
  60. package/src/core/lexer.js +251 -0
  61. package/src/core/parser.js +452 -0
  62. package/src/core/runtime.js +173 -0
  63. package/tests/run-tests.js +243 -0
@@ -0,0 +1,1184 @@
1
+ class RustGenerator {
2
+ constructor() {
3
+ this.indentLevel = 0;
4
+ this.indentString = ' ';
5
+ }
6
+
7
+ generate(ir) {
8
+ const code = [];
9
+
10
+ const functions = this.groupByFunctions(ir);
11
+
12
+ for (const func of functions) {
13
+ code.push(this.generateFunction(func));
14
+ code.push('');
15
+ }
16
+
17
+ code.push('fn main() {');
18
+ code.push('}');
19
+
20
+ return code.join('\n');
21
+ }
22
+
23
+ groupByFunctions(ir) {
24
+ const functions = [];
25
+ let current = { name: 'main', params: [], body: [] };
26
+
27
+ for (const inst of ir) {
28
+ if (inst.op === 'function') {
29
+ if (current.body.length > 0) {
30
+ functions.push(current);
31
+ }
32
+ current = { name: inst.arg1, params: [], body: [] };
33
+ } else if (inst.op === 'param') {
34
+ current.params.push(inst.arg1);
35
+ } else if (inst.op === 'end_function') {
36
+ functions.push(current);
37
+ current = { name: 'main', params: [], body: [] };
38
+ } else {
39
+ current.body.push(inst);
40
+ }
41
+ }
42
+
43
+ if (current.body.length > 0) {
44
+ functions.push(current);
45
+ }
46
+
47
+ return functions;
48
+ }
49
+
50
+ generateFunction(func) {
51
+ const lines = [];
52
+ const params = func.params.map(p => `${p}: i64`).join(', ');
53
+
54
+ lines.push(`fn ${func.name}(${params}) -> i64 {`);
55
+ this.indentLevel++;
56
+
57
+ for (const inst of func.body) {
58
+ const line = this.generateInstruction(inst);
59
+ if (line) {
60
+ lines.push(this.indent() + line);
61
+ }
62
+ }
63
+
64
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
65
+ lines.push(this.indent() + '0');
66
+ }
67
+
68
+ this.indentLevel--;
69
+ lines.push('}');
70
+
71
+ return lines.join('\n');
72
+ }
73
+
74
+ generateInstruction(inst) {
75
+ switch (inst.op) {
76
+ case 'assign':
77
+ return `let mut ${inst.result} = ${this.formatValue(inst.arg1)};`;
78
+
79
+ case '+':
80
+ case '-':
81
+ case '*':
82
+ case '/':
83
+ case '%':
84
+ return `let ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
85
+
86
+ case 'call':
87
+ return `let ${inst.result} = ${inst.arg1}();`;
88
+
89
+ case 'return':
90
+ return inst.arg1 ? this.formatValue(inst.arg1) : '0';
91
+
92
+ default:
93
+ return '';
94
+ }
95
+ }
96
+
97
+ formatValue(value) {
98
+ if (typeof value === 'string') {
99
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
100
+ return value;
101
+ }
102
+ return `"${value}"`;
103
+ }
104
+ return String(value);
105
+ }
106
+
107
+ indent() {
108
+ return this.indentString.repeat(this.indentLevel);
109
+ }
110
+ }
111
+
112
+ class CGenerator {
113
+ constructor() {
114
+ this.indentLevel = 0;
115
+ this.indentString = ' ';
116
+ }
117
+
118
+ generate(ir) {
119
+ const code = [];
120
+ code.push('#include <stdio.h>');
121
+ code.push('#include <stdlib.h>');
122
+ code.push('');
123
+
124
+ const functions = this.groupByFunctions(ir);
125
+
126
+ for (const func of functions) {
127
+ code.push(this.generateFunctionDeclaration(func));
128
+ }
129
+ code.push('');
130
+
131
+ for (const func of functions) {
132
+ code.push(this.generateFunction(func));
133
+ code.push('');
134
+ }
135
+
136
+ code.push('int main(void) {');
137
+ code.push(' return 0;');
138
+ code.push('}');
139
+
140
+ return code.join('\n');
141
+ }
142
+
143
+ groupByFunctions(ir) {
144
+ const functions = [];
145
+ let current = { name: 'main', params: [], body: [] };
146
+
147
+ for (const inst of ir) {
148
+ if (inst.op === 'function') {
149
+ if (current.body.length > 0) {
150
+ functions.push(current);
151
+ }
152
+ current = { name: inst.arg1, params: [], body: [] };
153
+ } else if (inst.op === 'param') {
154
+ current.params.push(inst.arg1);
155
+ } else if (inst.op === 'end_function') {
156
+ functions.push(current);
157
+ current = { name: 'main', params: [], body: [] };
158
+ } else {
159
+ current.body.push(inst);
160
+ }
161
+ }
162
+
163
+ if (current.body.length > 0) {
164
+ functions.push(current);
165
+ }
166
+
167
+ return functions;
168
+ }
169
+
170
+ generateFunctionDeclaration(func) {
171
+ const params = func.params.map(p => `int ${p}`).join(', ');
172
+ return `int ${func.name}(${params});`;
173
+ }
174
+
175
+ generateFunction(func) {
176
+ const lines = [];
177
+ const params = func.params.map(p => `int ${p}`).join(', ');
178
+
179
+ lines.push(`int ${func.name}(${params}) {`);
180
+ this.indentLevel++;
181
+
182
+ for (const inst of func.body) {
183
+ const line = this.generateInstruction(inst);
184
+ if (line) {
185
+ lines.push(this.indent() + line);
186
+ }
187
+ }
188
+
189
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
190
+ lines.push(this.indent() + 'return 0;');
191
+ }
192
+
193
+ this.indentLevel--;
194
+ lines.push('}');
195
+
196
+ return lines.join('\n');
197
+ }
198
+
199
+ generateInstruction(inst) {
200
+ switch (inst.op) {
201
+ case 'assign':
202
+ return `int ${inst.result} = ${this.formatValue(inst.arg1)};`;
203
+
204
+ case '+':
205
+ case '-':
206
+ case '*':
207
+ case '/':
208
+ case '%':
209
+ return `int ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
210
+
211
+ case 'call':
212
+ return `int ${inst.result} = ${inst.arg1}();`;
213
+
214
+ case 'return':
215
+ return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return 0;';
216
+
217
+ default:
218
+ return '';
219
+ }
220
+ }
221
+
222
+ formatValue(value) {
223
+ if (typeof value === 'string') {
224
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
225
+ return value;
226
+ }
227
+ return `"${value}"`;
228
+ }
229
+ return String(value);
230
+ }
231
+
232
+ indent() {
233
+ return this.indentString.repeat(this.indentLevel);
234
+ }
235
+ }
236
+
237
+ class CppGenerator extends CGenerator {
238
+ generate(ir) {
239
+ const code = [];
240
+ code.push('#include <iostream>');
241
+ code.push('#include <string>');
242
+ code.push('');
243
+ code.push('using namespace std;');
244
+ code.push('');
245
+
246
+ const functions = this.groupByFunctions(ir);
247
+
248
+ for (const func of functions) {
249
+ code.push(this.generateFunctionDeclaration(func));
250
+ }
251
+ code.push('');
252
+
253
+ for (const func of functions) {
254
+ code.push(this.generateFunction(func));
255
+ code.push('');
256
+ }
257
+
258
+ code.push('int main() {');
259
+ code.push(' return 0;');
260
+ code.push('}');
261
+
262
+ return code.join('\n');
263
+ }
264
+
265
+ generateFunctionDeclaration(func) {
266
+ const params = func.params.map(p => `auto ${p}`).join(', ');
267
+ return `auto ${func.name}(${params}) -> int;`;
268
+ }
269
+
270
+ generateFunction(func) {
271
+ const lines = [];
272
+ const params = func.params.map(p => `auto ${p}`).join(', ');
273
+
274
+ lines.push(`auto ${func.name}(${params}) -> int {`);
275
+ this.indentLevel++;
276
+
277
+ for (const inst of func.body) {
278
+ const line = this.generateInstruction(inst);
279
+ if (line) {
280
+ lines.push(this.indent() + line);
281
+ }
282
+ }
283
+
284
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
285
+ lines.push(this.indent() + 'return 0;');
286
+ }
287
+
288
+ this.indentLevel--;
289
+ lines.push('}');
290
+
291
+ return lines.join('\n');
292
+ }
293
+
294
+ generateInstruction(inst) {
295
+ switch (inst.op) {
296
+ case 'assign':
297
+ return `auto ${inst.result} = ${this.formatValue(inst.arg1)};`;
298
+
299
+ case '+':
300
+ case '-':
301
+ case '*':
302
+ case '/':
303
+ case '%':
304
+ return `auto ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
305
+
306
+ case 'call':
307
+ return `auto ${inst.result} = ${inst.arg1}();`;
308
+
309
+ case 'return':
310
+ return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return 0;';
311
+
312
+ default:
313
+ return '';
314
+ }
315
+ }
316
+ }
317
+
318
+ class CSharpGenerator {
319
+ constructor() {
320
+ this.indentLevel = 0;
321
+ this.indentString = ' ';
322
+ }
323
+
324
+ generate(ir) {
325
+ const code = [];
326
+ code.push('using System;');
327
+ code.push('');
328
+ code.push('namespace LumosProgram');
329
+ code.push('{');
330
+ this.indentLevel++;
331
+
332
+ code.push(this.indent() + 'class Program');
333
+ code.push(this.indent() + '{');
334
+ this.indentLevel++;
335
+
336
+ const functions = this.groupByFunctions(ir);
337
+
338
+ for (const func of functions) {
339
+ const funcCode = this.generateFunction(func);
340
+ funcCode.split('\n').forEach(line => {
341
+ code.push(this.indent() + line);
342
+ });
343
+ code.push('');
344
+ }
345
+
346
+ code.push(this.indent() + 'static void Main(string[] args)');
347
+ code.push(this.indent() + '{');
348
+ code.push(this.indent() + '}');
349
+
350
+ this.indentLevel--;
351
+ code.push(this.indent() + '}');
352
+
353
+ this.indentLevel--;
354
+ code.push('}');
355
+
356
+ return code.join('\n');
357
+ }
358
+
359
+ groupByFunctions(ir) {
360
+ const functions = [];
361
+ let current = { name: 'main', params: [], body: [] };
362
+
363
+ for (const inst of ir) {
364
+ if (inst.op === 'function') {
365
+ if (current.body.length > 0) {
366
+ functions.push(current);
367
+ }
368
+ current = { name: inst.arg1, params: [], body: [] };
369
+ } else if (inst.op === 'param') {
370
+ current.params.push(inst.arg1);
371
+ } else if (inst.op === 'end_function') {
372
+ functions.push(current);
373
+ current = { name: 'main', params: [], body: [] };
374
+ } else {
375
+ current.body.push(inst);
376
+ }
377
+ }
378
+
379
+ if (current.body.length > 0) {
380
+ functions.push(current);
381
+ }
382
+
383
+ return functions;
384
+ }
385
+
386
+ generateFunction(func) {
387
+ const lines = [];
388
+ const params = func.params.map(p => `object ${p}`).join(', ');
389
+
390
+ lines.push(`static object ${func.name}(${params})`);
391
+ lines.push('{');
392
+
393
+ const tempIndent = this.indentLevel;
394
+ this.indentLevel = 0;
395
+
396
+ for (const inst of func.body) {
397
+ const line = this.generateInstruction(inst);
398
+ if (line) {
399
+ lines.push(this.indentString + line);
400
+ }
401
+ }
402
+
403
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
404
+ lines.push(this.indentString + 'return null;');
405
+ }
406
+
407
+ this.indentLevel = tempIndent;
408
+
409
+ lines.push('}');
410
+
411
+ return lines.join('\n');
412
+ }
413
+
414
+ generateInstruction(inst) {
415
+ switch (inst.op) {
416
+ case 'assign':
417
+ return `var ${inst.result} = ${this.formatValue(inst.arg1)};`;
418
+
419
+ case '+':
420
+ case '-':
421
+ case '*':
422
+ case '/':
423
+ case '%':
424
+ return `var ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
425
+
426
+ case 'call':
427
+ return `var ${inst.result} = ${inst.arg1}();`;
428
+
429
+ case 'return':
430
+ return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return null;';
431
+
432
+ default:
433
+ return '';
434
+ }
435
+ }
436
+
437
+ formatValue(value) {
438
+ if (typeof value === 'string') {
439
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
440
+ return value;
441
+ }
442
+ return `"${value}"`;
443
+ }
444
+ return String(value);
445
+ }
446
+
447
+ indent() {
448
+ return this.indentString.repeat(this.indentLevel);
449
+ }
450
+ }
451
+
452
+ class JavaGenerator {
453
+ constructor() {
454
+ this.indentLevel = 0;
455
+ this.indentString = ' ';
456
+ }
457
+
458
+ generate(ir) {
459
+ const code = [];
460
+ code.push('public class LumosProgram {');
461
+ this.indentLevel++;
462
+
463
+ const functions = this.groupByFunctions(ir);
464
+
465
+ for (const func of functions) {
466
+ const funcCode = this.generateFunction(func);
467
+ funcCode.split('\n').forEach(line => {
468
+ code.push(this.indent() + line);
469
+ });
470
+ code.push('');
471
+ }
472
+
473
+ code.push(this.indent() + 'public static void main(String[] args) {');
474
+ code.push(this.indent() + '}');
475
+
476
+ this.indentLevel--;
477
+ code.push('}');
478
+
479
+ return code.join('\n');
480
+ }
481
+
482
+ groupByFunctions(ir) {
483
+ const functions = [];
484
+ let current = { name: 'main', params: [], body: [] };
485
+
486
+ for (const inst of ir) {
487
+ if (inst.op === 'function') {
488
+ if (current.body.length > 0) {
489
+ functions.push(current);
490
+ }
491
+ current = { name: inst.arg1, params: [], body: [] };
492
+ } else if (inst.op === 'param') {
493
+ current.params.push(inst.arg1);
494
+ } else if (inst.op === 'end_function') {
495
+ functions.push(current);
496
+ current = { name: 'main', params: [], body: [] };
497
+ } else {
498
+ current.body.push(inst);
499
+ }
500
+ }
501
+
502
+ if (current.body.length > 0) {
503
+ functions.push(current);
504
+ }
505
+
506
+ return functions;
507
+ }
508
+
509
+ generateFunction(func) {
510
+ const lines = [];
511
+ const params = func.params.map(p => `Object ${p}`).join(', ');
512
+
513
+ lines.push(`public static Object ${func.name}(${params}) {`);
514
+
515
+ const tempIndent = this.indentLevel;
516
+ this.indentLevel = 0;
517
+
518
+ for (const inst of func.body) {
519
+ const line = this.generateInstruction(inst);
520
+ if (line) {
521
+ lines.push(this.indentString + line);
522
+ }
523
+ }
524
+
525
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
526
+ lines.push(this.indentString + 'return null;');
527
+ }
528
+
529
+ this.indentLevel = tempIndent;
530
+
531
+ lines.push('}');
532
+
533
+ return lines.join('\n');
534
+ }
535
+
536
+ generateInstruction(inst) {
537
+ switch (inst.op) {
538
+ case 'assign':
539
+ return `Object ${inst.result} = ${this.formatValue(inst.arg1)};`;
540
+
541
+ case '+':
542
+ case '-':
543
+ case '*':
544
+ case '/':
545
+ case '%':
546
+ return `Object ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
547
+
548
+ case 'call':
549
+ return `Object ${inst.result} = ${inst.arg1}();`;
550
+
551
+ case 'return':
552
+ return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return null;';
553
+
554
+ default:
555
+ return '';
556
+ }
557
+ }
558
+
559
+ formatValue(value) {
560
+ if (typeof value === 'string') {
561
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
562
+ return value;
563
+ }
564
+ return `"${value}"`;
565
+ }
566
+ return String(value);
567
+ }
568
+
569
+ indent() {
570
+ return this.indentString.repeat(this.indentLevel);
571
+ }
572
+ }
573
+
574
+ class ElixirGenerator {
575
+ constructor() {
576
+ this.indentLevel = 0;
577
+ this.indentString = ' ';
578
+ }
579
+
580
+ generate(ir) {
581
+ const code = [];
582
+ code.push('defmodule LumosProgram do');
583
+ this.indentLevel++;
584
+
585
+ const functions = this.groupByFunctions(ir);
586
+
587
+ for (const func of functions) {
588
+ const funcCode = this.generateFunction(func);
589
+ funcCode.split('\n').forEach(line => {
590
+ code.push(this.indent() + line);
591
+ });
592
+ code.push('');
593
+ }
594
+
595
+ this.indentLevel--;
596
+ code.push('end');
597
+
598
+ return code.join('\n');
599
+ }
600
+
601
+ groupByFunctions(ir) {
602
+ const functions = [];
603
+ let current = { name: 'main', params: [], body: [] };
604
+
605
+ for (const inst of ir) {
606
+ if (inst.op === 'function') {
607
+ if (current.body.length > 0) {
608
+ functions.push(current);
609
+ }
610
+ current = { name: inst.arg1, params: [], body: [] };
611
+ } else if (inst.op === 'param') {
612
+ current.params.push(inst.arg1);
613
+ } else if (inst.op === 'end_function') {
614
+ functions.push(current);
615
+ current = { name: 'main', params: [], body: [] };
616
+ } else {
617
+ current.body.push(inst);
618
+ }
619
+ }
620
+
621
+ if (current.body.length > 0) {
622
+ functions.push(current);
623
+ }
624
+
625
+ return functions;
626
+ }
627
+
628
+ generateFunction(func) {
629
+ const lines = [];
630
+ const params = func.params.join(', ');
631
+
632
+ lines.push(`def ${func.name}(${params}) do`);
633
+
634
+ const tempIndent = this.indentLevel;
635
+ this.indentLevel = 0;
636
+
637
+ for (const inst of func.body) {
638
+ const line = this.generateInstruction(inst);
639
+ if (line) {
640
+ lines.push(this.indentString + line);
641
+ }
642
+ }
643
+
644
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
645
+ lines.push(this.indentString + 'nil');
646
+ }
647
+
648
+ this.indentLevel = tempIndent;
649
+
650
+ lines.push('end');
651
+
652
+ return lines.join('\n');
653
+ }
654
+
655
+ generateInstruction(inst) {
656
+ switch (inst.op) {
657
+ case 'assign':
658
+ return `${inst.result} = ${this.formatValue(inst.arg1)}`;
659
+
660
+ case '+':
661
+ case '-':
662
+ case '*':
663
+ case '/':
664
+ return `${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)}`;
665
+
666
+ case '%':
667
+ return `${inst.result} = rem(${this.formatValue(inst.arg1)}, ${this.formatValue(inst.arg2)})`;
668
+
669
+ case 'call':
670
+ return `${inst.result} = ${inst.arg1}()`;
671
+
672
+ case 'return':
673
+ return inst.arg1 ? this.formatValue(inst.arg1) : 'nil';
674
+
675
+ default:
676
+ return '';
677
+ }
678
+ }
679
+
680
+ formatValue(value) {
681
+ if (typeof value === 'string') {
682
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
683
+ return value;
684
+ }
685
+ return `"${value}"`;
686
+ }
687
+ return String(value);
688
+ }
689
+
690
+ indent() {
691
+ return this.indentString.repeat(this.indentLevel);
692
+ }
693
+ }
694
+
695
+ class FSharpGenerator {
696
+ constructor() {
697
+ this.indentLevel = 0;
698
+ this.indentString = ' ';
699
+ }
700
+
701
+ generate(ir) {
702
+ const code = [];
703
+
704
+ const functions = this.groupByFunctions(ir);
705
+
706
+ for (const func of functions) {
707
+ code.push(this.generateFunction(func));
708
+ code.push('');
709
+ }
710
+
711
+ code.push('[<EntryPoint>]');
712
+ code.push('let main argv =');
713
+ code.push(' 0');
714
+
715
+ return code.join('\n');
716
+ }
717
+
718
+ groupByFunctions(ir) {
719
+ const functions = [];
720
+ let current = { name: 'main', params: [], body: [] };
721
+
722
+ for (const inst of ir) {
723
+ if (inst.op === 'function') {
724
+ if (current.body.length > 0) {
725
+ functions.push(current);
726
+ }
727
+ current = { name: inst.arg1, params: [], body: [] };
728
+ } else if (inst.op === 'param') {
729
+ current.params.push(inst.arg1);
730
+ } else if (inst.op === 'end_function') {
731
+ functions.push(current);
732
+ current = { name: 'main', params: [], body: [] };
733
+ } else {
734
+ current.body.push(inst);
735
+ }
736
+ }
737
+
738
+ if (current.body.length > 0) {
739
+ functions.push(current);
740
+ }
741
+
742
+ return functions;
743
+ }
744
+
745
+ generateFunction(func) {
746
+ const lines = [];
747
+ const params = func.params.join(' ');
748
+
749
+ lines.push(`let ${func.name} ${params} =`);
750
+ this.indentLevel++;
751
+
752
+ for (const inst of func.body) {
753
+ const line = this.generateInstruction(inst);
754
+ if (line) {
755
+ lines.push(this.indent() + line);
756
+ }
757
+ }
758
+
759
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
760
+ lines.push(this.indent() + '0');
761
+ }
762
+
763
+ this.indentLevel--;
764
+
765
+ return lines.join('\n');
766
+ }
767
+
768
+ generateInstruction(inst) {
769
+ switch (inst.op) {
770
+ case 'assign':
771
+ return `let ${inst.result} = ${this.formatValue(inst.arg1)}`;
772
+
773
+ case '+':
774
+ case '-':
775
+ case '*':
776
+ case '/':
777
+ case '%':
778
+ return `let ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)}`;
779
+
780
+ case 'call':
781
+ return `let ${inst.result} = ${inst.arg1}()`;
782
+
783
+ case 'return':
784
+ return inst.arg1 ? this.formatValue(inst.arg1) : '0';
785
+
786
+ default:
787
+ return '';
788
+ }
789
+ }
790
+
791
+ formatValue(value) {
792
+ if (typeof value === 'string') {
793
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
794
+ return value;
795
+ }
796
+ return `"${value}"`;
797
+ }
798
+ return String(value);
799
+ }
800
+
801
+ indent() {
802
+ return this.indentString.repeat(this.indentLevel);
803
+ }
804
+ }
805
+
806
+ class AdaGenerator {
807
+ constructor() {
808
+ this.indentLevel = 0;
809
+ this.indentString = ' ';
810
+ }
811
+
812
+ generate(ir) {
813
+ const code = [];
814
+ code.push('with Ada.Text_IO; use Ada.Text_IO;');
815
+ code.push('');
816
+ code.push('procedure Lumos_Program is');
817
+ this.indentLevel++;
818
+
819
+ const functions = this.groupByFunctions(ir);
820
+
821
+ for (const func of functions) {
822
+ const funcCode = this.generateFunction(func);
823
+ funcCode.split('\n').forEach(line => {
824
+ code.push(this.indent() + line);
825
+ });
826
+ code.push('');
827
+ }
828
+
829
+ this.indentLevel--;
830
+ code.push('begin');
831
+ this.indentLevel++;
832
+ code.push(this.indent() + 'null;');
833
+ this.indentLevel--;
834
+ code.push('end Lumos_Program;');
835
+
836
+ return code.join('\n');
837
+ }
838
+
839
+ groupByFunctions(ir) {
840
+ const functions = [];
841
+ let current = { name: 'Main', params: [], body: [] };
842
+
843
+ for (const inst of ir) {
844
+ if (inst.op === 'function') {
845
+ if (current.body.length > 0) {
846
+ functions.push(current);
847
+ }
848
+ current = { name: this.capitalize(inst.arg1), params: [], body: [] };
849
+ } else if (inst.op === 'param') {
850
+ current.params.push(inst.arg1);
851
+ } else if (inst.op === 'end_function') {
852
+ functions.push(current);
853
+ current = { name: 'Main', params: [], body: [] };
854
+ } else {
855
+ current.body.push(inst);
856
+ }
857
+ }
858
+
859
+ if (current.body.length > 0) {
860
+ functions.push(current);
861
+ }
862
+
863
+ return functions;
864
+ }
865
+
866
+ capitalize(str) {
867
+ return str.charAt(0).toUpperCase() + str.slice(1);
868
+ }
869
+
870
+ generateFunction(func) {
871
+ const lines = [];
872
+ const params = func.params.map(p => `${this.capitalize(p)} : Integer`).join('; ');
873
+
874
+ lines.push(`function ${func.name}(${params}) return Integer is`);
875
+
876
+ const tempIndent = this.indentLevel;
877
+ this.indentLevel = 0;
878
+
879
+ for (const inst of func.body) {
880
+ const line = this.generateInstruction(inst);
881
+ if (line && !line.startsWith('begin') && !line.startsWith('end')) {
882
+ lines.push(this.indentString + line);
883
+ }
884
+ }
885
+
886
+ lines.push('begin');
887
+ lines.push(this.indentString + 'return 0;');
888
+ lines.push(`end ${func.name};`);
889
+
890
+ this.indentLevel = tempIndent;
891
+
892
+ return lines.join('\n');
893
+ }
894
+
895
+ generateInstruction(inst) {
896
+ switch (inst.op) {
897
+ case 'assign':
898
+ return `${this.capitalize(inst.result)} : Integer := ${this.formatValue(inst.arg1)};`;
899
+
900
+ case '+':
901
+ case '-':
902
+ case '*':
903
+ case '/':
904
+ return `${this.capitalize(inst.result)} : Integer := ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
905
+
906
+ case '%':
907
+ return `${this.capitalize(inst.result)} : Integer := ${this.formatValue(inst.arg1)} mod ${this.formatValue(inst.arg2)};`;
908
+
909
+ case 'call':
910
+ return `${this.capitalize(inst.result)} : Integer := ${this.capitalize(inst.arg1)};`;
911
+
912
+ case 'return':
913
+ return '';
914
+
915
+ default:
916
+ return '';
917
+ }
918
+ }
919
+
920
+ formatValue(value) {
921
+ if (typeof value === 'string') {
922
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
923
+ return this.capitalize(value);
924
+ }
925
+ return `"${value}"`;
926
+ }
927
+ return String(value);
928
+ }
929
+
930
+ indent() {
931
+ return this.indentString.repeat(this.indentLevel);
932
+ }
933
+ }
934
+
935
+ class DlangGenerator {
936
+ constructor() {
937
+ this.indentLevel = 0;
938
+ this.indentString = ' ';
939
+ }
940
+
941
+ generate(ir) {
942
+ const code = [];
943
+ code.push('import std.stdio;');
944
+ code.push('');
945
+
946
+ const functions = this.groupByFunctions(ir);
947
+
948
+ for (const func of functions) {
949
+ code.push(this.generateFunction(func));
950
+ code.push('');
951
+ }
952
+
953
+ code.push('void main() {');
954
+ code.push('}');
955
+
956
+ return code.join('\n');
957
+ }
958
+
959
+ groupByFunctions(ir) {
960
+ const functions = [];
961
+ let current = { name: 'main', params: [], body: [] };
962
+
963
+ for (const inst of ir) {
964
+ if (inst.op === 'function') {
965
+ if (current.body.length > 0) {
966
+ functions.push(current);
967
+ }
968
+ current = { name: inst.arg1, params: [], body: [] };
969
+ } else if (inst.op === 'param') {
970
+ current.params.push(inst.arg1);
971
+ } else if (inst.op === 'end_function') {
972
+ functions.push(current);
973
+ current = { name: 'main', params: [], body: [] };
974
+ } else {
975
+ current.body.push(inst);
976
+ }
977
+ }
978
+
979
+ if (current.body.length > 0) {
980
+ functions.push(current);
981
+ }
982
+
983
+ return functions;
984
+ }
985
+
986
+ generateFunction(func) {
987
+ const lines = [];
988
+ const params = func.params.map(p => `long ${p}`).join(', ');
989
+
990
+ lines.push(`long ${func.name}(${params}) {`);
991
+ this.indentLevel++;
992
+
993
+ for (const inst of func.body) {
994
+ const line = this.generateInstruction(inst);
995
+ if (line) {
996
+ lines.push(this.indent() + line);
997
+ }
998
+ }
999
+
1000
+ if (func.body.length === 0 || func.body[func.body.length - 1].op !== 'return') {
1001
+ lines.push(this.indent() + 'return 0;');
1002
+ }
1003
+
1004
+ this.indentLevel--;
1005
+ lines.push('}');
1006
+
1007
+ return lines.join('\n');
1008
+ }
1009
+
1010
+ generateInstruction(inst) {
1011
+ switch (inst.op) {
1012
+ case 'assign':
1013
+ return `auto ${inst.result} = ${this.formatValue(inst.arg1)};`;
1014
+
1015
+ case '+':
1016
+ case '-':
1017
+ case '*':
1018
+ case '/':
1019
+ case '%':
1020
+ return `auto ${inst.result} = ${this.formatValue(inst.arg1)} ${inst.op} ${this.formatValue(inst.arg2)};`;
1021
+
1022
+ case 'call':
1023
+ return `auto ${inst.result} = ${inst.arg1}();`;
1024
+
1025
+ case 'return':
1026
+ return inst.arg1 ? `return ${this.formatValue(inst.arg1)};` : 'return 0;';
1027
+
1028
+ default:
1029
+ return '';
1030
+ }
1031
+ }
1032
+
1033
+ formatValue(value) {
1034
+ if (typeof value === 'string') {
1035
+ if (value.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
1036
+ return value;
1037
+ }
1038
+ return `"${value}"`;
1039
+ }
1040
+ return String(value);
1041
+ }
1042
+
1043
+ indent() {
1044
+ return this.indentString.repeat(this.indentLevel);
1045
+ }
1046
+ }
1047
+
1048
+ class AssemblyGenerator {
1049
+ constructor() {
1050
+ this.registerAlloc = new Map();
1051
+ this.nextRegister = 0;
1052
+ this.labelCounter = 0;
1053
+ }
1054
+
1055
+ generate(ir) {
1056
+ const code = [];
1057
+ code.push('section .text');
1058
+ code.push('global _start');
1059
+ code.push('');
1060
+ code.push('_start:');
1061
+
1062
+ for (const inst of ir) {
1063
+ const asm = this.generateInstruction(inst);
1064
+ if (asm) {
1065
+ code.push(' ' + asm);
1066
+ }
1067
+ }
1068
+
1069
+ code.push(' mov eax, 1');
1070
+ code.push(' xor ebx, ebx');
1071
+ code.push(' int 0x80');
1072
+
1073
+ return code.join('\n');
1074
+ }
1075
+
1076
+ generateInstruction(inst) {
1077
+ switch (inst.op) {
1078
+ case 'assign':
1079
+ if (typeof inst.arg1 === 'number') {
1080
+ return `mov dword [${inst.result}], ${inst.arg1}`;
1081
+ }
1082
+ return `mov eax, [${inst.arg1}]
1083
+ mov [${inst.result}], eax`;
1084
+
1085
+ case '+':
1086
+ return `mov eax, [${inst.arg1}]
1087
+ add eax, [${inst.arg2}]
1088
+ mov [${inst.result}], eax`;
1089
+
1090
+ case '-':
1091
+ return `mov eax, [${inst.arg1}]
1092
+ sub eax, [${inst.arg2}]
1093
+ mov [${inst.result}], eax`;
1094
+
1095
+ case '*':
1096
+ return `mov eax, [${inst.arg1}]
1097
+ imul eax, [${inst.arg2}]
1098
+ mov [${inst.result}], eax`;
1099
+
1100
+ case '/':
1101
+ return `mov eax, [${inst.arg1}]
1102
+ xor edx, edx
1103
+ idiv dword [${inst.arg2}]
1104
+ mov [${inst.result}], eax`;
1105
+
1106
+ case 'return':
1107
+ return '';
1108
+
1109
+ default:
1110
+ return '';
1111
+ }
1112
+ }
1113
+ }
1114
+
1115
+ class LLVMGenerator {
1116
+ constructor() {
1117
+ this.nextTemp = 0;
1118
+ this.nextLabel = 0;
1119
+ }
1120
+
1121
+ generate(ir) {
1122
+ const code = [];
1123
+ code.push('define i32 @main() {');
1124
+ code.push('entry:');
1125
+
1126
+ for (const inst of ir) {
1127
+ const llvm = this.generateInstruction(inst);
1128
+ if (llvm) {
1129
+ code.push(' ' + llvm);
1130
+ }
1131
+ }
1132
+
1133
+ code.push(' ret i32 0');
1134
+ code.push('}');
1135
+
1136
+ return code.join('\n');
1137
+ }
1138
+
1139
+ generateInstruction(inst) {
1140
+ switch (inst.op) {
1141
+ case 'assign':
1142
+ if (typeof inst.arg1 === 'number') {
1143
+ return `%${inst.result} = alloca i32
1144
+ store i32 ${inst.arg1}, i32* %${inst.result}`;
1145
+ }
1146
+ return `%${inst.result} = load i32, i32* %${inst.arg1}`;
1147
+
1148
+ case '+':
1149
+ return `%${inst.result} = add i32 %${inst.arg1}, %${inst.arg2}`;
1150
+
1151
+ case '-':
1152
+ return `%${inst.result} = sub i32 %${inst.arg1}, %${inst.arg2}`;
1153
+
1154
+ case '*':
1155
+ return `%${inst.result} = mul i32 %${inst.arg1}, %${inst.arg2}`;
1156
+
1157
+ case '/':
1158
+ return `%${inst.result} = sdiv i32 %${inst.arg1}, %${inst.arg2}`;
1159
+
1160
+ case '%':
1161
+ return `%${inst.result} = srem i32 %${inst.arg1}, %${inst.arg2}`;
1162
+
1163
+ case 'return':
1164
+ return inst.arg1 ? `ret i32 %${inst.arg1}` : 'ret i32 0';
1165
+
1166
+ default:
1167
+ return '';
1168
+ }
1169
+ }
1170
+ }
1171
+
1172
+ module.exports = {
1173
+ RustGenerator,
1174
+ CGenerator,
1175
+ CppGenerator,
1176
+ CSharpGenerator,
1177
+ JavaGenerator,
1178
+ ElixirGenerator,
1179
+ FSharpGenerator,
1180
+ AdaGenerator,
1181
+ DlangGenerator,
1182
+ AssemblyGenerator,
1183
+ LLVMGenerator
1184
+ };