greybel-interpreter 0.1.6 → 0.2.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.
package/dist/cps.js CHANGED
@@ -1,4 +1,40 @@
1
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
+ };
2
38
  var __values = (this && this.__values) || function(o) {
3
39
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
40
  if (m) return m.call(o);
@@ -46,300 +82,589 @@ var nil_1 = __importDefault(require("./custom-types/nil"));
46
82
  var CPSMap = function (visit, context) {
47
83
  return {
48
84
  'AssignmentStatement': function (item) {
49
- return new assign_1.default(item, visit);
85
+ return new assign_1.default(item).prepare(visit);
50
86
  },
51
87
  'MemberExpression': function (item) {
52
- return new path_1.default(item, visit);
88
+ return new path_1.default(item).prepare(visit);
53
89
  },
54
90
  'FunctionDeclaration': function (item) {
55
- var e_1, _a, e_2, _b;
56
- var args = new argument_1.default(item.parameters);
57
- var body = new body_1.default(item.body);
58
- try {
59
- for (var _c = __values(item.parameters), _d = _c.next(); !_d.done; _d = _c.next()) {
60
- var parameterItem = _d.value;
61
- args.stack.push(visit(parameterItem));
62
- }
63
- }
64
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
65
- finally {
66
- try {
67
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
68
- }
69
- finally { if (e_1) throw e_1.error; }
70
- }
71
- try {
72
- for (var _e = __values(item.body), _f = _e.next(); !_f.done; _f = _e.next()) {
73
- var bodyItem = _f.value;
74
- body.stack.push(visit(bodyItem));
75
- }
76
- }
77
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
78
- finally {
79
- try {
80
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
81
- }
82
- finally { if (e_2) throw e_2.error; }
83
- }
84
- return new function_1.default(item, {
85
- args: args,
86
- body: body
91
+ return __awaiter(this, void 0, void 0, function () {
92
+ var args, body, _a, _b, parameterItem, _c, _d, e_1_1, _e, _f, bodyItem, _g, _h, e_2_1;
93
+ var e_1, _j, e_2, _k;
94
+ return __generator(this, function (_l) {
95
+ switch (_l.label) {
96
+ case 0:
97
+ args = new argument_1.default(item.parameters);
98
+ body = new body_1.default(item.body);
99
+ _l.label = 1;
100
+ case 1:
101
+ _l.trys.push([1, 6, 7, 8]);
102
+ _a = __values(item.parameters), _b = _a.next();
103
+ _l.label = 2;
104
+ case 2:
105
+ if (!!_b.done) return [3 /*break*/, 5];
106
+ parameterItem = _b.value;
107
+ _d = (_c = args.stack).push;
108
+ return [4 /*yield*/, visit(parameterItem)];
109
+ case 3:
110
+ _d.apply(_c, [_l.sent()]);
111
+ _l.label = 4;
112
+ case 4:
113
+ _b = _a.next();
114
+ return [3 /*break*/, 2];
115
+ case 5: return [3 /*break*/, 8];
116
+ case 6:
117
+ e_1_1 = _l.sent();
118
+ e_1 = { error: e_1_1 };
119
+ return [3 /*break*/, 8];
120
+ case 7:
121
+ try {
122
+ if (_b && !_b.done && (_j = _a.return)) _j.call(_a);
123
+ }
124
+ finally { if (e_1) throw e_1.error; }
125
+ return [7 /*endfinally*/];
126
+ case 8:
127
+ _l.trys.push([8, 13, 14, 15]);
128
+ _e = __values(item.body), _f = _e.next();
129
+ _l.label = 9;
130
+ case 9:
131
+ if (!!_f.done) return [3 /*break*/, 12];
132
+ bodyItem = _f.value;
133
+ _h = (_g = body.stack).push;
134
+ return [4 /*yield*/, visit(bodyItem)];
135
+ case 10:
136
+ _h.apply(_g, [_l.sent()]);
137
+ _l.label = 11;
138
+ case 11:
139
+ _f = _e.next();
140
+ return [3 /*break*/, 9];
141
+ case 12: return [3 /*break*/, 15];
142
+ case 13:
143
+ e_2_1 = _l.sent();
144
+ e_2 = { error: e_2_1 };
145
+ return [3 /*break*/, 15];
146
+ case 14:
147
+ try {
148
+ if (_f && !_f.done && (_k = _e.return)) _k.call(_e);
149
+ }
150
+ finally { if (e_2) throw e_2.error; }
151
+ return [7 /*endfinally*/];
152
+ case 15: return [2 /*return*/, new function_1.default(item, {
153
+ args: args,
154
+ body: body
155
+ })];
156
+ }
157
+ });
87
158
  });
88
159
  },
89
160
  'MapConstructorExpression': function (item) {
90
- return new map_1.default(item, visit);
161
+ return new map_1.default(item).prepare(visit);
91
162
  },
92
163
  'Identifier': function (item) {
93
- return new path_1.default(item, visit);
164
+ return new path_1.default(item).prepare(visit);
94
165
  },
95
166
  'ReturnStatement': function (item) {
96
- var arg = visit(item.argument);
97
- return new return_1.default(item, {
98
- arg: arg
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ var arg;
169
+ return __generator(this, function (_a) {
170
+ switch (_a.label) {
171
+ case 0: return [4 /*yield*/, visit(item.argument)];
172
+ case 1:
173
+ arg = _a.sent();
174
+ return [2 /*return*/, new return_1.default(item, {
175
+ arg: arg
176
+ })];
177
+ }
178
+ });
99
179
  });
100
180
  },
101
181
  'NumericLiteral': function (item) {
182
+ return Promise.resolve(
102
183
  // @ts-ignore: Key is always a literal
103
- return new number_1.default(item.value);
184
+ new number_1.default(item.value));
104
185
  },
105
186
  'WhileStatement': function (item) {
106
- var e_3, _a;
107
- var body = new body_1.default(item.body);
108
- var condition = visit(item.condition);
109
- try {
110
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
111
- var bodyItem = _c.value;
112
- body.stack.push(visit(bodyItem));
113
- }
114
- }
115
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
116
- finally {
117
- try {
118
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
119
- }
120
- finally { if (e_3) throw e_3.error; }
121
- }
122
- return new while_1.default(item, {
123
- body: body,
124
- condition: condition
187
+ return __awaiter(this, void 0, void 0, function () {
188
+ var body, condition, _a, _b, bodyItem, _c, _d, e_3_1;
189
+ var e_3, _e;
190
+ return __generator(this, function (_f) {
191
+ switch (_f.label) {
192
+ case 0:
193
+ body = new body_1.default(item.body);
194
+ return [4 /*yield*/, visit(item.condition)];
195
+ case 1:
196
+ condition = _f.sent();
197
+ _f.label = 2;
198
+ case 2:
199
+ _f.trys.push([2, 7, 8, 9]);
200
+ _a = __values(item.body), _b = _a.next();
201
+ _f.label = 3;
202
+ case 3:
203
+ if (!!_b.done) return [3 /*break*/, 6];
204
+ bodyItem = _b.value;
205
+ _d = (_c = body.stack).push;
206
+ return [4 /*yield*/, visit(bodyItem)];
207
+ case 4:
208
+ _d.apply(_c, [_f.sent()]);
209
+ _f.label = 5;
210
+ case 5:
211
+ _b = _a.next();
212
+ return [3 /*break*/, 3];
213
+ case 6: return [3 /*break*/, 9];
214
+ case 7:
215
+ e_3_1 = _f.sent();
216
+ e_3 = { error: e_3_1 };
217
+ return [3 /*break*/, 9];
218
+ case 8:
219
+ try {
220
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
221
+ }
222
+ finally { if (e_3) throw e_3.error; }
223
+ return [7 /*endfinally*/];
224
+ case 9: return [2 /*return*/, new while_1.default(item, {
225
+ body: body,
226
+ condition: condition
227
+ })];
228
+ }
229
+ });
125
230
  });
126
231
  },
127
232
  'StringLiteral': function (item) {
233
+ return Promise.resolve(
128
234
  // @ts-ignore: Key is always a literal
129
- return new string_1.default(item.value);
235
+ new string_1.default(item.value));
130
236
  },
131
237
  'IndexExpression': function (item) {
132
- return new path_1.default(item, visit);
238
+ return new path_1.default(item).prepare(visit);
133
239
  },
134
240
  'FeatureEnvarExpression': function (_item) {
135
241
  throw new Error('Not supported');
136
242
  },
137
243
  'IfShortcutStatement': function (item) {
138
- var e_4, _a;
139
- var op = new if_statement_1.default(item);
140
- try {
141
- for (var _b = __values(item.clauses), _c = _b.next(); !_c.done; _c = _b.next()) {
142
- var clausesItem = _c.value;
143
- op.clauses.push(visit(clausesItem));
144
- }
145
- }
146
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
147
- finally {
148
- try {
149
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
150
- }
151
- finally { if (e_4) throw e_4.error; }
152
- }
153
- return op;
244
+ return __awaiter(this, void 0, void 0, function () {
245
+ var op, _a, _b, clausesItem, _c, _d, e_4_1;
246
+ var e_4, _e;
247
+ return __generator(this, function (_f) {
248
+ switch (_f.label) {
249
+ case 0:
250
+ op = new if_statement_1.default(item);
251
+ _f.label = 1;
252
+ case 1:
253
+ _f.trys.push([1, 6, 7, 8]);
254
+ _a = __values(item.clauses), _b = _a.next();
255
+ _f.label = 2;
256
+ case 2:
257
+ if (!!_b.done) return [3 /*break*/, 5];
258
+ clausesItem = _b.value;
259
+ _d = (_c = op.clauses).push;
260
+ return [4 /*yield*/, visit(clausesItem)];
261
+ case 3:
262
+ _d.apply(_c, [_f.sent()]);
263
+ _f.label = 4;
264
+ case 4:
265
+ _b = _a.next();
266
+ return [3 /*break*/, 2];
267
+ case 5: return [3 /*break*/, 8];
268
+ case 6:
269
+ e_4_1 = _f.sent();
270
+ e_4 = { error: e_4_1 };
271
+ return [3 /*break*/, 8];
272
+ case 7:
273
+ try {
274
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
275
+ }
276
+ finally { if (e_4) throw e_4.error; }
277
+ return [7 /*endfinally*/];
278
+ case 8: return [2 /*return*/, op];
279
+ }
280
+ });
281
+ });
154
282
  },
155
283
  'IfShortcutClause': function (item) {
156
- var e_5, _a;
157
- var body = new body_1.default(item.body);
158
- var condition = visit(item.condition);
159
- try {
160
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
161
- var bodyItem = _c.value;
162
- body.stack.push(visit(bodyItem));
163
- }
164
- }
165
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
166
- finally {
167
- try {
168
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
169
- }
170
- finally { if (e_5) throw e_5.error; }
171
- }
172
- return new if_1.default(item, {
173
- condition: condition,
174
- body: body
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ var body, condition, _a, _b, bodyItem, _c, _d, e_5_1;
286
+ var e_5, _e;
287
+ return __generator(this, function (_f) {
288
+ switch (_f.label) {
289
+ case 0:
290
+ body = new body_1.default(item.body);
291
+ return [4 /*yield*/, visit(item.condition)];
292
+ case 1:
293
+ condition = _f.sent();
294
+ _f.label = 2;
295
+ case 2:
296
+ _f.trys.push([2, 7, 8, 9]);
297
+ _a = __values(item.body), _b = _a.next();
298
+ _f.label = 3;
299
+ case 3:
300
+ if (!!_b.done) return [3 /*break*/, 6];
301
+ bodyItem = _b.value;
302
+ _d = (_c = body.stack).push;
303
+ return [4 /*yield*/, visit(bodyItem)];
304
+ case 4:
305
+ _d.apply(_c, [_f.sent()]);
306
+ _f.label = 5;
307
+ case 5:
308
+ _b = _a.next();
309
+ return [3 /*break*/, 3];
310
+ case 6: return [3 /*break*/, 9];
311
+ case 7:
312
+ e_5_1 = _f.sent();
313
+ e_5 = { error: e_5_1 };
314
+ return [3 /*break*/, 9];
315
+ case 8:
316
+ try {
317
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
318
+ }
319
+ finally { if (e_5) throw e_5.error; }
320
+ return [7 /*endfinally*/];
321
+ case 9: return [2 /*return*/, new if_1.default(item, {
322
+ condition: condition,
323
+ body: body
324
+ })];
325
+ }
326
+ });
175
327
  });
176
328
  },
177
329
  'ElseifShortcutClause': function (item) {
178
- var e_6, _a;
179
- var body = new body_1.default(item.body);
180
- var condition = visit(item.condition);
181
- try {
182
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
183
- var bodyItem = _c.value;
184
- body.stack.push(visit(bodyItem));
185
- }
186
- }
187
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
188
- finally {
189
- try {
190
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
191
- }
192
- finally { if (e_6) throw e_6.error; }
193
- }
194
- return new else_if_1.default(item, {
195
- body: body,
196
- condition: condition
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ var body, condition, _a, _b, bodyItem, _c, _d, e_6_1;
332
+ var e_6, _e;
333
+ return __generator(this, function (_f) {
334
+ switch (_f.label) {
335
+ case 0:
336
+ body = new body_1.default(item.body);
337
+ return [4 /*yield*/, visit(item.condition)];
338
+ case 1:
339
+ condition = _f.sent();
340
+ _f.label = 2;
341
+ case 2:
342
+ _f.trys.push([2, 7, 8, 9]);
343
+ _a = __values(item.body), _b = _a.next();
344
+ _f.label = 3;
345
+ case 3:
346
+ if (!!_b.done) return [3 /*break*/, 6];
347
+ bodyItem = _b.value;
348
+ _d = (_c = body.stack).push;
349
+ return [4 /*yield*/, visit(bodyItem)];
350
+ case 4:
351
+ _d.apply(_c, [_f.sent()]);
352
+ _f.label = 5;
353
+ case 5:
354
+ _b = _a.next();
355
+ return [3 /*break*/, 3];
356
+ case 6: return [3 /*break*/, 9];
357
+ case 7:
358
+ e_6_1 = _f.sent();
359
+ e_6 = { error: e_6_1 };
360
+ return [3 /*break*/, 9];
361
+ case 8:
362
+ try {
363
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
364
+ }
365
+ finally { if (e_6) throw e_6.error; }
366
+ return [7 /*endfinally*/];
367
+ case 9: return [2 /*return*/, new else_if_1.default(item, {
368
+ body: body,
369
+ condition: condition
370
+ })];
371
+ }
372
+ });
197
373
  });
198
374
  },
199
375
  'ElseShortcutClause': function (item) {
200
- var e_7, _a;
201
- var body = new body_1.default(item.body);
202
- try {
203
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
204
- var bodyItem = _c.value;
205
- body.stack.push(visit(bodyItem));
206
- }
207
- }
208
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
209
- finally {
210
- try {
211
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
212
- }
213
- finally { if (e_7) throw e_7.error; }
214
- }
215
- return new else_1.default(item, {
216
- body: body
376
+ return __awaiter(this, void 0, void 0, function () {
377
+ var body, _a, _b, bodyItem, _c, _d, e_7_1;
378
+ var e_7, _e;
379
+ return __generator(this, function (_f) {
380
+ switch (_f.label) {
381
+ case 0:
382
+ body = new body_1.default(item.body);
383
+ _f.label = 1;
384
+ case 1:
385
+ _f.trys.push([1, 6, 7, 8]);
386
+ _a = __values(item.body), _b = _a.next();
387
+ _f.label = 2;
388
+ case 2:
389
+ if (!!_b.done) return [3 /*break*/, 5];
390
+ bodyItem = _b.value;
391
+ _d = (_c = body.stack).push;
392
+ return [4 /*yield*/, visit(bodyItem)];
393
+ case 3:
394
+ _d.apply(_c, [_f.sent()]);
395
+ _f.label = 4;
396
+ case 4:
397
+ _b = _a.next();
398
+ return [3 /*break*/, 2];
399
+ case 5: return [3 /*break*/, 8];
400
+ case 6:
401
+ e_7_1 = _f.sent();
402
+ e_7 = { error: e_7_1 };
403
+ return [3 /*break*/, 8];
404
+ case 7:
405
+ try {
406
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
407
+ }
408
+ finally { if (e_7) throw e_7.error; }
409
+ return [7 /*endfinally*/];
410
+ case 8: return [2 /*return*/, new else_1.default(item, {
411
+ body: body
412
+ })];
413
+ }
414
+ });
217
415
  });
218
416
  },
219
417
  'NilLiteral': function (item) {
220
- return new nil_1.default();
418
+ return Promise.resolve(new nil_1.default());
221
419
  },
222
420
  'ForGenericStatement': function (item) {
223
- var e_8, _a;
224
- var body = new body_1.default(item.body);
225
- var variable = visit(item.variable);
226
- var iterator = visit(item.iterator);
227
- try {
228
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
229
- var bodyItem = _c.value;
230
- body.stack.push(visit(bodyItem));
231
- }
232
- }
233
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
234
- finally {
235
- try {
236
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
237
- }
238
- finally { if (e_8) throw e_8.error; }
239
- }
240
- return new for_1.default(item, {
241
- body: body,
242
- variable: variable,
243
- iterator: iterator
421
+ return __awaiter(this, void 0, void 0, function () {
422
+ var body, variable, iterator, _a, _b, bodyItem, _c, _d, e_8_1;
423
+ var e_8, _e;
424
+ return __generator(this, function (_f) {
425
+ switch (_f.label) {
426
+ case 0:
427
+ body = new body_1.default(item.body);
428
+ return [4 /*yield*/, visit(item.variable)];
429
+ case 1:
430
+ variable = _f.sent();
431
+ return [4 /*yield*/, visit(item.iterator)];
432
+ case 2:
433
+ iterator = _f.sent();
434
+ _f.label = 3;
435
+ case 3:
436
+ _f.trys.push([3, 8, 9, 10]);
437
+ _a = __values(item.body), _b = _a.next();
438
+ _f.label = 4;
439
+ case 4:
440
+ if (!!_b.done) return [3 /*break*/, 7];
441
+ bodyItem = _b.value;
442
+ _d = (_c = body.stack).push;
443
+ return [4 /*yield*/, visit(bodyItem)];
444
+ case 5:
445
+ _d.apply(_c, [_f.sent()]);
446
+ _f.label = 6;
447
+ case 6:
448
+ _b = _a.next();
449
+ return [3 /*break*/, 4];
450
+ case 7: return [3 /*break*/, 10];
451
+ case 8:
452
+ e_8_1 = _f.sent();
453
+ e_8 = { error: e_8_1 };
454
+ return [3 /*break*/, 10];
455
+ case 9:
456
+ try {
457
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
458
+ }
459
+ finally { if (e_8) throw e_8.error; }
460
+ return [7 /*endfinally*/];
461
+ case 10: return [2 /*return*/, new for_1.default(item, {
462
+ body: body,
463
+ variable: variable,
464
+ iterator: iterator
465
+ })];
466
+ }
467
+ });
244
468
  });
245
469
  },
246
470
  'IfStatement': function (item) {
247
- var e_9, _a;
248
- var op = new if_statement_1.default(item);
249
- try {
250
- for (var _b = __values(item.clauses), _c = _b.next(); !_c.done; _c = _b.next()) {
251
- var clausesItem = _c.value;
252
- op.clauses.push(visit(clausesItem));
253
- }
254
- }
255
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
256
- finally {
257
- try {
258
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
259
- }
260
- finally { if (e_9) throw e_9.error; }
261
- }
262
- return op;
471
+ return __awaiter(this, void 0, void 0, function () {
472
+ var op, _a, _b, clausesItem, _c, _d, e_9_1;
473
+ var e_9, _e;
474
+ return __generator(this, function (_f) {
475
+ switch (_f.label) {
476
+ case 0:
477
+ op = new if_statement_1.default(item);
478
+ _f.label = 1;
479
+ case 1:
480
+ _f.trys.push([1, 6, 7, 8]);
481
+ _a = __values(item.clauses), _b = _a.next();
482
+ _f.label = 2;
483
+ case 2:
484
+ if (!!_b.done) return [3 /*break*/, 5];
485
+ clausesItem = _b.value;
486
+ _d = (_c = op.clauses).push;
487
+ return [4 /*yield*/, visit(clausesItem)];
488
+ case 3:
489
+ _d.apply(_c, [_f.sent()]);
490
+ _f.label = 4;
491
+ case 4:
492
+ _b = _a.next();
493
+ return [3 /*break*/, 2];
494
+ case 5: return [3 /*break*/, 8];
495
+ case 6:
496
+ e_9_1 = _f.sent();
497
+ e_9 = { error: e_9_1 };
498
+ return [3 /*break*/, 8];
499
+ case 7:
500
+ try {
501
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
502
+ }
503
+ finally { if (e_9) throw e_9.error; }
504
+ return [7 /*endfinally*/];
505
+ case 8: return [2 /*return*/, op];
506
+ }
507
+ });
508
+ });
263
509
  },
264
510
  'IfClause': function (item) {
265
- var e_10, _a;
266
- var body = new body_1.default(item.body);
267
- var condition = visit(item.condition);
268
- try {
269
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
270
- var bodyItem = _c.value;
271
- body.stack.push(visit(bodyItem));
272
- }
273
- }
274
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
275
- finally {
276
- try {
277
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
278
- }
279
- finally { if (e_10) throw e_10.error; }
280
- }
281
- return new if_1.default(item, {
282
- body: body,
283
- condition: condition
511
+ return __awaiter(this, void 0, void 0, function () {
512
+ var body, condition, _a, _b, bodyItem, _c, _d, e_10_1;
513
+ var e_10, _e;
514
+ return __generator(this, function (_f) {
515
+ switch (_f.label) {
516
+ case 0:
517
+ body = new body_1.default(item.body);
518
+ return [4 /*yield*/, visit(item.condition)];
519
+ case 1:
520
+ condition = _f.sent();
521
+ _f.label = 2;
522
+ case 2:
523
+ _f.trys.push([2, 7, 8, 9]);
524
+ _a = __values(item.body), _b = _a.next();
525
+ _f.label = 3;
526
+ case 3:
527
+ if (!!_b.done) return [3 /*break*/, 6];
528
+ bodyItem = _b.value;
529
+ _d = (_c = body.stack).push;
530
+ return [4 /*yield*/, visit(bodyItem)];
531
+ case 4:
532
+ _d.apply(_c, [_f.sent()]);
533
+ _f.label = 5;
534
+ case 5:
535
+ _b = _a.next();
536
+ return [3 /*break*/, 3];
537
+ case 6: return [3 /*break*/, 9];
538
+ case 7:
539
+ e_10_1 = _f.sent();
540
+ e_10 = { error: e_10_1 };
541
+ return [3 /*break*/, 9];
542
+ case 8:
543
+ try {
544
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
545
+ }
546
+ finally { if (e_10) throw e_10.error; }
547
+ return [7 /*endfinally*/];
548
+ case 9: return [2 /*return*/, new if_1.default(item, {
549
+ body: body,
550
+ condition: condition
551
+ })];
552
+ }
553
+ });
284
554
  });
285
555
  },
286
556
  'ElseifClause': function (item) {
287
- var e_11, _a;
288
- var body = new body_1.default(item.body);
289
- var condition = visit(item.condition);
290
- try {
291
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
292
- var bodyItem = _c.value;
293
- body.stack.push(visit(bodyItem));
294
- }
295
- }
296
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
297
- finally {
298
- try {
299
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
300
- }
301
- finally { if (e_11) throw e_11.error; }
302
- }
303
- return new else_if_1.default(item, {
304
- body: body,
305
- condition: condition
557
+ return __awaiter(this, void 0, void 0, function () {
558
+ var body, condition, _a, _b, bodyItem, _c, _d, e_11_1;
559
+ var e_11, _e;
560
+ return __generator(this, function (_f) {
561
+ switch (_f.label) {
562
+ case 0:
563
+ body = new body_1.default(item.body);
564
+ return [4 /*yield*/, visit(item.condition)];
565
+ case 1:
566
+ condition = _f.sent();
567
+ _f.label = 2;
568
+ case 2:
569
+ _f.trys.push([2, 7, 8, 9]);
570
+ _a = __values(item.body), _b = _a.next();
571
+ _f.label = 3;
572
+ case 3:
573
+ if (!!_b.done) return [3 /*break*/, 6];
574
+ bodyItem = _b.value;
575
+ _d = (_c = body.stack).push;
576
+ return [4 /*yield*/, visit(bodyItem)];
577
+ case 4:
578
+ _d.apply(_c, [_f.sent()]);
579
+ _f.label = 5;
580
+ case 5:
581
+ _b = _a.next();
582
+ return [3 /*break*/, 3];
583
+ case 6: return [3 /*break*/, 9];
584
+ case 7:
585
+ e_11_1 = _f.sent();
586
+ e_11 = { error: e_11_1 };
587
+ return [3 /*break*/, 9];
588
+ case 8:
589
+ try {
590
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
591
+ }
592
+ finally { if (e_11) throw e_11.error; }
593
+ return [7 /*endfinally*/];
594
+ case 9: return [2 /*return*/, new else_if_1.default(item, {
595
+ body: body,
596
+ condition: condition
597
+ })];
598
+ }
599
+ });
306
600
  });
307
601
  },
308
602
  'ElseClause': function (item) {
309
- var e_12, _a;
310
- var body = new body_1.default(item.body);
311
- try {
312
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
313
- var bodyItem = _c.value;
314
- body.stack.push(visit(bodyItem));
315
- }
316
- }
317
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
318
- finally {
319
- try {
320
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
321
- }
322
- finally { if (e_12) throw e_12.error; }
323
- }
324
- return new else_1.default(item, {
325
- body: body
603
+ return __awaiter(this, void 0, void 0, function () {
604
+ var body, _a, _b, bodyItem, _c, _d, e_12_1;
605
+ var e_12, _e;
606
+ return __generator(this, function (_f) {
607
+ switch (_f.label) {
608
+ case 0:
609
+ body = new body_1.default(item.body);
610
+ _f.label = 1;
611
+ case 1:
612
+ _f.trys.push([1, 6, 7, 8]);
613
+ _a = __values(item.body), _b = _a.next();
614
+ _f.label = 2;
615
+ case 2:
616
+ if (!!_b.done) return [3 /*break*/, 5];
617
+ bodyItem = _b.value;
618
+ _d = (_c = body.stack).push;
619
+ return [4 /*yield*/, visit(bodyItem)];
620
+ case 3:
621
+ _d.apply(_c, [_f.sent()]);
622
+ _f.label = 4;
623
+ case 4:
624
+ _b = _a.next();
625
+ return [3 /*break*/, 2];
626
+ case 5: return [3 /*break*/, 8];
627
+ case 6:
628
+ e_12_1 = _f.sent();
629
+ e_12 = { error: e_12_1 };
630
+ return [3 /*break*/, 8];
631
+ case 7:
632
+ try {
633
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
634
+ }
635
+ finally { if (e_12) throw e_12.error; }
636
+ return [7 /*endfinally*/];
637
+ case 8: return [2 /*return*/, new else_1.default(item, {
638
+ body: body
639
+ })];
640
+ }
641
+ });
326
642
  });
327
643
  },
328
644
  'NegationExpression': function (item) {
329
- var arg = visit(item.argument);
330
- return new not_1.default(item, { arg: arg });
645
+ return __awaiter(this, void 0, void 0, function () {
646
+ var arg;
647
+ return __generator(this, function (_a) {
648
+ switch (_a.label) {
649
+ case 0: return [4 /*yield*/, visit(item.argument)];
650
+ case 1:
651
+ arg = _a.sent();
652
+ return [2 /*return*/, new not_1.default(item, { arg: arg })];
653
+ }
654
+ });
655
+ });
331
656
  },
332
657
  'ContinueStatement': function (item) {
333
- return new continue_1.default(item);
658
+ return Promise.resolve(new continue_1.default(item));
334
659
  },
335
660
  'BreakStatement': function (item) {
336
- return new break_1.default(item);
661
+ return Promise.resolve(new break_1.default(item));
337
662
  },
338
663
  'CallExpression': function (item) {
339
- return new call_1.default(item, visit);
664
+ return new call_1.default(item).prepare(visit);
340
665
  },
341
666
  'CallStatement': function (item) {
342
- return new call_1.default(item, visit);
667
+ return new call_1.default(item).prepare(visit);
343
668
  },
344
669
  'FeatureImportExpression': function (_item) {
345
670
  throw new Error('Not supported');
@@ -348,66 +673,109 @@ var CPSMap = function (visit, context) {
348
673
  throw new Error('Not supported');
349
674
  },
350
675
  'ImportCodeExpression': function (item) {
351
- var resourceHandler = context.resourceHandler;
352
- var target = resourceHandler.getTargetRelativeTo(context.target,
353
- // @ts-ignore: FileSystemDirectory is always a string
354
- item.fileSystemDirectory.value);
355
- var code = context.resourceHandler.get(target);
356
- var parser = new greybel_core_1.Parser(code);
357
- var chunk = parser.parseChunk();
358
- return visit(chunk);
676
+ return __awaiter(this, void 0, void 0, function () {
677
+ var resourceHandler, target, code, parser, chunk;
678
+ return __generator(this, function (_a) {
679
+ switch (_a.label) {
680
+ case 0:
681
+ resourceHandler = context.resourceHandler;
682
+ return [4 /*yield*/, resourceHandler.getTargetRelativeTo(context.target,
683
+ // @ts-ignore: FileSystemDirectory is always a string
684
+ item.fileSystemDirectory.value)];
685
+ case 1:
686
+ target = _a.sent();
687
+ return [4 /*yield*/, context.resourceHandler.get(target)];
688
+ case 2:
689
+ code = _a.sent();
690
+ parser = new greybel_core_1.Parser(code);
691
+ chunk = parser.parseChunk();
692
+ return [2 /*return*/, visit(chunk)];
693
+ }
694
+ });
695
+ });
359
696
  },
360
697
  'FeatureDebuggerExpression': function (item) {
361
- return new debugger_1.default(item);
698
+ return Promise.resolve(new debugger_1.default(item));
362
699
  },
363
700
  'ListConstructorExpression': function (item) {
364
- return new list_1.default(item, visit);
701
+ return new list_1.default(item).prepare(visit);
365
702
  },
366
703
  'BooleanLiteral': function (item) {
704
+ return Promise.resolve(
367
705
  // @ts-ignore: Key is always a literal
368
- return new boolean_1.default(item.value);
706
+ new boolean_1.default(item.value));
369
707
  },
370
708
  'EmptyExpression': function (item) { },
371
709
  'BinaryExpression': function (item) {
372
- return new logical_and_binary_1.default(item, visit);
710
+ return new logical_and_binary_1.default(item).prepare(visit);
373
711
  },
374
712
  'BinaryNegatedExpression': function (item) {
375
- return new binary_negated_expression_1.default(item, visit);
713
+ return new binary_negated_expression_1.default(item).prepare(visit);
376
714
  },
377
715
  'LogicalExpression': function (item) {
378
- return new logical_and_binary_1.default(item, visit);
716
+ return new logical_and_binary_1.default(item).prepare(visit);
379
717
  },
380
718
  'UnaryExpression': function (item) {
381
- var arg = visit(item.argument);
382
- var op;
383
- if ('@' === item.operator) {
384
- op = new reference_1.default(item, { arg: arg });
385
- }
386
- else if ('new' === item.operator) {
387
- op = new new_1.default(item, { arg: arg });
388
- }
389
- else {
390
- throw new Error('Unknown unary expression.');
391
- }
392
- return op;
719
+ return __awaiter(this, void 0, void 0, function () {
720
+ var arg, op;
721
+ return __generator(this, function (_a) {
722
+ switch (_a.label) {
723
+ case 0: return [4 /*yield*/, visit(item.argument)];
724
+ case 1:
725
+ arg = _a.sent();
726
+ if ('@' === item.operator) {
727
+ op = new reference_1.default(item, { arg: arg });
728
+ }
729
+ else if ('new' === item.operator) {
730
+ op = new new_1.default(item, { arg: arg });
731
+ }
732
+ else {
733
+ throw new Error('Unknown unary expression.');
734
+ }
735
+ return [2 /*return*/, op];
736
+ }
737
+ });
738
+ });
393
739
  },
394
740
  'Chunk': function (item) {
395
- var e_13, _a;
396
- var op = new body_1.default(item.body);
397
- try {
398
- for (var _b = __values(item.body), _c = _b.next(); !_c.done; _c = _b.next()) {
399
- var bodyItem = _c.value;
400
- op.stack.push(visit(bodyItem));
401
- }
402
- }
403
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
404
- finally {
405
- try {
406
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
407
- }
408
- finally { if (e_13) throw e_13.error; }
409
- }
410
- return op;
741
+ return __awaiter(this, void 0, void 0, function () {
742
+ var op, _a, _b, bodyItem, _c, _d, e_13_1;
743
+ var e_13, _e;
744
+ return __generator(this, function (_f) {
745
+ switch (_f.label) {
746
+ case 0:
747
+ op = new body_1.default(item.body);
748
+ _f.label = 1;
749
+ case 1:
750
+ _f.trys.push([1, 6, 7, 8]);
751
+ _a = __values(item.body), _b = _a.next();
752
+ _f.label = 2;
753
+ case 2:
754
+ if (!!_b.done) return [3 /*break*/, 5];
755
+ bodyItem = _b.value;
756
+ _d = (_c = op.stack).push;
757
+ return [4 /*yield*/, visit(bodyItem)];
758
+ case 3:
759
+ _d.apply(_c, [_f.sent()]);
760
+ _f.label = 4;
761
+ case 4:
762
+ _b = _a.next();
763
+ return [3 /*break*/, 2];
764
+ case 5: return [3 /*break*/, 8];
765
+ case 6:
766
+ e_13_1 = _f.sent();
767
+ e_13 = { error: e_13_1 };
768
+ return [3 /*break*/, 8];
769
+ case 7:
770
+ try {
771
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
772
+ }
773
+ finally { if (e_13) throw e_13.error; }
774
+ return [7 /*endfinally*/];
775
+ case 8: return [2 /*return*/, op];
776
+ }
777
+ });
778
+ });
411
779
  }
412
780
  };
413
781
  };
@@ -418,20 +786,30 @@ var CPS = /** @class */ (function () {
418
786
  me.cpsMap = (0, exports.CPSMap)(me.visit.bind(me), context);
419
787
  }
420
788
  CPS.prototype.visit = function (o) {
421
- var me = this;
422
- if (o == null)
423
- return '';
424
- if (o.type == null) {
425
- console.error('Error ast type:', o);
426
- throw new Error('Unexpected as type');
427
- }
428
- var fn = me.cpsMap[o.type];
429
- if (fn == null) {
430
- console.error('Error ast:', o);
431
- throw new Error('Type does not exist ' + o.type);
432
- }
433
- var result = fn.call(me, o);
434
- return result;
789
+ return __awaiter(this, void 0, void 0, function () {
790
+ var me, fn, result;
791
+ return __generator(this, function (_a) {
792
+ switch (_a.label) {
793
+ case 0:
794
+ me = this;
795
+ if (o == null)
796
+ return [2 /*return*/, ''];
797
+ if (o.type == null) {
798
+ console.error('Error ast type:', o);
799
+ throw new Error('Unexpected as type');
800
+ }
801
+ fn = me.cpsMap[o.type];
802
+ if (fn == null) {
803
+ console.error('Error ast:', o);
804
+ throw new Error('Type does not exist ' + o.type);
805
+ }
806
+ return [4 /*yield*/, fn.call(me, o)];
807
+ case 1:
808
+ result = _a.sent();
809
+ return [2 /*return*/, result];
810
+ }
811
+ });
812
+ });
435
813
  };
436
814
  return CPS;
437
815
  }());