greybel-interpreter 0.1.0 → 0.1.4

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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/dist/context.d.ts +68 -68
  4. package/dist/context.js +399 -399
  5. package/dist/cps.d.ts +15 -15
  6. package/dist/cps.js +438 -438
  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 -281
  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 -132
  19. package/dist/expressions/assign.d.ts +12 -12
  20. package/dist/expressions/assign.js +156 -156
  21. package/dist/expressions/binary-negated-expression.d.ts +18 -18
  22. package/dist/expressions/binary-negated-expression.js +115 -115
  23. package/dist/expressions/call.d.ts +13 -13
  24. package/dist/expressions/call.js +177 -177
  25. package/dist/expressions/list.d.ts +13 -13
  26. package/dist/expressions/list.js +119 -119
  27. package/dist/expressions/logical-and-binary.d.ts +22 -22
  28. package/dist/expressions/logical-and-binary.js +192 -188
  29. package/dist/expressions/map.d.ts +18 -18
  30. package/dist/expressions/map.js +171 -171
  31. package/dist/expressions/path.d.ts +30 -30
  32. package/dist/expressions/path.js +310 -310
  33. package/dist/index.d.ts +37 -35
  34. package/dist/index.js +91 -82
  35. package/dist/interpreter.d.ts +20 -20
  36. package/dist/interpreter.js +55 -55
  37. package/dist/operations/argument.d.ts +8 -8
  38. package/dist/operations/argument.js +129 -129
  39. package/dist/operations/body.d.ts +8 -8
  40. package/dist/operations/body.js +144 -144
  41. package/dist/operations/break.d.ts +7 -7
  42. package/dist/operations/break.js +34 -34
  43. package/dist/operations/continue.d.ts +7 -7
  44. package/dist/operations/continue.js +34 -34
  45. package/dist/operations/debugger.d.ts +7 -7
  46. package/dist/operations/debugger.js +32 -32
  47. package/dist/operations/else-if.d.ts +12 -12
  48. package/dist/operations/else-if.js +31 -31
  49. package/dist/operations/else.d.ts +10 -10
  50. package/dist/operations/else.js +30 -30
  51. package/dist/operations/for.d.ts +16 -16
  52. package/dist/operations/for.js +139 -139
  53. package/dist/operations/function.d.ts +21 -21
  54. package/dist/operations/function.js +125 -125
  55. package/dist/operations/if-statement.d.ts +8 -8
  56. package/dist/operations/if-statement.js +138 -138
  57. package/dist/operations/if.d.ts +12 -12
  58. package/dist/operations/if.js +31 -31
  59. package/dist/operations/new.d.ts +11 -11
  60. package/dist/operations/new.js +96 -96
  61. package/dist/operations/not.d.ts +11 -11
  62. package/dist/operations/not.js +95 -95
  63. package/dist/operations/reference.d.ts +11 -11
  64. package/dist/operations/reference.js +102 -102
  65. package/dist/operations/return.d.ts +11 -11
  66. package/dist/operations/return.js +102 -102
  67. package/dist/operations/top.d.ts +12 -12
  68. package/dist/operations/top.js +84 -84
  69. package/dist/operations/while.d.ts +14 -14
  70. package/dist/operations/while.js +123 -123
  71. package/dist/resource.d.ts +9 -9
  72. package/dist/resource.js +29 -29
  73. package/dist/typer.d.ts +6 -6
  74. package/dist/typer.js +91 -99
  75. package/dist/types/custom-type.d.ts +19 -19
  76. package/dist/types/custom-type.js +58 -58
  77. package/dist/types/expression.d.ts +6 -6
  78. package/dist/types/expression.js +9 -9
  79. package/dist/types/operation.d.ts +9 -9
  80. package/dist/types/operation.js +38 -38
  81. package/package.json +45 -45
