js-slang
Version:
Javascript-based implementations of Source, written in Typescript
71 lines • 3.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.transpilerCommand = void 0;
const path_1 = require("path");
const extra_typings_1 = require("@commander-js/extra-typings");
const astring_1 = require("astring");
const gpu_1 = require("../gpu/gpu");
const index_1 = require("../index");
const lazy_1 = require("../lazy/lazy");
const bundler_1 = require("../modules/preprocessor/bundler");
const linker_1 = require("../modules/preprocessor/linker");
const transpiler_1 = require("../transpiler/transpiler");
const types_1 = require("../types");
const utils_1 = require("./utils");
exports.transpilerCommand = new extra_typings_1.Command('transpiler')
.addOption((0, utils_1.getVariantOption)(types_1.Variant.DEFAULT, [types_1.Variant.DEFAULT, types_1.Variant.GPU, types_1.Variant.LAZY, types_1.Variant.NATIVE]))
.addOption((0, utils_1.getChapterOption)(types_1.Chapter.SOURCE_4, utils_1.chapterParser))
.option('-p, --pretranspile', "only pretranspile (e.g. GPU -> Source) and don't perform Source -> JS transpilation")
.option('-o, --out <outFile>', 'Specify a file to write to')
.argument('<filename>')
.action(async (fileName, opts) => {
if (!(0, utils_1.validateChapterAndVariantCombo)(opts)) {
console.log('Invalid language combination!');
return;
}
const fs = require('fs/promises');
const context = (0, index_1.createContext)(opts.chapter, opts.variant);
const entrypointFilePath = (0, path_1.resolve)(fileName);
const linkerResult = await (0, linker_1.default)(async (p) => {
try {
const text = await fs.readFile(p, 'utf-8');
return text;
}
catch (error) {
if (error.code === 'ENOENT')
return undefined;
throw error;
}
}, entrypointFilePath, context, {}, true);
if (!linkerResult.ok) {
console.log((0, index_1.parseError)(context.errors, linkerResult.verboseErrors));
return;
}
const { programs, topoOrder } = linkerResult;
const bundledProgram = (0, bundler_1.default)(programs, entrypointFilePath, topoOrder, context);
switch (opts.variant) {
case types_1.Variant.GPU:
(0, gpu_1.transpileToGPU)(bundledProgram);
break;
case types_1.Variant.LAZY:
(0, lazy_1.transpileToLazy)(bundledProgram);
break;
}
const transpiled = opts.pretranspile
? (0, astring_1.generate)(bundledProgram)
: (0, transpiler_1.transpile)(bundledProgram, context).transpiled;
if (context.errors.length > 0) {
console.log((0, index_1.parseError)(context.errors, linkerResult.verboseErrors));
return;
}
if (opts.out) {
const resolvedOut = (0, path_1.resolve)(opts.out);
await fs.writeFile(resolvedOut, transpiled);
console.log(`Code written to ${resolvedOut}`);
}
else {
console.log(transpiled);
}
});
//# sourceMappingURL=transpiler.js.map
;