UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

91 lines 3.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleResult = exports.validChapterVariant = exports.validateChapterAndVariantCombo = exports.getLanguageOption = exports.getVariantOption = exports.getChapterOption = exports.chapterParser = void 0; const extra_typings_1 = require("@commander-js/extra-typings"); const constants_1 = require("../constants"); const types_1 = require("../types"); const stringify_1 = require("../utils/stringify"); const closure_1 = require("../cse-machine/closure"); const __1 = require(".."); const misc_1 = require("../utils/misc"); function chapterParser(str) { let foundChapter; if (/^-?[0-9]+$/.test(str)) { // Chapter is fully numeric const value = parseInt(str); foundChapter = (0, misc_1.objectKeys)(types_1.Chapter).find(chapterName => types_1.Chapter[chapterName] === value); if (foundChapter === undefined) { throw new Error(`Invalid chapter value: ${str}`); } } else { foundChapter = str; } if (foundChapter in types_1.Chapter) { return types_1.Chapter[foundChapter]; } throw new Error(`Invalid chapter value: ${str}`); } exports.chapterParser = chapterParser; 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 validateChapterAndVariantCombo(language) { for (const { chapter, variant } of constants_1.sourceLanguages) { if (language.chapter === chapter && language.variant === variant) return true; } return false; } exports.validateChapterAndVariantCombo = validateChapterAndVariantCombo; /** * Returns true iff the given chapter and variant combination is supported. */ function validChapterVariant(language) { const { chapter, variant } = language; for (const lang of constants_1.sourceLanguages) { if (lang.chapter === chapter && lang.variant === variant) return true; } for (const lang of constants_1.scmLanguages) { if (lang.chapter === chapter && lang.variant === variant) return true; } for (const lang of constants_1.pyLanguages) { if (lang.chapter === chapter && lang.variant === variant) return true; } return false; } exports.validChapterVariant = validChapterVariant; 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)}`; } exports.handleResult = handleResult; //# sourceMappingURL=utils.js.map