greybel-interpreter 0.5.7 → 0.6.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.
@@ -52,18 +52,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
52
52
  };
53
53
  var _a;
54
54
  Object.defineProperty(exports, "__esModule", { value: true });
55
- exports.ExpressionSegment = exports.OPERATIONS = exports.multiplyString = exports.toPrimitive = void 0;
55
+ exports.ExpressionSegment = exports.OPERATIONS = exports.multiplyString = void 0;
56
56
  var greybel_core_1 = require("greybel-core");
57
57
  var greyscript_core_1 = require("greyscript-core");
58
58
  var expression_1 = require("../types/expression");
59
59
  var typer_1 = require("../typer");
60
- var toPrimitive = function (v) {
61
- return (0, typer_1.isCustomValue)(v) ? v.valueOf() : v;
62
- };
63
- exports.toPrimitive = toPrimitive;
64
60
  var multiplyString = function (a, b) {
65
- var aVal = a.valueOf() || '';
66
- var bVal = b.valueOf();
61
+ var aVal = a.toString();
62
+ var bVal = b.toNumber();
67
63
  return new Array(bVal)
68
64
  .fill(aVal)
69
65
  .join('');
@@ -77,12 +73,13 @@ exports.OPERATIONS = (_a = {},
77
73
  else if ((0, typer_1.isCustomList)(a) && (0, typer_1.isCustomList)(b)) {
78
74
  return a.concat(b);
79
75
  }
80
- var aVal = (0, typer_1.isCustomString)(a) ? (a.valueOf() || '') : (0, exports.toPrimitive)(a);
81
- var bVal = (0, typer_1.isCustomString)(b) ? (b.valueOf() || '') : (0, exports.toPrimitive)(b);
82
- return aVal + bVal;
76
+ else if ((0, typer_1.isCustomString)(a) || (0, typer_1.isCustomString)(b)) {
77
+ return a.toString() + b.toString();
78
+ }
79
+ return a.toNumber() + b.toNumber();
83
80
  },
84
- _a[greybel_core_1.Operator.Minus] = function (a, b) { return (0, exports.toPrimitive)(a) - (0, exports.toPrimitive)(b); },
85
- _a[greybel_core_1.Operator.Slash] = function (a, b) { return (0, exports.toPrimitive)(a) / (0, exports.toPrimitive)(b); },
81
+ _a[greybel_core_1.Operator.Minus] = function (a, b) { return a.toNumber() - b.toNumber(); },
82
+ _a[greybel_core_1.Operator.Slash] = function (a, b) { return a.toNumber() / b.toNumber(); },
86
83
  _a[greybel_core_1.Operator.Asterik] = function (a, b) {
87
84
  if ((0, typer_1.isCustomString)(a) && (0, typer_1.isCustomNumber)(b)) {
88
85
  return (0, exports.multiplyString)(a, b);
@@ -90,25 +87,23 @@ exports.OPERATIONS = (_a = {},
90
87
  else if ((0, typer_1.isCustomString)(b) && (0, typer_1.isCustomNumber)(a)) {
91
88
  return (0, exports.multiplyString)(b, a);
92
89
  }
93
- var aVal = (0, exports.toPrimitive)(a);
94
- var bVal = (0, exports.toPrimitive)(b);
95
- return aVal * bVal;
90
+ return a.toNumber() * b.toNumber();
96
91
  },
97
- _a[greybel_core_1.Operator.Xor] = function (a, b) { return (0, exports.toPrimitive)(a) ^ (0, exports.toPrimitive)(b); },
98
- _a[greybel_core_1.Operator.BitwiseOr] = function (a, b) { return (0, exports.toPrimitive)(a) | (0, exports.toPrimitive)(b); },
99
- _a[greybel_core_1.Operator.LessThan] = function (a, b) { return (0, exports.toPrimitive)(a) < (0, exports.toPrimitive)(b); },
100
- _a[greybel_core_1.Operator.GreaterThan] = function (a, b) { return (0, exports.toPrimitive)(a) > (0, exports.toPrimitive)(b); },
101
- _a[greybel_core_1.Operator.LeftShift] = function (a, b) { return (0, exports.toPrimitive)(a) << (0, exports.toPrimitive)(b); },
102
- _a[greybel_core_1.Operator.RightShift] = function (a, b) { return (0, exports.toPrimitive)(a) >> (0, exports.toPrimitive)(b); },
103
- _a[greybel_core_1.Operator.UnsignedRightShift] = function (a, b) { return (0, exports.toPrimitive)(a) >>> (0, exports.toPrimitive)(b); },
104
- _a[greybel_core_1.Operator.BitwiseAnd] = function (a, b) { return (0, exports.toPrimitive)(a) & (0, exports.toPrimitive)(b); },
105
- _a[greybel_core_1.Operator.PercentSign] = function (a, b) { return (0, exports.toPrimitive)(a) % (0, exports.toPrimitive)(b); },
106
- _a[greybel_core_1.Operator.GreaterThanOrEqual] = function (a, b) { return (0, exports.toPrimitive)(a) >= (0, exports.toPrimitive)(b); },
107
- _a[greybel_core_1.Operator.Equal] = function (a, b) { return (0, exports.toPrimitive)(a) == (0, exports.toPrimitive)(b); },
108
- _a[greybel_core_1.Operator.LessThanOrEqual] = function (a, b) { return (0, exports.toPrimitive)(a) <= (0, exports.toPrimitive)(b); },
109
- _a[greybel_core_1.Operator.NotEqual] = function (a, b) { return (0, exports.toPrimitive)(a) != (0, exports.toPrimitive)(b); },
110
- _a[greybel_core_1.Operator.And] = function (a, b) { return (0, exports.toPrimitive)(a) && (0, exports.toPrimitive)(b); },
111
- _a[greybel_core_1.Operator.Or] = function (a, b) { return (0, exports.toPrimitive)(a) || (0, exports.toPrimitive)(b); },
92
+ _a[greybel_core_1.Operator.Xor] = function (a, b) { return a.toNumber() ^ b.toNumber(); },
93
+ _a[greybel_core_1.Operator.BitwiseOr] = function (a, b) { return a.toNumber() | b.toNumber(); },
94
+ _a[greybel_core_1.Operator.LessThan] = function (a, b) { return a.toNumber() < b.toNumber(); },
95
+ _a[greybel_core_1.Operator.GreaterThan] = function (a, b) { return a.toNumber() > b.toNumber(); },
96
+ _a[greybel_core_1.Operator.LeftShift] = function (a, b) { return a.toNumber() << b.toNumber(); },
97
+ _a[greybel_core_1.Operator.RightShift] = function (a, b) { return a.toNumber() >> b.toNumber(); },
98
+ _a[greybel_core_1.Operator.UnsignedRightShift] = function (a, b) { return a.toNumber() >>> b.toNumber(); },
99
+ _a[greybel_core_1.Operator.BitwiseAnd] = function (a, b) { return a.toNumber() & b.toNumber(); },
100
+ _a[greybel_core_1.Operator.PercentSign] = function (a, b) { return a.toNumber() % b.toNumber(); },
101
+ _a[greybel_core_1.Operator.GreaterThanOrEqual] = function (a, b) { return a.toNumber() >= b.toNumber(); },
102
+ _a[greybel_core_1.Operator.Equal] = function (a, b) { return a.toNumber() == b.toNumber(); },
103
+ _a[greybel_core_1.Operator.LessThanOrEqual] = function (a, b) { return a.toNumber() <= b.toNumber(); },
104
+ _a[greybel_core_1.Operator.NotEqual] = function (a, b) { return a.toNumber() != b.toNumber(); },
105
+ _a[greybel_core_1.Operator.And] = function (a, b) { return a.toTruthy() && b.toTruthy(); },
106
+ _a[greybel_core_1.Operator.Or] = function (a, b) { return a.toTruthy() || b.toTruthy(); },
112
107
  _a);
113
108
  var ExpressionSegment = /** @class */ (function () {
114
109
  function ExpressionSegment(type, operator, left, right) {
@@ -162,9 +157,9 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
162
157
  case 0:
163
158
  me = this;
164
159
  evaluate = function (node) { return __awaiter(_this, void 0, void 0, function () {
165
- var expr, _a, binaryResult, _b, _c, _d, logicalLeft, logicalResult, _e, _f, _g;
166
- return __generator(this, function (_h) {
167
- switch (_h.label) {
160
+ var expr, _a, binaryResult, _b, _c, _d, _e, _f, logicalLeft, _g, logicalResult, _h, _j, _k, _l;
161
+ return __generator(this, function (_m) {
162
+ switch (_m.label) {
168
163
  case 0:
169
164
  if (!(0, typer_1.isCustomValue)(node)) return [3 /*break*/, 1];
170
165
  return [2 /*return*/, node];
@@ -182,30 +177,32 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
182
177
  return [3 /*break*/, 8];
183
178
  case 2:
184
179
  _c = (_b = exports.OPERATIONS)[expr.operator];
180
+ _d = typer_1.cast;
185
181
  return [4 /*yield*/, evaluate(expr.left)];
186
182
  case 3:
187
- _d = [_h.sent()];
183
+ _e = [_d.apply(void 0, [_m.sent()])];
184
+ _f = typer_1.cast;
188
185
  return [4 /*yield*/, evaluate(expr.right)];
189
186
  case 4:
190
- binaryResult = _c.apply(_b, _d.concat([_h.sent()]));
191
- return [2 /*return*/, Number.isNaN(binaryResult) ? null : binaryResult];
192
- case 5: return [4 /*yield*/, evaluate(expr.left)];
187
+ binaryResult = _c.apply(_b, _e.concat([_f.apply(void 0, [_m.sent()])]));
188
+ return [2 /*return*/, binaryResult];
189
+ case 5:
190
+ _g = typer_1.cast;
191
+ return [4 /*yield*/, evaluate(expr.left)];
193
192
  case 6:
194
- logicalLeft = _h.sent();
195
- if ((0, typer_1.isCustomList)(logicalLeft) && !logicalLeft.valueOf()) {
196
- logicalLeft = false;
197
- }
198
- if (expr.operator === greybel_core_1.Operator.And && !(0, exports.toPrimitive)(logicalLeft)) {
193
+ logicalLeft = _g.apply(void 0, [_m.sent()]);
194
+ if (expr.operator === greybel_core_1.Operator.And && !logicalLeft.toTruthy()) {
199
195
  return [2 /*return*/, false];
200
196
  }
201
- else if (expr.operator === greybel_core_1.Operator.Or && (0, exports.toPrimitive)(logicalLeft)) {
197
+ else if (expr.operator === greybel_core_1.Operator.Or && logicalLeft.toTruthy()) {
202
198
  return [2 /*return*/, true];
203
199
  }
204
- _f = (_e = exports.OPERATIONS)[expr.operator];
205
- _g = [logicalLeft];
200
+ _j = (_h = exports.OPERATIONS)[expr.operator];
201
+ _k = [logicalLeft];
202
+ _l = typer_1.cast;
206
203
  return [4 /*yield*/, evaluate(expr.right)];
207
204
  case 7:
208
- logicalResult = _f.apply(_e, _g.concat([_h.sent()]));
205
+ logicalResult = _j.apply(_h, _k.concat([_l.apply(void 0, [_m.sent()])]));
209
206
  return [2 /*return*/, logicalResult];
210
207
  case 8: return [2 /*return*/, node.get(operationContext)];
211
208
  }
@@ -67,6 +67,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
67
67
  Object.defineProperty(exports, "__esModule", { value: true });
68
68
  exports.ExpressionSegment = exports.ValuesSegment = void 0;
69
69
  var expression_1 = require("../types/expression");
70
+ var operation_1 = require("../types/operation");
70
71
  var map_1 = __importDefault(require("../custom-types/map"));
71
72
  var typer_1 = require("../typer");
72
73
  var ValuesSegment = /** @class */ (function () {
@@ -138,46 +139,52 @@ var MapExpression = /** @class */ (function (_super) {
138
139
  map = new Map();
139
140
  _b.label = 1;
140
141
  case 1:
141
- _b.trys.push([1, 9, 10, 11]);
142
+ _b.trys.push([1, 11, 12, 13]);
142
143
  values_1 = __values(values), values_1_1 = values_1.next();
143
144
  _b.label = 2;
144
145
  case 2:
145
- if (!!values_1_1.done) return [3 /*break*/, 8];
146
+ if (!!values_1_1.done) return [3 /*break*/, 10];
146
147
  current = values_1_1.value;
147
148
  key = void 0;
148
149
  value = void 0;
149
150
  key = current.key;
150
151
  if (!(0, typer_1.isCustomValue)(current.value)) return [3 /*break*/, 3];
151
152
  value = current.value;
152
- return [3 /*break*/, 6];
153
+ return [3 /*break*/, 8];
153
154
  case 3:
154
155
  if (!(current.value instanceof expression_1.Expression)) return [3 /*break*/, 5];
155
156
  return [4 /*yield*/, current.value.get(operationContext)];
156
157
  case 4:
157
158
  value = _b.sent();
158
- return [3 /*break*/, 6];
159
+ return [3 /*break*/, 8];
159
160
  case 5:
160
- operationContext.debugger.raise('Unexpected value', me, current.value);
161
- _b.label = 6;
161
+ if (!(current.value instanceof operation_1.Operation)) return [3 /*break*/, 7];
162
+ return [4 /*yield*/, current.value.get(operationContext)];
162
163
  case 6:
164
+ value = _b.sent();
165
+ return [3 /*break*/, 8];
166
+ case 7:
167
+ operationContext.debugger.raise('Unexpected value', me, current.value);
168
+ _b.label = 8;
169
+ case 8:
163
170
  // @ts-ignore: Key is always a literal
164
171
  map.set(key, value);
165
- _b.label = 7;
166
- case 7:
172
+ _b.label = 9;
173
+ case 9:
167
174
  values_1_1 = values_1.next();
168
175
  return [3 /*break*/, 2];
169
- case 8: return [3 /*break*/, 11];
170
- case 9:
176
+ case 10: return [3 /*break*/, 13];
177
+ case 11:
171
178
  e_1_1 = _b.sent();
172
179
  e_1 = { error: e_1_1 };
173
- return [3 /*break*/, 11];
174
- case 10:
180
+ return [3 /*break*/, 13];
181
+ case 12:
175
182
  try {
176
183
  if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
177
184
  }
178
185
  finally { if (e_1) throw e_1.error; }
179
186
  return [7 /*endfinally*/];
180
- case 11: return [2 /*return*/, new map_1.default(map)];
187
+ case 13: return [2 /*return*/, new map_1.default(map)];
181
188
  }
182
189
  });
183
190
  });
@@ -198,14 +198,14 @@ var PathExpression = /** @class */ (function (_super) {
198
198
  me = this;
199
199
  evaluate = function (node) {
200
200
  return __awaiter(this, void 0, void 0, function () {
201
- var traverselPath, traversedPath, position, handle, current, functionContext, origin, value, origin, left, right;
202
- return __generator(this, function (_a) {
203
- switch (_a.label) {
201
+ var traverselPath, traversedPath, position, handle, current, functionContext, origin, _a, value, origin, left, right;
202
+ return __generator(this, function (_b) {
203
+ switch (_b.label) {
204
204
  case 0:
205
205
  traverselPath = [].concat(node.value);
206
206
  traversedPath = [];
207
207
  position = 0;
208
- _a.label = 1;
208
+ _b.label = 1;
209
209
  case 1:
210
210
  if (!(current = traverselPath.shift())) return [3 /*break*/, 30];
211
211
  if (!(0, typer_1.isCustomValue)(current)) return [3 /*break*/, 2];
@@ -215,13 +215,13 @@ var PathExpression = /** @class */ (function (_super) {
215
215
  if (!(current instanceof expression_1.Expression)) return [3 /*break*/, 4];
216
216
  return [4 /*yield*/, current.get(operationContext, me.expr)];
217
217
  case 3:
218
- handle = _a.sent();
218
+ handle = _b.sent();
219
219
  return [3 /*break*/, 29];
220
220
  case 4:
221
221
  if (!(current instanceof operation_1.Operation)) return [3 /*break*/, 6];
222
222
  return [4 /*yield*/, current.get(operationContext)];
223
223
  case 5:
224
- handle = _a.sent();
224
+ handle = _b.sent();
225
225
  return [3 /*break*/, 29];
226
226
  case 6:
227
227
  if (!(current instanceof PathSegment)) return [3 /*break*/, 12];
@@ -239,18 +239,19 @@ var PathExpression = /** @class */ (function (_super) {
239
239
  if (!(traverselPath.length > 0)) return [3 /*break*/, 11];
240
240
  return [4 /*yield*/, (handle || operationContext).get(traversedPath)];
241
241
  case 8:
242
- origin = _a.sent();
242
+ origin = _b.sent();
243
243
  if (!(0, typer_1.isCustomValue)(origin)) return [3 /*break*/, 9];
244
244
  handle = origin;
245
245
  traversedPath = [];
246
246
  return [3 /*break*/, 11];
247
247
  case 9:
248
248
  if (!(origin instanceof Function)) return [3 /*break*/, 11];
249
+ _a = typer_1.cast;
249
250
  return [4 /*yield*/, origin.call(handle)];
250
251
  case 10:
251
- handle = _a.sent();
252
+ handle = _a.apply(void 0, [_b.sent()]);
252
253
  traversedPath = [];
253
- _a.label = 11;
254
+ _b.label = 11;
254
255
  case 11: return [3 /*break*/, 29];
255
256
  case 12:
256
257
  if (!(current instanceof IndexSegment)) return [3 /*break*/, 19];
@@ -262,31 +263,31 @@ var PathExpression = /** @class */ (function (_super) {
262
263
  if (!(current instanceof expression_1.Expression)) return [3 /*break*/, 15];
263
264
  return [4 /*yield*/, current.get(operationContext)];
264
265
  case 14:
265
- value = _a.sent();
266
+ value = _b.sent();
266
267
  traversedPath.push(value.toString());
267
268
  return [3 /*break*/, 16];
268
269
  case 15:
269
270
  operationContext.debugger.raise('Unexpected index', me, current);
270
- _a.label = 16;
271
+ _b.label = 16;
271
272
  case 16:
272
273
  if (!(handle && !parentExpr)) return [3 /*break*/, 18];
273
274
  return [4 /*yield*/, handle.get(traversedPath, me.expr)];
274
275
  case 17:
275
- origin = _a.sent();
276
+ origin = _b.sent();
276
277
  if (origin instanceof Function || origin instanceof function_1.default) {
277
278
  handle = origin;
278
279
  traversedPath = [];
279
280
  }
280
- _a.label = 18;
281
+ _b.label = 18;
281
282
  case 18: return [3 /*break*/, 29];
282
283
  case 19:
283
284
  if (!(current instanceof SliceSegment)) return [3 /*break*/, 28];
284
285
  if (!!handle) return [3 /*break*/, 21];
285
286
  return [4 /*yield*/, operationContext.get(traversedPath)];
286
287
  case 20:
287
- handle = _a.sent();
288
+ handle = _b.sent();
288
289
  traversedPath = [];
289
- _a.label = 21;
290
+ _b.label = 21;
290
291
  case 21:
291
292
  if (!(0, typer_1.isCustomList)(handle) && !(0, typer_1.isCustomString)(handle)) {
292
293
  operationContext.debugger.raise('Invalid type for slice', me, handle);
@@ -299,8 +300,8 @@ var PathExpression = /** @class */ (function (_super) {
299
300
  if (!(left instanceof expression_1.Expression)) return [3 /*break*/, 24];
300
301
  return [4 /*yield*/, left.get(operationContext)];
301
302
  case 23:
302
- left = _a.sent();
303
- _a.label = 24;
303
+ left = _b.sent();
304
+ _b.label = 24;
304
305
  case 24:
305
306
  right = current.right;
306
307
  if (!(0, typer_1.isCustomValue)(right)) return [3 /*break*/, 25];
@@ -310,14 +311,14 @@ var PathExpression = /** @class */ (function (_super) {
310
311
  if (!(right instanceof expression_1.Expression)) return [3 /*break*/, 27];
311
312
  return [4 /*yield*/, right.get(operationContext)];
312
313
  case 26:
313
- right = _a.sent();
314
- _a.label = 27;
314
+ right = _b.sent();
315
+ _b.label = 27;
315
316
  case 27:
316
317
  handle = handle.slice(left, right);
317
318
  return [3 /*break*/, 29];
318
319
  case 28:
319
320
  operationContext.debugger.raise('Unexpected handle', me, current);
320
- _a.label = 29;
321
+ _b.label = 29;
321
322
  case 29:
322
323
  position++;
323
324
  return [3 /*break*/, 1];
@@ -338,7 +339,9 @@ var PathExpression = /** @class */ (function (_super) {
338
339
  if (!(resultExpr.path.length === 0)) return [3 /*break*/, 2];
339
340
  return [2 /*return*/, resultExpr.handle];
340
341
  case 2:
341
- if (!(0, typer_1.isCustomMap)(resultExpr.handle)) return [3 /*break*/, 7];
342
+ if (!((0, typer_1.isCustomMap)(resultExpr.handle) ||
343
+ (0, typer_1.isCustomList)(resultExpr.handle) ||
344
+ (0, typer_1.isCustomString)(resultExpr.handle))) return [3 /*break*/, 7];
342
345
  context = resultExpr.handle;
343
346
  return [4 /*yield*/, context.get(resultExpr.path)];
344
347
  case 3:
@@ -351,7 +354,9 @@ var PathExpression = /** @class */ (function (_super) {
351
354
  return [4 /*yield*/, value_1.call(context)];
352
355
  case 5: return [2 /*return*/, _a.apply(void 0, [_c.sent()])];
353
356
  case 6: return [2 /*return*/, value_1];
354
- case 7: return [2 /*return*/, (0, typer_1.cast)(resultExpr.handle.callMethod(resultExpr.path))];
357
+ case 7:
358
+ operationContext.debugger.raise('Unexpected handle get', me, resultExpr);
359
+ _c.label = 8;
355
360
  case 8: return [4 /*yield*/, operationContext.get(resultExpr.path)];
356
361
  case 9:
357
362
  value = _c.sent();
@@ -81,19 +81,19 @@ var ArgumentOperation = /** @class */ (function (_super) {
81
81
  }
82
82
  ArgumentOperation.prototype.get = function (operationContext) {
83
83
  return __awaiter(this, void 0, void 0, function () {
84
- var me, stack, args, stack_1, stack_1_1, entity, _a, _b, arg, e_1_1;
85
- var e_1, _c;
86
- return __generator(this, function (_d) {
87
- switch (_d.label) {
84
+ var me, stack, args, stack_1, stack_1_1, entity, arg, arg, e_1_1;
85
+ var e_1, _a;
86
+ return __generator(this, function (_b) {
87
+ switch (_b.label) {
88
88
  case 0:
89
89
  me = this;
90
90
  stack = me.stack;
91
91
  args = [];
92
- _d.label = 1;
92
+ _b.label = 1;
93
93
  case 1:
94
- _d.trys.push([1, 11, 12, 13]);
94
+ _b.trys.push([1, 11, 12, 13]);
95
95
  stack_1 = __values(stack), stack_1_1 = stack_1.next();
96
- _d.label = 2;
96
+ _b.label = 2;
97
97
  case 2:
98
98
  if (!!stack_1_1.done) return [3 /*break*/, 10];
99
99
  entity = stack_1_1.value;
@@ -104,34 +104,34 @@ var ArgumentOperation = /** @class */ (function (_super) {
104
104
  if (!(entity instanceof assign_1.default)) return [3 /*break*/, 6];
105
105
  return [4 /*yield*/, entity.get(operationContext, me)];
106
106
  case 4:
107
- _d.sent();
108
- _b = (_a = args).push;
109
- return [4 /*yield*/, entity.expr.left.get(operationContext)];
107
+ _b.sent();
108
+ return [4 /*yield*/, entity.expr.left.get(operationContext, me)];
110
109
  case 5:
111
- _b.apply(_a, [_d.sent()]);
110
+ arg = _b.sent();
111
+ args.push(arg);
112
112
  return [3 /*break*/, 9];
113
113
  case 6:
114
114
  if (!(entity instanceof path_1.default)) return [3 /*break*/, 8];
115
115
  return [4 /*yield*/, entity.get(operationContext, me)];
116
116
  case 7:
117
- arg = _d.sent();
117
+ arg = _b.sent();
118
118
  operationContext.set(arg.path, new nil_1.default());
119
119
  args.push(arg);
120
120
  return [3 /*break*/, 9];
121
121
  case 8:
122
122
  operationContext.debugger.raise('Unexpected argument', me, entity);
123
- _d.label = 9;
123
+ _b.label = 9;
124
124
  case 9:
125
125
  stack_1_1 = stack_1.next();
126
126
  return [3 /*break*/, 2];
127
127
  case 10: return [3 /*break*/, 13];
128
128
  case 11:
129
- e_1_1 = _d.sent();
129
+ e_1_1 = _b.sent();
130
130
  e_1 = { error: e_1_1 };
131
131
  return [3 /*break*/, 13];
132
132
  case 12:
133
133
  try {
134
- if (stack_1_1 && !stack_1_1.done && (_c = stack_1.return)) _c.call(stack_1);
134
+ if (stack_1_1 && !stack_1_1.done && (_a = stack_1.return)) _a.call(stack_1);
135
135
  }
136
136
  finally { if (e_1) throw e_1.error; }
137
137
  return [7 /*endfinally*/];
@@ -17,5 +17,7 @@ export default class FunctionOperation extends FunctionOperationBase {
17
17
  fork(context: any): FunctionOperation;
18
18
  get(operationContext: OperationContext): FunctionOperation;
19
19
  toString(): string;
20
+ toTruthy(): boolean;
21
+ toNumber(): number;
20
22
  run(operationContext: OperationContext): Promise<any>;
21
23
  }
@@ -85,6 +85,12 @@ var FunctionOperation = /** @class */ (function (_super) {
85
85
  FunctionOperation.prototype.toString = function () {
86
86
  return 'Function';
87
87
  };
88
+ FunctionOperation.prototype.toTruthy = function () {
89
+ return true;
90
+ };
91
+ FunctionOperation.prototype.toNumber = function () {
92
+ return 0;
93
+ };
88
94
  FunctionOperation.prototype.run = function (operationContext) {
89
95
  return __awaiter(this, void 0, void 0, function () {
90
96
  var me, opc, incArgs, args, argMap, functionContext, index, max;
@@ -82,68 +82,102 @@ var IfStatementOperation = /** @class */ (function (_super) {
82
82
  }
83
83
  IfStatementOperation.prototype.run = function (operationContext) {
84
84
  return __awaiter(this, void 0, void 0, function () {
85
- var me, clauses, clauses_1, clauses_1_1, clause, condition, isValid, e_1_1;
85
+ var me, clauses, _loop_1, clauses_1, clauses_1_1, clause, state_1, e_1_1;
86
86
  var e_1, _a;
87
87
  return __generator(this, function (_b) {
88
88
  switch (_b.label) {
89
89
  case 0:
90
90
  me = this;
91
91
  clauses = me.clauses;
92
+ _loop_1 = function (clause) {
93
+ var condition, resolveCondition_1;
94
+ return __generator(this, function (_c) {
95
+ switch (_c.label) {
96
+ case 0:
97
+ if (!(clause instanceof if_1.default || clause instanceof else_if_1.default)) return [3 /*break*/, 4];
98
+ condition = clause.condition;
99
+ resolveCondition_1 = function (item) {
100
+ return __awaiter(this, void 0, void 0, function () {
101
+ var value, value;
102
+ return __generator(this, function (_a) {
103
+ switch (_a.label) {
104
+ case 0:
105
+ if (!(item instanceof expression_1.Expression)) return [3 /*break*/, 2];
106
+ return [4 /*yield*/, item.get(operationContext)];
107
+ case 1:
108
+ value = _a.sent();
109
+ return [2 /*return*/, resolveCondition_1(value)];
110
+ case 2:
111
+ if (!(item instanceof operation_1.Operation)) return [3 /*break*/, 4];
112
+ return [4 /*yield*/, item.get(operationContext)];
113
+ case 3:
114
+ value = _a.sent();
115
+ return [2 /*return*/, resolveCondition_1(value)];
116
+ case 4:
117
+ if ((0, typer_1.isCustomValue)(item)) {
118
+ return [2 /*return*/, item.toTruthy()];
119
+ }
120
+ else if (typeof item === 'boolean') {
121
+ return [2 /*return*/, item];
122
+ }
123
+ _a.label = 5;
124
+ case 5:
125
+ operationContext.debugger.raise('Unexpected condition', me, item);
126
+ return [2 /*return*/];
127
+ }
128
+ });
129
+ });
130
+ };
131
+ return [4 /*yield*/, resolveCondition_1(condition)];
132
+ case 1:
133
+ if (!_c.sent()) return [3 /*break*/, 3];
134
+ return [4 /*yield*/, clause.body.run(operationContext)];
135
+ case 2:
136
+ _c.sent();
137
+ return [2 /*return*/, "break"];
138
+ case 3: return [3 /*break*/, 7];
139
+ case 4:
140
+ if (!(clause instanceof else_1.default)) return [3 /*break*/, 6];
141
+ return [4 /*yield*/, clause.body.run(operationContext)];
142
+ case 5:
143
+ _c.sent();
144
+ return [2 /*return*/, "break"];
145
+ case 6:
146
+ operationContext.debugger.raise('Invalid operation in if statement.', me, clause);
147
+ _c.label = 7;
148
+ case 7: return [2 /*return*/];
149
+ }
150
+ });
151
+ };
92
152
  _b.label = 1;
93
153
  case 1:
94
- _b.trys.push([1, 14, 15, 16]);
154
+ _b.trys.push([1, 6, 7, 8]);
95
155
  clauses_1 = __values(clauses), clauses_1_1 = clauses_1.next();
96
156
  _b.label = 2;
97
157
  case 2:
98
- if (!!clauses_1_1.done) return [3 /*break*/, 13];
158
+ if (!!clauses_1_1.done) return [3 /*break*/, 5];
99
159
  clause = clauses_1_1.value;
100
- if (!(clause instanceof if_1.default || clause instanceof else_if_1.default)) return [3 /*break*/, 9];
101
- condition = clause.condition;
102
- isValid = void 0;
103
- if (!(0, typer_1.isCustomValue)(condition)) return [3 /*break*/, 3];
104
- isValid = condition.valueOf();
105
- return [3 /*break*/, 6];
160
+ return [5 /*yield**/, _loop_1(clause)];
106
161
  case 3:
107
- if (!(condition instanceof expression_1.Expression ||
108
- condition instanceof operation_1.Operation)) return [3 /*break*/, 5];
109
- return [4 /*yield*/, condition.get(operationContext)];
162
+ state_1 = _b.sent();
163
+ if (state_1 === "break")
164
+ return [3 /*break*/, 5];
165
+ _b.label = 4;
110
166
  case 4:
111
- isValid = _b.sent();
112
- return [3 /*break*/, 6];
113
- case 5:
114
- operationContext.debugger.raise('Unexpected condition in clause', me, clause.condition);
115
- _b.label = 6;
116
- case 6:
117
- if (!isValid.valueOf()) return [3 /*break*/, 8];
118
- return [4 /*yield*/, clause.body.run(operationContext)];
119
- case 7:
120
- _b.sent();
121
- return [3 /*break*/, 13];
122
- case 8: return [3 /*break*/, 12];
123
- case 9:
124
- if (!(clause instanceof else_1.default)) return [3 /*break*/, 11];
125
- return [4 /*yield*/, clause.body.run(operationContext)];
126
- case 10:
127
- _b.sent();
128
- return [3 /*break*/, 13];
129
- case 11:
130
- operationContext.debugger.raise('Invalid operation in if statement.', me, clause);
131
- _b.label = 12;
132
- case 12:
133
167
  clauses_1_1 = clauses_1.next();
134
168
  return [3 /*break*/, 2];
135
- case 13: return [3 /*break*/, 16];
136
- case 14:
169
+ case 5: return [3 /*break*/, 8];
170
+ case 6:
137
171
  e_1_1 = _b.sent();
138
172
  e_1 = { error: e_1_1 };
139
- return [3 /*break*/, 16];
140
- case 15:
173
+ return [3 /*break*/, 8];
174
+ case 7:
141
175
  try {
142
176
  if (clauses_1_1 && !clauses_1_1.done && (_a = clauses_1.return)) _a.call(clauses_1);
143
177
  }
144
178
  finally { if (e_1) throw e_1.error; }
145
179
  return [7 /*endfinally*/];
146
- case 16: return [2 /*return*/];
180
+ case 8: return [2 /*return*/];
147
181
  }
148
182
  });
149
183
  });
@@ -71,7 +71,7 @@ var NotOperation = /** @class */ (function (_super) {
71
71
  case 0:
72
72
  me = this;
73
73
  if (!(0, typer_1.isCustomValue)(me.arg)) return [3 /*break*/, 1];
74
- arg = me.arg.valueOf();
74
+ arg = me.arg.toTruthy();
75
75
  return [3 /*break*/, 4];
76
76
  case 1:
77
77
  if (!(me.arg instanceof expression_1.Expression)) return [3 /*break*/, 3];
@@ -79,7 +79,7 @@ var NotOperation = /** @class */ (function (_super) {
79
79
  case 2:
80
80
  arg = _a.sent();
81
81
  if ((0, typer_1.isCustomValue)(arg)) {
82
- arg = arg.valueOf();
82
+ arg = arg.toTruthy();
83
83
  }
84
84
  return [3 /*break*/, 4];
85
85
  case 3: