UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

125 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.miscBuiltinFunctions = void 0; const ArrowFunctionExpression_1 = require("../nodes/Expression/ArrowFunctionExpression"); const Identifier_1 = require("../nodes/Expression/Identifier"); const Literal_1 = require("../nodes/Expression/Literal"); const lists_1 = require("./lists"); const _1 = require("."); exports.miscBuiltinFunctions = { arity: { definition: (args) => { if (args[0] instanceof ArrowFunctionExpression_1.StepperArrowFunctionExpression) { return new Literal_1.StepperLiteral(args[0].params.length); } if (args[0] instanceof Identifier_1.StepperIdentifier) { if (args[0].name.startsWith('math_')) { // Math builtins const func = Math[args[0].name.split('_')[1]]; if (typeof func !== 'function') { throw new Error('arity expects a function as argument'); } return new Literal_1.StepperLiteral(func.length); } if (Object.keys(lists_1.listBuiltinFunctions).includes(args[0].name)) { return new Literal_1.StepperLiteral(lists_1.listBuiltinFunctions[args[0].name].arity); } if (Object.keys(exports.miscBuiltinFunctions).includes(args[0].name)) { return new Literal_1.StepperLiteral(exports.miscBuiltinFunctions[args[0].name].arity); } } throw new Error('arity expects a function as argument'); }, arity: 1 }, char_at: { definition: (args) => { const str = args[0].value; const index = args[1].value; return new Literal_1.StepperLiteral(str.charAt(index)); }, arity: 2 }, display: { definition: (args) => { return args[0]; }, arity: 1 }, error: { definition: (args) => { const errorMessage = args[0].value; throw new Error(errorMessage); }, arity: 1 }, get_time: { definition: (_args) => { return new Literal_1.StepperLiteral(Date.now()); }, arity: 0 }, is_boolean: { definition: (args) => { return new Literal_1.StepperLiteral(typeof args[0].value === 'boolean'); }, arity: 1 }, is_function: { definition: (args) => { return new Literal_1.StepperLiteral(args[0] instanceof ArrowFunctionExpression_1.StepperArrowFunctionExpression || (args[0] instanceof Identifier_1.StepperIdentifier && (0, _1.isBuiltinFunction)(args[0].name))); }, arity: 1 }, is_number: { definition: (args) => { return new Literal_1.StepperLiteral(typeof args[0].value === 'number'); }, arity: 1 }, is_string: { definition: (args) => { return new Literal_1.StepperLiteral(typeof args[0].value === 'string'); }, arity: 1 }, is_undefined: { definition: (args) => { return new Literal_1.StepperLiteral(args[0].value === undefined); }, arity: 1 }, parse_int: { definition: (args) => { const str = args[0].value; const radix = args.length > 1 ? args[1].value : 10; return new Literal_1.StepperLiteral(parseInt(str, radix)); }, arity: 2 }, prompt: { definition: (args) => { const message = args[0].value; const result = window.prompt(message); return new Literal_1.StepperLiteral(result !== null ? result : null); }, arity: 0 }, stringify: { definition: (args) => { const value = args[0]; let stringified; if (value instanceof Literal_1.StepperLiteral) { stringified = JSON.stringify(value.value); } else { stringified = value.toString(); } return new Literal_1.StepperLiteral(stringified); }, arity: 1 } }; //# sourceMappingURL=misc.js.map