greybel-interpreter 0.5.4 → 0.5.8

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
@@ -18,7 +18,6 @@ export declare enum ContextState {
18
18
  export declare class Scope extends CustomMap {
19
19
  context: OperationContext;
20
20
  constructor(context: OperationContext);
21
- set(path: string[], value: any): Promise<void>;
22
21
  get(path: string[]): Promise<any>;
23
22
  getCallable(path: string[]): Promise<Callable>;
24
23
  }
@@ -70,9 +69,9 @@ export declare class OperationContext {
70
69
  processState: OperationContextProcessState;
71
70
  isProtected: boolean;
72
71
  injected: boolean;
73
- api: Scope | null;
74
- locals: Scope | null;
75
- globals: Scope | null;
72
+ api: OperationContext | null;
73
+ locals: OperationContext | null;
74
+ globals: OperationContext | null;
76
75
  constructor(options: OperationContextOptions);
77
76
  step(item: ASTBase): Promise<void>;
78
77
  setLastActive(opc: OperationContext): OperationContext;
@@ -81,10 +80,12 @@ export declare class OperationContext {
81
80
  exit(): Promise<OperationContext>;
82
81
  isPending(): boolean;
83
82
  setPending(pending: boolean): OperationContext;
84
- lookupType(validate: (type: ContextType) => boolean): Scope;
85
- lookupAPI(): Scope;
86
- lookupGlobals(): Scope;
87
- lookupLocals(): Scope;
83
+ lookupAllOfType(validate: (type: ContextType) => boolean): OperationContext[];
84
+ lookupType(validate: (type: ContextType) => boolean): OperationContext;
85
+ lookupAllScopes(): OperationContext[];
86
+ lookupAPI(): OperationContext;
87
+ lookupGlobals(): OperationContext;
88
+ lookupLocals(): OperationContext;
88
89
  valueOf(): CustomMap;
89
90
  extend(map: Map<string, any>): OperationContext;
90
91
  set(path: any[], value: any): Promise<void>;
package/dist/context.js CHANGED
@@ -14,6 +14,42 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ 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;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
17
53
  var __read = (this && this.__read) || function (o, n) {
18
54
  var m = typeof Symbol === "function" && o[Symbol.iterator];
19
55
  if (!m) return o;
@@ -67,87 +103,81 @@ var Scope = /** @class */ (function (_super) {
67
103
  _this.context = context;
68
104
  return _this;
69
105
  }
70
- Scope.prototype.set = function (path, value) {
71
- var me = this;
72
- var traversalPath = [].concat(path);
73
- var current = traversalPath.shift();
74
- if (current != null) {
75
- if (current === 'locals') {
76
- return me.context.locals.set(traversalPath, value);
77
- }
78
- else if (current === 'globals') {
79
- return me.context.globals.set(traversalPath, value);
80
- }
81
- else if (me.context.locals != null) {
82
- return map_1.default.prototype.set.call(me.context.locals, path, value);
83
- }
84
- else {
85
- throw new Error("Cannot set path ".concat(path.join('.')));
86
- }
87
- }
88
- };
89
106
  Scope.prototype.get = function (path) {
90
- var _a, _b, _c;
91
- var me = this;
92
- var traversalPath = [].concat(path);
93
- var current = traversalPath.shift();
94
- if (current != null) {
95
- if (current === 'locals') {
96
- return traversalPath.length === 0
97
- ? Promise.resolve(me.context.locals)
98
- : me.context.locals.get(traversalPath);
99
- }
100
- else if (current === 'globals') {
101
- return traversalPath.length === 0
102
- ? Promise.resolve(me.context.globals)
103
- : me.context.globals.get(traversalPath);
104
- }
105
- else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
106
- return map_1.default.prototype.get.call(me.context.locals, path);
107
- }
108
- else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
109
- return map_1.default.prototype.get.call(me.context.globals, path);
110
- }
111
- else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
112
- return map_1.default.prototype.get.call(me.context.api, path);
113
- }
114
- else if (me.value.has(current)) {
115
- return _super.prototype.get.call(this, path);
116
- }
117
- else {
118
- throw new Error("Cannot get path ".concat(path.join('.')));
119
- }
120
- }
121
- return null;
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var me, traversalPath, current;
109
+ return __generator(this, function (_a) {
110
+ switch (_a.label) {
111
+ case 0:
112
+ me = this;
113
+ traversalPath = [].concat(path);
114
+ current = traversalPath.shift();
115
+ if (path.length === 0) {
116
+ return [2 /*return*/, Promise.resolve(me)];
117
+ }
118
+ if (!(current === 'locals' || current === 'globals')) return [3 /*break*/, 1];
119
+ return [2 /*return*/, me.context.get(path)];
120
+ case 1: return [4 /*yield*/, me.has(path)];
121
+ case 2:
122
+ if (!_a.sent()) return [3 /*break*/, 3];
123
+ return [2 /*return*/, _super.prototype.get.call(this, path)];
124
+ case 3: return [4 /*yield*/, me.context.api.scope.has(path)];
125
+ case 4:
126
+ if (_a.sent()) {
127
+ return [2 /*return*/, me.context.api.get(path)];
128
+ }
129
+ else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
130
+ return [2 /*return*/, Promise.resolve(map_1.default.intrinsics.get(current).bind(null, me))];
131
+ }
132
+ else if (me.context.previous) {
133
+ return [2 /*return*/, me.context.previous.get(path)];
134
+ }
135
+ else {
136
+ throw new Error("Cannot get path ".concat(path.join('.')));
137
+ }
138
+ _a.label = 5;
139
+ case 5: return [2 /*return*/];
140
+ }
141
+ });
142
+ });
122
143
  };
