greybel-interpreter 0.2.4 → 0.2.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/LICENSE CHANGED
File without changes
package/README.md CHANGED
File without changes
package/dist/context.d.ts CHANGED
@@ -44,16 +44,16 @@ export interface OperationContextProcessState {
44
44
  export interface OperationContextOptions {
45
45
  target?: string;
46
46
  isProtected?: boolean;
47
- type?: string;
48
- state?: string;
47
+ type?: ContextType;
48
+ state?: ContextState;
49
49
  previous?: OperationContext;
50
50
  debugger?: Debugger;
51
51
  cps?: CPS;
52
52
  processState?: OperationContextProcessState;
53
53
  }
54
54
  export interface OperationContextForkOptions {
55
- type: string;
56
- state: string;
55
+ type: ContextType;
56
+ state: ContextState;
57
57
  target?: string;
58
58
  }
59
59
  export declare class OperationContext {
@@ -61,14 +61,21 @@ export declare class OperationContext {
61
61
  line: number;
62
62
  debugger: Debugger;
63
63
  previous: OperationContext | null;
64
- type: string;
65
- state: string;
64
+ type: ContextType;
65
+ state: ContextState;
66
66
  scope: Scope;
67
67
  isProtected: boolean;
68
68
  memory: Map<string, any>;
69
69
  cps: CPS | null;
70
70
  processState: OperationContextProcessState;
71
+ api: Scope | null;
72
+ locals: Scope | null;
73
+ globals: Scope | null;
71
74
  constructor(options: OperationContextOptions);
75
+ lookupType(validate: (type: ContextType) => boolean): Scope;
76
+ lookupAPI(): Scope;
77
+ lookupGlobals(): Scope;
78
+ lookupLocals(): Scope;
72
79
  valueOf(): CustomMap;
73
80
  extend(map: Map<string, any>): OperationContext;
74
81
  set(path: any[], value: any): Promise<void>;
package/dist/context.js CHANGED
@@ -110,76 +110,47 @@ var Scope = /** @class */ (function (_super) {
110
110
  var traversalPath = [].concat(path);
111
111
  var current = traversalPath.shift();
112
112
  if (current != null) {
113
- if (current === 'globals') {
114
- if (me.context.type === ContextType.GLOBAL) {
115
- return me.set(traversalPath, value);
116
- }
117
- else if (me.context.previous && !me.context.previous.isProtected) {
118
- return me.context.previous.set(traversalPath, value);
119
- }
120
- else {
121
- throw new Error("Cannot set globals scope for ".concat(path.join('.')));
122
- }
113
+ if (current === 'locals') {
114
+ return me.context.locals.set(traversalPath, value);
123
115
  }
124
- else if (current === 'locals') {
125
- if (me.context.type === ContextType.GLOBAL ||
126
- me.context.type === ContextType.FUNCTION) {
127
- return me.set(traversalPath, value);
128
- }
129
- else if (me.context.previous && !me.context.previous.isProtected) {
130
- return me.context.previous.set(traversalPath, value);
131
- }
132
- else {
133
- throw new Error("Cannot set locals scope for ".concat(path.join('.')));
134
- }
116
+ else if (current === 'globals') {
117
+ return me.context.globals.set(traversalPath, value);
118
+ }
119
+ else if (me.context.locals != null) {
120
+ return map_1.default.prototype.set.call(me.context.locals, path, value);
135
121
  }
136
122
  else {
137
- return _super.prototype.set.call(this, path, value);
123
+ throw new Error("Cannot set path ".concat(path.join('.')));
138
124
  }
139
125
  }
140
126
  };
141
127
  Scope.prototype.get = function (path) {
128
+ var _a, _b, _c;
142
129
  var me = this;
143
130
  var traversalPath = [].concat(path);
144
131
  var current = traversalPath.shift();
145
132
  if (current != null) {
146
- if (me.value.has(current)) {
147
- return _super.prototype.get.call(this, path);
148
- }
149
- else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
150
- return map_1.default.intrinsics.get(current).bind(null, me);
133
+ if (current === 'locals') {
134
+ return traversalPath.length === 0
135
+ ? Promise.resolve(me.context.locals)
136
+ : me.context.locals.get(traversalPath);
151
137
  }
152
138
  else if (current === 'globals') {
153
- if (me.context.type === ContextType.GLOBAL) {
154
- if (path.length === 1) {
155
- return Promise.resolve(me);
156
- }
157
- return me.get(traversalPath);
158
- }
159
- else if (me.context.previous) {
160
- return me.context.previous.get(traversalPath);
161
- }
162
- else {
163
- throw new Error("Cannot find globals scope for ".concat(path.join('.')));
164
- }
139
+ return traversalPath.length === 0
140
+ ? Promise.resolve(me.context.globals)
141
+ : me.context.globals.get(traversalPath);
165
142
  }
166
- else if (current === 'locals') {
167
- if (me.context.type === ContextType.GLOBAL ||
168
- me.context.type === ContextType.FUNCTION) {
169
- if (path.length === 1) {
170
- return Promise.resolve(me);
171
- }
172
- return me.get(traversalPath);
173
- }
174
- else if (me.context.previous) {
175
- return me.context.previous.get(traversalPath);
176
- }
177
- else {
178
- throw new Error("Cannot find locals scope for ".concat(path.join('.')));
179
- }
143
+ else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
144
+ return map_1.default.prototype.get.call(me.context.locals, path);
145
+ }
146
+ else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
147
+ return map_1.default.prototype.get.call(me.context.globals, path);
180
148
  }
181
- else if (me.context.previous) {
182
- return me.context.previous.get(path);
149
+ else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
150
+ return map_1.default.prototype.get.call(me.context.api, path);
151
+ }
152
+ else if (me.value.has(current)) {
153
+ return _super.prototype.get.call(this, path);
183
154
  }
184
155
  else {
185
156
  throw new Error("Cannot get path ".concat(path.join('.')));
@@ -188,59 +159,28 @@ var Scope = /** @class */ (function (_super) {
188
159
  return null;
189
160
  };
190
161
  Scope.prototype.getCallable = function (path) {
162
+ var _a, _b, _c;
191
163
  var me = this;
192
164
  var traversalPath = [].concat(path);
193
165
  var current = traversalPath.shift();
194
166
  if (current != null) {
195
- if (me.value.has(current)) {
196
- return _super.prototype.getCallable.call(this, path);
197
- }
198
- else if (path.length === 1 && map_1.default.intrinsics.has(current)) {
199
- return Promise.resolve({
200
- origin: map_1.default.intrinsics.get(current).bind(null, me),
201
- context: me
202
- });
167
+ if (current === 'locals') {
168
+ return me.context.locals.getCallable(traversalPath);
203
169
  }
204
170
  else if (current === 'globals') {
205
- if (me.context.type === ContextType.GLOBAL) {
206
- if (path.length === 1) {
207
- return Promise.resolve({
208
- origin: me,
209
- context: null
210
- });
211
- }
212
- return me.getCallable(traversalPath);
213
- }
214
- else if (me.context.previous) {
215
- return me.context.previous.getCallable(traversalPath);
216
- }
217
- else {
218
- throw new Error("Cannot find callable in globals scope for ".concat(path.join('.')));
219
- }
171
+ return me.context.globals.getCallable(traversalPath);
220
172
  }
221
- else if (current === 'locals') {
222
- if (me.context.type === ContextType.GLOBAL ||
223
- me.context.type === ContextType.FUNCTION) {
224
- if (path.length === 1) {
225
- return Promise.resolve({
226
- origin: me,
227
- context: null
228
- });
229
- }
230
- return me.getCallable(traversalPath);
231
- }
232
- else if (me.context.previous) {
233
- return me.context.previous.getCallable(traversalPath);
234
- }
235
- else {
236
- throw new Error("Cannot find callable in locals scope for ".concat(path.join('.')));
237
- }
173
+ else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
174
+ return map_1.default.prototype.getCallable.call(me.context.locals, path);
238
175
  }
239
- else if (me.context.previous) {
240
- return me.context.previous.getCallable(path);
176
+ else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
177
+ return map_1.default.prototype.getCallable.call(me.context.globals, path);
178
+ }
179
+ else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
180
+ return map_1.default.prototype.getCallable.call(me.context.api, path);
241
181
  }
242
182
  else {
243
- throw new Error("Cannot get callable path ".concat(path.join('.')));
183
+ return _super.prototype.getCallable.call(this, path);
244
184
  }
245
185
  }
246
186
  return Promise.resolve({
@@ -364,7 +304,33 @@ var OperationContext = /** @class */ (function () {
364
304
  me.processState = options.processState || {
365
305
  exit: false
366
306
  };
307
+ me.api = me.lookupAPI();
308
+ me.globals = me.lookupGlobals();
309
+ me.locals = me.lookupLocals();
367
310
  }
311
+ OperationContext.prototype.lookupType = function (validate) {
312
+ var me = this;
313
+ if (validate(me.type)) {
314
+ return me.scope;
315
+ }
316
+ var current = me.previous;
317
+ while (current) {
318
+ if (validate(current.type)) {
319
+ return current.scope;
320
+ }
321
+ current = current.previous;
322
+ }
323
+ return null;
324
+ };
325
+ OperationContext.prototype.lookupAPI = function () {
326
+ return this.lookupType(function (type) { return [ContextType.API].includes(type); });
327
+ };
328
+ OperationContext.prototype.lookupGlobals = function () {
329
+ return this.lookupType(function (type) { return [ContextType.GLOBAL].includes(type); });
330
+ };
331
+ OperationContext.prototype.lookupLocals = function () {
332
+ return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
333
+ };
368
334
  OperationContext.prototype.valueOf = function () {
369
335
  return this.scope.valueOf();
370
336
  };
@@ -200,11 +200,10 @@ var CustomMap = /** @class */ (function (_super) {
200
200
  };
201
201
  CustomMap.prototype.createInstance = function () {
202
202
  var me = this;
203
- var value = new Map();
204
- var newInstance = new CustomMap(value);
203
+ var newInstance = new CustomMap();
205
204
  newInstance.isInstance = true;
206
205
  me.value.forEach(function (item, key) {
207
- value.set(key, item instanceof operation_1.FunctionOperationBase
206
+ newInstance.value.set(key, item instanceof operation_1.FunctionOperationBase
208
207
  ? item.fork(newInstance)
209
208
  : item);
210
209
  });
@@ -162,7 +162,7 @@ var AssignExpression = /** @class */ (function (_super) {
162
162
  });
163
163
  });
164
164
  };
165
- operationContext.debugger.debug('AssignExpression', 'get', 'expr', me.expr);
165
+ operationContext.debugger.debug('Line', me.ast.line, 'AssignExpression', 'get', 'expr', me.expr);
166
166
  return [4 /*yield*/, evaluate(me.expr)];
167
167
  case 1: return [2 /*return*/, _a.sent()];
168
168
  }
@@ -123,7 +123,7 @@ var BinaryNegatedExpression = /** @class */ (function (_super) {
123
123
  });
124
124
  });
125
125
  };
126
- operationContext.debugger.debug('BinaryNegatedExpression', 'get', 'expr', me.expr);
126
+ operationContext.debugger.debug('Line', me.ast.line, 'BinaryNegatedExpression', 'get', 'expr', me.expr);
127
127
  return evaluate(me.expr);
128
128
  };
129
129
  return BinaryNegatedExpression;
@@ -169,7 +169,7 @@ var CallExpression = /** @class */ (function (_super) {
169
169
  return [4 /*yield*/, node.path.get(opc, me.expr)];
170
170
  case 2:
171
171
  pathExpr = _f.sent();
172
- operationContext.debugger.debug('CallExpression', 'pathExpr', pathExpr);
172
+ operationContext.debugger.debug('Line', me.ast.line, 'CallExpression', 'pathExpr', pathExpr);
173
173
  if (!pathExpr.handle) return [3 /*break*/, 8];
174
174
  if (!(0, typer_1.isCustomMap)(pathExpr.handle)) return [3 /*break*/, 7];
175
175
  return [4 /*yield*/, pathExpr.handle.getCallable(pathExpr.path)];
@@ -203,7 +203,7 @@ var CallExpression = /** @class */ (function (_super) {
203
203
  });
204
204
  });
205
205
  };
206
- operationContext.debugger.debug('CallExpression', 'get', 'expr', me.expr);
206
+ operationContext.debugger.debug('Line', me.ast.line, 'CallExpression', 'get', 'expr', me.expr);
207
207
  return evaluate(me.expr);
208
208
  };
209
209
  return CallExpression;
@@ -143,7 +143,7 @@ var ImportExpression = /** @class */ (function (_super) {
143
143
  });
144
144
  });
145
145
  };
146
- operationContext.debugger.debug('ImportExpression', 'get', 'expr', me.expr);
146
+ operationContext.debugger.debug('Line', me.ast.line, 'ImportExpression', 'get', 'expr', me.expr);
147
147
  return [4 /*yield*/, evaluate(me.expr)];
148
148
  case 1: return [2 /*return*/, _a.sent()];
149
149
  }
@@ -120,7 +120,7 @@ var IncludeExpression = /** @class */ (function (_super) {
120
120
  });
121
121
  });
