greybel-interpreter 0.6.0 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,9 @@ 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;
11
+ valueOf(): boolean;
10
12
  }
@@ -26,15 +26,21 @@ 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
+ };
41
+ CustomBoolean.prototype.valueOf = function () {
42
+ return this.value;
43
+ };
38
44
  CustomBoolean.intrinsics = new Map();
39
45
  return CustomBoolean;
40
46
  }(custom_type_1.CustomLiteralType));
@@ -18,7 +18,9 @@ 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;
24
+ valueOf(): any[];
23
25
  fork(): CustomList;
24
26
  }
@@ -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,20 @@ 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
+ };
227
+ CustomList.prototype.valueOf = function () {
228
+ return this.value;
229
+ };
226
230
  CustomList.prototype.fork = function () {
227
231
  return new CustomList(this.value);
228
232
  };
@@ -22,7 +22,9 @@ 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;
28
+ valueOf(): Map<string, any>;
27
29
  fork(): CustomMap;
28
30
  }
@@ -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,10 +228,13 @@ 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
  };
235
+ CustomMap.prototype.valueOf = function () {
236
+ return this.value;
237
+ };
234
238
  CustomMap.prototype.fork = function () {
235
239
  return new CustomMap(this.value);
236
240
  };
@@ -3,6 +3,8 @@ 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;
9
+ valueOf(): any[];
8
10
  }
@@ -26,12 +26,18 @@ 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';
34
37
  };
38
+ CustomNil.prototype.valueOf = function () {
39
+ return null;
40
+ };
35
41
  CustomNil.intrinsics = new Map();
36
42
  return CustomNil;
37
43
  }(custom_type_1.CustomLiteralType));
@@ -4,7 +4,9 @@ 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;
10
+ valueOf(): number;
9
11
  fork(): CustomNumber;
10
12
  }
@@ -26,12 +26,18 @@ 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
+ };
38
+ CustomNumber.prototype.valueOf = function () {
39
+ return this.value;
40
+ };
35
41
  CustomNumber.prototype.fork = function () {
36
42
  return new CustomNumber(this.value);
37
43
  };
@@ -17,7 +17,9 @@ 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;
23
+ valueOf(): string;
22
24
  fork(): CustomString;
23
25
  }
@@ -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,18 @@ 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
+ };
163
+ CustomString.prototype.valueOf = function () {
164
+ return this.value;
165
+ };
161
166
  CustomString.prototype.fork = function () {
162
167
  return new CustomString(this.value);
163
168
  };
@@ -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.valueOf() >= b.valueOf(); },
102
+ _a[greybel_core_1.Operator.Equal] = function (a, b) { return a.valueOf() == b.valueOf(); },
103
+ _a[greybel_core_1.Operator.LessThanOrEqual] = function (a, b) { return a.valueOf() <= b.valueOf(); },
104
+ _a[greybel_core_1.Operator.NotEqual] = function (a, b) { return a.valueOf() != b.valueOf(); },
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,8 @@ 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;
22
+ valueOf(): FunctionOperation;
20
23
  run(operationContext: OperationContext): Promise<any>;
21
24
  }
