mirror of
https://github.com/hi-language/transpiler.git
synced 2026-01-14 00:28:05 +00:00
Feat: Create main transpiler entry point
This commit is contained in:
34
src/index.js
Normal file
34
src/index.js
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user