122
122
  };
123
- operationContext.debugger.debug('IncludeExpression', 'get', 'expr', me.expr);
123
+ operationContext.debugger.debug('Line', me.ast.line, 'IncludeExpression', 'get', 'expr', me.expr);
124
124
  return [4 /*yield*/, evaluate(me.expr)];
125
125
  case 1: return [2 /*return*/, _a.sent()];
126
126
  }
@@ -126,7 +126,7 @@ var ListExpression = /** @class */ (function (_super) {
126
126
  });
127
127
  });
128
128
  };
129
- operationContext.debugger.debug('ListExpression', 'get', 'expr', me.expr);
129
+ operationContext.debugger.debug('Line', me.ast.line, 'ListExpression', 'get', 'expr', me.expr);
130
130
  return evaluate(me.expr.values);
131
131
  };
132
132
  return ListExpression;
@@ -204,7 +204,7 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
204
204
  }
205
205
  });
206
206
  }); };
207
- operationContext.debugger.debug('LogicalAndBinaryExpression', 'get', 'expr', me.expr);
207
+ operationContext.debugger.debug('Line', me.ast.line, 'LogicalAndBinaryExpression', 'get', 'expr', me.expr);
208
208
  return evaluate(me.expr);
209
209
  };
210
210
  return LogicalAndBinaryExpression;
