js-slang
Version:
Javascript-based implementations of Source, written in Typescript
62 lines • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLanguageOption = exports.getVariantOption = exports.getChapterOption = void 0;
exports.chapterParser = chapterParser;
exports.handleResult = handleResult;
const extra_typings_1 = require("@commander-js/extra-typings");
const __1 = require("..");
const closure_1 = require("../cse-machine/closure");
const langs_1 = require("../langs");
const misc_1 = require("../utils/misc");
const stringify_1 = require("../utils/stringify");
function chapterParser(str) {
let foundChapter;
if (/^-?[0-9]+$/.test(str)) {
// Chapter is fully numeric
const value = parseInt(str);
foundChapter = (0, misc_1.objectKeys)(langs_1.Chapter).find(chapterName => langs_1.Chapter[chapterName] === value);
if (foundChapter === undefined) {
throw new Error(`Invalid chapter value: ${str}`);
}
}
else {
foundChapter = str;
}
if (foundChapter in langs_1.Chapter) {
return langs_1.Chapter[foundChapter];
}
throw new Error(`Invalid chapter value: ${str}`);
}
const getChapterOption = (defaultValue, argParser) => {
return new extra_typings_1.Option('--chapter <chapter>').default(defaultValue).argParser(argParser);
};
exports.getChapterOption = getChapterOption;
const getVariantOption = (defaultValue, choices) => {
return new extra_typings_1.Option('--variant <variant>').default(defaultValue).choices(choices);
};
exports.getVariantOption = getVariantOption;
const getLanguageOption = () => {
return new extra_typings_1.Option('--languageOptions <options>')
.default({})
.argParser((value) => {
const languageOptions = value.split(',').map(lang => {
const [key, value] = lang.split('=');
return { [key]: value };
});
return Object.assign({}, ...languageOptions);
});
};
exports.getLanguageOption = getLanguageOption;
function handleResult(result, context, verboseErrors) {
if (result.status === 'finished') {
if (result.representation !== undefined) {
return result.representation;
}
if (result.value instanceof closure_1.default || typeof result.value === 'function') {
return result.value.toString();
}
return (0, stringify_1.stringify)(result.value);
}
return `Error: ${(0, __1.parseError)(context.errors, verboseErrors)}`;
}
//# sourceMappingURL=utils.js.map