UNPKG

@jilib/ji

Version:

Ji Language — Symbols as Functions

147 lines (113 loc) 6.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.terminationSymbol = void 0; var _marks = _interopRequireDefault(require("../marks")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var terminationSymbol = function terminationSymbol(left) { return ( /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(state) { var _state$vocabulary, vocabulary, _state$collected, collected, _state$output, output, pending, pend, pendfn, expectsLines, pres, expectsPend, result, pressedState, stredOutput, resultingOutput; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _state$vocabulary = state.vocabulary, vocabulary = _state$vocabulary === void 0 ? {} : _state$vocabulary, _state$collected = state.collected, collected = _state$collected === void 0 ? '' : _state$collected, _state$output = state.output, output = _state$output === void 0 ? '' : _state$output; pending = _toConsumableArray(state.pending || []); if (!(pending.length > 0)) { _context.next = 24; break; } pend = pending.pop(); pendfn = vocabulary[pend.el]; expectsLines = pendfn.length >= 3; if (!expectsLines) { _context.next = 8; break; } return _context.abrupt("return", _objectSpread({}, state, { pending: [].concat(_toConsumableArray(pending), [pend]), collected: "".concat(collected, "\n") })); case 8: _context.next = 10; return pendfn(pend.left, collected); case 10: pres = _context.sent; if (!(typeof pres === 'function')) { _context.next = 23; break; } expectsPend = pres.length >= 2; result = _objectSpread({}, state, { pending: _toConsumableArray(pending) }); if (!expectsPend) { _context.next = 16; break; } return _context.abrupt("return", _objectSpread({}, result, { collected: '\n', pending: [].concat(_toConsumableArray(pending), [pend]) })); case 16: _context.next = 18; return pres(_objectSpread({}, result, { pending: pending })); case 18: pressedState = _context.sent; if (!(typeof pressedState === 'string' || isFinite(pressedState))) { _context.next = 22; break; } stredOutput = _objectSpread({}, result, { output: "".concat(result.output).concat(pressedState).concat(_marks["default"].ts), collected: '', pending: _toConsumableArray(pending) }); return _context.abrupt("return", stredOutput); case 22: return _context.abrupt("return", pressedState); case 23: return _context.abrupt("return", _objectSpread({}, state, { output: "".concat(state.output).concat(pres).concat(_marks["default"].ts), collected: '', pending: _toConsumableArray(pending) })); case 24: resultingOutput = "".concat(output).concat(left).concat(_marks["default"].ts); return _context.abrupt("return", _objectSpread({}, state, { output: resultingOutput, collected: '' })); case 26: case "end": return _context.stop(); } } }, _callee); })); return function (_x) { return _ref.apply(this, arguments); }; }() ); }; exports.terminationSymbol = terminationSymbol; var _default = terminationSymbol; exports["default"] = _default;