whatlang-interpreter
Version:
Interpreter for WhatLang 2024, a stack-based esolang
51 lines (50 loc) • 1.62 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference types="node" />
const commander_1 = require("commander");
const fs_1 = __importDefault(require("fs"));
const whatlang_interpreter_1 = require("./whatlang_interpreter");
const program = new commander_1.Command();
program
.name("what")
.description("WhatLang, a stack-based programming language")
.argument("[file]", "file to execute")
.option("-e, --execute <code>", "execute code directly")
.action((file, options) => {
let code = "";
if (options.execute) {
code = options.execute;
}
else if (file && file !== "-") {
try {
code = fs_1.default.readFileSync(file, "utf-8");
}
catch (err) {
console.error(`Error reading file: ${err.message}`);
process.exit(1);
}
}
else if (process.stdin.isTTY && !file) {
// interactive mode not implemented yet
return program.help();
}
else {
const stdinBuffer = fs_1.default.readFileSync(0, "utf-8");
code = stdinBuffer;
}
return executeCode(code);
});
program.parse(process.argv);
async function executeCode(code) {
try {
await (0, whatlang_interpreter_1.eval_what)(code, [[]], { ...whatlang_interpreter_1.default_var_dict }, t => process.stdout.write(String(t)));
}
catch (err) {
console.error(err);
process.exit(126);
}
}
;