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,181 @@
1
+ const AssemblyBackends = {
2
+ x86: require('../backends/assembly/x86'),
3
+ arm: require('../backends/assembly/arm'),
4
+ wasm: require('../backends/assembly/wasm')
5
+ };
6
+
7
+ const CompiledBackends = {
8
+ c: require('../backends/compiled/c'),
9
+ cpp: require('../backends/compiled/cpp'),
10
+ rust: require('../backends/compiled/rust'),
11
+ go: require('../backends/compiled/go'),
12
+ java: require('../backends/compiled/java'),
13
+ csharp: require('../backends/compiled/csharp'),
14
+ swift: require('../backends/compiled/swift')
15
+ };
16
+
17
+ const InterpretedBackends = {
18
+ python: require('../backends/interpreted/python'),
19
+ ruby: require('../backends/interpreted/ruby'),
20
+ php: require('../backends/interpreted/php'),
21
+ perl: require('../backends/interpreted/perl'),
22
+ lua: require('../backends/interpreted/lua')
23
+ };
24
+
25
+ const ScriptingBackends = {
26
+ javascript: require('../backends/scripting/javascript'),
27
+ typescript: require('../backends/scripting/typescript'),
28
+ bash: require('../backends/scripting/bash'),
29
+ powershell: require('../backends/scripting/powershell'),
30
+ vbscript: require('../backends/scripting/vbscript')
31
+ };
32
+
33
+ const FunctionalBackends = {
34
+ haskell: require('../backends/functional/haskell'),
35
+ scala: require('../backends/functional/scala'),
36
+ elixir: require('../backends/functional/elixir'),
37
+ erlang: require('../backends/functional/erlang'),
38
+ fsharp: require('../backends/functional/fsharp'),
39
+ clojure: require('../backends/functional/clojure')
40
+ };
41
+
42
+ const WebBackends = {
43
+ html: require('../backends/web/html'),
44
+ css: require('../backends/web/css'),
45
+ jsx: require('../backends/web/jsx'),
46
+ vue: require('../backends/web/vue')
47
+ };
48
+
49
+ const DatabaseBackends = {
50
+ sql: require('../backends/database/sql'),
51
+ postgresql: require('../backends/database/postgresql'),
52
+ mysql: require('../backends/database/mysql'),
53
+ sqlite: require('../backends/database/sqlite'),
54
+ mongodb: require('../backends/database/mongodb')
55
+ };
56
+
57
+ const SpecializedBackends = {
58
+ ada: require('../backends/specialized/ada'),
59
+ cobol: require('../backends/specialized/cobol'),
60
+ fortran: require('../backends/specialized/fortran'),
61
+ lisp: require('../backends/specialized/lisp'),
62
+ prolog: require('../backends/specialized/prolog'),
63
+ mlang: require('../backends/specialized/mlang')
64
+ };
65
+
66
+ class Compiler {
67
+ constructor() {
68
+ this.backends = {
69
+ ...AssemblyBackends,
70
+ ...CompiledBackends,
71
+ ...InterpretedBackends,
72
+ ...ScriptingBackends,
73
+ ...FunctionalBackends,
74
+ ...WebBackends,
75
+ ...DatabaseBackends,
76
+ ...SpecializedBackends
77
+ };
78
+
79
+ this.extensionMap = {
80
+ x86: '.asm',
81
+ arm: '.s',
82
+ wasm: '.wat',
83
+ c: '.c',
84
+ cpp: '.cpp',
85
+ rust: '.rs',
86
+ go: '.go',
87
+ java: '.java',
88
+ csharp: '.cs',
89
+ swift: '.swift',
90
+ python: '.py',
91
+ ruby: '.rb',
92
+ php: '.php',
93
+ perl: '.pl',
94
+ lua: '.lua',
95
+ javascript: '.js',
96
+ typescript: '.ts',
97
+ bash: '.sh',
98
+ powershell: '.ps1',
99
+ vbscript: '.vbs',
100
+ haskell: '.hs',
101
+ scala: '.scala',
102
+ elixir: '.ex',
103
+ erlang: '.erl',
104
+ fsharp: '.fs',
105
+ clojure: '.clj',
106
+ html: '.html',
107
+ css: '.css',
108
+ jsx: '.jsx',
109
+ vue: '.vue',
110
+ sql: '.sql',
111
+ postgresql: '.sql',
112
+ mysql: '.sql',
113
+ sqlite: '.sql',
114
+ mongodb: '.js',
115
+ ada: '.adb',
116
+ cobol: '.cob',
117
+ fortran: '.f90',
118
+ lisp: '.lisp',
119
+ prolog: '.pl',
120
+ mlang: '.m'
121
+ };
122
+ }
123
+
124
+ compile(ast, target, options = {}) {
125
+ const normalizedTarget = target.toLowerCase();
126
+
127
+ if (!this.backends[normalizedTarget]) {
128
+ throw new Error(`Unsupported compilation target: ${target}`);
129
+ }
130
+
131
+ const backend = this.backends[normalizedTarget];
132
+ const compiled = backend.generate(ast, options);
133
+
134
+ if (options.optimize) {
135
+ return this.optimize(compiled, normalizedTarget);
136
+ }
137
+
138
+ return compiled;
139
+ }
140
+
141
+ getExtension(target) {
142
+ const normalizedTarget = target.toLowerCase();
143
+ return this.extensionMap[normalizedTarget] || '.txt';
144
+ }
145
+
146
+ optimize(code, target) {
147
+ return code;
148
+ }
149
+
150
+ getSupportedTargets() {
151
+ return Object.keys(this.backends);
152
+ }
153
+
154
+ getTargetInfo(target) {
155
+ const normalizedTarget = target.toLowerCase();
156
+ if (!this.backends[normalizedTarget]) {
157
+ return null;
158
+ }
159
+
160
+ return {
161
+ name: target,
162
+ extension: this.getExtension(target),
163
+ type: this.getTargetType(normalizedTarget),
164
+ backend: this.backends[normalizedTarget]
165
+ };
166
+ }
167
+
168
+ getTargetType(target) {
169
+ if (AssemblyBackends[target]) return 'assembly';
170
+ if (CompiledBackends[target]) return 'compiled';
171
+ if (InterpretedBackends[target]) return 'interpreted';
172
+ if (ScriptingBackends[target]) return 'scripting';
173
+ if (FunctionalBackends[target]) return 'functional';
174
+ if (WebBackends[target]) return 'web';
175
+ if (DatabaseBackends[target]) return 'database';
176
+ if (SpecializedBackends[target]) return 'specialized';
177
+ return 'unknown';
178
+ }
179
+ }
180
+
181
+ module.exports = Compiler;
@@ -0,0 +1,408 @@
1
+ class BreakException extends Error {
2
+ constructor() {
3
+ super('Break');
4
+ this.name = 'BreakException';
5
+ }
6
+ }
7
+
8
+ class ContinueException extends Error {
9
+ constructor() {
10
+ super('Continue');
11
+ this.name = 'ContinueException';
12
+ }
13
+ }
14
+
15
+ class ReturnException extends Error {
16
+ constructor(value) {
17
+ super('Return');
18
+ this.name = 'ReturnException';
19
+ this.value = value;
20
+ }
21
+ }
22
+
23
+ class Evaluator {
24
+ constructor(runtime) {
25
+ this.runtime = runtime;
26
+ this.globalScope = {};
27
+ this.currentScope = this.globalScope;
28
+ }
29
+
30
+ evaluate(ast) {
31
+ return this.evaluateNode(ast);
32
+ }
33
+
34
+ evaluateNode(node) {
35
+ if (!node) return null;
36
+
37
+ switch (node.type) {
38
+ case 'Program':
39
+ return this.evaluateProgram(node);
40
+ case 'VariableDeclaration':
41
+ return this.evaluateVariableDeclaration(node);
42
+ case 'FunctionDeclaration':
43
+ return this.evaluateFunctionDeclaration(node);
44
+ case 'ClassDeclaration':
45
+ return this.evaluateClassDeclaration(node);
46
+ case 'ImportStatement':
47
+ return this.evaluateImportStatement(node);
48
+ case 'TryStatement':
49
+ return this.evaluateTryStatement(node);
50
+ case 'IfStatement':
51
+ return this.evaluateIfStatement(node);
52
+ case 'WhileStatement':
53
+ return this.evaluateWhileStatement(node);
54
+ case 'ForStatement':
55
+ return this.evaluateForStatement(node);
56
+ case 'ReturnStatement':
57
+ return this.evaluateReturnStatement(node);
58
+ case 'Break':
59
+ throw new BreakException();
60
+ case 'Continue':
61
+ throw new ContinueException();
62
+ case 'ExpressionStatement':
63
+ return this.evaluateNode(node.expression);
64
+ case 'Assignment':
65
+ return this.evaluateAssignment(node);
66
+ case 'BinaryExpression':
67
+ return this.evaluateBinaryExpression(node);
68
+ case 'UnaryExpression':
69
+ return this.evaluateUnaryExpression(node);
70
+ case 'CallExpression':
71
+ return this.evaluateCallExpression(node);
72
+ case 'IndexExpression':
73
+ return this.evaluateIndexExpression(node);
74
+ case 'MemberExpression':
75
+ return this.evaluateMemberExpression(node);
76
+ case 'Identifier':
77
+ return this.evaluateIdentifier(node);
78
+ case 'Literal':
79
+ return node.value;
80
+ case 'ArrayLiteral':
81
+ return this.evaluateArrayLiteral(node);
82
+ case 'ObjectLiteral':
83
+ return this.evaluateObjectLiteral(node);
84
+ default:
85
+ throw new Error(`Unknown node type: ${node.type}`);
86
+ }
87
+ }
88
+
89
+ evaluateProgram(node) {
90
+ let result = null;
91
+ for (const statement of node.statements) {
92
+ result = this.evaluateNode(statement);
93
+ }
94
+ return result;
95
+ }
96
+
97
+ evaluateVariableDeclaration(node) {
98
+ const value = node.initializer ? this.evaluateNode(node.initializer) : null;
99
+ this.currentScope[node.name] = value;
100
+ return value;
101
+ }
102
+
103
+ evaluateFunctionDeclaration(node) {
104
+ const func = {
105
+ type: 'function',
106
+ parameters: node.parameters,
107
+ body: node.body,
108
+ scope: this.currentScope
109
+ };
110
+ this.currentScope[node.name] = func;
111
+ return func;
112
+ }
113
+
114
+ evaluateClassDeclaration(node) {
115
+ const classObj = {
116
+ type: 'class',
117
+ name: node.name,
118
+ superclass: node.superclass,
119
+ methods: {},
120
+ properties: {}
121
+ };
122
+
123
+ const previousScope = this.currentScope;
124
+ this.currentScope = {};
125
+
126
+ for (const method of node.methods) {
127
+ this.evaluateFunctionDeclaration(method);
128
+ classObj.methods[method.name] = this.currentScope[method.name];
129
+ }
130
+
131
+ for (const property of node.properties) {
132
+ this.evaluateVariableDeclaration(property);
133
+ classObj.properties[property.name] = this.currentScope[property.name];
134
+ }
135
+
136
+ this.currentScope = previousScope;
137
+ this.currentScope[node.name] = classObj;
138
+ return classObj;
139
+ }
140
+
141
+ evaluateImportStatement(node) {
142
+ const module = this.runtime.loadModule(node.source);
143
+ for (const spec of node.specifiers) {
144
+ if (spec.name === '*') {
145
+ Object.assign(this.currentScope, module);
146
+ } else {
147
+ this.currentScope[spec.alias] = module[spec.name];
148
+ }
149
+ }
150
+ return module;
151
+ }
152
+
153
+ evaluateTryStatement(node) {
154
+ try {
155
+ return this.evaluateBlock(node.tryBlock);
156
+ } catch (error) {
157
+ if (node.catchClause) {
158
+ const previousScope = this.currentScope;
159
+ this.currentScope = Object.create(this.currentScope);
160
+ if (node.catchClause.parameter) {
161
+ this.currentScope[node.catchClause.parameter] = error;
162
+ }
163
+ const result = this.evaluateBlock(node.catchClause.body);
164
+ this.currentScope = previousScope;
165
+ return result;
166
+ } else {
167
+ throw error;
168
+ }
169
+ } finally {
170
+ if (node.finallyBlock) {
171
+ this.evaluateBlock(node.finallyBlock);
172
+ }
173
+ }
174
+ }
175
+
176
+ evaluateIfStatement(node) {
177
+ if (this.evaluateNode(node.condition)) {
178
+ return this.evaluateBlock(node.thenBranch);
179
+ }
180
+
181
+ for (const elif of node.elifBranches || []) {
182
+ if (this.evaluateNode(elif.condition)) {
183
+ return this.evaluateBlock(elif.body);
184
+ }
185
+ }
186
+
187
+ if (node.elseBranch) {
188
+ return this.evaluateBlock(node.elseBranch);
189
+ }
190
+
191
+ return null;
192
+ }
193
+
194
+ evaluateWhileStatement(node) {
195
+ let result = null;
196
+ let iterations = 0;
197
+ const maxIterations = 1000000;
198
+
199
+ while (this.evaluateNode(node.condition)) {
200
+ if (iterations++ > maxIterations) {
201
+ throw new Error('Infinite loop detected');
202
+ }
203
+
204
+ try {
205
+ result = this.evaluateBlock(node.body);
206
+ } catch (error) {
207
+ if (error instanceof BreakException) break;
208
+ if (error instanceof ContinueException) continue;
209
+ throw error;
210
+ }
211
+ }
212
+
213
+ return result;
214
+ }
215
+
216
+ evaluateForStatement(node) {
217
+ const start = this.evaluateNode(node.start);
218
+ const end = this.evaluateNode(node.end);
219
+ const step = this.evaluateNode(node.step);
220
+
221
+ let result = null;
222
+ const previousScope = this.currentScope;
223
+ this.currentScope = Object.create(this.currentScope);
224
+
225
+ for (let i = start; i <= end; i += step) {
226
+ this.currentScope[node.iterator] = i;
227
+
228
+ try {
229
+ result = this.evaluateBlock(node.body);
230
+ } catch (error) {
231
+ if (error instanceof BreakException) break;
232
+ if (error instanceof ContinueException) continue;
233
+ throw error;
234
+ }
235
+ }
236
+
237
+ this.currentScope = previousScope;
238
+ return result;
239
+ }
240
+
241
+ evaluateReturnStatement(node) {
242
+ const value = node.value ? this.evaluateNode(node.value) : null;
243
+ throw new ReturnException(value);
244
+ }
245
+
246
+ evaluateBlock(statements) {
247
+ let result = null;
248
+ for (const statement of statements) {
249
+ result = this.evaluateNode(statement);
250
+ }
251
+ return result;
252
+ }
253
+
254
+ evaluateAssignment(node) {
255
+ const value = this.evaluateNode(node.value);
256
+
257
+ if (node.target.type === 'Identifier') {
258
+ const name = node.target.name;
259
+
260
+ if (node.operator === '=') {
261
+ this.currentScope[name] = value;
262
+ } else {
263
+ const current = this.currentScope[name] || 0;
264
+ switch (node.operator) {
265
+ case '+=': this.currentScope[name] = current + value; break;
266
+ case '-=': this.currentScope[name] = current - value; break;
267
+ case '*=': this.currentScope[name] = current * value; break;
268
+ case '/=': this.currentScope[name] = current / value; break;
269
+ }
270
+ }
271
+
272
+ return this.currentScope[name];
273
+ }
274
+
275
+ if (node.target.type === 'IndexExpression') {
276
+ const object = this.evaluateNode(node.target.object);
277
+ const index = this.evaluateNode(node.target.index);
278
+ object[index] = value;
279
+ return value;
280
+ }
281
+
282
+ if (node.target.type === 'MemberExpression') {
283
+ const object = this.evaluateNode(node.target.object);
284
+ object[node.target.property] = value;
285
+ return value;
286
+ }
287
+
288
+ throw new Error('Invalid assignment target');
289
+ }
290
+
291
+ evaluateBinaryExpression(node) {
292
+ const left = this.evaluateNode(node.left);
293
+ const right = this.evaluateNode(node.right);
294
+
295
+ switch (node.operator) {
296
+ case '+': return left + right;
297
+ case '-': return left - right;
298
+ case '*': return left * right;
299
+ case '/': return left / right;
300
+ case '%': return left % right;
301
+ case '==': return left == right;
302
+ case '!=': return left != right;
303
+ case '<': return left < right;
304
+ case '<=': return left <= right;
305
+ case '>': return left > right;
306
+ case '>=': return left >= right;
307
+ case 'and': case '&&': return left && right;
308
+ case 'or': case '||': return left || right;
309
+ default:
310
+ throw new Error(`Unknown binary operator: ${node.operator}`);
311
+ }
312
+ }
313
+
314
+ evaluateUnaryExpression(node) {
315
+ const operand = this.evaluateNode(node.operand);
316
+
317
+ switch (node.operator) {
318
+ case '-': return -operand;
319
+ case '+': return +operand;
320
+ case 'not': case '!': return !operand;
321
+ default:
322
+ throw new Error(`Unknown unary operator: ${node.operator}`);
323
+ }
324
+ }
325
+
326
+ evaluateCallExpression(node) {
327
+ const callee = this.evaluateNode(node.callee);
328
+ const args = node.arguments.map(arg => this.evaluateNode(arg));
329
+
330
+ if (typeof callee === 'function') {
331
+ return callee(...args);
332
+ }
333
+
334
+ if (callee.type === 'function') {
335
+ const previousScope = this.currentScope;
336
+ this.currentScope = Object.create(callee.scope);
337
+
338
+ for (let i = 0; i < callee.parameters.length; i++) {
339
+ this.currentScope[callee.parameters[i]] = args[i];
340
+ }
341
+
342
+ try {
343
+ this.evaluateBlock(callee.body);
344
+ this.currentScope = previousScope;
345
+ return null;
346
+ } catch (error) {
347
+ this.currentScope = previousScope;
348
+ if (error instanceof ReturnException) {
349
+ return error.value;
350
+ }
351
+ throw error;
352
+ }
353
+ }
354
+
355
+ throw new Error('Not a function');
356
+ }
357
+
358
+ evaluateIndexExpression(node) {
359
+ const object = this.evaluateNode(node.object);
360
+ const index = this.evaluateNode(node.index);
361
+ return object[index];
362
+ }
363
+
364
+ evaluateMemberExpression(node) {
365
+ const object = this.evaluateNode(node.object);
366
+ return object[node.property];
367
+ }
368
+
369
+ evaluateIdentifier(node) {
370
+ const name = node.name;
371
+
372
+ if (name in this.currentScope) {
373
+ return this.currentScope[name];
374
+ }
375
+
376
+ let scope = this.currentScope;
377
+ while (Object.getPrototypeOf(scope)) {
378
+ scope = Object.getPrototypeOf(scope);
379
+ if (name in scope) {
380
+ return scope[name];
381
+ }
382
+ }
383
+
384
+ if (name in this.globalScope) {
385
+ return this.globalScope[name];
386
+ }
387
+
388
+ if (name in this.runtime.builtins) {
389
+ return this.runtime.builtins[name];
390
+ }
391
+
392
+ throw new Error(`Undefined variable: ${name}`);
393
+ }
394
+
395
+ evaluateArrayLiteral(node) {
396
+ return node.elements.map(el => this.evaluateNode(el));
397
+ }
398
+
399
+ evaluateObjectLiteral(node) {
400
+ const obj = {};
401
+ for (const prop of node.properties) {
402
+ obj[prop.key] = this.evaluateNode(prop.value);
403
+ }
404
+ return obj;
405
+ }
406
+ }
407
+
408
+ module.exports = Evaluator;