mirror of
https://github.com/AeThex-Corporation/AeThex-OS.git
synced 2026-04-17 22:07:20 +00:00
197 lines
No EOL
6.9 KiB
JavaScript
197 lines
No EOL
6.9 KiB
JavaScript
"use strict";
|
|
/**
|
|
* AeThex Compiler - JavaScript Code Generator
|
|
* Generates JavaScript code from AST
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.JavaScriptGenerator = void 0;
|
|
class JavaScriptGenerator {
|
|
constructor() {
|
|
this.indent = 0;
|
|
}
|
|
generate(ast) {
|
|
const lines = [];
|
|
//Runtime header
|
|
lines.push('// Generated by AeThex Compiler v1.0.0');
|
|
lines.push('// Target: JavaScript');
|
|
lines.push('');
|
|
// Generate imports
|
|
for (const node of ast.body) {
|
|
if (node.type === 'Import') {
|
|
lines.push(this.generateImport(node));
|
|
}
|
|
}
|
|
if (ast.body.some(n => n.type === 'Import')) {
|
|
lines.push('');
|
|
}
|
|
// Generate realities
|
|
for (const node of ast.body) {
|
|
if (node.type === 'Reality') {
|
|
lines.push(this.generateReality(node));
|
|
lines.push('');
|
|
}
|
|
}
|
|
// Generate journeys
|
|
for (const node of ast.body) {
|
|
if (node.type === 'Journey') {
|
|
lines.push(this.generateJourney(node));
|
|
lines.push('');
|
|
}
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
generateImport(node) {
|
|
const specifiers = node.specifiers.join(', ');
|
|
return `const { ${specifiers} } = require('${node.source}');`;
|
|
}
|
|
generateReality(node) {
|
|
const lines = [];
|
|
lines.push(`const ${node.name} = {`);
|
|
for (const [key, value] of Object.entries(node.properties)) {
|
|
if (Array.isArray(value)) {
|
|
lines.push(` ${key}: ${JSON.stringify(value)},`);
|
|
}
|
|
else if (typeof value === 'string') {
|
|
lines.push(` ${key}: "${value}",`);
|
|
}
|
|
else {
|
|
lines.push(` ${key}: ${JSON.stringify(value)},`);
|
|
}
|
|
}
|
|
lines.push('};');
|
|
return lines.join('\n');
|
|
}
|
|
generateJourney(node) {
|
|
const lines = [];
|
|
const params = node.params.join(', ');
|
|
lines.push(`function ${node.name}(${params}) {`);
|
|
this.indent++;
|
|
for (const stmt of node.body) {
|
|
const generated = this.generateStatement(stmt);
|
|
if (generated) {
|
|
lines.push(this.indentLine(generated));
|
|
}
|
|
}
|
|
this.indent--;
|
|
lines.push('}');
|
|
return lines.join('\n');
|
|
}
|
|
generateStatement(stmt) {
|
|
switch (stmt.type) {
|
|
case 'LetStatement':
|
|
return this.generateLetStatement(stmt);
|
|
case 'WhenStatement':
|
|
return this.generateWhenStatement(stmt);
|
|
case 'NotifyStatement':
|
|
return this.generateNotifyStatement(stmt);
|
|
case 'RevealStatement':
|
|
return this.generateRevealStatement(stmt);
|
|
case 'SyncStatement':
|
|
return this.generateSyncStatement(stmt);
|
|
case 'ReturnStatement':
|
|
return 'return;';
|
|
case 'ExpressionStatement':
|
|
return this.generateExpression(stmt.expression) + ';';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
generateLetStatement(stmt) {
|
|
const value = this.generateExpression(stmt.value);
|
|
return `const ${stmt.identifier} = ${value};`;
|
|
}
|
|
generateWhenStatement(stmt) {
|
|
const lines = [];
|
|
const condition = this.generateExpression(stmt.condition);
|
|
lines.push(`if (${condition}) {`);
|
|
this.indent++;
|
|
for (const s of stmt.consequent) {
|
|
lines.push(this.indentLine(this.generateStatement(s)));
|
|
}
|
|
this.indent--;
|
|
if (stmt.alternate && stmt.alternate.length > 0) {
|
|
lines.push(this.indentLine('} else {'));
|
|
this.indent++;
|
|
for (const s of stmt.alternate) {
|
|
lines.push(this.indentLine(this.generateStatement(s)));
|
|
}
|
|
this.indent--;
|
|
}
|
|
lines.push(this.indentLine('}'));
|
|
return lines.join('\n');
|
|
}
|
|
generateNotifyStatement(stmt) {
|
|
const message = this.generateExpression(stmt.message);
|
|
return `console.log(${message});`;
|
|
}
|
|
generateRevealStatement(stmt) {
|
|
const value = this.generateExpression(stmt.value);
|
|
return `return ${value};`;
|
|
}
|
|
generateSyncStatement(stmt) {
|
|
const target = this.generateExpression(stmt.target);
|
|
const platforms = JSON.stringify(stmt.platforms);
|
|
return `await DataSync.sync(${target}, ${platforms});`;
|
|
}
|
|
generateExpression(expr) {
|
|
switch (expr.type) {
|
|
case 'BinaryExpression':
|
|
return this.generateBinaryExpression(expr);
|
|
case 'UnaryExpression':
|
|
return this.generateUnaryExpression(expr);
|
|
case 'CallExpression':
|
|
return this.generateCallExpression(expr);
|
|
case 'MemberExpression':
|
|
return this.generateMemberExpression(expr);
|
|
case 'Identifier':
|
|
return expr.name;
|
|
case 'Literal':
|
|
return typeof expr.value === 'string' ? `"${expr.value}"` : String(expr.value);
|
|
case 'ArrayExpression':
|
|
return this.generateArrayExpression(expr);
|
|
case 'ObjectExpression':
|
|
return this.generateObjectExpression(expr);
|
|
case 'NewExpression':
|
|
return this.generateNewExpression(expr);
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
generateBinaryExpression(expr) {
|
|
const left = this.generateExpression(expr.left);
|
|
const right = this.generateExpression(expr.right);
|
|
return `(${left} ${expr.operator} ${right})`;
|
|
}
|
|
generateUnaryExpression(expr) {
|
|
const operand = this.generateExpression(expr.operand);
|
|
return `(${expr.operator}${operand})`;
|
|
}
|
|
generateCallExpression(expr) {
|
|
const callee = this.generateExpression(expr.callee);
|
|
const args = expr.arguments.map(arg => this.generateExpression(arg)).join(', ');
|
|
return `${callee}(${args})`;
|
|
}
|
|
generateMemberExpression(expr) {
|
|
const object = this.generateExpression(expr.object);
|
|
return `${object}.${expr.property.name}`;
|
|
}
|
|
generateArrayExpression(expr) {
|
|
const elements = expr.elements.map(el => this.generateExpression(el)).join(', ');
|
|
return `[${elements}]`;
|
|
}
|
|
generateObjectExpression(expr) {
|
|
const properties = expr.properties
|
|
.map(prop => `${prop.key}: ${this.generateExpression(prop.value)}`)
|
|
.join(', ');
|
|
return `{ ${properties} }`;
|
|
}
|
|
generateNewExpression(expr) {
|
|
const args = expr.arguments.map(arg => this.generateExpression(arg)).join(', ');
|
|
return `new ${expr.callee.name}(${args})`;
|
|
}
|
|
indentLine(line) {
|
|
return ' '.repeat(this.indent) + line;
|
|
}
|
|
}
|
|
exports.JavaScriptGenerator = JavaScriptGenerator;
|
|
//# sourceMappingURL=JavaScriptGenerator.js.map
|