/**
* Universidad de La Laguna
* Escuela Superior de Ingeniería y Tecnología
* Grado en Ingeniería Informática
* Procesadores de Lenguajes
*
* @author Juan Rodríguez Suárez
* @since Mar 04 2024
* @desc Contains all the code to transpile a mathematical expression to JavaScript.
*/
const calcParser = require("./calc").parser;
const fileSystem = require('fs/promises');
const { dependencies, scopeAnalysis } = require('./scope.js');
const codeGen = require('./code-generation.js');
/**
* @brief Transpiles the input expression into a JavaScript file
* @param {string} expression - The input
* @param {string} inputFile - The input file
* @param {string} jsFile - The output JavaScript file
* @param {string} astFile - The output AST file
*/
async function transpile(expression, inputFile, jsFile, astFile) {
const INPUT = expression || await fileSystem.readFile(inputFile, 'utf-8');
let ast = { ast: null, dependencies: [], symbolTable: [], used: []};
try {
ast.ast = calcParser.parse(INPUT);
} catch (error) {
console.error(error.message);
return;
}
ast = dependencies(ast);
ast = scopeAnalysis(ast);
if (astFile !== undefined) {
await fileSystem.writeFile(astFile, JSON.stringify(ast.ast, null, 2));
}
const CODE = await codeGen(ast);
if (jsFile === undefined) {
console.log(CODE);
return;
}
await fileSystem.writeFile(jsFile, CODE);
}
module.exports = transpile;