All files transpile.js

75% Statements 15/20
66.66% Branches 4/6
100% Functions 1/1
75% Lines 15/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                      1x 1x 1x 1x                   34x 34x 34x 34x         34x 34x 34x     34x 34x       34x     1x  
/**
 * 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);
  Iif (astFile !== undefined) {
    await fileSystem.writeFile(astFile, JSON.stringify(ast.ast, null, 2));
  }
  const CODE = await codeGen(ast);
  Iif (jsFile === undefined) {
    console.log(CODE);
    return;
  }
  await fileSystem.writeFile(jsFile, CODE);
}
 
module.exports = transpile;