esther-medina-quintero-parser-nearley
Version:
68 lines (56 loc) • 1.81 kB
JavaScript
/**
* @description A executable to be able to compile eggc lang files
* @author Esther M. Quintero <alu0101434780@ull.edu.es>
* @since 12/03/2024
*/
;
const fs = require("fs");
const { program } = require("commander");
const { parseFromFile } = require("../index.js");
const { version } = require("../package.json");
/**
* A function that compiles a eggc file
* @param {string} origin The name of the origin file
* @param {string} destination The name of the destination file
* @throws Will throw if there are errors in the program or if the files
* can't be opened
*/
const compile = (origin, options) => {
// console.log("---> Origin: ", origin);
let destination = options.out;
if (destination == undefined) {
destination = options.out = origin.match(/^[^.]*/)[0] + ".json";
}
console.log("---> Destination: ", destination);
console.log(parseFromFile(origin));
const ast = parseFromFile(origin);
console.log("---> Ast: ", ast);
if (options.run) {
const {runFromEVM} = require("@crguezl/eloquentjsegg/lib/eggvm.js");
runFromEVM(options.out);
}
const astString = JSON.stringify(ast, null, 2);
console.log(astString);
fs.writeFileSync(destination, astString);
};
program
.version(version)
.description("A compiler for the eggc language")
.usage("[options] <file>")
.argument('<origin>', 'The path of the file to compile')
.option(
'-o, --out <destination>', 'The path of the file to write the output to'
)
.option(
'-r, --run', 'Run the program after compiling it'
)
.action((origin, options) => {
try {
compile(origin, options);
} catch (error) {
console.error('There was an error: ' + error.message);
process.exit(1);
}
});
program.parse(process.argv);