greybel-interpreter 0.3.9 → 0.4.0

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
@@ -35,11 +35,11 @@ export declare class Debugger {
35
35
  next(): Debugger;
36
36
  resume(): Promise<void>;
37
37
  interact(operationContext: OperationContext, item: ASTBase): void;
38
- run(code: string): Promise<void>;
39
38
  }
40
39
  export interface OperationContextProcessState {
41
40
  exit: boolean;
42
41
  pending: boolean;
42
+ last: OperationContext | null;
43
43
  }
44
44
  export interface OperationContextOptions {
45
45
  target?: string;
@@ -73,10 +73,12 @@ export declare class OperationContext {
73
73
  globals: Scope | null;
74
74
  constructor(options: OperationContextOptions);
75
75
  step(item: ASTBase): Promise<void>;
76
- exit(): Promise<OperationContext>;
77
- setPending(pending: boolean): OperationContext;
76
+ setLastActive(opc: OperationContext): OperationContext;
77
+ getLastActive(): OperationContext | null;
78
78
  isExit(): boolean;
79
+ exit(): Promise<OperationContext>;
79
80
  isPending(): boolean;
81
+ setPending(pending: boolean): OperationContext;
80
82
  lookupType(validate: (type: ContextType) => boolean): Scope;
81
83
  lookupAPI(): Scope;
82
84
  lookupGlobals(): Scope;
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,7 +45,6 @@ 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";
@@ -250,41 +213,8 @@ var Debugger = /** @class */ (function () {
250
213
  var me = this;
251
214
  console.warn("Debugger is not setup.");
252
215
  console.info(operationContext);
253
- me.lastContext = operationContext;
254
216
  me.breakpoint = false;
255
217
  };
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
218
  return Debugger;
289
219
  }());
290
220
  exports.Debugger = Debugger;
@@ -303,7 +233,8 @@ var OperationContext = /** @class */ (function () {
303
233
  me.cps = options.cps;
304
234
  me.processState = options.processState || {
305
235
  exit: false,
306
- pending: false
236
+ pending: false,
237
+ last: null
307
238
  };
308
239
  me.api = me.lookupAPI();
309
240
  me.globals = me.lookupGlobals();
@@ -313,12 +244,26 @@ var OperationContext = /** @class */ (function () {
313
244
  var me = this;
314
245
  var dbgr = me.debugger;
315
246
  me.stackItem = item;
247
+ me.setLastActive(me);
316
248
  if (dbgr.getBreakpoint(me)) {
317
249
  dbgr.interact(me, item);
318
250
  return dbgr.resume();
319
251
  }
320
252
  return Promise.resolve();
321
253
  };
254
+ OperationContext.prototype.setLastActive = function (opc) {
255
+ var me = this;
256
+ if (opc.type !== ContextType.INJECTION) {
257
+ me.processState.last = opc;
258
+ }
259
+ return me;
260
+ };
261
+ OperationContext.prototype.getLastActive = function () {
262
+ return this.processState.last;
263
+ };
264
+ OperationContext.prototype.isExit = function () {
265
+ return this.processState.exit;
266
+ };
322
267
  OperationContext.prototype.exit = function () {
323
268
  var me = this;
324
269
  var state = me.processState;
@@ -335,17 +280,14 @@ var OperationContext = /** @class */ (function () {
335
280
  }
336
281
  return Promise.reject(new Error('No running process found.'));
337
282
  };
283
+ OperationContext.prototype.isPending = function () {
284
+ return this.processState.pending;
285
+ };
338
286
  OperationContext.prototype.setPending = function (pending) {
339
287
  var me = this;
340
288
  me.processState.pending = pending;
341
289
  return me;
342
290
  };
343
- OperationContext.prototype.isExit = function () {
344
- return this.processState.exit;
345
- };
346
- OperationContext.prototype.isPending = function () {
347
- return this.processState.exit;
348
- };
349
291
  OperationContext.prototype.lookupType = function (validate) {
350
292
  var me = this;
351
293
  if (validate(me.type)) {
@@ -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,22 +99,22 @@ 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({
116
+ body = _a.sent();
117
+ injectionCtx = (context || me.globalContext).fork({
118
118
  type: context_1.ContextType.INJECTION,
119
119
  state: context_1.ContextState.TEMPORARY
120
120
  });
@@ -131,6 +131,19 @@ var Interpreter = /** @class */ (function (_super) {
131
131
  });
132
132
  });
133
133
  };
134
+ Interpreter.prototype.injectInLastContext = function (code) {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ var me, last;
137
+ return __generator(this, function (_a) {
138
+ me = this;
139
+ last = me.apiContext.getLastActive();
140
+ if (me.apiContext.isPending()) {
141
+ me.inject(code, last);
142
+ }
143
+ return [2 /*return*/, me];
144
+ });
145
+ });
146
+ };
134
147
  Interpreter.prototype.digest = function (customCode) {
135
148
  return __awaiter(this, void 0, void 0, function () {
136
149
  var me, code, _a, parser, chunk, body, process_1, err_2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",