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,173 @@
1
+ class Runtime {
2
+ constructor() {
3
+ this.builtins = this.initializeBuiltins();
4
+ this.modules = {};
5
+ }
6
+
7
+ initializeBuiltins() {
8
+ return {
9
+ print: (...args) => {
10
+ console.log(...args);
11
+ return null;
12
+ },
13
+
14
+ println: (...args) => {
15
+ console.log(...args);
16
+ return null;
17
+ },
18
+
19
+ input: (prompt = '') => {
20
+ if (typeof window !== 'undefined') {
21
+ return window.prompt(prompt);
22
+ }
23
+ return '';
24
+ },
25
+
26
+ len: (obj) => {
27
+ if (Array.isArray(obj) || typeof obj === 'string') {
28
+ return obj.length;
29
+ }
30
+ if (typeof obj === 'object' && obj !== null) {
31
+ return Object.keys(obj).length;
32
+ }
33
+ return 0;
34
+ },
35
+
36
+ str: (value) => String(value),
37
+
38
+ int: (value) => parseInt(value, 10),
39
+
40
+ float: (value) => parseFloat(value),
41
+
42
+ bool: (value) => Boolean(value),
43
+
44
+ type: (value) => typeof value,
45
+
46
+ range: (start, end, step = 1) => {
47
+ const result = [];
48
+ for (let i = start; i <= end; i += step) {
49
+ result.push(i);
50
+ }
51
+ return result;
52
+ },
53
+
54
+ map: (arr, func) => arr.map(func),
55
+
56
+ filter: (arr, func) => arr.filter(func),
57
+
58
+ reduce: (arr, func, initial) => arr.reduce(func, initial),
59
+
60
+ sort: (arr, compareFn) => arr.slice().sort(compareFn),
61
+
62
+ reverse: (arr) => arr.slice().reverse(),
63
+
64
+ join: (arr, separator = ',') => arr.join(separator),
65
+
66
+ split: (str, separator) => str.split(separator),
67
+
68
+ push: (arr, ...items) => {
69
+ arr.push(...items);
70
+ return arr;
71
+ },
72
+
73
+ pop: (arr) => arr.pop(),
74
+
75
+ shift: (arr) => arr.shift(),
76
+
77
+ unshift: (arr, ...items) => {
78
+ arr.unshift(...items);
79
+ return arr;
80
+ },
81
+
82
+ slice: (arr, start, end) => arr.slice(start, end),
83
+
84
+ indexOf: (arr, item) => arr.indexOf(item),
85
+
86
+ includes: (arr, item) => arr.includes(item),
87
+
88
+ keys: (obj) => Object.keys(obj),
89
+
90
+ values: (obj) => Object.values(obj),
91
+
92
+ entries: (obj) => Object.entries(obj),
93
+
94
+ assign: (target, ...sources) => Object.assign(target, ...sources),
95
+
96
+ freeze: (obj) => Object.freeze(obj),
97
+
98
+ seal: (obj) => Object.seal(obj),
99
+
100
+ Math: {
101
+ abs: Math.abs,
102
+ acos: Math.acos,
103
+ asin: Math.asin,
104
+ atan: Math.atan,
105
+ atan2: Math.atan2,
106
+ ceil: Math.ceil,
107
+ cos: Math.cos,
108
+ exp: Math.exp,
109
+ floor: Math.floor,
110
+ log: Math.log,
111
+ max: Math.max,
112
+ min: Math.min,
113
+ pow: Math.pow,
114
+ random: Math.random,
115
+ round: Math.round,
116
+ sin: Math.sin,
117
+ sqrt: Math.sqrt,
118
+ tan: Math.tan,
119
+ PI: Math.PI,
120
+ E: Math.E
121
+ },
122
+
123
+ Date: {
124
+ now: () => new Date(),
125
+ parse: (str) => new Date(str),
126
+ timestamp: () => Date.now(),
127
+ format: (date, format) => date.toISOString()
128
+ },
129
+
130
+ JSON: {
131
+ parse: JSON.parse,
132
+ stringify: JSON.stringify
133
+ },
134
+
135
+ setTimeout: typeof setTimeout !== 'undefined' ? setTimeout : () => {},
136
+ setInterval: typeof setInterval !== 'undefined' ? setInterval : () => {},
137
+ clearTimeout: typeof clearTimeout !== 'undefined' ? clearTimeout : () => {},
138
+ clearInterval: typeof clearInterval !== 'undefined' ? clearInterval : () => {},
139
+
140
+ Promise: typeof Promise !== 'undefined' ? Promise : class {},
141
+
142
+ fetch: typeof fetch !== 'undefined' ? fetch : async () => ({})
143
+ };
144
+ }
145
+
146
+ loadModule(modulePath) {
147
+ if (this.modules[modulePath]) {
148
+ return this.modules[modulePath];
149
+ }
150
+
151
+ try {
152
+ const module = require(modulePath);
153
+ this.modules[modulePath] = module;
154
+ return module;
155
+ } catch (error) {
156
+ throw new Error(`Failed to load module: ${modulePath}`);
157
+ }
158
+ }
159
+
160
+ registerModule(name, module) {
161
+ this.modules[name] = module;
162
+ }
163
+
164
+ getBuiltin(name) {
165
+ return this.builtins[name];
166
+ }
167
+
168
+ hasBuiltin(name) {
169
+ return name in this.builtins;
170
+ }
171
+ }
172
+
173
+ module.exports = Runtime;
@@ -0,0 +1,243 @@
1
+ const { compiler, interpret, compileFile } = require('../index-enhanced.cjs');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ class TestRunner {
6
+ constructor() {
7
+ this.tests = [];
8
+ this.passed = 0;
9
+ this.failed = 0;
10
+ }
11
+
12
+ test(description, testFn) {
13
+ this.tests.push({ description, testFn });
14
+ }
15
+
16
+ assertEqual(actual, expected, message) {
17
+ if (actual !== expected) {
18
+ throw new Error(`${message}\nExpected: ${expected}\nActual: ${actual}`);
19
+ }
20
+ }
21
+
22
+ assertContains(actual, substring, message) {
23
+ if (!actual.includes(substring)) {
24
+ throw new Error(`${message}\nExpected to contain: ${substring}\nActual: ${actual}`);
25
+ }
26
+ }
27
+
28
+ assertThrows(fn, message) {
29
+ try {
30
+ fn();
31
+ throw new Error(`${message}\nExpected function to throw but it did not`);
32
+ } catch (error) {
33
+ if (error.message.includes('Expected function to throw')) {
34
+ throw error;
35
+ }
36
+ }
37
+ }
38
+
39
+ async run() {
40
+ console.log('\n=== Running Lumos Language Tests ===\n');
41
+
42
+ for (const test of this.tests) {
43
+ try {
44
+ await test.testFn();
45
+ this.passed++;
46
+ console.log(`✓ ${test.description}`);
47
+ } catch (error) {
48
+ this.failed++;
49
+ console.log(`✗ ${test.description}`);
50
+ console.log(` Error: ${error.message}`);
51
+ }
52
+ }
53
+
54
+ console.log(`\n=== Test Results ===`);
55
+ console.log(`Total: ${this.tests.length}`);
56
+ console.log(`Passed: ${this.passed}`);
57
+ console.log(`Failed: ${this.failed}`);
58
+ console.log(`Success Rate: ${((this.passed / this.tests.length) * 100).toFixed(2)}%\n`);
59
+
60
+ return this.failed === 0;
61
+ }
62
+ }
63
+
64
+ const runner = new TestRunner();
65
+
66
+ runner.test('Variable declaration and assignment', () => {
67
+ const result = interpret('let x = 5');
68
+ runner.assertEqual(result, 'x = 5', 'Variable declaration failed');
69
+ });
70
+
71
+ runner.test('Arithmetic operations', () => {
72
+ interpret('let a = 10');
73
+ interpret('let b = 20');
74
+ const result = interpret('let c = a + b');
75
+ runner.assertContains(result, '30', 'Arithmetic operation failed');
76
+ });
77
+
78
+ runner.test('Function definition', () => {
79
+ const result = interpret('def test(x) { let y = x * 2 }');
80
+ runner.assertEqual(result, 'Function test defined.', 'Function definition failed');
81
+ });
82
+
83
+ runner.test('For loop execution', () => {
84
+ const result = interpret('for i = 1 to 5 { let x = i }');
85
+ runner.assertContains(result, 'Looped', 'For loop failed');
86
+ });
87
+
88
+ runner.test('While loop execution', () => {
89
+ interpret('let count = 0');
90
+ const result = interpret('while (count < 3) { let count = count + 1 }');
91
+ runner.assertEqual(result, 'While loop executed.', 'While loop failed');
92
+ });
93
+
94
+ runner.test('If-else conditional', () => {
95
+ interpret('let val = 15');
96
+ const result = interpret('if (val > 10) { let status = "high" } else { let status = "low" }');
97
+ runner.assertContains(result, 'high', 'If-else conditional failed');
98
+ });
99
+
100
+ runner.test('Times iteration', () => {
101
+ const result = interpret('3.times do |i| { let x = i }');
102
+ runner.assertContains(result, '0', 'Times iteration failed');
103
+ });
104
+
105
+ runner.test('Compilation to JavaScript', () => {
106
+ const code = 'let x = 5';
107
+ const compiled = compiler.compile(code, 'javascript');
108
+ runner.assertContains(compiled, 'let x', 'JavaScript compilation failed');
109
+ });
110
+
111
+ runner.test('Compilation to TypeScript', () => {
112
+ const code = 'let name = "test"';
113
+ const compiled = compiler.compile(code, 'typescript');
114
+ runner.assertContains(compiled, 'any', 'TypeScript compilation failed');
115
+ });
116
+
117
+ runner.test('Compilation to Python', () => {
118
+ const code = 'let value = 42';
119
+ const compiled = compiler.compile(code, 'python');
120
+ runner.assertContains(compiled, 'value', 'Python compilation failed');
121
+ });
122
+
123
+ runner.test('Compilation to Rust', () => {
124
+ const code = 'let num = 10';
125
+ const compiled = compiler.compile(code, 'rust');
126
+ runner.assertContains(compiled, 'let', 'Rust compilation failed');
127
+ });
128
+
129
+ runner.test('Compilation to C', () => {
130
+ const code = 'let integer = 100';
131
+ const compiled = compiler.compile(code, 'c');
132
+ runner.assertContains(compiled, 'int', 'C compilation failed');
133
+ });
134
+
135
+ runner.test('Compilation to Go', () => {
136
+ const code = 'let data = 50';
137
+ const compiled = compiler.compile(code, 'go');
138
+ runner.assertContains(compiled, 'package', 'Go compilation failed');
139
+ });
140
+
141
+ runner.test('Compilation to PHP', () => {
142
+ const code = 'let text = "hello"';
143
+ const compiled = compiler.compile(code, 'php');
144
+ runner.assertContains(compiled, '<?php', 'PHP compilation failed');
145
+ });
146
+
147
+ runner.test('Compilation to Ruby', () => {
148
+ const code = 'let result = 25';
149
+ const compiled = compiler.compile(code, 'ruby');
150
+ runner.assertContains(compiled, 'result', 'Ruby compilation failed');
151
+ });
152
+
153
+ runner.test('Compilation to Java', () => {
154
+ const code = 'let object = 1';
155
+ const compiled = compiler.compile(code, 'java');
156
+ runner.assertContains(compiled, 'class', 'Java compilation failed');
157
+ });
158
+
159
+ runner.test('Compilation to C#', () => {
160
+ const code = 'let property = 99';
161
+ const compiled = compiler.compile(code, 'csharp');
162
+ runner.assertContains(compiled, 'namespace', 'C# compilation failed');
163
+ });
164
+
165
+ runner.test('Compilation to Elixir', () => {
166
+ const code = 'let atom = 7';
167
+ const compiled = compiler.compile(code, 'elixir');
168
+ runner.assertContains(compiled, 'defmodule', 'Elixir compilation failed');
169
+ });
170
+
171
+ runner.test('Compilation to F#', () => {
172
+ const code = 'let func = 3';
173
+ const compiled = compiler.compile(code, 'fsharp');
174
+ runner.assertContains(compiled, 'let', 'F# compilation failed');
175
+ });
176
+
177
+ runner.test('Error handling for invalid expressions', () => {
178
+ runner.assertThrows(
179
+ () => interpret('let x ='),
180
+ 'Should throw error for invalid expression'
181
+ );
182
+ });
183
+
184
+ runner.test('Error handling for undefined functions', () => {
185
+ runner.assertThrows(
186
+ () => interpret('undefinedFunc()'),
187
+ 'Should throw error for undefined function'
188
+ );
189
+ });
190
+
191
+ runner.test('Tokenizer creates correct tokens', () => {
192
+ const tokens = compiler.tokenize('let x = 5');
193
+ runner.assertEqual(tokens.length >= 4, true, 'Tokenizer should create multiple tokens');
194
+ });
195
+
196
+ runner.test('Parser creates AST', () => {
197
+ const tokens = compiler.tokenize('let x = 5');
198
+ const ast = compiler.parse(tokens);
199
+ runner.assertEqual(ast.type, 'Program', 'Parser should create Program node');
200
+ });
201
+
202
+ runner.test('Type inference for numbers', () => {
203
+ const type = compiler.typeSystem.inferType({ type: 'Literal', value: 42 });
204
+ runner.assertEqual(type, 'number', 'Should infer number type');
205
+ });
206
+
207
+ runner.test('Type inference for strings', () => {
208
+ const type = compiler.typeSystem.inferType({ type: 'Literal', value: 'text' });
209
+ runner.assertEqual(type, 'string', 'Should infer string type');
210
+ });
211
+
212
+ runner.test('Optimizer performs constant folding', () => {
213
+ const ir = [
214
+ { op: '+', arg1: 2, arg2: 3, result: 't0' }
215
+ ];
216
+ const optimizer = new (require('../src/compiler/core/compiler').Optimizer)();
217
+ const optimized = optimizer.constantFolding(ir);
218
+ runner.assertEqual(optimized[0].op, 'assign', 'Should fold constants');
219
+ });
220
+
221
+ runner.test('Multiple variable declarations', () => {
222
+ interpret('let a = 1');
223
+ interpret('let b = 2');
224
+ interpret('let c = 3');
225
+ const result = interpret('let sum = a + b + c');
226
+ runner.assertContains(result, '6', 'Multiple variables failed');
227
+ });
228
+
229
+ runner.test('Nested conditionals', () => {
230
+ interpret('let x = 15');
231
+ const result = interpret('if (x > 10) { if (x > 20) { let level = "high" } else { let level = "medium" } } else { let level = "low" }');
232
+ runner.assertContains(result, 'medium', 'Nested conditionals failed');
233
+ });
234
+
235
+ runner.test('Function with parameters', () => {
236
+ interpret('def multiply(a, b) { let product = a * b }');
237
+ const result = interpret('multiply(6, 7)');
238
+ runner.assertContains(result, '42', 'Function with parameters failed');
239
+ });
240
+
241
+ runner.run().then(success => {
242
+ process.exit(success ? 0 : 1);
243
+ });