greybel-interpreter 0.2.0 → 0.2.1

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