UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

129 lines 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rawDisplay = rawDisplay; exports.error_message = error_message; exports.timed = timed; exports.is_number = is_number; exports.is_undefined = is_undefined; exports.is_string = is_string; exports.is_boolean = is_boolean; exports.is_object = is_object; exports.is_function = is_function; exports.is_NaN = is_NaN; exports.has_own_property = has_own_property; exports.is_array = is_array; exports.array_length = array_length; exports.parse_int = parse_int; exports.char_at = char_at; exports.arity = arity; exports.get_time = get_time; const closure_1 = require("../cse-machine/closure"); const stringify_1 = require("../utils/stringify"); /** * A function that displays to console.log by default (for a REPL). * * @param value the value to be represented and displayed. * @param externalContext a property of Context that can hold * any information required for external use (optional). */ function rawDisplay(value, str, _externalContext) { console.log((str === undefined ? '' : str + ' ') + value.toString()); return value; } function error_message(value, ...strs) { const output = (strs[0] === undefined ? '' : strs[0] + ' ') + (0, stringify_1.stringify)(value); throw new Error(output); } function timed(context, f, externalContext, displayBuiltin) { return (...args) => { const start = get_time(); const result = f(...args); const diff = get_time() - start; displayBuiltin('Duration: ' + Math.round(diff) + 'ms', '', externalContext); return result; }; } function is_number(v) { return typeof v === 'number'; } function is_undefined(xs) { return typeof xs === 'undefined'; } function is_string(xs) { return typeof xs === 'string'; } function is_boolean(xs) { return typeof xs === 'boolean'; } function is_object(xs) { return typeof xs === 'object' || is_function(xs); } function is_function(xs) { return typeof xs === 'function'; } function is_NaN(x) { return is_number(x) && isNaN(x); } function has_own_property(obj, p) { return obj.hasOwnProperty(p); } function is_array(a) { return a instanceof Array; } function array_length(xs) { return xs.length; } /** * Source version of parseInt. Both arguments are required. * * @param str String representation of the integer to be parsed. Required. * @param radix Base to parse the given `str`. Required. * * An error is thrown if `str` is not of type string, or `radix` is not an * integer within the range 2, 36 inclusive. */ function parse_int(str, radix) { if (typeof str === 'string' && typeof radix === 'number' && Number.isInteger(radix) && 2 <= radix && radix <= 36) { return parseInt(str, radix); } else { throw new Error('parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive.'); } } function char_at(str, index) { if (typeof str !== 'string') { throw new Error('char_at expects the first argument to be a string.'); } else if (typeof index !== 'number' || !Number.isInteger(index) || index < 0) { throw new Error('char_at expects the second argument to be a nonnegative integer.'); } return str[index]; } /** * arity returns the number of parameters a given function `f` expects. * * @param f Function whose arity is to be found. Required. * * An error is thrown if `f` is not a function. */ function arity(f) { if (f instanceof closure_1.default) { const params = f.node.params; const hasVarArgs = params[params.length - 1]?.type === 'RestElement'; return hasVarArgs ? params.length - 1 : params.length; } else if (typeof f === 'function') { return f.length; } else { throw new Error('arity expects a function as argument'); } } function get_time() { return new Date().getTime(); } //# sourceMappingURL=misc.js.map