greybel-interpreter 0.6.8 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/dist/context.d.ts +98 -98
  4. package/dist/context.js +456 -456
  5. package/dist/cps.d.ts +17 -17
  6. package/dist/cps.js +871 -871
  7. package/dist/custom-types/boolean.d.ts +12 -12
  8. package/dist/custom-types/boolean.js +47 -47
  9. package/dist/custom-types/list.d.ts +26 -26
  10. package/dist/custom-types/list.js +236 -236
  11. package/dist/custom-types/map.d.ts +26 -26
  12. package/dist/custom-types/map.js +245 -245
  13. package/dist/custom-types/nil.d.ts +10 -10
  14. package/dist/custom-types/nil.js +44 -44
  15. package/dist/custom-types/number.d.ts +12 -12
  16. package/dist/custom-types/number.js +47 -47
  17. package/dist/custom-types/string.d.ts +25 -25
  18. package/dist/custom-types/string.js +172 -172
  19. package/dist/expressions/assign.d.ts +13 -13
  20. package/dist/expressions/assign.js +174 -174
  21. package/dist/expressions/binary-negated-expression.d.ts +18 -18
  22. package/dist/expressions/binary-negated-expression.js +127 -127
  23. package/dist/expressions/call.d.ts +14 -14
  24. package/dist/expressions/call.js +208 -208
  25. package/dist/expressions/import.d.ts +14 -14
  26. package/dist/expressions/import.js +155 -155
  27. package/dist/expressions/include.d.ts +13 -13
  28. package/dist/expressions/include.js +132 -132
  29. package/dist/expressions/list.d.ts +14 -14
  30. package/dist/expressions/list.js +134 -134
  31. package/dist/expressions/logical-and-binary.d.ts +22 -22
  32. package/dist/expressions/logical-and-binary.js +222 -222
  33. package/dist/expressions/map.d.ts +19 -19
  34. package/dist/expressions/map.js +197 -197
  35. package/dist/expressions/path.d.ts +31 -31
  36. package/dist/expressions/path.js +383 -383
  37. package/dist/index.d.ts +36 -36
  38. package/dist/index.js +89 -89
  39. package/dist/interpreter.d.ts +29 -29
  40. package/dist/interpreter.js +210 -210
  41. package/dist/operations/argument.d.ts +8 -8
  42. package/dist/operations/argument.js +145 -145
  43. package/dist/operations/body.d.ts +8 -8
  44. package/dist/operations/body.js +140 -140
  45. package/dist/operations/break.d.ts +7 -7
  46. package/dist/operations/break.js +34 -34
  47. package/dist/operations/continue.d.ts +7 -7
  48. package/dist/operations/continue.js +34 -34
  49. package/dist/operations/debugger.d.ts +7 -7
  50. package/dist/operations/debugger.js +32 -32
  51. package/dist/operations/else-if.d.ts +12 -12
  52. package/dist/operations/else-if.js +31 -31
  53. package/dist/operations/else.d.ts +10 -10
  54. package/dist/operations/else.js +30 -30
  55. package/dist/operations/for.d.ts +16 -16
  56. package/dist/operations/for.js +143 -143
  57. package/dist/operations/function.d.ts +26 -24
  58. package/dist/operations/function.js +145 -142
  59. package/dist/operations/if-statement.d.ts +8 -8
  60. package/dist/operations/if-statement.js +169 -169
  61. package/dist/operations/if.d.ts +12 -12
  62. package/dist/operations/if.js +31 -31
  63. package/dist/operations/new.d.ts +11 -11
  64. package/dist/operations/new.js +96 -96
  65. package/dist/operations/not.d.ts +11 -11
  66. package/dist/operations/not.js +95 -95
  67. package/dist/operations/reference.d.ts +11 -11
  68. package/dist/operations/reference.js +102 -102
  69. package/dist/operations/return.d.ts +11 -11
  70. package/dist/operations/return.js +106 -106
  71. package/dist/operations/while.d.ts +14 -14
  72. package/dist/operations/while.js +133 -133
  73. package/dist/resource.d.ts +9 -9
  74. package/dist/resource.js +29 -29
  75. package/dist/typer.d.ts +6 -6
  76. package/dist/typer.js +95 -95
  77. package/dist/types/custom-type.d.ts +22 -22
  78. package/dist/types/custom-type.js +67 -67
  79. package/dist/types/expression.d.ts +7 -7
  80. package/dist/types/expression.js +9 -9
  81. package/dist/types/operation.d.ts +10 -10
  82. package/dist/types/operation.js +38 -38
  83. package/package.json +44 -45