@@ -188,7 +188,7 @@ var MapExpression = /** @class */ (function (_super) {
188
188
  });
189
189
  });
190
190
  };
191
- operationContext.debugger.debug('MapExpression', 'get', 'expr', me.expr);
191
+ operationContext.debugger.debug('Line', me.ast.line, 'MapExpression', 'get', 'expr', me.expr);
192
192
  return evaluate(me.expr.values);
193
193
  };
194
194
  return MapExpression;
@@ -315,7 +315,7 @@ var PathExpression = /** @class */ (function (_super) {
315
315
  });
316
316
  });
317
317
  };
318
- operationContext.debugger.debug('PathExpression', 'get', 'expr', me.expr);
318
+ operationContext.debugger.debug('Line', me.ast.line, 'PathExpression', 'get', 'expr', me.expr);
319
319
  return [4 /*yield*/, evaluate(me.expr)];
320
320
  case 1:
321
321
  resultExpr = _c.sent();
@@ -99,9 +99,7 @@ var Interpreter = /** @class */ (function () {
99
99
  topOperation = new (_b.apply(top_1.default, _c.concat([(_d.body = _e.sent(),
100
100
  _d)])))();
101
101
  mainContext.extend(me.api);
102
- return [4 /*yield*/, mainContext.set(['params'], (0, typer_1.cast)(me.params))];
103
- case 4:
104
- _e.sent();
102
+ mainContext.scope.value.set('params', (0, typer_1.cast)(me.params));
105
103
  me.context = mainContext;
106
104
  return [2 /*return*/, topOperation.run(mainContext)
107
105
  .catch(function (err) {
@@ -61,10 +61,14 @@ var __values = (this && this.__values) || function(o) {
61
61
  };
62
62
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
63
63
  };
64
+ var __importDefault = (this && this.__importDefault) || function (mod) {
65
+ return (mod && mod.__esModule) ? mod : { "default": mod };
66
+ };
64
67
  Object.defineProperty(exports, "__esModule", { value: true });
65
68
  var typer_1 = require("../typer");
66
69
  var operation_1 = require("../types/operation");
67
70
  var expression_1 = require("../types/expression");
71
+ var assign_1 = __importDefault(require("../expressions/assign"));
68
72
  var ArgumentOperation = /** @class */ (function (_super) {
69
73
  __extends(ArgumentOperation, _super);
70
74
  function ArgumentOperation(ast) {
@@ -76,50 +80,60 @@ var ArgumentOperation = /** @class */ (function (_super) {
76
80
  }
77
81
  ArgumentOperation.prototype.get = function (operationContext) {
78
82
  return __awaiter(this, void 0, void 0, function () {
79
- var me, stack, args, stack_1, stack_1_1, entity, _a, _b, e_1_1;
80
- var e_1, _c;
81
- return __generator(this, function (_d) {
82
- switch (_d.label) {
83
+ var me, stack, args, stack_1, stack_1_1, entity, _a, _b, _c, _d, e_1_1;
84
+ var e_1, _e;
85
+ return __generator(this, function (_f) {
86
+ switch (_f.label) {
83
87
  case 0:
84
88
  me = this;
85
89
  stack = me.stack;
86
90
  args = [];
87
- _d.label = 1;
91
+ _f.label = 1;
88
92
  case 1:
89
- _d.trys.push([1, 8, 9, 10]);
93
+ _f.trys.push([1, 11, 12, 13]);
90
94
  stack_1 = __values(stack), stack_1_1 = stack_1.next();
91
- _d.label = 2;
95
+ _f.label = 2;
92
96
  case 2:
93
- if (!!stack_1_1.done) return [3 /*break*/, 7];
97
+ if (!!stack_1_1.done) return [3 /*break*/, 10];
94
98
  entity = stack_1_1.value;
95
99
  if (!(0, typer_1.isCustomValue)(entity)) return [3 /*break*/, 3];
96
100
  args.push(entity);
97
- return [3 /*break*/, 6];
101
+ return [3 /*break*/, 9];
98
102
  case 3:
99
- if (!(entity instanceof expression_1.Expression)) return [3 /*break*/, 5];
100
- _b = (_a = args).push;
103
+ if (!(entity instanceof assign_1.default)) return [3 /*break*/, 6];
101
104
  return [4 /*yield*/, entity.get(operationContext, me)];
102
105
  case 4:
103
- _b.apply(_a, [_d.sent()]);
104
- return [3 /*break*/, 6];
106
+ _f.sent();
107
+ _b = (_a = args).push;
108
+ return [4 /*yield*/, entity.expr.left.get(operationContext)];
105
109
  case 5:
106
- operationContext.debugger.raise('Unexpected argument', me, entity);
107
- _d.label = 6;
110
+ _b.apply(_a, [_f.sent()]);
111
+ return [3 /*break*/, 9];
108
112
  case 6:
113
+ if (!(entity instanceof expression_1.Expression)) return [3 /*break*/, 8];
114
+ _d = (_c = args).push;
115
+ return [4 /*yield*/, entity.get(operationContext, me)];
116
+ case 7:
117
+ _d.apply(_c, [_f.sent()]);
118
+ return [3 /*break*/, 9];
119
+ case 8:
120
+ operationContext.debugger.raise('Unexpected argument', me, entity);
121
+ _f.label = 9;
122
+ case 9:
109
123
  stack_1_1 = stack_1.next();
110
124
  return [3 /*break*/, 2];
111
- case 7: return [3 /*break*/, 10];
112
- case 8:
113
- e_1_1 = _d.sent();
125
+ case 10: return [3 /*break*/, 13];
126
+ case 11:
127
+ e_1_1 = _f.sent();
114
128
  e_1 = { error: e_1_1 };
115
- return [3 /*break*/, 10];
116
- case 9:
129
+ return [3 /*break*/, 13];
130
+ case 12:
117
131
  try {
118
- if (stack_1_1 && !stack_1_1.done && (_c = stack_1.return)) _c.call(stack_1);
132
+ if (stack_1_1 && !stack_1_1.done && (_e = stack_1.return)) _e.call(stack_1);
119
133
  }
120
134
  finally { if (e_1) throw e_1.error; }
121
135
  return [7 /*endfinally*/];
122
- case 10: return [2 /*return*/, args];
136
+ case 13: return [2 /*return*/, args];
123
137
  }
124
138
  });
125
139
  });
@@ -110,14 +110,17 @@ var FunctionOperation = /** @class */ (function (_super) {
110
110
  max = args.length;
111
111
  _a.label = 2;
112
112
  case 2:
113
- if (!(index < max)) return [3 /*break*/, 4];
114
- return [4 /*yield*/, opc.set(args[index].path[0], incArgs[index])];
113
+ if (!(index < max)) return [3 /*break*/, 5];
114
+ if (!incArgs[index]) return [3 /*break*/, 4];
115
+ return [4 /*yield*/, opc.set(args[index].path, incArgs[index])];
115
116
  case 3:
116
117
  _a.sent();
118
+ _a.label = 4;
119
+ case 4:
117
120
  index++;
118
121
  return [3 /*break*/, 2];
119
- case 4: return [4 /*yield*/, me.body.run(opc)];
120
- case 5:
122
+ case 5: return [4 /*yield*/, me.body.run(opc)];
123
+ case 6:
121
124
  _a.sent();
122
125
  return [2 /*return*/, functionContext.value];
123
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.2.4",
3
+ "version": "0.2.8",
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.1.5",
38
- "greyscript-core": "^0.1.1",
37
+ "greybel-core": "^0.1.7",
38
+ "greyscript-core": "^0.1.4",
39
39
  "uuid": "^8.3.2"
40
40
  },
41
41
  "keywords": [