greybel-interpreter 0.7.0 → 1.0.0

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 (137) hide show
  1. package/.editorconfig +15 -0
  2. package/.eslintrc +48 -0
  3. package/LICENSE +21 -21
  4. package/README.md +2 -2
  5. package/dist/context.d.ts +107 -98
  6. package/dist/context.js +358 -456
  7. package/dist/cps.d.ts +15 -17
  8. package/dist/cps.js +220 -872
  9. package/dist/handler/error.d.ts +6 -0
  10. package/dist/{operations/debugger.js → handler/error.js} +35 -32
  11. package/dist/handler/output.d.ts +6 -0
  12. package/dist/{operations/if.js → handler/output.js} +35 -31
  13. package/dist/handler/resource.d.ts +12 -0
  14. package/dist/handler/resource.js +51 -0
  15. package/dist/handler-container.d.ts +14 -0
  16. package/dist/handler-container.js +16 -0
  17. package/dist/index.d.ts +44 -36
  18. package/dist/index.js +129 -89
  19. package/dist/interpreter.d.ts +39 -29
  20. package/dist/interpreter.js +292 -210
  21. package/dist/intrinsics-container.d.ts +7 -0
  22. package/dist/intrinsics-container.js +19 -0
  23. package/dist/operations/assign.d.ts +13 -0
  24. package/dist/operations/assign.js +111 -0
  25. package/dist/operations/block.d.ts +12 -0
  26. package/dist/operations/{body.js → block.js} +108 -140
  27. package/dist/operations/break.d.ts +10 -7
  28. package/dist/operations/break.js +41 -34
  29. package/dist/operations/call.d.ts +13 -0
  30. package/dist/operations/call.js +124 -0
  31. package/dist/operations/chunk.d.ts +12 -0
  32. package/dist/operations/chunk.js +85 -0
  33. package/dist/operations/continue.d.ts +10 -7
  34. package/dist/operations/continue.js +41 -34
  35. package/dist/operations/debugger-statement.d.ts +10 -0
  36. package/dist/operations/{else-if.js → debugger-statement.js} +39 -31
  37. package/dist/operations/evaluate.d.ts +39 -0
  38. package/dist/operations/evaluate.js +366 -0
  39. package/dist/operations/for.d.ts +15 -16
  40. package/dist/operations/for.js +146 -143
  41. package/dist/operations/function-reference.d.ts +12 -0
  42. package/dist/operations/function-reference.js +103 -0
  43. package/dist/operations/function.d.ts +12 -26
  44. package/dist/operations/function.js +198 -145
  45. package/dist/operations/if-statement.d.ts +19 -8
  46. package/dist/operations/if-statement.js +181 -169
  47. package/dist/operations/import.d.ts +14 -0
  48. package/dist/{expressions/include.js → operations/import.js} +115 -132
  49. package/dist/operations/include.d.ts +13 -0
  50. package/dist/operations/include.js +95 -0
  51. package/dist/operations/list.d.ts +11 -0
  52. package/dist/{expressions → operations}/list.js +111 -142
  53. package/dist/operations/literal.d.ts +11 -0
  54. package/dist/operations/literal.js +58 -0
  55. package/dist/operations/map.d.ts +11 -0
  56. package/dist/operations/{argument.js → map.js} +167 -145
  57. package/dist/operations/negated-binary.d.ts +11 -0
  58. package/dist/{expressions/binary-negated-expression.js → operations/negated-binary.js} +109 -127
  59. package/dist/operations/new-instance.d.ts +11 -0
  60. package/dist/operations/{new.js → new-instance.js} +100 -97
  61. package/dist/operations/noop.d.ts +9 -0
  62. package/dist/operations/{else.js → noop.js} +36 -30
  63. package/dist/operations/not.d.ts +11 -11
  64. package/dist/operations/not.js +96 -96
  65. package/dist/operations/operation.d.ts +13 -0
  66. package/dist/operations/operation.js +11 -0
  67. package/dist/operations/reference.d.ts +9 -11
  68. package/dist/operations/reference.js +37 -103
  69. package/dist/operations/resolve.d.ts +38 -0
  70. package/dist/operations/resolve.js +330 -0
  71. package/dist/operations/return.d.ts +11 -11
  72. package/dist/operations/return.js +101 -106
  73. package/dist/operations/while.d.ts +13 -14
  74. package/dist/operations/while.js +129 -133
  75. package/dist/types/boolean.d.ts +11 -0
  76. package/dist/{custom-types → types}/boolean.js +46 -47
  77. package/dist/types/default.d.ts +11 -0
  78. package/dist/types/default.js +20 -0
  79. package/dist/types/function.d.ts +32 -0
  80. package/dist/types/function.js +174 -0
  81. package/dist/types/generics.d.ts +17 -0
  82. package/dist/types/{operation.js → generics.js} +40 -38
  83. package/dist/types/interface.d.ts +22 -0
  84. package/dist/types/interface.js +102 -0
  85. package/dist/types/list.d.ts +31 -0
  86. package/dist/types/list.js +227 -0
  87. package/dist/types/map.d.ts +30 -0
  88. package/dist/types/map.js +224 -0
  89. package/dist/types/nil.d.ts +9 -0
  90. package/dist/{custom-types → types}/nil.js +44 -44
  91. package/dist/types/number.d.ts +11 -0
  92. package/dist/{custom-types → types}/number.js +46 -47
  93. package/dist/types/string.d.ts +33 -0
  94. package/dist/types/string.js +149 -0
  95. package/dist/utils/path.d.ts +10 -0
  96. package/dist/utils/path.js +59 -0
  97. package/package.json +58 -44
  98. package/dist/custom-types/boolean.d.ts +0 -12
  99. package/dist/custom-types/list.d.ts +0 -26
  100. package/dist/custom-types/list.js +0 -236
  101. package/dist/custom-types/map.d.ts +0 -26
  102. package/dist/custom-types/map.js +0 -245
  103. package/dist/custom-types/nil.d.ts +0 -10
  104. package/dist/custom-types/number.d.ts +0 -12
  105. package/dist/custom-types/string.d.ts +0 -25
  106. package/dist/custom-types/string.js +0 -172
  107. package/dist/expressions/assign.d.ts +0 -13
  108. package/dist/expressions/assign.js +0 -174
  109. package/dist/expressions/binary-negated-expression.d.ts +0 -18
  110. package/dist/expressions/call.d.ts +0 -14
  111. package/dist/expressions/call.js +0 -209
  112. package/dist/expressions/import.d.ts +0 -14
  113. package/dist/expressions/import.js +0 -155
  114. package/dist/expressions/include.d.ts +0 -13
  115. package/dist/expressions/list.d.ts +0 -14
  116. package/dist/expressions/logical-and-binary.d.ts +0 -22
  117. package/dist/expressions/logical-and-binary.js +0 -222
  118. package/dist/expressions/map.d.ts +0 -19
  119. package/dist/expressions/map.js +0 -197
  120. package/dist/expressions/path.d.ts +0 -31
  121. package/dist/expressions/path.js +0 -384
  122. package/dist/operations/argument.d.ts +0 -8
  123. package/dist/operations/body.d.ts +0 -8
  124. package/dist/operations/debugger.d.ts +0 -7
  125. package/dist/operations/else-if.d.ts +0 -12
  126. package/dist/operations/else.d.ts +0 -10
  127. package/dist/operations/if.d.ts +0 -12
  128. package/dist/operations/new.d.ts +0 -11
  129. package/dist/resource.d.ts +0 -9
  130. package/dist/resource.js +0 -29
  131. package/dist/typer.d.ts +0 -6
  132. package/dist/typer.js +0 -95
  133. package/dist/types/custom-type.d.ts +0 -22
  134. package/dist/types/custom-type.js +0 -67
  135. package/dist/types/expression.d.ts +0 -7
  136. package/dist/types/expression.js +0 -9
  137. package/dist/types/operation.d.ts +0 -10
