Feat: Create main transpiler entry point

This commit is contained in:
2025-09-26 08:22:16 -07:00
parent f4b52641f2
commit 8780854a21

34
src/index.js Normal file
View File

@@ -0,0 +1,34 @@
import { HiLexer } from './lexer.js';
import { parser } from './parser.js';
import { astBuilder } from './ast-builder.js';
import { generate } from './generator.js';
/**
* Transpiles a string of Hi source code into JavaScript.
* @param {string} sourceCode The Hi source code.
* @returns {string} The equivalent JavaScript code.
*/
export function hi2js(sourceCode) {
if (!sourceCode.trim()) {
return "";
}
// 1. Lexing
const lexResult = HiLexer.tokenize(sourceCode);
if (lexResult.errors.length > 0) {
throw new Error(`Lexing error: ${lexResult.errors[0].message}`);
}
// 2. Parsing
parser.input = lexResult.tokens;
const cst = parser.program();
if (parser.errors.length > 0) {
const err = parser.errors[0];
throw new Error(`Parsing error: ${err.message}`);
}
// 3. Building AST from CST
const ast = astBuilder.visit(cst);
// 4. Generating JavaScript from AST
return generate(ast);
}