@jilib/ji
Version:
Ji Language — Symbols as Functions
135 lines (102 loc) • 6.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.shortTermination = 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 shortTermination = function shortTermination(left) {
return (
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(state) {
var _state$vocabulary, vocabulary, _state$collected, collected, pending, pend, symbol, shortTerminator, pres, expectsPend, result, pressedState, stredOutput;
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;
pending = _toConsumableArray(state.pending || []);
if (!(pending.length > 0)) {
_context.next = 23;
break;
}
pend = pending.pop();
symbol = vocabulary[pend.el];
shortTerminator = symbol.shortTerminator;
if (!(symbol && shortTerminator)) {
_context.next = 23;
break;
}
_context.next = 9;
return symbol(pend.left, collected);
case 9:
pres = _context.sent;
if (!(typeof pres === 'function')) {
_context.next = 22;
break;
}
expectsPend = pres.length >= 2;
result = _objectSpread({}, state, {
pending: _toConsumableArray(pending)
});
if (!expectsPend) {
_context.next = 15;
break;
}
return _context.abrupt("return", _objectSpread({}, result, {
collected: '\n',
pending: [].concat(_toConsumableArray(pending), [pend])
}));
case 15:
_context.next = 17;
return pres(_objectSpread({}, result, {
pending: pending
}));
case 17:
pressedState = _context.sent;
if (!(typeof pressedState === 'string' || isFinite(pressedState))) {
_context.next = 21;
break;
}
stredOutput = _objectSpread({}, result, {
output: "".concat(result.output).concat(pressedState),
collected: '',
pending: _toConsumableArray(pending)
});
return _context.abrupt("return", stredOutput);
case 21:
return _context.abrupt("return", pressedState);
case 22:
return _context.abrupt("return", 'NOT RETURNED PROPERLY');
case 23:
return _context.abrupt("return", _objectSpread({}, state, {
collected: "".concat(state.collected || '').concat(_marks["default"].shortTerminator)
}));
case 24:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}()
);
};
exports.shortTermination = shortTermination;
var _default = shortTermination;
exports["default"] = _default;