package/dist/context.js CHANGED
@@ -1,456 +1,456 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- var __read = (this && this.__read) || function (o, n) {
54
- var m = typeof Symbol === "function" && o[Symbol.iterator];
55
- if (!m) return o;
56
- var i = m.call(o), r, ar = [], e;
57
- try {
58
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
59
- }
60
- catch (error) { e = { error: error }; }
61
- finally {
62
- try {
63
- if (r && !r.done && (m = i["return"])) m.call(i);
64
- }
65
- finally { if (e) throw e.error; }
66
- }
67
- return ar;
68
- };
69
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
70
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
- if (ar || !(i in from)) {
72
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
- ar[i] = from[i];
74
- }
75
- }
76
- return to.concat(ar || Array.prototype.slice.call(from));
77
- };
78
- var __importDefault = (this && this.__importDefault) || function (mod) {
79
- return (mod && mod.__esModule) ? mod : { "default": mod };
80
- };
81
- Object.defineProperty(exports, "__esModule", { value: true });
82
- exports.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
83
- var map_1 = __importDefault(require("./custom-types/map"));
84
- var ContextType;
85
- (function (ContextType) {
86
- ContextType["API"] = "API";
87
- ContextType["GLOBAL"] = "GLOBAL";
88
- ContextType["FUNCTION"] = "FUNCTION";
89
- ContextType["EXTERNAL"] = "EXTERNAL";
90
- ContextType["LOOP"] = "LOOP";
91
- ContextType["MAP"] = "MAP";
92
- ContextType["CALL"] = "CALL";
93
- })(ContextType = exports.ContextType || (exports.ContextType = {}));
94
- var ContextState;
95
- (function (ContextState) {
96
- ContextState["TEMPORARY"] = "TEMPORARY";
97
- ContextState["DEFAULT"] = "DEFAULT";
98
- })(ContextState = exports.ContextState || (exports.ContextState = {}));
99
- var Scope = /** @class */ (function (_super) {
100
- __extends(Scope, _super);
101
- function Scope(context) {
102
- var _this = _super.call(this) || this;
103
- _this.context = context;
104
- return _this;
105
- }
106
- Scope.prototype.get = function (path) {
107
- return __awaiter(this, void 0, void 0, function () {
108
- var me, traversalPath, current;
109
- return __generator(this, function (_a) {
110
- switch (_a.label) {
111
- case 0:
112
- me = this;
113
- traversalPath = [].concat(path);
114
- current = traversalPath.shift();
115
- if (path.length === 0) {
116
- return [2 /*return*/, Promise.resolve(me)];
117
- }
118
- if (!(current === 'locals' || current === 'globals')) return [3 /*break*/, 1];
119
- return [2 /*return*/, me.context.get(path)];
120
- case 1: return [4 /*yield*/, me.has(path)];
121
- case 2:
122
- if (!_a.sent()) return [3 /*break*/, 3];
123
- return [2 /*return*/, _super.prototype.get.call(this, path)];
124
- case 3: return [4 /*yield*/, me.context.api.scope.has(path)];
125
- case 4:
126
- if (_a.sent()) {
127
- return [2 /*return*/, me.context.api.get(path)];
128
- }
129
- else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
130
- return [2 /*return*/, Promise.resolve(map_1.default.intrinsics.get(current).bind(null, me))];
131
- }
132
- else if (me.context.previous) {
133
- return [2 /*return*/, me.context.previous.get(path)];
134
- }
135
- else {
136
- throw new Error("Cannot get path ".concat(path.join('.')));
137
- }
138
- _a.label = 5;
139
- case 5: return [2 /*return*/];
140
- }
141
- });
142
- });
143
- };
144
- Scope.prototype.getCallable = function (path) {
145
- return __awaiter(this, void 0, void 0, function () {
146
- var me, traversalPath, current;
147
- return __generator(this, function (_a) {
148
- switch (_a.label) {
149
- case 0:
150
- me = this;
151
- traversalPath = [].concat(path);
152
- current = traversalPath.shift();
153
- if (current === 'locals' || current === 'globals') {
154
- return [2 /*return*/, me.context.getCallable(path)];
155
- }
156
- return [4 /*yield*/, me.has(path)];
157
- case 1:
158
- if (!_a.sent()) return [3 /*break*/, 2];
159
- return [2 /*return*/, _super.prototype.getCallable.call(this, path)];
160
- case 2: return [4 /*yield*/, me.context.api.scope.has(path)];
161
- case 3:
162
- if (_a.sent()) {
163
- return [2 /*return*/, me.context.api.getCallable(path)];
164
- }
165
- else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
166
- return [2 /*return*/, Promise.resolve({
167
- origin: map_1.default.intrinsics.get(current).bind(null, me),
168
- context: me
169
- })];
170
- }
171
- else if (me.context.previous) {
172
- return [2 /*return*/, me.context.previous.getCallable(path)];
173
- }
174
- else {
175
- throw new Error("Cannot get callable path ".concat(path.join('.')));
176
- }
177
- _a.label = 4;
178
- case 4: return [2 /*return*/];
179
- }
180
- });
181
- });
182
- };
183
- return Scope;
184
- }(map_1.default));
185
- exports.Scope = Scope;
186
- var Debugger = /** @class */ (function () {
187
- function Debugger() {
188
- var me = this;
189
- me.breakpoint = false;
190
- me.nextStep = false;
191
- me.lastContext = null;
192
- }
193
- Debugger.prototype.raise = function (message) {
194
- var args = [];
195
- for (var _i = 1; _i < arguments.length; _i++) {
196
- args[_i - 1] = arguments[_i];
197
- }
198
- throw new Error(message);
199
- };
200
- Debugger.prototype.debug = function (message) {
201
- var args = [];
202
- for (var _i = 1; _i < arguments.length; _i++) {
203
- args[_i - 1] = arguments[_i];
204
- }
205
- console.info.apply(console, __spreadArray([message], __read(args), false));
206
- };
207
- Debugger.prototype.setBreakpoint = function (state) {
208
- var me = this;
209
- me.breakpoint = state;
210
- return me;
211
- };
212
- Debugger.prototype.getBreakpoint = function (operationContext) {
213
- return this.breakpoint;
214
- };
215
- Debugger.prototype.next = function () {
216
- var me = this;
217
- me.nextStep = true;
218
- return me;
219
- };
220
- Debugger.prototype.resume = function () {
221
- var me = this;
222
- if (!me.breakpoint) {
223
- return Promise.resolve();
224
- }
225
- return new Promise(function (resolve) {
226
- var check = function () {
227
- if (!me.breakpoint) {
228
- resolve();
229
- }
230
- else if (me.nextStep) {
231
- me.nextStep = false;
232
- resolve();
233
- }
234
- else {
235
- setTimeout(check, 10);
236
- }
237
- };
238
- setTimeout(check, 10);
239
- });
240
- };
241
- Debugger.prototype.interact = function (operationContext, item, entity) {
242
- var me = this;
243
- console.warn("Debugger is not setup.");
244
- console.info(operationContext);
245
- me.breakpoint = false;
246
- };
247
- return Debugger;
248
- }());
249
- exports.Debugger = Debugger;
250
- var OperationContext = /** @class */ (function () {
251
- function OperationContext(options) {
252
- var me = this;
253
- me.target = options.target || 'unknown';
254
- me.stackItem = null;
255
- me.previous = options.previous || null;
256
- me.type = options.type || ContextType.API;
257
- me.state = options.state || ContextState.DEFAULT;
258
- me.scope = new Scope(me);
259
- me.isProtected = options.isProtected || false;
260
- me.injected = options.injected || false;
261
- me.memory = new Map();
262
- me.debugger = options.debugger || new Debugger();
263
- me.cps = options.cps;
264
- me.processState = options.processState || {
265
- exit: false,
266
- pending: false,
267
- last: null
268
- };
269
- me.api = me.lookupAPI();
270
- me.globals = me.lookupGlobals();
271
- me.locals = me.lookupLocals() || me;
272
- }
273
- OperationContext.prototype.step = function (entity) {
274
- var me = this;
275
- var dbgr = me.debugger;
276
- if (!me.injected) {
277
- me.stackItem = entity.ast;
278
- me.target = entity.target || me.target;
279
- me.setLastActive(me);
280
- if (dbgr.getBreakpoint(me)) {
281
- dbgr.interact(me, entity.ast, entity);
282
- return dbgr.resume();
283
- }
284
- }
285
- return Promise.resolve();
286
- };
287
- OperationContext.prototype.setLastActive = function (opc) {
288
- var me = this;
289
- if (!opc.injected) {
290
- me.processState.last = opc;
291
- }
292
- return me;
293
- };
294
- OperationContext.prototype.getLastActive = function () {
295
- return this.processState.last;
296
- };
297
- OperationContext.prototype.isExit = function () {
298
- return this.processState.exit;
299
- };
300
- OperationContext.prototype.exit = function () {
301
- var me = this;
302
- var state = me.processState;
303
- if (state.pending) {
304
- state.exit = true;
305
- return new Promise(function (resolve) {
306
- var check = function () {
307
- if (!state.pending) {
308
- state.exit = false;
309
- resolve(me);
310
- }
311
- else {
312
- setTimeout(check, 10);
313
- }
314
- };
315
- setTimeout(check, 10);
316
- });
317
- }
318
- return Promise.reject(new Error('No running process found.'));
319
- };
320
- OperationContext.prototype.isPending = function () {
321
- return this.processState.pending;
322
- };
323
- OperationContext.prototype.setPending = function (pending) {
324
- var me = this;
325
- me.processState.pending = pending;
326
- return me;
327
- };
328
- OperationContext.prototype.lookupAllOfType = function (validate) {
329
- var me = this;
330
- var result = [];
331
- if (validate(me.type)) {
332
- result.push(me);
333
- }
334
- var current = me.previous;
335
- while (current) {
336
- if (validate(current.type)) {
337
- result.push(current);
338
- }
339
- current = current.previous;
340
- }
341
- return result;
342
- };
343
- OperationContext.prototype.lookupType = function (validate) {
344
- var me = this;
345
- if (validate(me.type)) {
346
- return me;
347
- }
348
- var current = me.previous;
349
- while (current) {
350
- if (validate(current.type)) {
351
- return current;
352
- }
353
- current = current.previous;
354
- }
355
- return null;
356
- };
357
- OperationContext.prototype.lookupAllScopes = function () {
358
- return this.lookupAllOfType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
359
- };
360
- OperationContext.prototype.lookupAPI = function () {
361
- return this.lookupType(function (type) { return [ContextType.API].includes(type); });
362
- };
363
- OperationContext.prototype.lookupGlobals = function () {
364
- return this.lookupType(function (type) { return [ContextType.GLOBAL].includes(type); });
365
- };
366
- OperationContext.prototype.lookupLocals = function () {
367
- return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
368
- };
369
- OperationContext.prototype.extend = function (map) {
370
- var _a;
371
- var me = this;
372
- if (me.state === ContextState.TEMPORARY) {
373
- (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
374
- }
375
- else {
376
- me.scope.extend(map);
377
- }
378
- return me;
379
- };
380
- OperationContext.prototype.set = function (path, value) {
381
- var _a;
382
- var me = this;
383
- var traversalPath = [].concat(path);
384
- var current = traversalPath.shift();
385
- if (current === 'locals') {
386
- return me.locals.set(traversalPath, value);
387
- }
388
- else if (current === 'globals') {
389
- return me.globals.set(traversalPath, value);
390
- }
391
- if (me.state === ContextState.TEMPORARY) {
392
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
393
- }
394
- return me.locals.scope.set(path, value);
395
- };
396
- OperationContext.prototype.get = function (path) {
397
- var _a;
398
- var me = this;
399
- var traversalPath = [].concat(path);
400
- var current = traversalPath.shift();
401
- if (current === 'locals') {
402
- return me.locals.get(traversalPath);
403
- }
404
- else if (current === 'globals') {
405
- return me.globals.get(traversalPath);
406
- }
407
- else if (me.state === ContextState.TEMPORARY) {
408
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
409
- }
410
- return me.locals.scope.get(path);
411
- };
412
- OperationContext.prototype.getCallable = function (path) {
413
- var me = this;
414
- var traversalPath = [].concat(path);
415
- var current = traversalPath.shift();
416
- if (current === 'locals') {
417
- return me.locals.getCallable(traversalPath);
418
- }
419
- else if (current === 'globals') {
420
- return me.globals.getCallable(traversalPath);
421
- }
422
- return me.locals.scope.getCallable(path);
423
- };
424
- OperationContext.prototype.setMemory = function (key, value) {
425
- var me = this;
426
- me.memory.set(key, value);
427
- return me;
428
- };
429
- OperationContext.prototype.getMemory = function (key) {
430
- var me = this;
431
- return me.memory.get(key);
432
- };
433
- OperationContext.prototype.fork = function (_a) {
434
- var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
435
- var me = this;
436
- var opc = new OperationContext({
437
- target: target || me.target,
438
- previous: me,
439
- type: type,
440
- state: state,
441
- debugger: me.debugger,
442
- cps: me.cps,
443
- processState: me.processState,
444
- injected: injected || me.injected
445
- });
446
- if (type !== ContextType.FUNCTION) {
447
- if (type !== ContextType.LOOP) {
448
- opc.setMemory('loopContext', me.getMemory('loopContext'));
449
- }
450
- opc.setMemory('functionContext', me.getMemory('functionContext'));
451
- }
452
- return opc;
453
- };
454
- return OperationContext;
455
- }());
456
- exports.OperationContext = OperationContext;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __read = (this && this.__read) || function (o, n) {
54
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
55
+ if (!m) return o;
56
+ var i = m.call(o), r, ar = [], e;
57
+ try {
58
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
59
+ }
60
+ catch (error) { e = { error: error }; }
61
+ finally {
62
+ try {
63
+ if (r && !r.done && (m = i["return"])) m.call(i);
64
+ }
65
+ finally { if (e) throw e.error; }
66
+ }
67
+ return ar;
68
+ };
69
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
70
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
+ if (ar || !(i in from)) {
72
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
+ ar[i] = from[i];
74
+ }
75
+ }
76
+ return to.concat(ar || Array.prototype.slice.call(from));
77
+ };
78
+ var __importDefault = (this && this.__importDefault) || function (mod) {
79
+ return (mod && mod.__esModule) ? mod : { "default": mod };
80
+ };
81
+ Object.defineProperty(exports, "__esModule", { value: true });
82
+ exports.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
83
+ var map_1 = __importDefault(require("./custom-types/map"));
84
+ var ContextType;
85
+ (function (ContextType) {
86
+ ContextType["API"] = "API";
87
+ ContextType["GLOBAL"] = "GLOBAL";
88
+ ContextType["FUNCTION"] = "FUNCTION";
89
+ ContextType["EXTERNAL"] = "EXTERNAL";
90
+ ContextType["LOOP"] = "LOOP";
91
+ ContextType["MAP"] = "MAP";
92
+ ContextType["CALL"] = "CALL";
93
+ })(ContextType = exports.ContextType || (exports.ContextType = {}));
94
+ var ContextState;
95
+ (function (ContextState) {
96
+ ContextState["TEMPORARY"] = "TEMPORARY";
97
+ ContextState["DEFAULT"] = "DEFAULT";
98
+ })(ContextState = exports.ContextState || (exports.ContextState = {}));
99
+ var Scope = /** @class */ (function (_super) {
100
+ __extends(Scope, _super);
101
+ function Scope(context) {
102
+ var _this = _super.call(this) || this;
103
+ _this.context = context;
104
+ return _this;
105
+ }
106
+ Scope.prototype.get = function (path) {
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var me, traversalPath, current;
109
+ return __generator(this, function (_a) {
110
+ switch (_a.label) {
111
+ case 0:
112
+ me = this;
113
+ traversalPath = [].concat(path);
114
+ current = traversalPath.shift();
115
+ if (path.length === 0) {
116
+ return [2 /*return*/, Promise.resolve(me)];
117
+ }
118
+ if (!(current === 'locals' || current === 'globals')) return [3 /*break*/, 1];
119
+ return [2 /*return*/, me.context.get(path)];
120
+ case 1: return [4 /*yield*/, me.has(path)];
121
+ case 2:
122
+ if (!_a.sent()) return [3 /*break*/, 3];
123
+ return [2 /*return*/, _super.prototype.get.call(this, path)];
124
+ case 3: return [4 /*yield*/, me.context.api.scope.has(path)];
125
+ case 4:
126
+ if (_a.sent()) {
127
+ return [2 /*return*/, me.context.api.get(path)];
128
+ }
129
+ else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
130
+ return [2 /*return*/, Promise.resolve(map_1.default.intrinsics.get(current).bind(null, me))];
131
+ }
132
+ else if (me.context.previous) {
133
+ return [2 /*return*/, me.context.previous.get(path)];
134
+ }
135
+ else {
136
+ throw new Error("Cannot get path ".concat(path.join('.')));
137
+ }
138
+ _a.label = 5;
139
+ case 5: return [2 /*return*/];
140
+ }
141
+ });
142
+ });
143
+ };
144
+ Scope.prototype.getCallable = function (path) {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var me, traversalPath, current;
147
+ return __generator(this, function (_a) {
148
+ switch (_a.label) {
149
+ case 0:
150
+ me = this;
151
+ traversalPath = [].concat(path);
152
+ current = traversalPath.shift();
153
+ if (current === 'locals' || current === 'globals') {
154
+ return [2 /*return*/, me.context.getCallable(path)];
155
+ }
156
+ return [4 /*yield*/, me.has(path)];
157
+ case 1:
158
+ if (!_a.sent()) return [3 /*break*/, 2];
159
+ return [2 /*return*/, _super.prototype.getCallable.call(this, path)];
160
+ case 2: return [4 /*yield*/, me.context.api.scope.has(path)];
161
+ case 3:
162
+ if (_a.sent()) {
163
+ return [2 /*return*/, me.context.api.getCallable(path)];
164
+ }
165
+ else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
166
+ return [2 /*return*/, Promise.resolve({
167
+ origin: map_1.default.intrinsics.get(current).bind(null, me),
168
+ context: me
169
+ })];
170
+ }
171
+ else if (me.context.previous) {
172
+ return [2 /*return*/, me.context.previous.getCallable(path)];
173
+ }
174
+ else {
175
+ throw new Error("Cannot get callable path ".concat(path.join('.')));
176
+ }
177
+ _a.label = 4;
178
+ case 4: return [2 /*return*/];
179
+ }
180
+ });
181
+ });
182
+ };
183
+ return Scope;
184
+ }(map_1.default));
185
+ exports.Scope = Scope;
186
+ var Debugger = /** @class */ (function () {
187
+ function Debugger() {
188
+ var me = this;
189
+ me.breakpoint = false;
190
+ me.nextStep = false;
191
+ me.lastContext = null;
192
+ }
193
+ Debugger.prototype.raise = function (message) {
194
+ var args = [];
195
+ for (var _i = 1; _i < arguments.length; _i++) {
196
+ args[_i - 1] = arguments[_i];
197
+ }
198
+ throw new Error(message);
199
+ };
200
+ Debugger.prototype.debug = function (message) {
201
+ var args = [];
202
+ for (var _i = 1; _i < arguments.length; _i++) {
203
+ args[_i - 1] = arguments[_i];
204
+ }
205
+ console.info.apply(console, __spreadArray([message], __read(args), false));
206
+ };
207
+ Debugger.prototype.setBreakpoint = function (state) {
208
+ var me = this;
209
+ me.breakpoint = state;
210
+ return me;
211
+ };
212
+ Debugger.prototype.getBreakpoint = function (operationContext) {
213
+ return this.breakpoint;
214
+ };
215
+ Debugger.prototype.next = function () {
216
+ var me = this;
217
+ me.nextStep = true;
218
+ return me;
219
+ };
220
+ Debugger.prototype.resume = function () {
221
+ var me = this;
222
+ if (!me.breakpoint) {
223
+ return Promise.resolve();
224
+ }
225
+ return new Promise(function (resolve) {
226
+ var check = function () {
227
+ if (!me.breakpoint) {
228
+ resolve();
229
+ }
230
+ else if (me.nextStep) {
231
+ me.nextStep = false;
232
+ resolve();
233
+ }
234
+ else {
235
+ setTimeout(check, 10);
236
+ }
237
+ };
238
+ setTimeout(check, 10);
239
+ });
240
+ };
241
+ Debugger.prototype.interact = function (operationContext, item, entity) {
242
+ var me = this;
243
+ console.warn("Debugger is not setup.");
244
+ console.info(operationContext);
245
+ me.breakpoint = false;
246
+ };
247
+ return Debugger;
248
+ }());
249
+ exports.Debugger = Debugger;
250
+ var OperationContext = /** @class */ (function () {
251
+ function OperationContext(options) {
252
+ var me = this;
253
+ me.target = options.target || 'unknown';
254
+ me.stackItem = null;
255
+ me.previous = options.previous || null;
256
+ me.type = options.type || ContextType.API;
257
+ me.state = options.state || ContextState.DEFAULT;
258
+ me.scope = new Scope(me);
259
+ me.isProtected = options.isProtected || false;
260
+ me.injected = options.injected || false;
261
+ me.memory = new Map();
262
+ me.debugger = options.debugger || new Debugger();
263
+ me.cps = options.cps;
264
+ me.processState = options.processState || {
265
+ exit: false,
266
+ pending: false,
267
+ last: null
268
+ };
269
+ me.api = me.lookupAPI();
270
+ me.globals = me.lookupGlobals();
271
+ me.locals = me.lookupLocals() || me;
272
+ }
273
+ OperationContext.prototype.step = function (entity) {
274
+ var me = this;
275
+ var dbgr = me.debugger;
276
+ if (!me.injected) {
277
+ me.stackItem = entity.ast;
278
+ me.target = entity.target || me.target;
279
+ me.setLastActive(me);
280
+ if (dbgr.getBreakpoint(me)) {
281
+ dbgr.interact(me, entity.ast, entity);
282
+ return dbgr.resume();
283
+ }
284
+ }
285
+ return Promise.resolve();
286
+ };
287
+ OperationContext.prototype.setLastActive = function (opc) {
288
+ var me = this;
289
+ if (!opc.injected) {
290
+ me.processState.last = opc;
291
+ }
292
+ return me;
293
+ };
294
+ OperationContext.prototype.getLastActive = function () {
295
+ return this.processState.last;
296
+ };
297
+ OperationContext.prototype.isExit = function () {
298
+ return this.processState.exit;
299
+ };
300
+ OperationContext.prototype.exit = function () {
301
+ var me = this;
302
+ var state = me.processState;
303
+ if (state.pending) {
304
+ state.exit = true;
305
+ return new Promise(function (resolve) {
306
+ var check = function () {
307
+ if (!state.pending) {
308
+ state.exit = false;
309
+ resolve(me);
310
+ }
311
+ else {
312
+ setTimeout(check, 10);
313
+ }
314
+ };
315
+ setTimeout(check, 10);
316
+ });
317
+ }
318
+ return Promise.reject(new Error('No running process found.'));
319
+ };
320
+ OperationContext.prototype.isPending = function () {
321
+ return this.processState.pending;
322
+ };
323
+ OperationContext.prototype.setPending = function (pending) {
324
+ var me = this;
325
+ me.processState.pending = pending;
326
+ return me;
327
+ };
328
+ OperationContext.prototype.lookupAllOfType = function (validate) {
329
+ var me = this;
330
+ var result = [];
331
+ if (validate(me.type)) {
332
+ result.push(me);
333
+ }
334
+ var current = me.previous;
335
+ while (current) {
336
+ if (validate(current.type)) {
337
+ result.push(current);
338
+ }
339
+ current = current.previous;
340
+ }
341
+ return result;
342
+ };
343
+ OperationContext.prototype.lookupType = function (validate) {
344
+ var me = this;
345
+ if (validate(me.type)) {
346
+ return me;
347
+ }
348
+ var current = me.previous;
349
+ while (current) {
350
+ if (validate(current.type)) {
351
+ return current;
352
+ }
353
+ current = current.previous;
354
+ }
355
+ return null;
356
+ };
357
+ OperationContext.prototype.lookupAllScopes = function () {
358
+ return this.lookupAllOfType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
359
+ };
360
+ OperationContext.prototype.lookupAPI = function () {
361
+ return this.lookupType(function (type) { return [ContextType.API].includes(type); });
362
+ };
363
+ OperationContext.prototype.lookupGlobals = function () {
364
+ return this.lookupType(function (type) { return [ContextType.GLOBAL].includes(type); });
365
+ };
366
+ OperationContext.prototype.lookupLocals = function () {
367
+ return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
368
+ };
369
+ OperationContext.prototype.extend = function (map) {
370
+ var _a;
371
+ var me = this;
372
+ if (me.state === ContextState.TEMPORARY) {
373
+ (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
374
+ }
375
+ else {
376
+ me.scope.extend(map);
377
+ }
378
+ return me;
379
+ };
380
+ OperationContext.prototype.set = function (path, value) {
381
+ var _a;
382
+ var me = this;
383
+ var traversalPath = [].concat(path);
384
+ var current = traversalPath.shift();
385
+ if (current === 'locals') {
386
+ return me.locals.set(traversalPath, value);
387
+ }
388
+ else if (current === 'globals') {
389
+ return me.globals.set(traversalPath, value);
390
+ }
391
+ if (me.state === ContextState.TEMPORARY) {
392
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
393
+ }
394
+ return me.locals.scope.set(path, value);
395
+ };
396
+ OperationContext.prototype.get = function (path) {
397
+ var _a;
398
+ var me = this;
399
+ var traversalPath = [].concat(path);
400
+ var current = traversalPath.shift();
401
+ if (current === 'locals') {
402
+ return me.locals.get(traversalPath);
403
+ }
404
+ else if (current === 'globals') {
405
+ return me.globals.get(traversalPath);
406
+ }
407
+ else if (me.state === ContextState.TEMPORARY) {
408
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
409
+ }
410
+ return me.locals.scope.get(path);
411
+ };
412
+ OperationContext.prototype.getCallable = function (path) {
413
+ var me = this;
414
+ var traversalPath = [].concat(path);
415
+ var current = traversalPath.shift();
416
+ if (current === 'locals') {
417
+ return me.locals.getCallable(traversalPath);
418
+ }
419
+ else if (current === 'globals') {
420
+ return me.globals.getCallable(traversalPath);
421
+ }
422
+ return me.locals.scope.getCallable(path);
423
+ };
424
+ OperationContext.prototype.setMemory = function (key, value) {
425
+ var me = this;
426
+ me.memory.set(key, value);
427
+ return me;
428
+ };
429
+ OperationContext.prototype.getMemory = function (key) {
430
+ var me = this;
431
+ return me.memory.get(key);
432
+ };
433
+ OperationContext.prototype.fork = function (_a) {
434
+ var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
435
+ var me = this;
436
+ var opc = new OperationContext({
437
+ target: target || me.target,
438
+ previous: me,
439
+ type: type,
440
+ state: state,
441
+ debugger: me.debugger,
442
+ cps: me.cps,
443
+ processState: me.processState,
444
+ injected: injected || me.injected
445
+ });
446
+ if (type !== ContextType.FUNCTION) {
447
+ if (type !== ContextType.LOOP) {
448
+ opc.setMemory('loopContext', me.getMemory('loopContext'));
449
+ }
450
+ opc.setMemory('functionContext', me.getMemory('functionContext'));
451
+ }
452
+ return opc;
453
+ };
454
+ return OperationContext;
455
+ }());
456
+ exports.OperationContext = OperationContext;