greybel-interpreter 0.6.0 → 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.
package/dist/context.d.ts CHANGED
@@ -86,7 +86,6 @@ export declare class OperationContext {
86
86
  lookupAPI(): OperationContext;
87
87
  lookupGlobals(): OperationContext;
88
88
  lookupLocals(): OperationContext;
89
- valueOf(): CustomMap;
90
89
  extend(map: Map<string, any>): OperationContext;
91
90
  set(path: any[], value: any): Promise<void>;
92
91
  get(path: any[]): Promise<any>;
package/dist/context.js CHANGED
@@ -365,9 +365,6 @@ var OperationContext = /** @class */ (function () {
365
365
  OperationContext.prototype.lookupLocals = function () {
366
366
  return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
367
367
  };
368
- OperationContext.prototype.valueOf = function () {
369
- return this.scope;
370
- };
371
368
  OperationContext.prototype.extend = function (map) {
372
369
  var _a;
373
370
  var me = this;
@@ -4,7 +4,8 @@ export default class CustomBoolean extends CustomLiteralType {
4
4
  value: boolean;
5
5
  constructor(value: boolean);
6
6
  getType(): string;
7
- valueOf(): boolean;
8
7
  toString(): string;
9
8
  fork(): CustomBoolean;
9
+ toNumber(): number;
10
+ toTruthy(): boolean;
10
11
  }
@@ -26,15 +26,18 @@ var CustomBoolean = /** @class */ (function (_super) {
26
26
  CustomBoolean.prototype.getType = function () {
27
27
  return 'boolean';
28
28
  };
29
- CustomBoolean.prototype.valueOf = function () {
30
- return this.value;
31
- };
32
29
  CustomBoolean.prototype.toString = function () {
33
30
  return this.value.toString();
34
31
  };
35
32
  CustomBoolean.prototype.fork = function () {
36
33
  return new CustomBoolean(this.value);
37
34
  };
35
+ CustomBoolean.prototype.toNumber = function () {
36
+ return Number(this.value);
37
+ };
38
+ CustomBoolean.prototype.toTruthy = function () {
39
+ return this.value;
40
+ };
38
41
  CustomBoolean.intrinsics = new Map();
39
42
  return CustomBoolean;
40
43
  }(custom_type_1.CustomLiteralType));
@@ -18,7 +18,8 @@ export default class CustomList extends CustomObjectType {
18
18
  get(path: any[]): Promise<any>;
19
19
  getCallable(path: any[]): Promise<Callable>;
20
20
  getType(): string;
21
- valueOf(): CustomList | null;
22
21
  toString(): string;
22
+ toNumber(): number;
23
+ toTruthy(): boolean;
23
24
  fork(): CustomList;
24
25
  }
@@ -96,7 +96,7 @@ var CustomList = /** @class */ (function (_super) {
96
96
  function CustomList(value) {
97
97
  var _this = _super.call(this) || this;
98
98
  _this.slice = function (a, b) {
99
- return new CustomList(this.value.slice(a === null || a === void 0 ? void 0 : a.valueOf(), b === null || b === void 0 ? void 0 : b.valueOf()));
99
+ return new CustomList(this.value.slice(a.toNumber(), b.toNumber()));
100
100
  };
101
101
  _this.value = value;
102
102
  return _this;
@@ -213,16 +213,17 @@ var CustomList = /** @class */ (function (_super) {
213
213
  CustomList.prototype.getType = function () {
214
214
  return 'list';
215
215
  };
216
- CustomList.prototype.valueOf = function () {
217
- var me = this;
218
- var value = me.value;
219
- return value.length === 0 ? null : me;
220
- };
221
216
  CustomList.prototype.toString = function () {
222
217
  var me = this;
223
- var body = me.value.map(function (item) { var _a; return (_a = item === null || item === void 0 ? void 0 : item.valueOf()) === null || _a === void 0 ? void 0 : _a.toString(); });
218
+ var body = me.value.map(function (item) { return item === null || item === void 0 ? void 0 : item.toString(); });
224
219
  return "[".concat(body.join(','), "]");
225
220
  };
221
+ CustomList.prototype.toNumber = function () {
222
+ return 0;
223
+ };
224
+ CustomList.prototype.toTruthy = function () {
225
+ return this.value.length > 0;
226
+ };
226
227
  CustomList.prototype.fork = function () {
227
228
  return new CustomList(this.value);
228
229
  };
@@ -22,7 +22,8 @@ export default class CustomMap extends CustomObjectType implements Iterable<Cust
22
22
  getCallable(path: string[]): Promise<Callable>;
23
23
  createInstance(): CustomMap;
24
24
  getType(): string;
25
- valueOf(): CustomMap | null;
25
+ toNumber(): number;
26
+ toTruthy(): boolean;
26
27
  toString(): string;
27
28
  fork(): CustomMap;
28
29
  }
@@ -216,10 +216,11 @@ var CustomMap = /** @class */ (function (_super) {
216
216
  var me = this;
217
217
  return ((_a = me.value.get('classID')) === null || _a === void 0 ? void 0 : _a.toString()) || 'map';
218
218
  };
219
- CustomMap.prototype.valueOf = function () {
220
- var me = this;
221
- var value = me.value;
222
- return value.size === 0 ? null : me;
219
+ CustomMap.prototype.toNumber = function () {
220
+ return Number.NaN;
221
+ };
222
+ CustomMap.prototype.toTruthy = function () {
223
+ return this.value.size > 0;
223
224
  };
224
225
  CustomMap.prototype.toString = function () {
225
226
  var me = this;
@@ -227,7 +228,7 @@ var CustomMap = /** @class */ (function (_super) {
227
228
  .entries(me.value)
228
229
  .map(function (_a) {
229
230
  var _b = __read(_a, 2), key = _b[0], value = _b[1];
230
- return "\"".concat(key, "\": ").concat(value.valueOf().toString());
231
+ return "\"".concat(key, "\": ").concat(value === null || value === void 0 ? void 0 : value.toString());
231
232
  });
232
233
  return "{".concat(body.join(','), "}");
233
234
  };
@@ -3,6 +3,7 @@ export default class CustomNil extends CustomLiteralType {
3
3
  static intrinsics: Map<string, Function>;
4
4
  value: null;
5
5
  getType(): string;
6
- valueOf(): null;
6
+ toNumber(): number;
7
+ toTruthy(): boolean;
7
8
  toString(): string;
8
9
  }
@@ -26,8 +26,11 @@ var CustomNil = /** @class */ (function (_super) {
26
26
  CustomNil.prototype.getType = function () {
27
27
  return 'null';
28
28
  };
29
- CustomNil.prototype.valueOf = function () {
30
- return null;
29
+ CustomNil.prototype.toNumber = function () {
30
+ return Number.NaN;
31
+ };
32
+ CustomNil.prototype.toTruthy = function () {
33
+ return false;
31
34
  };
32
35
  CustomNil.prototype.toString = function () {
33
36
  return 'null';
@@ -4,7 +4,8 @@ export default class CustomNumber extends CustomLiteralType {
4
4
  value: number;
5
5
  constructor(value: number);
6
6
  getType(): string;
7
- valueOf(): number;
8
7
  toString(): string;
8
+ toNumber(): number;
9
+ toTruthy(): boolean;
9
10
  fork(): CustomNumber;
10
11
  }
@@ -26,12 +26,15 @@ var CustomNumber = /** @class */ (function (_super) {
26
26
  CustomNumber.prototype.getType = function () {
27
27
  return 'number';
28
28
  };
29
- CustomNumber.prototype.valueOf = function () {
30
- return this.value;
31
- };
32
29
  CustomNumber.prototype.toString = function () {
33
30
  return this.value.toString();
34
31
  };
32
+ CustomNumber.prototype.toNumber = function () {
33
+ return this.value;
34
+ };
35
+ CustomNumber.prototype.toTruthy = function () {
36
+ return Number.isNaN(this.value) ? false : !!this.value;
37
+ };
35
38
  CustomNumber.prototype.fork = function () {
36
39
  return new CustomNumber(this.value);
37
40
  };
@@ -17,7 +17,8 @@ export default class CustomString extends CustomLiteralType {
17
17
  get(path: any[]): Promise<any>;
18
18
  getCallable(path: any[]): Promise<Callable>;
19
19
  getType(): string;
20
- valueOf(): string | null;
21
20
  toString(): string;
21
+ toNumber(): number;
22
+ toTruthy(): boolean;
22
23
  fork(): CustomString;
23
24
  }
@@ -97,7 +97,7 @@ var CustomString = /** @class */ (function (_super) {
97
97
  return !Number.isNaN(Number(value));
98
98
  };
99
99
  CustomString.prototype.slice = function (a, b) {
100
- return new CustomString(this.value.slice(a === null || a === void 0 ? void 0 : a.valueOf(), b === null || b === void 0 ? void 0 : b.valueOf()));
100
+ return new CustomString(this.value.slice(a === null || a === void 0 ? void 0 : a.toNumber(), b === null || b === void 0 ? void 0 : b.toNumber()));
101
101
  };
102
102
  CustomString.prototype[Symbol.iterator] = function () {
103
103
  return new CustomStringIterator(this.value);
@@ -151,13 +151,15 @@ var CustomString = /** @class */ (function (_super) {
151
151
  CustomString.prototype.getType = function () {
152
152
  return 'string';
153
153
  };
154
- CustomString.prototype.valueOf = function () {
155
- var me = this;
156
- return me.value.length === 0 ? null : me.value;
157
- };
158
154
  CustomString.prototype.toString = function () {
159
155
  return this.value;
160
156
  };
157
+ CustomString.prototype.toNumber = function () {
158
+ return Number(this.value);
159
+ };
160
+ CustomString.prototype.toTruthy = function () {
161
+ return this.value.length > 0;
162
+ };
161
163
  CustomString.prototype.fork = function () {
162
164
  return new CustomString(this.value);
163
165
  };
@@ -2,7 +2,6 @@ import { ASTUnaryExpression } from 'greybel-core';
2
2
  import { Expression } from '../types/expression';
3
3
  import { OperationContext } from '../context';
4
4
  import { CustomType } from '../types/custom-type';
5
- export declare const toPrimitive: (v: CustomType | any) => any;
6
5
  export declare type OperationMap = {
7
6
  [key: string]: (a: CustomType) => any;
8
7
  };
@@ -52,17 +52,13 @@ 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.toPrimitive = void 0;
55
+ exports.ExpressionSegment = void 0;
56
56
  var greybel_core_1 = require("greybel-core");
57
57
  var expression_1 = require("../types/expression");
58
58
  var typer_1 = require("../typer");
59
- var toPrimitive = function (v) {
60
- return (0, typer_1.isCustomValue)(v) ? v.valueOf() : v;
61
- };
62
- exports.toPrimitive = toPrimitive;
63
59
  var OPERATIONS = (_a = {},
64
- _a[greybel_core_1.Operator.Plus] = function (a) { return (0, exports.toPrimitive)(a); },
65
- _a[greybel_core_1.Operator.Minus] = function (a) { return -(0, exports.toPrimitive)(a); },
60
+ _a[greybel_core_1.Operator.Plus] = function (a) { return a.toNumber(); },
61
+ _a[greybel_core_1.Operator.Minus] = function (a) { return -a.toNumber(); },
66
62
  _a);
67
63
  var ExpressionSegment = /** @class */ (function () {
68
64
  function ExpressionSegment(operator, arg) {
@@ -2,7 +2,6 @@ import { ASTEvaluationExpression } from 'greybel-core';
2
2
  import { Expression } from '../types/expression';
3
3
  import { OperationContext } from '../context';
4
4
  import { CustomType } from '../types/custom-type';
5
- export declare const toPrimitive: (v: CustomType | any) => any;
6
5
  export declare const multiplyString: (a: CustomType, b: CustomType) => string;
7
6
  export declare type OperationMap = {
8
7
  [key: string]: (a: CustomType, b: CustomType) => any;
@@ -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
  }
@@ -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:
@@ -80,30 +80,33 @@ var WhileOperation = /** @class */ (function (_super) {
80
80
  isBreak: false,
81
81
  isContinue: false
82
82
  };
83
- resolveCondition = function () {
83
+ resolveCondition = function (item) {
84
84
  return __awaiter(this, void 0, void 0, function () {
85
85
  var value, value;
86
86
  return __generator(this, function (_a) {
87
87
  switch (_a.label) {
88
88
  case 0:
89
- if (!(me.condition instanceof expression_1.Expression)) return [3 /*break*/, 2];
90
- return [4 /*yield*/, me.condition.get(opc)];
89
+ if (!(item instanceof expression_1.Expression)) return [3 /*break*/, 2];
90
+ return [4 /*yield*/, item.get(opc)];
91
91
  case 1:
92
92
  value = _a.sent();
93
- return [2 /*return*/, !!(value === null || value === void 0 ? void 0 : value.valueOf())];
93
+ return [2 /*return*/, resolveCondition(value)];
94
94
  case 2:
95
- if (!(me.condition instanceof operation_1.Operation)) return [3 /*break*/, 4];
96
- return [4 /*yield*/, me.condition.get(opc)];
95
+ if (!(item instanceof operation_1.Operation)) return [3 /*break*/, 4];
96
+ return [4 /*yield*/, item.get(opc)];
97
97
  case 3:
98
98
  value = _a.sent();
99
- return [2 /*return*/, !!(value === null || value === void 0 ? void 0 : value.valueOf())];
99
+ return [2 /*return*/, resolveCondition(value)];
100
100
  case 4:
101
- if ((0, typer_1.isCustomValue)(me.condition)) {
102
- return [2 /*return*/, me.condition.valueOf()];
101
+ if ((0, typer_1.isCustomValue)(item)) {
102
+ return [2 /*return*/, item.toTruthy()];
103
+ }
104
+ else if (typeof item === 'boolean') {
105
+ return [2 /*return*/, item];
103
106
  }
104
107
  _a.label = 5;
105
108
  case 5:
106
- operationContext.debugger.raise('Unexpected condition', me, me.condition);
109
+ operationContext.debugger.raise('Unexpected condition', me, item);
107
110
  return [2 /*return*/];
108
111
  }
109
112
  });
@@ -111,7 +114,7 @@ var WhileOperation = /** @class */ (function (_super) {
111
114
  };
112
115
  opc.setMemory('loopContext', loopContext);
113
116
  _a.label = 1;
114
- case 1: return [4 /*yield*/, resolveCondition()];
117
+ case 1: return [4 /*yield*/, resolveCondition(me.condition)];
115
118
  case 2:
116
119
  if (!_a.sent()) return [3 /*break*/, 4];
117
120
  loopContext.isContinue = false;
@@ -1,8 +1,9 @@
1
1
  export declare abstract class CustomType {
2
2
  static intrinsics: Map<string, Function>;
3
3
  getType(): string;
4
- valueOf(): any;
4
+ toNumber(): number;
5
5
  toString(): string;
6
+ toTruthy(): boolean;
6
7
  }
7
8
  export declare abstract class CustomLiteralType extends CustomType {
8
9
  value: any;
@@ -22,12 +22,15 @@ var CustomType = /** @class */ (function () {
22
22
  CustomType.prototype.getType = function () {
23
23
  throw new Error('Implentation of "getType" missing');
24
24
  };
25
- CustomType.prototype.valueOf = function () {
26
- throw new Error('Implentation of "valueOf" missing');
25
+ CustomType.prototype.toNumber = function () {
26
+ throw new Error('Implentation of "toNumber" missing');
27
27
  };
28
28
  CustomType.prototype.toString = function () {
29
29
  throw new Error('Implentation of "toString" missing');
30
30
  };
31
+ CustomType.prototype.toTruthy = function () {
32
+ throw new Error('Implentation of "toTruthy" missing');
33
+ };
31
34
  return CustomType;
32
35
  }());
33
36
  exports.CustomType = CustomType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",