123
144
  Scope.prototype.getCallable = function (path) {
124
- var _a, _b, _c;
125
- var me = this;
126
- var traversalPath = [].concat(path);
127
- var current = traversalPath.shift();
128
- if (current != null) {
129
- if (current === 'locals') {
130
- return me.context.locals.getCallable(traversalPath);
131
- }
132
- else if (current === 'globals') {
133
- return me.context.globals.getCallable(traversalPath);
134
- }
135
- else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
136
- return map_1.default.prototype.getCallable.call(me.context.locals, path);
137
- }
138
- else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
139
- return map_1.default.prototype.getCallable.call(me.context.globals, path);
140
- }
141
- else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
142
- return map_1.default.prototype.getCallable.call(me.context.api, path);
143
- }
144
- else {
145
- return _super.prototype.getCallable.call(this, path);
146
- }
147
- }
148
- return Promise.resolve({
149
- origin: me,
150
- context: null
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var me, traversalPath, current;
147
+ return __generator(this, function (_a) {
148
+ switch (_a.label) {
149
+ case 0:
150
+ me = this;
151
+ traversalPath = [].concat(path);
152
+ current = traversalPath.shift();
153
+ if (current === 'locals' || current === 'globals') {
154
+ return [2 /*return*/, me.context.getCallable(path)];
155
+ }
156
+ return [4 /*yield*/, me.has(path)];
157
+ case 1:
158
+ if (!_a.sent()) return [3 /*break*/, 2];
159
+ return [2 /*return*/, _super.prototype.getCallable.call(this, path)];
160
+ case 2: return [4 /*yield*/, me.context.api.scope.has(path)];
161
+ case 3:
162
+ if (_a.sent()) {
163
+ return [2 /*return*/, me.context.api.getCallable(path)];
164
+ }
165
+ else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
166
+ return [2 /*return*/, Promise.resolve({
167
+ origin: map_1.default.intrinsics.get(current).bind(null, me),
168
+ context: me
169
+ })];
170
+ }
171
+ else if (me.context.previous) {
172
+ return [2 /*return*/, me.context.previous.getCallable(path)];
173
+ }
174
+ else {
175
+ throw new Error("Cannot get callable path ".concat(path.join('.')));
176
+ }
177
+ _a.label = 4;
178
+ case 4: return [2 /*return*/];
179
+ }
180
+ });
151
181
  });
152
182
  };
153
183
  return Scope;
@@ -238,7 +268,7 @@ var OperationContext = /** @class */ (function () {
238
268
  };
239
269
  me.api = me.lookupAPI();
240
270
  me.globals = me.lookupGlobals();
241
- me.locals = me.lookupLocals();
271
+ me.locals = me.lookupLocals() || me;
242
272
  }
