AeThex-OS/packages/aethex-cli/lib/compiler/Compiler.js

158 lines
No EOL
5.1 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/**
* AeThex Compiler - Main Compiler Class
* Orchestrates lexing, parsing, and code generation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AeThexCompiler = void 0;
const Lexer_1 = require("./Lexer");
const Parser_1 = require("./Parser");
const JavaScriptGenerator_1 = require("./JavaScriptGenerator");
const LuaGenerator_1 = require("./LuaGenerator");
class AeThexCompiler {
constructor(options = {}) {
this.errors = [];
this.warnings = [];
this.target = options.target || 'javascript';
this.sourceFile = options.sourceFile || 'unknown';
}
/**
* Compile AeThex source code to target language
*/
compile(sourceCode) {
this.errors = [];
this.warnings = [];
try {
// Stage 1: Lexical Analysis
const lexer = new Lexer_1.Lexer(sourceCode);
const tokens = lexer.tokenize();
// Stage 2: Parsing
const parser = new Parser_1.Parser(tokens);
const ast = parser.parse();
// Stage 3: Semantic Analysis (basic for now)
this.semanticAnalysis(ast);
if (this.errors.length > 0) {
return {
success: false,
errors: this.errors,
warnings: this.warnings
};
}
// Stage 4: Code Generation
const code = this.generateCode(ast);
return {
success: true,
code,
errors: this.errors,
warnings: this.warnings
};
}
catch (error) {
this.errors.push({
type: 'CompilationError',
message: error.message,
file: this.sourceFile
});
return {
success: false,
errors: this.errors,
warnings: this.warnings
};
}
}
/**
* Basic semantic analysis
*/
semanticAnalysis(ast) {
// Check for duplicate journey names
const journeyNames = new Set();
for (const node of ast.body) {
if (node.type === 'Journey') {
if (journeyNames.has(node.name)) {
this.errors.push({
type: 'SemanticError',
message: `Duplicate journey name: ${node.name}`,
line: node.line,
file: this.sourceFile
});
}
journeyNames.add(node.name);
}
}
// Warn about missing platform declarations
for (const node of ast.body) {
if (node.type === 'Reality') {
if (!node.properties.platforms) {
this.warnings.push({
type: 'SemanticError',
message: `Reality "${node.name}" has no platform declaration`,
line: node.line,
file: this.sourceFile
});
}
}
}
}
/**
* Generate code for target platform
*/
generateCode(ast) {
switch (this.target) {
case 'javascript':
return new JavaScriptGenerator_1.JavaScriptGenerator().generate(ast);
case 'roblox':
return new LuaGenerator_1.LuaGenerator().generate(ast);
case 'uefn':
// TODO: Verse generator
throw new Error('UEFN (Verse) target not yet implemented');
case 'unity':
// TODO: C# generator
throw new Error('Unity (C#) target not yet implemented');
default:
throw new Error(`Unknown target: ${this.target}`);
}
}
/**
* Get target file extension
*/
static getExtension(target) {
switch (target) {
case 'javascript':
return 'js';
case 'roblox':
return 'lua';
case 'uefn':
return 'verse';
case 'unity':
return 'cs';
default:
return 'txt';
}
}
/**
* Format errors for display
*/
static formatErrors(result) {
const lines = [];
if (result.errors.length > 0) {
lines.push('❌ Compilation failed with errors:\n');
for (const err of result.errors) {
const location = err.line ? `:${err.line}` : '';
lines.push(` ${err.file}${location} - ${err.message}`);
}
}
if (result.warnings.length > 0) {
lines.push('\n⚠ Warnings:\n');
for (const warn of result.warnings) {
const location = warn.line ? `:${warn.line}` : '';
lines.push(` ${warn.file}${location} - ${warn.message}`);
}
}
if (result.success && result.errors.length === 0) {
lines.push('✅ Compilation successful!');
}
return lines.join('\n');
}
}
exports.AeThexCompiler = AeThexCompiler;
//# sourceMappingURL=Compiler.js.map