package/dist/context.js CHANGED
@@ -1,456 +1,358 @@
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 __read = (this && this.__read) || function (o, n) {
18
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
19
+ if (!m) return o;
20
+ var i = m.call(o), r, ar = [], e;
21
+ try {
22
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
+ }
24
+ catch (error) { e = { error: error }; }
25
+ finally {
26
+ try {
27
+ if (r && !r.done && (m = i["return"])) m.call(i);
28
+ }
29
+ finally { if (e) throw e.error; }
30
+ }
31
+ return ar;
32
+ };
33
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
+ if (ar || !(i in from)) {
36
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
+ ar[i] = from[i];
38
+ }
39
+ }
40
+ return to.concat(ar || Array.prototype.slice.call(from));
41
+ };
42
+ var __importDefault = (this && this.__importDefault) || function (mod) {
43
+ return (mod && mod.__esModule) ? mod : { "default": mod };
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
+ var handler_container_1 = __importDefault(require("./handler-container"));
48
+ var default_1 = __importDefault(require("./types/default"));
49
+ var map_1 = __importDefault(require("./types/map"));
50
+ var path_1 = __importDefault(require("./utils/path"));
51
+ var ContextType;
52
+ (function (ContextType) {
53
+ ContextType[ContextType["Api"] = 0] = "Api";
54
+ ContextType[ContextType["Global"] = 1] = "Global";
55
+ ContextType[ContextType["Function"] = 2] = "Function";
56
+ ContextType[ContextType["External"] = 3] = "External";
57
+ ContextType[ContextType["Loop"] = 4] = "Loop";
58
+ ContextType[ContextType["Map"] = 5] = "Map";
59
+ ContextType[ContextType["Call"] = 6] = "Call";
60
+ })(ContextType = exports.ContextType || (exports.ContextType = {}));
61
+ var ContextState;
62
+ (function (ContextState) {
63
+ ContextState[ContextState["Temporary"] = 0] = "Temporary";
64
+ ContextState[ContextState["Default"] = 1] = "Default";
65
+ })(ContextState = exports.ContextState || (exports.ContextState = {}));
66
+ var Scope = /** @class */ (function (_super) {
67
+ __extends(Scope, _super);
68
+ function Scope(context) {
69
+ var _this = _super.call(this) || this;
70
+ _this.context = context;
71
+ return _this;
72
+ }
73
+ Scope.prototype.get = function (path) {
74
+ if (typeof path === 'string') {
75
+ return this.get(new path_1.default([path]));
76
+ }
77
+ if (path.count() === 0) {
78
+ return this;
79
+ }
80
+ var traversalPath = path.clone();
81
+ var current = traversalPath.next();
82
+ if (current === 'locals' || current === 'globals') {
83
+ return this.context.get(traversalPath);
84
+ }
85
+ else if (this.has(path)) {
86
+ return _super.prototype.get.call(this, path);
87
+ }
88
+ else if (this.context.api.scope.has(path)) {
89
+ return this.context.api.scope.get(path);
90
+ }
91
+ else if (path.count() === 1 && map_1.default.getIntrinsics().has(current)) {
92
+ return map_1.default.getIntrinsics().get(current);
93
+ }
94
+ else if (this.context.previous !== null) {
95
+ return this.context.previous.get(path);
96
+ }
97
+ throw new Error("Unknown path ".concat(path.toString(), "."));
98
+ };
99
+ return Scope;
100
+ }(map_1.default));
101
+ exports.Scope = Scope;
102
+ var Debugger = /** @class */ (function () {
103
+ function Debugger() {
104
+ this.breakpoint = false;
105
+ this.nextStep = false;
106
+ /* eslint-disable no-use-before-define */
107
+ this.lastContext = null;
108
+ }
109
+ Debugger.prototype.getLastContext = function () {
110
+ return this.lastContext;
111
+ };
112
+ Debugger.prototype.debug = function () {
113
+ var segments = [];
114
+ for (var _i = 0; _i < arguments.length; _i++) {
115
+ segments[_i] = arguments[_i];
116
+ }
117
+ console.debug.apply(console, __spreadArray([], __read(segments), false));
118
+ return default_1.default.Void;
119
+ };
120
+ Debugger.prototype.setBreakpoint = function (breakpoint) {
121
+ this.breakpoint = breakpoint;
122
+ return this;
123
+ };
124
+ Debugger.prototype.getBreakpoint = function (_ctx) {
125
+ return this.breakpoint;
126
+ };
127
+ Debugger.prototype.next = function () {
128
+ this.nextStep = true;
129
+ return this;
130
+ };
131
+ Debugger.prototype.resume = function () {
132
+ var _this = this;
133
+ if (this.breakpoint) {
134
+ return Promise.resolve();
135
+ }
136
+ return new Promise(function (resolve) {
137
+ var check = function () {
138
+ if (_this.breakpoint) {
139
+ resolve();
140
+ }
141
+ else if (_this.nextStep) {
142
+ _this.nextStep = false;
143
+ resolve();
144
+ }
145
+ else {
146
+ process.nextTick(check);
147
+ }
148
+ };
149
+ check();
150
+ });
151
+ };
152
+ Debugger.prototype.interact = function (ctx, _ast, _op) {
153
+ var me = this;
154
+ console.warn('Debugger is not setup.');
155
+ console.info(ctx);
156
+ me.breakpoint = false;
157
+ };
158
+ return Debugger;
159
+ }());
160
+ exports.Debugger = Debugger;
161
+ var ProcessState = /** @class */ (function () {
162
+ function ProcessState() {
163
+ this.isExit = false;
164
+ this.isPending = false;
165
+ /* eslint-disable no-use-before-define */
166
+ this.last = null;
167
+ }
168
+ return ProcessState;
169
+ }());
170
+ exports.ProcessState = ProcessState;
171
+ var LoopState = /** @class */ (function () {
172
+ function LoopState() {
173
+ this.isBreak = false;
174
+ this.isContinue = false;
175
+ }
176
+ return LoopState;
177
+ }());
178
+ exports.LoopState = LoopState;
179
+ var FunctionState = /** @class */ (function () {
180
+ function FunctionState() {
181
+ this.value = default_1.default.Void;
182
+ this.isReturn = false;
183
+ }
184
+ return FunctionState;
185
+ }());
186
+ exports.FunctionState = FunctionState;
187
+ var OperationContext = /** @class */ (function () {
188
+ function OperationContext(options) {
189
+ if (options === void 0) { options = {}; }
190
+ this.target = options.target || 'unknown';
191
+ this.stackItem = null;
192
+ this.previous = options.previous || null;
193
+ this.type = options.type || ContextType.Api;
194
+ this.state = options.state || ContextState.Default;
195
+ this.scope = new Scope(this);
196
+ this.isProtected = options.isProtected || false;
197
+ this.injected = options.injected || false;
198
+ this.debugger = options.debugger || new Debugger();
199
+ this.handler = options.handler || new handler_container_1.default();
200
+ this.cps = options.cps || null;
201
+ this.processState = options.processState || new ProcessState();
202
+ this.api = this.lookupApi();
203
+ this.globals = this.lookupGlobals();
204
+ this.locals = this.lookupLocals() || this;
205
+ }
206
+ OperationContext.prototype.step = function (op) {
207
+ if (!this.injected) {
208
+ this.stackItem = op.item;
209
+ this.target = op.target || this.target;
210
+ this.setLastActive(this);
211
+ if (this.debugger.getBreakpoint(this)) {
212
+ this.debugger.interact(this, op.item, op);
213
+ return this.debugger.resume();
214
+ }
215
+ }
216
+ return Promise.resolve();
217
+ };
218
+ OperationContext.prototype.setLastActive = function (ctx) {
219
+ if (!ctx.injected) {
220
+ this.processState.last = ctx;
221
+ }
222
+ return this;
223
+ };
224
+ OperationContext.prototype.getLastActive = function () {
225
+ return this.processState.last;
226
+ };
227
+ OperationContext.prototype.isExit = function () {
228
+ return this.processState.isExit;
229
+ };
230
+ OperationContext.prototype.isPending = function () {
231
+ return this.processState.isPending;
232
+ };
233
+ OperationContext.prototype.setPending = function (pending) {
234
+ this.processState.isPending = pending;
235
+ return this;
236
+ };
237
+ OperationContext.prototype.exit = function () {
238
+ var _this = this;
239
+ if (this.processState.isPending) {
240
+ this.processState.isExit = true;
241
+ return new Promise(function (resolve) {
242
+ var check = function () {
243
+ if (!_this.processState.isPending) {
244
+ _this.processState.isExit = false;
245
+ resolve(_this);
246
+ }
247
+ else {
248
+ process.nextTick(check);
249
+ }
250
+ };
251
+ check();
252
+ });
253
+ }
254
+ return Promise.reject(new Error('No running process found.'));
255
+ };
256
+ OperationContext.prototype.lookupType = function (allowedTypes) {
257
+ if (allowedTypes.includes(this.type)) {
258
+ return this;
259
+ }
260
+ var current = this.previous;
261
+ while (current !== null) {
262
+ if (allowedTypes.includes(current.type)) {
263
+ return current;
264
+ }
265
+ current = current.previous;
266
+ }
267
+ return null;
268
+ };
269
+ OperationContext.prototype.lookupApi = function () {
270
+ return this.lookupType(OperationContext.lookupApiType);
271
+ };
272
+ OperationContext.prototype.lookupGlobals = function () {
273
+ return this.lookupType(OperationContext.lookupGlobalsType);
274
+ };
275
+ OperationContext.prototype.lookupLocals = function () {
276
+ return this.lookupType(OperationContext.lookupLocalsType);
277
+ };
278
+ OperationContext.prototype.extend = function (map) {
279
+ var _a;
280
+ if (this.state === ContextState.Temporary) {
281
+ (_a = this.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
282
+ }
283
+ else {
284
+ this.scope.extend(map);
285
+ }
286
+ return this;
287
+ };
288
+ OperationContext.prototype.set = function (path, value) {
289
+ var _a;
290
+ if (typeof path === 'string') {
291
+ this.set(new path_1.default([path]), value);
292
+ return;
293
+ }
294
+ var traversalPath = path.clone();
295
+ var current = traversalPath.next();
296
+ if (current === 'locals') {
297
+ this.locals.set(traversalPath, value);
298
+ }
299
+ else if (current === 'globals') {
300
+ this.globals.set(traversalPath, value);
301
+ }
302
+ else if (this.state === ContextState.Temporary) {
303
+ (_a = this.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
304
+ }
305
+ else {
306
+ this.locals.scope.set(path, value);
307
+ }
308
+ };
309
+ OperationContext.prototype.get = function (path) {
310
+ var _a;
311
+ if (typeof path === 'string') {
312
+ return this.get(new path_1.default([path]));
313
+ }
314
+ var traversalPath = path.clone();
315
+ var current = traversalPath.next();
316
+ if (current === 'locals') {
317
+ return this.locals.get(traversalPath);
318
+ }
319
+ else if (current === 'globals') {
320
+ return this.globals.get(traversalPath);
321
+ }
322
+ else if (this.state === ContextState.Temporary) {
323
+ return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
324
+ }
325
+ return this.locals.scope.get(path);
326
+ };
327
+ OperationContext.prototype.fork = function (options) {
328
+ var newContext = new OperationContext({
329
+ target: options.target || this.target,
330
+ previous: this,
331
+ type: options.type,
332
+ state: options.state,
333
+ isProtected: false,
334
+ injected: this.injected,
335
+ debugger: this.debugger,
336
+ handler: this.handler,
337
+ cps: this.cps,
338
+ processState: this.processState
339
+ });
340
+ if (this.type !== ContextType.Function) {
341
+ if (this.type !== ContextType.Loop) {
342
+ newContext.loopState = this.loopState;
343
+ }
344
+ newContext.functionState = this.functionState;
345
+ }
346
+ return newContext;
347
+ };
348
+ OperationContext.lookupApiType = [ContextType.Api];
349
+ OperationContext.lookupGlobalsType = [
350
+ ContextType.Global
351
+ ];
352
+ OperationContext.lookupLocalsType = [
353
+ ContextType.Global,
354
+ ContextType.Function
355
+ ];
356
+ return OperationContext;
357
+ }());
358
+ exports.default = OperationContext;