greybel-interpreter 0.3.8 → 0.4.2

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
@@ -1,14 +1,11 @@
1
1
  import CustomMap from './custom-types/map';
2
- import { Operation } from './types/operation';
3
- import { Expression } from './types/expression';
4
2
  import { Callable } from './types/custom-type';
5
- import { ASTPosition } from 'greyscript-core';
3
+ import { ASTBase } from 'greybel-core';
6
4
  import CPS from './cps';
7
5
  export declare enum ContextType {
8
6
  API = "API",
9
7
  GLOBAL = "GLOBAL",
10
8
  FUNCTION = "FUNCTION",
11
- INJECTION = "INJECTION",
12
9
  EXTERNAL = "EXTERNAL",
13
10
  LOOP = "LOOP",
14
11
  MAP = "MAP",
@@ -36,16 +33,17 @@ export declare class Debugger {
36
33
  getBreakpoint(operationContext: OperationContext): boolean;
37
34
  next(): Debugger;
38
35
  resume(): Promise<void>;
39
- interact(operationContext: OperationContext, item: Operation | Expression): void;
40
- run(code: string): Promise<void>;
36
+ interact(operationContext: OperationContext, item: ASTBase): void;
41
37
  }
42
38
  export interface OperationContextProcessState {
43
39
  exit: boolean;
44
40
  pending: boolean;
41
+ last: OperationContext | null;
45
42
  }
46
43
  export interface OperationContextOptions {
47
44
  target?: string;
48
45
  isProtected?: boolean;
46
+ injected?: boolean;
49
47
  type?: ContextType;
50
48
  state?: ContextState;
51
49
  previous?: OperationContext;
@@ -57,27 +55,32 @@ export interface OperationContextForkOptions {
57
55
  type: ContextType;
58
56
  state: ContextState;
59
57
  target?: string;
58
+ injected?: boolean;
60
59
  }
61
60
  export declare class OperationContext {
62
61
  target: string;
63
- position: ASTPosition;
62
+ stackItem: ASTBase | null;
64
63
  debugger: Debugger;
65
64
  previous: OperationContext | null;
66
65
  type: ContextType;
67
66
  state: ContextState;
68
67
  scope: Scope;
69
- isProtected: boolean;
70
68
  memory: Map<string, any>;
71
69
  cps: CPS | null;
72
70
  processState: OperationContextProcessState;
71
+ isProtected: boolean;
72
+ injected: boolean;
73
73
  api: Scope | null;
74
74
  locals: Scope | null;
75
75
  globals: Scope | null;
76
76
  constructor(options: OperationContextOptions);
77
- exit(): Promise<OperationContext>;
78
- setPending(pending: boolean): OperationContext;
77
+ step(item: ASTBase): Promise<void>;
78
+ setLastActive(opc: OperationContext): OperationContext;
79
+ getLastActive(): OperationContext | null;
79
80
  isExit(): boolean;
81
+ exit(): Promise<OperationContext>;
80
82
  isPending(): boolean;
83
+ setPending(pending: boolean): OperationContext;
81
84
  lookupType(validate: (type: ContextType) => boolean): Scope;
82
85
  lookupAPI(): Scope;
83
86
  lookupGlobals(): Scope;
@@ -89,5 +92,5 @@ export declare class OperationContext {
89
92
  getCallable(path: string[]): Promise<Callable>;
90
93
  setMemory(key: string, value: any): OperationContext;
91
94
  getMemory(key: string): any;
92
- fork({ type, state, target }: OperationContextForkOptions): OperationContext;
95
+ fork({ type, state, target, injected }: OperationContextForkOptions): OperationContext;
93
96
  }
package/dist/context.js CHANGED
@@ -14,42 +14,6 @@ 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
- };
53
17
  var __read = (this && this.__read) || function (o, n) {
54
18
  var m = typeof Symbol === "function" && o[Symbol.iterator];
55
19
  if (!m) return o;
@@ -81,13 +45,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
81
45
  Object.defineProperty(exports, "__esModule", { value: true });
82
46
  exports.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
83
47
  var map_1 = __importDefault(require("./custom-types/map"));
84
- var greybel_core_1 = require("greybel-core");
85
48
  var ContextType;
86
49
  (function (ContextType) {
87
50
  ContextType["API"] = "API";
88
51
  ContextType["GLOBAL"] = "GLOBAL";
89
52
  ContextType["FUNCTION"] = "FUNCTION";
90
- ContextType["INJECTION"] = "INJECTION";
91
53
  ContextType["EXTERNAL"] = "EXTERNAL";
92
54
  ContextType["LOOP"] = "LOOP";
93
55
  ContextType["MAP"] = "MAP";
@@ -218,7 +180,7 @@ var Debugger = /** @class */ (function () {
218
180
  return me;
219
181
  };
220
182
  Debugger.prototype.getBreakpoint = function (operationContext) {
221
- return operationContext.type !== ContextType.INJECTION && this.breakpoint;
183
+ return this.breakpoint;
222
184
  };
223
185
  Debugger.prototype.next = function () {
224
186
  var me = this;
@@ -250,41 +212,8 @@ var Debugger = /** @class */ (function () {
250
212
  var me = this;
251
213
  console.warn("Debugger is not setup.");
252
214
  console.info(operationContext);
253
- me.lastContext = operationContext;
254
215
  me.breakpoint = false;
255
216
  };
256
- Debugger.prototype.run = function (code) {
257
- return __awaiter(this, void 0, void 0, function () {
258
- var me, parser, chunk, item, context, err_1;
259
- return __generator(this, function (_a) {
260
- switch (_a.label) {
261
- case 0:
262
- me = this;
263
- _a.label = 1;
264
- case 1:
265
- _a.trys.push([1, 4, , 5]);
266
- parser = new greybel_core_1.Parser(code);
267
- chunk = parser.parseChunk();
268
- return [4 /*yield*/, me.lastContext.cps.visit(chunk)];
269
- case 2:
270
- item = _a.sent();
271
- context = me.lastContext.fork({
272
- type: ContextType.INJECTION,
273
- state: ContextState.TEMPORARY
274
- });
275
- return [4 /*yield*/, item.run(context)];
276
- case 3:
277
- _a.sent();
278
- return [3 /*break*/, 5];
279
- case 4:
280
- err_1 = _a.sent();
281
- console.error(err_1);
282
- return [3 /*break*/, 5];
283
- case 5: return [2 /*return*/];
284
- }
285
- });
286
- });
287
- };
288
217
  return Debugger;
289
218
  }());
290
219
  exports.Debugger = Debugger;
@@ -292,26 +221,51 @@ var OperationContext = /** @class */ (function () {
292
221
  function OperationContext(options) {
293
222
  var me = this;
294
223
  me.target = options.target || 'unknown';
295
- me.position = {
296
- line: 0,
297
- character: 0
298
- };
224
+ me.stackItem = null;
299
225
  me.previous = options.previous || null;
300
226
  me.type = options.type || ContextType.API;
301
227
  me.state = options.state || ContextState.DEFAULT;
302
228
  me.scope = new Scope(me);
303
229
  me.isProtected = options.isProtected || false;
230
+ me.injected = options.injected || false;
304
231
  me.memory = new Map();
305
232
  me.debugger = options.debugger || new Debugger();
306
233
  me.cps = options.cps;
307
234
  me.processState = options.processState || {
308
235
  exit: false,
309
- pending: false
236
+ pending: false,
237
+ last: null
310
238
  };
311
239
  me.api = me.lookupAPI();
312
240
  me.globals = me.lookupGlobals();
313
241
  me.locals = me.lookupLocals();
314
242
  }
243
+ OperationContext.prototype.step = function (item) {
244
+ var me = this;
245
+ var dbgr = me.debugger;
246
+ if (!me.injected) {
247
+ me.stackItem = item;
248
+ me.setLastActive(me);
249
+ if (dbgr.getBreakpoint(me)) {
250
+ dbgr.interact(me, item);
251
+ return dbgr.resume();
252
+ }
253
+ }
254
+ return Promise.resolve();
255
+ };
256
+ OperationContext.prototype.setLastActive = function (opc) {
257
+ var me = this;
258
+ if (!opc.injected) {
259
+ me.processState.last = opc;
260
+ }
261
+ return me;
262
+ };
263
+ OperationContext.prototype.getLastActive = function () {
264
+ return this.processState.last;
265
+ };
266
+ OperationContext.prototype.isExit = function () {
267
+ return this.processState.exit;
268
+ };
315
269
  OperationContext.prototype.exit = function () {
316
270
  var me = this;
317
271
  var state = me.processState;
@@ -328,17 +282,14 @@ var OperationContext = /** @class */ (function () {
328
282
  }
329
283
  return Promise.reject(new Error('No running process found.'));
330
284
  };
285
+ OperationContext.prototype.isPending = function () {
286
+ return this.processState.pending;
287
+ };
331
288
  OperationContext.prototype.setPending = function (pending) {
332
289
  var me = this;
333
290
  me.processState.pending = pending;
334
291
  return me;
335
292
  };
336
- OperationContext.prototype.isExit = function () {
337
- return this.processState.exit;
338
- };
339
- OperationContext.prototype.isPending = function () {
340
- return this.processState.exit;
341
- };
342
293
  OperationContext.prototype.lookupType = function (validate) {
343
294
  var me = this;
344
295
  if (validate(me.type)) {
@@ -412,7 +363,7 @@ var OperationContext = /** @class */ (function () {
412
363
  return me.memory.get(key);
413
364
  };
414
365
  OperationContext.prototype.fork = function (_a) {
415
- var type = _a.type, state = _a.state, target = _a.target;
366
+ var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
416
367
  var me = this;
417
368
  var opc = new OperationContext({
418
369
  target: target || me.target,
@@ -421,7 +372,8 @@ var OperationContext = /** @class */ (function () {
421
372
  state: state,
422
373
  debugger: me.debugger,
423
374
  cps: me.cps,
424
- processState: me.processState
375
+ processState: me.processState,
376
+ injected: injected || me.injected
425
377
  });
426
378
  if (type !== ContextType.FUNCTION) {
427
379
  if (type !== ContextType.LOOP) {
@@ -22,7 +22,8 @@ export default class Interpreter extends EventEmitter {
22
22
  constructor(options: InterpreterOptions);
23
23
  setTarget(target: string): Interpreter;
24
24
  setDebugger(dbgr: Debugger): Interpreter;
25
- inject(code: string): Promise<Interpreter>;
25
+ inject(code: string, context?: OperationContext): Promise<Interpreter>;
26
+ injectInLastContext(code: string): Promise<Interpreter>;
26
27
  digest(customCode?: string): Promise<Interpreter>;
27
28
  exit(): Promise<OperationContext>;
28
29
  }
@@ -99,24 +99,25 @@ var Interpreter = /** @class */ (function (_super) {
99
99
  me.globalContext.debugger = dbgr;
100
100
  return me;
101
101
  };
102
- Interpreter.prototype.inject = function (code) {
102
+ Interpreter.prototype.inject = function (code, context) {
103
103
  return __awaiter(this, void 0, void 0, function () {
104
- var me, parser, chunk, body, context, err_1;
104
+ var me, parser, chunk, body, injectionCtx, err_1;
105
105
  return __generator(this, function (_a) {
106
106
  switch (_a.label) {
107
107
  case 0:
108
108
  me = this;
109
+ _a.label = 1;
110
+ case 1:
111
+ _a.trys.push([1, 4, , 5]);
109
112
  parser = new greybel_core_1.Parser(code);
110
113
  chunk = parser.parseChunk();
111
114
  return [4 /*yield*/, me.cps.visit(chunk)];
112
- case 1:
113
- body = _a.sent();
114
- _a.label = 2;
115
115
  case 2:
116
- _a.trys.push([2, 4, , 5]);
117
- context = me.globalContext.fork({
118
- type: context_1.ContextType.INJECTION,
119
- state: context_1.ContextState.TEMPORARY
116
+ body = _a.sent();
117
+ injectionCtx = (context || me.globalContext).fork({
118
+ type: context_1.ContextType.CALL,
119
+ state: context_1.ContextState.TEMPORARY,
120
+ injected: true
120
121
  });
121
122
  return [4 /*yield*/, body.run(context)];
122
123
  case 3:
@@ -131,6 +132,19 @@ var Interpreter = /** @class */ (function (_super) {
131
132
  });
132
133
  });
133
134
  };
135
+ Interpreter.prototype.injectInLastContext = function (code) {
136
+ return __awaiter(this, void 0, void 0, function () {
137
+ var me, last;
138
+ return __generator(this, function (_a) {
139
+ me = this;
140
+ last = me.apiContext.getLastActive();
141
+ if (me.apiContext.isPending()) {
142
+ me.inject(code, last);
143
+ }
144
+ return [2 /*return*/, me];
145
+ });
146
+ });
147
+ };
134
148
  Interpreter.prototype.digest = function (customCode) {
135
149
  return __awaiter(this, void 0, void 0, function () {
136
150
  var me, code, _a, parser, chunk, body, process_1, err_2;
@@ -94,48 +94,43 @@ var BodyOperation = /** @class */ (function (_super) {
94
94
  }
95
95
  _d.label = 1;
96
96
  case 1:
97
- _d.trys.push([1, 11, 12, 13]);
97
+ _d.trys.push([1, 10, 11, 12]);
98
98
  _a = __values(me.stack), _b = _a.next();
99
99
  _d.label = 2;
100
100
  case 2:
101
- if (!!_b.done) return [3 /*break*/, 10];
101
+ if (!!_b.done) return [3 /*break*/, 9];
102
102
  entity = _b.value;
103
- operationContext.position = entity.ast.start;
104
- if (!dbgr.getBreakpoint(operationContext)) return [3 /*break*/, 4];
105
- dbgr.interact(operationContext, entity);
106
- return [4 /*yield*/, dbgr.resume()];
103
+ return [4 /*yield*/, operationContext.step(entity.ast)];
107
104
  case 3:
108
105
  _d.sent();
109
- _d.label = 4;
110
- case 4:
111
- if (!(entity instanceof expression_1.Expression)) return [3 /*break*/, 6];
106
+ if (!(entity instanceof expression_1.Expression)) return [3 /*break*/, 5];
112
107
  return [4 /*yield*/, entity.get(operationContext)];
113
- case 5:
108
+ case 4:
114
109
  _d.sent();
115
- return [3 /*break*/, 8];
116
- case 6: return [4 /*yield*/, entity.run(operationContext)];
117
- case 7:
110
+ return [3 /*break*/, 7];
111
+ case 5: return [4 /*yield*/, entity.run(operationContext)];
112
+ case 6:
118
113
  _d.sent();
114
+ _d.label = 7;
115
+ case 7:
116
+ if (isEOL() || operationContext.isExit())
117
+ return [3 /*break*/, 9];
119
118
  _d.label = 8;
120
119
  case 8:
121
- if (isEOL() || operationContext.isExit())
122
- return [3 /*break*/, 10];
123
- _d.label = 9;
124
- case 9:
125
120
  _b = _a.next();
126
121
  return [3 /*break*/, 2];
127
- case 10: return [3 /*break*/, 13];
128
- case 11:
122
+ case 9: return [3 /*break*/, 12];
123
+ case 10:
129
124
  e_1_1 = _d.sent();
130
125
  e_1 = { error: e_1_1 };
131
- return [3 /*break*/, 13];
132
- case 12:
126
+ return [3 /*break*/, 12];
127
+ case 11:
133
128
  try {
134
129
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
135
130
  }
136
131
  finally { if (e_1) throw e_1.error; }
137
132
  return [7 /*endfinally*/];
138
- case 13: return [2 /*return*/];
133
+ case 12: return [2 /*return*/];
139
134
  }
140
135
  });
141
136
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.3.8",
3
+ "version": "0.4.2",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",