243
273
  OperationContext.prototype.step = function (item) {
244
274
  var me = this;
@@ -294,20 +324,38 @@ var OperationContext = /** @class */ (function () {
294
324
  me.processState.pending = pending;
295
325
  return me;
296
326
  };
327
+ OperationContext.prototype.lookupAllOfType = function (validate) {
328
+ var me = this;
329
+ var result = [];
330
+ if (validate(me.type)) {
331
+ result.push(me);
332
+ }
333
+ var current = me.previous;
334
+ while (current) {
335
+ if (validate(current.type)) {
336
+ result.push(current);
337
+ }
338
+ current = current.previous;
339
+ }
340
+ return result;
341
+ };
297
342
  OperationContext.prototype.lookupType = function (validate) {
298
343
  var me = this;
299
344
  if (validate(me.type)) {
300
- return me.scope;
345
+ return me;
301
346
  }
302
347
  var current = me.previous;
303
348
  while (current) {
304
349
  if (validate(current.type)) {
305
- return current.scope;
350
+ return current;
306
351
  }
307
352
  current = current.previous;
308
353
  }
309
354
  return null;
310
355
  };
356
+ OperationContext.prototype.lookupAllScopes = function () {
357
+ return this.lookupAllOfType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
358
+ };
311
359
  OperationContext.prototype.lookupAPI = function () {
312
360
  return this.lookupType(function (type) { return [ContextType.API].includes(type); });
313
361
  };
@@ -318,7 +366,7 @@ var OperationContext = /** @class */ (function () {
318
366
  return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
319
367
  };
320
368
  OperationContext.prototype.valueOf = function () {
321
- return this.scope.valueOf();
369
+ return this.scope;
322
370
  };
323
371
  OperationContext.prototype.extend = function (map) {
324
372
  var _a;
@@ -334,28 +382,46 @@ var OperationContext = /** @class */ (function () {
334
382
  OperationContext.prototype.set = function (path, value) {
335
383
  var _a;
336
384
  var me = this;
385
+ var traversalPath = [].concat(path);
386
+ var current = traversalPath.shift();
387
+ if (current === 'locals') {
388
+ return me.locals.set(traversalPath, value);
389
+ }
390
+ else if (current === 'globals') {
391
+ return me.globals.set(traversalPath, value);
392
+ }
337
393
  if (me.state === ContextState.TEMPORARY) {
338
394
  return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
339
395
  }
340
- else {
341
- return me.scope.set(path, value);
342
- }
396
+ return me.locals.scope.set(path, value);
343
397
  };
344
398
  OperationContext.prototype.get = function (path) {
345
399
  var _a;
346
400
  var me = this;
347
- if (me.state === ContextState.TEMPORARY) {
401
+ var traversalPath = [].concat(path);
402
+ var current = traversalPath.shift();
403
+ if (current === 'locals') {
404
+ return me.locals.get(traversalPath);
405
+ }
406
+ else if (current === 'globals') {
407
+ return me.globals.get(traversalPath);
408
+ }
409
+ else if (me.state === ContextState.TEMPORARY) {
348
410
  return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
349
411
  }
350
- return me.scope.get(path);
412
+ return me.locals.scope.get(path);
351
413
  };
352
414
  OperationContext.prototype.getCallable = function (path) {
353
- var _a;
354
415
  var me = this;
355
- if (me.state === ContextState.TEMPORARY) {
356
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.getCallable(path);
416
+ var traversalPath = [].concat(path);
417
+ var current = traversalPath.shift();
418
+ if (current === 'locals') {
419
+ return me.locals.getCallable(traversalPath);
420
+ }
421
+ else if (current === 'globals') {
422
+ return me.globals.getCallable(traversalPath);
357
423
  }
358
- return me.scope.getCallable(path);
424
+ return me.locals.scope.getCallable(path);
359
425
  };
360
426
  OperationContext.prototype.setMemory = function (key, value) {
361
427
  var me = this;
@@ -5,6 +5,7 @@ export declare class CustomListIterator implements Iterator<any> {
5
5
  constructor(value: any);
6
6
  next(): IteratorResult<any>;
7
7
  }
8
+ export declare function itemAtIndex(list: any[], n: number): number;
8
9
  export default class CustomList extends CustomObjectType {
9
10
  static intrinsics: Map<string, Function>;
10
11
  value: any[];
@@ -13,7 +14,6 @@ export default class CustomList extends CustomObjectType {
13
14
  [Symbol.iterator](): CustomListIterator;
14
15
  concat(list: CustomList): CustomList;
15
16
  slice: (a: CustomLiteralType, b: CustomLiteralType) => CustomList;
16
- toIndex(value: string): number | null;
17
17
  set(path: any[], value: any): Promise<void>;
18
18
  get(path: any[]): Promise<any>;
19
19
  getCallable(path: any[]): Promise<Callable>;
@@ -76,7 +76,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
76
76
  return to.concat(ar || Array.prototype.slice.call(from));
77
77
  };
78
78
  Object.defineProperty(exports, "__esModule", { value: true });
79
- exports.CustomListIterator = void 0;
79
+ exports.itemAtIndex = exports.CustomListIterator = void 0;
80
80
  var operation_1 = require("../types/operation");
81
81
  var custom_type_1 = require("../types/custom-type");
82
82
  var CustomListIterator = /** @class */ (function () {
@@ -101,6 +101,15 @@ var CustomListIterator = /** @class */ (function () {
101
101
  return CustomListIterator;
102
102
  }());
103
103
  exports.CustomListIterator = CustomListIterator;
104
+ function itemAtIndex(list, n) {
105
+ n = Math.trunc(n) || 0;
106
+ if (n < 0)
107
+ n += list.length;
108
+ if (n < 0 || n >= list.length)
109
+ return -1;
110
+ return n;
111
+ }
112
+ exports.itemAtIndex = itemAtIndex;
104
113
  var CustomList = /** @class */ (function (_super) {
105
114
  __extends(CustomList, _super);
106
115
  function CustomList(value) {
@@ -120,51 +129,34 @@ var CustomList = /** @class */ (function (_super) {
120
129
  CustomList.prototype.concat = function (list) {
121
130
  return new CustomList(this.value.concat(list.value));
122
131
  };
123
- CustomList.prototype.toIndex = function (value) {
124
- var me = this;
125
- var casted = Number(value);
126
- var result = casted < 0 ? me.value.length + casted : casted;
127
- if (result < 0) {
128
- return null;
129
- }
130
- return result;
131
- };
132
132
  CustomList.prototype.set = function (path, value) {
133
133
  return __awaiter(this, void 0, void 0, function () {
134
- var me, traversalPath, refs, last, current, origin, index, setterIndex;
134
+ var me, traversalPath, refs, last, current, indexResult, sub, lastIndex;
135
135
  return __generator(this, function (_a) {
136
136
  me = this;
137
137
  traversalPath = [].concat(path);
138
138
  refs = me.value;
139
139
  last = traversalPath.pop();
140
140
  current = traversalPath.shift();
141
- origin = refs;
142
141
  if (current != null) {
143
- index = me.toIndex(current);
144
- if (origin.hasOwnProperty(index)) {
145
- origin = origin[index];
146
- if (origin instanceof custom_type_1.CustomObjectType) {
147
- return [2 /*return*/, origin.set(traversalPath.concat([last]), value)];
142
+ indexResult = itemAtIndex(refs, Number(current));
143
+ if (refs.hasOwnProperty(indexResult)) {
144
+ sub = refs[indexResult];
145
+ if (sub instanceof custom_type_1.CustomObjectType) {
146
+ return [2 /*return*/, sub.set(traversalPath.concat([last]), value)];
148
147
  }
149
148
  }
150
- else {
151
- throw new Error("Cannot set path ".concat(path.join('.')));
152
- }
149
+ throw new Error("Cannot set path ".concat(path.join('.')));
153
150
  }
154
- if (origin) {
155
- setterIndex = me.toIndex(last);
156
- if (!origin.hasOwnProperty(setterIndex)) {
157
- throw new Error("Index error (list index ".concat(setterIndex, " out of range)"));
158
- }
159
- if (value instanceof operation_1.FunctionOperationBase) {
160
- origin[setterIndex] = value.fork(me);
161
- }
162
- else {
163
- origin[setterIndex] = value;
164
- }
151
+ lastIndex = itemAtIndex(refs, Number(last));
152
+ if (!refs.hasOwnProperty(lastIndex)) {
153
+ throw new Error("Index error (list index ".concat(lastIndex, " out of range)"));
154
+ }
155
+ if (value instanceof operation_1.FunctionOperationBase) {
156
+ refs[lastIndex] = value.fork(me);
165
157
  }
166
158
  else {
167
- throw new Error("Cannot set path ".concat(path.join('.')));
159
+ refs[lastIndex] = value;
168
160
  }
169
161
  return [2 /*return*/];
170
162
  });
@@ -172,7 +164,7 @@ var CustomList = /** @class */ (function (_super) {
172
164
  };
173
165
  CustomList.prototype.get = function (path) {
174
166
  return __awaiter(this, void 0, void 0, function () {
175
- var me, traversalPath, refs, current, currentValue, origin, index;
167
+ var me, traversalPath, refs, current, currentIndex, sub;
176
168
  return __generator(this, function (_a) {
177
169
  me = this;
178
170
  if (path.length === 0) {
@@ -181,46 +173,45 @@ var CustomList = /** @class */ (function (_super) {
181
173
  traversalPath = [].concat(path);
182
174
  refs = me.value;
183
175
  current = traversalPath.shift();
184
- currentValue = current.valueOf();
185
- origin = refs;
186
- if (currentValue != null) {
187
- index = me.toIndex(current);
188
- if (origin.hasOwnProperty(index)) {
189
- origin = origin[index];
190
- if (traversalPath.length > 0 && origin instanceof custom_type_1.CustomObjectType) {
191
- return [2 /*return*/, origin.get(traversalPath)];
176
+ if (current != null) {
177
+ currentIndex = itemAtIndex(refs, Number(current));
178
+ if (refs.hasOwnProperty(currentIndex)) {
179
+ sub = refs[currentIndex];
180
+ if (traversalPath.length > 0 && sub instanceof custom_type_1.CustomObjectType) {
181
+ return [2 /*return*/, sub.get(traversalPath)];
182
+ }
183
+ if (traversalPath.length === 0) {
184
+ return [2 /*return*/, sub];
192
185
  }
193
186
  }
194
- else if (path.length === 1 && CustomList.intrinsics.has(currentValue)) {
195
- return [2 /*return*/, CustomList.intrinsics.get(currentValue).bind(null, me)];
196
- }
197
- else {
198
- throw new Error("Cannot get path ".concat(path.join('.')));
187
+ else if (path.length === 1 && CustomList.intrinsics.has(current)) {
188
+ return [2 /*return*/, CustomList.intrinsics.get(current).bind(null, me)];
199
189
  }
200
190
  }
201
- else {
202
- return [2 /*return*/, null];
203
- }
204
- return [2 /*return*/, origin];
191
+ return [2 /*return*/, null];
205
192
  });
206
193
  });
207
194
  };
208
195
  CustomList.prototype.getCallable = function (path) {
209
196
  return __awaiter(this, void 0, void 0, function () {
210
- var me, traversalPath, refs, current, origin, context, index;
197
+ var me, traversalPath, refs, current, currentIndex, sub;
211
198
  return __generator(this, function (_a) {
212
199
  me = this;
213
200
  traversalPath = [].concat(path);
214
201
  refs = me.value;
215
202
  current = traversalPath.shift();
216
- origin = refs;
217
203
  if (current != null) {
218
- index = me.toIndex(current);
219
- if (origin.hasOwnProperty(index)) {
220
- context = origin;
221
- origin = origin[index];
222
- if (origin instanceof custom_type_1.CustomObjectType) {
223
- return [2 /*return*/, origin.getCallable(traversalPath)];
204
+ currentIndex = itemAtIndex(refs, Number(current));
205
+ if (refs.hasOwnProperty(currentIndex)) {
206
+ sub = refs[currentIndex];
207
+ if (sub instanceof custom_type_1.CustomObjectType) {
208
+ return [2 /*return*/, sub.getCallable(traversalPath)];
209
+ }
210
+ if (traversalPath.length === 0) {
211
+ return [2 /*return*/, Promise.resolve({
212
+ origin: sub,
213
+ context: me
214
+ })];
224
215
  }
225
216
  }
226
217
  else if (path.length === 1 && CustomList.intrinsics.has(current)) {
@@ -229,19 +220,17 @@ var CustomList = /** @class */ (function (_super) {
229
220
  context: me
230
221
  }];
231
222
  }
232
- else {
233
- throw new Error("Cannot get path ".concat(path.join('.')));
234
- }
235
223
  }
236
224
  return [2 /*return*/, {
237
- origin: origin,
238
- context: context
225
+ origin: null,
226
+ context: me
239
227
  }];
240
228
  });
241
229
  });
242
230
  };
243
231
  CustomList.prototype.callMethod = function (method) {
244
232
  var _a;
233
+ var _b;
245
234
  var args = [];
246
235
  for (var _i = 1; _i < arguments.length; _i++) {
247
236
  args[_i - 1] = arguments[_i];
@@ -250,16 +239,16 @@ var CustomList = /** @class */ (function (_super) {
250
239
  throw new Error('Unexpected method length');
251
240
  }
252
241
  var me = this;
253
- var member = method[0].valueOf();
242
+ var member = (_b = method[0]) === null || _b === void 0 ? void 0 : _b.toString();
254
243
  if (CustomList.isNumber(member)) {
255
- var index = me.toIndex(member);
256
- if (!me.value.hasOwnProperty(index)) {
244
+ var memberIndex = itemAtIndex(me.value, Number(member));
245
+ if (!me.value.hasOwnProperty(memberIndex)) {
257
246
  return null;
258
247
  }
259
248
  if (method.length > 1) {
260
- return (_a = me.value[index]).callMethod.apply(_a, __spreadArray([method.slice(1)], __read(args), false));
249
+ return (_a = me.value[memberIndex]).callMethod.apply(_a, __spreadArray([method.slice(1)], __read(args), false));
261
250
  }
262
- return me.value[index];
251
+ return me.value[memberIndex];
263
252
  }
264
253
  if (!CustomList.intrinsics.has(member)) {
265
254
  throw new Error("Cannot access ".concat(member, " in list"));
@@ -16,6 +16,7 @@ export default class CustomMap extends CustomObjectType implements Iterable<Cust
16
16
  constructor(value?: Map<string, any>);
17
17
  [Symbol.iterator](): CustomMapIterator;
18
18
  extend(value: Map<string, any>): CustomMap;
19
+ has(path: string[]): Promise<boolean>;
19
20
  set(path: string[], value: any): Promise<void>;
20
21
  get(path: string[]): Promise<any>;
21
22
  getCallable(path: string[]): Promise<Callable>;
@@ -87,34 +87,52 @@ var CustomMap = /** @class */ (function (_super) {
87
87
  me.value = new Map(__spreadArray(__spreadArray([], __read(me.value.entries()), false), __read(value.entries()), false));
88
88
  return me;
89
89
  };
90
+ CustomMap.prototype.has = function (path) {
91
+ var me = this;
92
+ if (path.length === 0) {
93
+ return Promise.resolve(true);
94
+ }
95
+ var traversalPath = [].concat(path);
96
+ var refs = me.value;
97
+ var current = traversalPath.shift();
98
+ if (current != null) {
99
+ if (refs.has(current)) {
100
+ var sub = refs.get(current);
101
+ if (traversalPath.length > 0 && sub instanceof custom_type_1.CustomObjectType) {
102
+ return sub.has(traversalPath);
103
+ }
104
+ if (traversalPath.length === 0) {
105
+ return Promise.resolve(true);
106
+ }
107
+ }
108
+ }
109
+ return Promise.resolve(false);
110
+ };
90
111
  CustomMap.prototype.set = function (path, value) {
91
112
  var me = this;
113
+ if (!path) {
114
+ console.warn('Cannot set empty path for ', me);
115
+ return;
116
+ }
92
117
  var traversalPath = [].concat(path);
93
118
  var refs = me.value;
94
119
  var last = traversalPath.pop();
95
120
  var current = traversalPath.shift();
96
- var origin = refs;
97
121
  if (current != null) {
98
- if (origin.has(current)) {
99
- origin = origin.get(current);
100
- if (origin instanceof custom_type_1.CustomObjectType) {
101
- return origin.set(traversalPath.concat(last), value);
122
+ if (refs.has(current)) {
123
+ var sub = refs.get(current);
124
+ if (sub instanceof custom_type_1.CustomObjectType) {
125
+ return sub.set(traversalPath.concat(last), value);
102
126
  }
103
127
  }
104
- else {
105
- throw new Error("Cannot set path ".concat(path.join('.')));
106
- }
128
+ console.warn("Cannot set path ".concat(path === null || path === void 0 ? void 0 : path.join('.')));
129
+ return;
107
130
  }
108
- if (origin) {
109
- if (value instanceof operation_1.FunctionOperationBase) {
110
- origin.set(last, value.fork(me));
111
- }
112
- else {
113
- origin.set(last, value);
114
- }
131
+ if (value instanceof operation_1.FunctionOperationBase) {
132
+ refs.set(last, value.fork(me));
115
133
  }
116
134
  else {
117
- throw new Error("Cannot set path ".concat(path.join('.')));
135
+ refs.set(last, value);
118
136
  }
119
137
  };
120
138
  CustomMap.prototype.get = function (path) {
@@ -125,40 +143,41 @@ var CustomMap = /** @class */ (function (_super) {
125
143
  var traversalPath = [].concat(path);
126
144
  var refs = me.value;
127
145
  var current = traversalPath.shift();
128
- var currentValue = current.valueOf();
129
- var origin = refs;
130
- if (currentValue != null) {
131
- if (origin.has(currentValue)) {
132
- origin = origin.get(currentValue);
133
- if (traversalPath.length > 0 && origin instanceof custom_type_1.CustomObjectType) {
134
- return origin.get(traversalPath);
146
+ if (current != null) {
147
+ if (refs.has(current)) {
148
+ var sub = refs.get(current);
149
+ if (traversalPath.length > 0 && sub instanceof custom_type_1.CustomObjectType) {
150
+ return sub.get(traversalPath);
151
+ }
152
+ if (traversalPath.length === 0) {
153
+ return sub;
135
154
  }
136
155
  }
137
- else if (path.length === 1 && CustomMap.intrinsics.has(currentValue)) {
138
- return Promise.resolve(CustomMap.intrinsics.get(currentValue).bind(null, me));
156
+ else if (path.length === 1 && CustomMap.intrinsics.has(current)) {
157
+ return Promise.resolve(CustomMap.intrinsics.get(current).bind(null, me));
139
158
  }
140
159
  else {
141
160
  throw new Error("Cannot get path ".concat(path.join('.')));
142
161
  }
143
162
  }
144
- else {
145
- return null;
146
- }
147
- return Promise.resolve(origin);
163
+ return Promise.resolve(null);
148
164
  };
149
165
  CustomMap.prototype.getCallable = function (path) {
150
166
  var me = this;
151
167
  var traversalPath = [].concat(path);
152
168
  var refs = me.value;
153
169
  var current = traversalPath.shift();
154
- var origin = refs;
155
- var context;
156
170
  if (current != null) {
157
- if (origin.has(current)) {
158
- context = origin;
159
- origin = origin.get(current);
160
- if (origin instanceof custom_type_1.CustomObjectType) {
161
- return origin.getCallable(traversalPath);
171
+ if (refs.has(current)) {
172
+ var sub = refs.get(current);
173
+ if (sub instanceof custom_type_1.CustomObjectType) {
174
+ return sub.getCallable(traversalPath);
175
+ }
176
+ if (traversalPath.length === 0) {
177
+ return Promise.resolve({
178
+ origin: sub,
179
+ context: me
180
+ });
162
181
  }
163
182
  }
164
183
  else if (path.length === 1 && CustomMap.intrinsics.has(current)) {
@@ -168,16 +187,17 @@ var CustomMap = /** @class */ (function (_super) {
168
187
  });
169
188
  }
170
189
  else {
171
- throw new Error("Cannot get path ".concat(path.join('.')));
190
+ throw new Error("Cannot get callable path ".concat(path.join('.')));
172
191
  }
173
192
  }
174
193
  return Promise.resolve({
175
- origin: origin,
176
- context: context
194
+ origin: null,
195
+ context: me
177
196
  });
178
197
  };
179
198
  CustomMap.prototype.callMethod = function (method) {
180
199
  var _a;
200
+ var _b;
181
201
  var args = [];
182
202
  for (var _i = 1; _i < arguments.length; _i++) {
183
203
  args[_i - 1] = arguments[_i];
@@ -186,7 +206,7 @@ var CustomMap = /** @class */ (function (_super) {
186
206
  throw new Error('Unexpected method length');
187
207
  }
188
208
  var me = this;
189
- var key = method[0].valueOf();
209
+ var key = (_b = method[0]) === null || _b === void 0 ? void 0 : _b.toString();
190
210
  if (method.length > 1) {
191
211
  if (me.value.has(key)) {
192
212
  return (_a = me.value.get(key)).callMethod.apply(_a, __spreadArray([method.slice(1)], __read(args), false));
@@ -162,7 +162,7 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
162
162
  case 0:
163
163
  me = this;
164
164
  evaluate = function (node) { return __awaiter(_this, void 0, void 0, function () {
165
- var expr, _a, binaryResult, _b, _c, _d, logicalResult, _e, _f, _g;
165
+ var expr, _a, binaryResult, _b, _c, _d, logicalLeft, logicalResult, _e, _f, _g;
166
166
  return __generator(this, function (_h) {
167
167
  switch (_h.label) {
168
168
  case 0:
@@ -189,11 +189,20 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
189
189
  case 4:
190
190
  binaryResult = _c.apply(_b, _d.concat([_h.sent()]));
191
191
  return [2 /*return*/, Number.isNaN(binaryResult) ? null : binaryResult];
192
- case 5:
193
- _f = (_e = exports.OPERATIONS)[expr.operator];
194
- return [4 /*yield*/, evaluate(expr.left)];
192
+ case 5: return [4 /*yield*/, evaluate(expr.left)];
195
193
  case 6:
196
- _g = [_h.sent()];
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)) {
199
+ return [2 /*return*/, false];
200
+ }
201
+ else if (expr.operator === greybel_core_1.Operator.Or && (0, exports.toPrimitive)(logicalLeft)) {
202
+ return [2 /*return*/, true];
203
+ }
204
+ _f = (_e = exports.OPERATIONS)[expr.operator];
205
+ _g = [logicalLeft];
197
206
  return [4 /*yield*/, evaluate(expr.right)];
198
207
  case 7:
199
208
  logicalResult = _f.apply(_e, _g.concat([_h.sent()]));
@@ -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
  });
@@ -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*/];
@@ -50,10 +50,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
50
50
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
51
  }
52
52
  };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
53
56
  Object.defineProperty(exports, "__esModule", { value: true });
54
57
  var operation_1 = require("../types/operation");
55
58
  var expression_1 = require("../types/expression");
56
59
  var typer_1 = require("../typer");
60
+ var nil_1 = __importDefault(require("../custom-types/nil"));
57
61
  var ReturnOperation = /** @class */ (function (_super) {
58
62
  __extends(ReturnOperation, _super);
59
63
  function ReturnOperation(ast, options) {
@@ -87,7 +91,7 @@ var ReturnOperation = /** @class */ (function (_super) {
87
91
  arg = _a.sent();
88
92
  return [3 /*break*/, 6];
89
93
  case 5:
90
- operationContext.debugger.raise('Unexpected return value', me, me.arg);
94
+ arg = new nil_1.default();
91
95
  _a.label = 6;
92
96
  case 6:
93
97
  functionContext.value = arg;
@@ -82,7 +82,7 @@ var WhileOperation = /** @class */ (function (_super) {
82
82
  };
83
83
  resolveCondition = function () {
84
84
  return __awaiter(this, void 0, void 0, function () {
85
- var value;
85
+ var value, value;
86
86
  return __generator(this, function (_a) {
87
87
  switch (_a.label) {
88
88
  case 0:
@@ -90,13 +90,19 @@ var WhileOperation = /** @class */ (function (_super) {
90
90
  return [4 /*yield*/, me.condition.get(opc)];
91
91
  case 1:
92
92
  value = _a.sent();
93
- return [2 /*return*/, value.valueOf()];
93
+ return [2 /*return*/, !!(value === null || value === void 0 ? void 0 : value.valueOf())];
94
94
  case 2:
95
+ if (!(me.condition instanceof operation_1.Operation)) return [3 /*break*/, 4];
96
+ return [4 /*yield*/, me.condition.get(opc)];
97
+ case 3:
98
+ value = _a.sent();
99
+ return [2 /*return*/, !!(value === null || value === void 0 ? void 0 : value.valueOf())];
100
+ case 4:
95
101
  if ((0, typer_1.isCustomValue)(me.condition)) {
96
102
  return [2 /*return*/, me.condition.valueOf()];
97
103
  }
98
- _a.label = 3;
99
- case 3:
104
+ _a.label = 5;
105
+ case 5:
100
106
  operationContext.debugger.raise('Unexpected condition', me, me.condition);
101
107
  return [2 /*return*/];
102
108
  }
@@ -9,6 +9,7 @@ export declare abstract class CustomLiteralType extends CustomType {
9
9
  }
10
10
  export declare abstract class CustomObjectType extends CustomType {
11
11
  value: any;
12
+ has(path: any[]): Promise<boolean>;
12
13
  set(path: any[], value: any): Promise<void>;
13
14
  get(path: any[]): Promise<any>;
14
15
  getCallable(path: any[]): Promise<Callable>;
@@ -44,6 +44,9 @@ var CustomObjectType = /** @class */ (function (_super) {
44
44
  function CustomObjectType() {
45
45
  return _super !== null && _super.apply(this, arguments) || this;
46
46
  }
47
+ CustomObjectType.prototype.has = function (path) {
48
+ throw new Error('Implentation of "has" missing');
49
+ };
47
50
  CustomObjectType.prototype.set = function (path, value) {
48
51
  throw new Error('Implentation of "set" missing');
49
52
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.5.4",
3
+ "version": "0.5.8",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",