@@ -85,6 +85,15 @@ 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 Number.NaN;
93
+ };
94
+ FunctionOperation.prototype.valueOf = function () {
95
+ return this;
96
+ };
88
97
  FunctionOperation.prototype.run = function (operationContext) {
89
98
  return __awaiter(this, void 0, void 0, function () {
90
99
  var me, opc, incArgs, args, argMap, functionContext, index, max;
@@ -82,7 +82,7 @@ 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, clauses_1, clauses_1_1, clause, condition, resolveCondition, e_1_1;
86
86
  var e_1, _a;
87
87
  return __generator(this, function (_b) {
88
88
  switch (_b.label) {
@@ -91,59 +91,75 @@ var IfStatementOperation = /** @class */ (function (_super) {
91
91
  clauses = me.clauses;
92
92
  _b.label = 1;
93
93
  case 1:
94
- _b.trys.push([1, 14, 15, 16]);
94
+ _b.trys.push([1, 11, 12, 13]);
95
95
  clauses_1 = __values(clauses), clauses_1_1 = clauses_1.next();
96
96
  _b.label = 2;
97
97
  case 2:
98
- if (!!clauses_1_1.done) return [3 /*break*/, 13];
98
+ if (!!clauses_1_1.done) return [3 /*break*/, 10];
99
99
  clause = clauses_1_1.value;
100
- if (!(clause instanceof if_1.default || clause instanceof else_if_1.default)) return [3 /*break*/, 9];
100
+ if (!(clause instanceof if_1.default || clause instanceof else_if_1.default)) return [3 /*break*/, 6];
101
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];
102
+ resolveCondition = function (item) {
103
+ return __awaiter(this, void 0, void 0, function () {
104
+ var value, value;
105
+ return __generator(this, function (_a) {
106
+ switch (_a.label) {
107
+ case 0:
108
+ if (!(item instanceof expression_1.Expression)) return [3 /*break*/, 2];
109
+ return [4 /*yield*/, item.get(operationContext)];
110
+ case 1:
111
+ value = _a.sent();
112
+ return [2 /*return*/, value.toTruthy()];
113
+ case 2:
114
+ if (!(item instanceof operation_1.Operation)) return [3 /*break*/, 4];
115
+ return [4 /*yield*/, item.get(operationContext)];
116
+ case 3:
117
+ value = _a.sent();
118
+ return [2 /*return*/, value.toTruthy()];
119
+ case 4:
120
+ if ((0, typer_1.isCustomValue)(item)) {
121
+ return [2 /*return*/, item.toTruthy()];
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(condition)];
106
132
  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)];
133
+ if (!_b.sent()) return [3 /*break*/, 5];
134
+ return [4 /*yield*/, clause.body.run(operationContext)];
110
135
  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;
136
+ _b.sent();
137
+ return [3 /*break*/, 10];
138
+ case 5: return [3 /*break*/, 9];
116
139
  case 6:
117
- if (!isValid.valueOf()) return [3 /*break*/, 8];
140
+ if (!(clause instanceof else_1.default)) return [3 /*break*/, 8];
118
141
  return [4 /*yield*/, clause.body.run(operationContext)];
119
142
  case 7:
120
143
  _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:
144
+ return [3 /*break*/, 10];
145
+ case 8:
130
146
  operationContext.debugger.raise('Invalid operation in if statement.', me, clause);
131
- _b.label = 12;
132
- case 12:
147
+ _b.label = 9;
148
+ case 9:
133
149
  clauses_1_1 = clauses_1.next();
134
150
  return [3 /*break*/, 2];
135
- case 13: return [3 /*break*/, 16];
136
- case 14:
151
+ case 10: return [3 /*break*/, 13];
152
+ case 11:
137
153
  e_1_1 = _b.sent();
138
154
  e_1 = { error: e_1_1 };
139
- return [3 /*break*/, 16];
140
- case 15:
155
+ return [3 /*break*/, 13];
156
+ case 12:
141
157
  try {
142
158
  if (clauses_1_1 && !clauses_1_1.done && (_a = clauses_1.return)) _a.call(clauses_1);
143
159
  }
144
160
  finally { if (e_1) throw e_1.error; }
145
161
  return [7 /*endfinally*/];
146
- case 16: return [2 /*return*/];
162
+ case 13: return [2 /*return*/];
147
163
  }
148
164
  });
149
165
  });
@@ -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,13 +79,13 @@ 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:
86
86
  operationContext.debugger.raise('Unexpected not operation', me, me.arg);
87
87
  _a.label = 4;
88
- case 4: return [2 /*return*/, !arg];
88
+ case 4: return [2 /*return*/, (0, typer_1.cast)(!arg)];
89
89
  }
90
90
  });
91
91
  });
@@ -80,30 +80,30 @@ 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*/, value.toTruthy()];
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*/, value.toTruthy()];
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
103
  }
104
104
  _a.label = 5;
105
105
  case 5:
106
- operationContext.debugger.raise('Unexpected condition', me, me.condition);
106
+ operationContext.debugger.raise('Unexpected condition', me, item);
107
107
  return [2 /*return*/];
108
108
  }
109
109
  });
@@ -111,7 +111,7 @@ var WhileOperation = /** @class */ (function (_super) {
111
111
  };
112
112
  opc.setMemory('loopContext', loopContext);
113
113
  _a.label = 1;
114
- case 1: return [4 /*yield*/, resolveCondition()];
114
+ case 1: return [4 /*yield*/, resolveCondition(me.condition)];
115
115
  case 2:
116
116
  if (!_a.sent()) return [3 /*break*/, 4];
117
117
  loopContext.isContinue = false;
@@ -1,8 +1,10 @@
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;
7
+ valueOf(): any;
6
8
  }
7
9
  export declare abstract class CustomLiteralType extends CustomType {
8
10
  value: any;
@@ -22,12 +22,18 @@ 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
+ };
34
+ CustomType.prototype.valueOf = function () {
35
+ throw new Error('Implentation of "valueOf" missing');
36
+ };
31
37
  return CustomType;
32
38
  }());
33
39
  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.4",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -34,8 +34,8 @@
34
34
  "typescript": "^4.5.4"
35
35
  },
36
36
  "dependencies": {
37
- "greybel-core": "^0.2.3",
38
- "greyscript-core": "^0.2.4",
37
+ "greybel-core": "^0.2.5",
38
+ "greyscript-core": "^0.2.6",
39
39
  "uuid": "^8.3.2"
40
40
  },
41
41
  "keywords": [