package/dist/context.js CHANGED
@@ -1,399 +1,399 @@
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, 3, , 4]);
284
- parser = new greybel_core_1.Parser(code);
285
- chunk = parser.parseChunk();
286
- item = me.lastContext.cps.visit(chunk);
287
- context = me.lastContext.fork(ContextType.INJECTION, ContextState.TEMPORARY);
288
- return [4 /*yield*/, item.run(context)];
289
- case 2:
290
- _a.sent();
291
- return [3 /*break*/, 4];
292
- case 3:
293
- err_1 = _a.sent();
294
- console.error(err_1);
295
- return [3 /*break*/, 4];
296
- case 4: return [2 /*return*/];
297
- }
298
- });
299
- });
300
- };
301
- return Debugger;
302
- }());
303
- exports.Debugger = Debugger;
304
- var OperationContext = /** @class */ (function () {
305
- function OperationContext(options) {
306
- var me = this;
307
- me.previous = options.previous || null;
308
- me.type = options.type || ContextType.API;
309
- me.state = options.state || ContextState.DEFAULT;
310
- me.scope = new Scope(me);
311
- me.isProtected = options.isProtected || false;
312
- me.memory = new Map();
313
- me.debugger = options.debugger || new Debugger();
314
- me.cps = options.cps;
315
- }
316
- OperationContext.prototype.valueOf = function () {
317
- return this.scope.valueOf();
318
- };
319
- OperationContext.prototype.extend = function (map) {
320
- var _a;
321
- var me = this;
322
- if (me.state === ContextState.TEMPORARY) {
323
- (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
324
- }
325
- else {
326
- me.scope.extend(map);
327
- }
328
- return me;
329
- };
330
- OperationContext.prototype.set = function (path, value) {
331
- var _a;
332
- return __awaiter(this, void 0, void 0, function () {
333
- var me;
334
- return __generator(this, function (_b) {
335
- switch (_b.label) {
336
- case 0:
337
- me = this;
338
- if (!(me.state === ContextState.TEMPORARY)) return [3 /*break*/, 2];
339
- return [4 /*yield*/, ((_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value))];
340
- case 1:
341
- _b.sent();
342
- return [3 /*break*/, 4];
343
- case 2: return [4 /*yield*/, me.scope.set(path, value)];
344
- case 3:
345
- _b.sent();
346
- _b.label = 4;
347
- case 4: return [2 /*return*/, me];
348
- }
349
- });
350
- });
351
- };
352
- OperationContext.prototype.get = function (path) {
353
- var _a;
354
- var me = this;
355
- if (me.state === ContextState.TEMPORARY) {
356
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
357
- }
358
- return me.scope.get(path);
359
- };
360
- OperationContext.prototype.setMemory = function (key, value) {
361
- var me = this;
362
- me.memory.set(key, value);
363
- return me;
364
- };
365
- OperationContext.prototype.getMemory = function (key) {
366
- var me = this;
367
- return me.memory.get(key);
368
- };
369
- OperationContext.prototype.getCallable = function (path) {
370
- var _a;
371
- var me = this;
372
- if (me.state === ContextState.TEMPORARY) {
373
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.getCallable(path);
374
- }
375
- return me.scope.getCallable(path);
376
- };
377
- OperationContext.prototype.fork = function (type, state) {
378
- var me = this;
379
- var opc = new OperationContext({
380
- previous: me,
381
- type: type,
382
- state: state,
383
- debugger: me.debugger,
384
- cps: me.cps
385
- });
386
- if (me.type === ContextType.FUNCTION || me.type === ContextType.GLOBAL) {
387
- opc.scope.refs.set('locals', opc.scope);
388
- }
389
- if (type !== ContextType.FUNCTION) {
390
- if (type !== ContextType.LOOP) {
391
- opc.setMemory('loopContext', me.getMemory('loopContext'));
392
- }
393
- opc.setMemory('functionContext', me.getMemory('functionContext'));
394
- }
395
- return opc;
396
- };
397
- return OperationContext;
398
- }());
399
- 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["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, 3, , 4]);
284
+ parser = new greybel_core_1.Parser(code);
285
+ chunk = parser.parseChunk();
286
+ item = me.lastContext.cps.visit(chunk);
287
+ context = me.lastContext.fork(ContextType.INJECTION, ContextState.TEMPORARY);
288
+ return [4 /*yield*/, item.run(context)];
289
+ case 2:
290
+ _a.sent();
291
+ return [3 /*break*/, 4];
292
+ case 3:
293
+ err_1 = _a.sent();
294
+ console.error(err_1);
295
+ return [3 /*break*/, 4];
296
+ case 4: return [2 /*return*/];
297
+ }
298
+ });
299
+ });
300
+ };
301
+ return Debugger;
302
+ }());
303
+ exports.Debugger = Debugger;
304
+ var OperationContext = /** @class */ (function () {
305
+ function OperationContext(options) {
306
+ var me = this;
307
+ me.previous = options.previous || null;
308
+ me.type = options.type || ContextType.API;
309
+ me.state = options.state || ContextState.DEFAULT;
310
+ me.scope = new Scope(me);
311
+ me.isProtected = options.isProtected || false;
312
+ me.memory = new Map();
313
+ me.debugger = options.debugger || new Debugger();
314
+ me.cps = options.cps;
315
+ }
316
+ OperationContext.prototype.valueOf = function () {
317
+ return this.scope.valueOf();
318
+ };
319
+ OperationContext.prototype.extend = function (map) {
320
+ var _a;
321
+ var me = this;
322
+ if (me.state === ContextState.TEMPORARY) {
323
+ (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
324
+ }
325
+ else {
326
+ me.scope.extend(map);
327
+ }
328
+ return me;
329
+ };
330
+ OperationContext.prototype.set = function (path, value) {
331
+ var _a;
332
+ return __awaiter(this, void 0, void 0, function () {
333
+ var me;
334
+ return __generator(this, function (_b) {
335
+ switch (_b.label) {
336
+ case 0:
337
+ me = this;
338
+ if (!(me.state === ContextState.TEMPORARY)) return [3 /*break*/, 2];
339
+ return [4 /*yield*/, ((_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value))];
340
+ case 1:
341
+ _b.sent();
342
+ return [3 /*break*/, 4];
343
+ case 2: return [4 /*yield*/, me.scope.set(path, value)];
344
+ case 3:
345
+ _b.sent();
346
+ _b.label = 4;
347
+ case 4: return [2 /*return*/, me];
348
+ }
349
+ });
350
+ });
351
+ };
352
+ OperationContext.prototype.get = function (path) {
353
+ var _a;
354
+ var me = this;
355
+ if (me.state === ContextState.TEMPORARY) {
356
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
357
+ }
358
+ return me.scope.get(path);
359
+ };
360
+ OperationContext.prototype.setMemory = function (key, value) {
361
+ var me = this;
362
+ me.memory.set(key, value);
363
+ return me;
364
+ };
365
+ OperationContext.prototype.getMemory = function (key) {
366
+ var me = this;
367
+ return me.memory.get(key);
368
+ };
369
+ OperationContext.prototype.getCallable = function (path) {
370
+ var _a;
371
+ var me = this;
372
+ if (me.state === ContextState.TEMPORARY) {
373
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.getCallable(path);
374
+ }
375
+ return me.scope.getCallable(path);
376
+ };
377
+ OperationContext.prototype.fork = function (type, state) {
378
+ var me = this;
379
+ var opc = new OperationContext({
380
+ previous: me,
381
+ type: type,
382
+ state: state,
383
+ debugger: me.debugger,
384
+ cps: me.cps
385
+ });
386
+ if (me.type === ContextType.FUNCTION || me.type === ContextType.GLOBAL) {
387
+ opc.scope.refs.set('locals', opc.scope);
388
+ }
389
+ if (type !== ContextType.FUNCTION) {
390
+ if (type !== ContextType.LOOP) {
391
+ opc.setMemory('loopContext', me.getMemory('loopContext'));
392
+ }
393
+ opc.setMemory('functionContext', me.getMemory('functionContext'));
394
+ }
395
+ return opc;
396
+ };
397
+ return OperationContext;
398
+ }());
399
+ exports.OperationContext = OperationContext;