greybel-interpreter 0.3.0 → 0.3.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
@@ -40,6 +40,7 @@ export declare class Debugger {
40
40
  }
41
41
  export interface OperationContextProcessState {
42
42
  exit: boolean;
43
+ pending: boolean;
43
44
  }
44
45
  export interface OperationContextOptions {
45
46
  target?: string;
@@ -72,6 +73,10 @@ export declare class OperationContext {
72
73
  locals: Scope | null;
73
74
  globals: Scope | null;
74
75
  constructor(options: OperationContextOptions);
76
+ exit(): Promise<OperationContext>;
77
+ setPending(pending: boolean): OperationContext;
78
+ isExit(): boolean;
79
+ isPending(): boolean;
75
80
  lookupType(validate: (type: ContextType) => boolean): Scope;
76
81
  lookupAPI(): Scope;
77
82
  lookupGlobals(): Scope;
package/dist/context.js CHANGED
@@ -302,12 +302,40 @@ var OperationContext = /** @class */ (function () {
302
302
  me.debugger = options.debugger || new Debugger();
303
303
  me.cps = options.cps;
304
304
  me.processState = options.processState || {
305
- exit: false
305
+ exit: false,
306
+ pending: false
306
307
  };
307
308
  me.api = me.lookupAPI();
308
309
  me.globals = me.lookupGlobals();
309
310
  me.locals = me.lookupLocals();
310
311
  }
312
+ OperationContext.prototype.exit = function () {
313
+ var me = this;
314
+ var state = me.processState;
315
+ if (state.pending) {
316
+ state.exit = true;
317
+ return new Promise(function (resolve) {
318
+ setImmediate(function () {
319
+ if (!state.pending) {
320
+ state.exit = false;
321
+ resolve(me);
322
+ }
323
+ });
324
+ });
325
+ }
326
+ return Promise.reject(new Error('No running process found.'));
327
+ };
328
+ OperationContext.prototype.setPending = function (pending) {
329
+ var me = this;
330
+ me.processState.pending = pending;
331
+ return me;
332
+ };
333
+ OperationContext.prototype.isExit = function () {
334
+ return this.processState.exit;
335
+ };
336
+ OperationContext.prototype.isPending = function () {
337
+ return this.processState.exit;
338
+ };
311
339
  OperationContext.prototype.lookupType = function (validate) {
312
340
  var me = this;
313
341
  if (validate(me.type)) {
package/dist/index.d.ts CHANGED
@@ -19,7 +19,6 @@ export { default as ElseOperation } from './operations/else';
19
19
  export { default as ContinueOperation } from './operations/continue';
20
20
  export { default as BreakOperation } from './operations/break';
21
21
  export { default as BodyOperation } from './operations/body';
22
- export { default as TopOperation } from './operations/top';
23
22
  export { default as DebuggerOperation } from './operations/debugger';
24
23
  export { default as CustomBoolean } from './custom-types/boolean';
25
24
  export { default as CustomNumber } from './custom-types/number';
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.toCustomValue = exports.isCustomNumber = exports.isCustomString = exports.isCustomList = exports.isCustomMap = exports.isCustomValue = exports.Interpreter = exports.CPSMap = exports.CPS = exports.CustomList = exports.CustomMap = exports.CustomNil = exports.CustomString = exports.CustomNumber = exports.CustomBoolean = exports.DebuggerOperation = exports.TopOperation = exports.BodyOperation = exports.BreakOperation = exports.ContinueOperation = exports.ElseOperation = exports.ElseIfOperation = exports.IfOperation = exports.IfStatementOperation = exports.NotOperation = exports.NewOperation = exports.ReturnOperation = exports.FunctionOperation = exports.ForOperation = exports.WhileOperation = exports.ArgumentOperation = exports.BinaryNegatedExpression = exports.PathExpression = exports.MapExpression = exports.LogicalAndBinaryExpression = exports.ListExpression = exports.CallExpression = exports.AssignExpression = void 0;
16
+ exports.toCustomValue = exports.isCustomNumber = exports.isCustomString = exports.isCustomList = exports.isCustomMap = exports.isCustomValue = exports.Interpreter = exports.CPSMap = exports.CPS = exports.CustomList = exports.CustomMap = exports.CustomNil = exports.CustomString = exports.CustomNumber = exports.CustomBoolean = exports.DebuggerOperation = exports.BodyOperation = exports.BreakOperation = exports.ContinueOperation = exports.ElseOperation = exports.ElseIfOperation = exports.IfOperation = exports.IfStatementOperation = exports.NotOperation = exports.NewOperation = exports.ReturnOperation = exports.FunctionOperation = exports.ForOperation = exports.WhileOperation = exports.ArgumentOperation = exports.BinaryNegatedExpression = exports.PathExpression = exports.MapExpression = exports.LogicalAndBinaryExpression = exports.ListExpression = exports.CallExpression = exports.AssignExpression = void 0;
17
17
  var assign_1 = require("./expressions/assign");
18
18
  Object.defineProperty(exports, "AssignExpression", { enumerable: true, get: function () { return __importDefault(assign_1).default; } });
19
19
  var call_1 = require("./expressions/call");
@@ -56,8 +56,6 @@ var break_1 = require("./operations/break");
56
56
  Object.defineProperty(exports, "BreakOperation", { enumerable: true, get: function () { return __importDefault(break_1).default; } });
57
57
  var body_1 = require("./operations/body");
58
58
  Object.defineProperty(exports, "BodyOperation", { enumerable: true, get: function () { return __importDefault(body_1).default; } });
59
- var top_1 = require("./operations/top");
60
- Object.defineProperty(exports, "TopOperation", { enumerable: true, get: function () { return __importDefault(top_1).default; } });
61
59
  var debugger_1 = require("./operations/debugger");
62
60
  Object.defineProperty(exports, "DebuggerOperation", { enumerable: true, get: function () { return __importDefault(debugger_1).default; } });
63
61
  var boolean_1 = require("./custom-types/boolean");
@@ -1,22 +1,28 @@
1
+ /// <reference types="node" />
2
+ import CPS from './cps';
1
3
  import { OperationContext, Debugger } from './context';
2
4
  import { ResourceHandler } from './resource';
5
+ import EventEmitter from 'events';
3
6
  export interface InterpreterOptions {
4
7
  target?: string;
5
- code?: string;
6
8
  api?: Map<string, any>;
7
9
  params?: any[];
8
10
  resourceHandler?: ResourceHandler;
9
11
  debugger?: Debugger;
10
12
  }
11
- export default class Interpreter {
13
+ export default class Interpreter extends EventEmitter {
12
14
  target: string;
13
- code: string | null;
14
15
  api: Map<string, any>;
15
16
  params: any[];
16
17
  resourceHandler: ResourceHandler;
17
18
  debugger: Debugger;
18
- context: OperationContext | null;
19
+ apiContext: OperationContext;
20
+ globalContext: OperationContext;
21
+ cps: CPS;
19
22
  constructor(options: InterpreterOptions);
20
- digest(): Promise<any>;
21
- exit(): void;
23
+ setTarget(target: string): Interpreter;
24
+ setDebugger(dbgr: Debugger): Interpreter;
25
+ inject(code: string): Promise<Interpreter>;
26
+ digest(customCode?: string): Promise<Interpreter>;
27
+ exit(): Promise<OperationContext>;
22
28
  }
@@ -1,4 +1,19 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
18
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
19
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -43,80 +58,139 @@ var cps_1 = __importDefault(require("./cps"));
43
58
  var context_1 = require("./context");
44
59
  var greybel_core_1 = require("greybel-core");
45
60
  var resource_1 = require("./resource");
46
- var top_1 = __importDefault(require("./operations/top"));
47
61
  var typer_1 = require("./typer");
62
+ var events_1 = __importDefault(require("events"));
48
63
  ;
49
- var Interpreter = /** @class */ (function () {
64
+ var Interpreter = /** @class */ (function (_super) {
65
+ __extends(Interpreter, _super);
50
66
  function Interpreter(options) {
51
- var me = this;
67
+ var _this = _super.call(this) || this;
68
+ var me = _this;
52
69
  me.resourceHandler = options.resourceHandler || new resource_1.ResourceProvider().getHandler();
53
70
  me.debugger = options.debugger || new context_1.Debugger();
54
71
  me.api = options.api || new Map();
55
72
  me.params = options.params || [];
56
- me.context = null;
57
- if (options.target) {
58
- me.target = options.target;
59
- me.code = null;
60
- }
61
- else {
62
- me.target = 'unknown';
63
- me.code = options.code;
64
- }
73
+ me.setTarget(options.target || 'unknown');
74
+ return _this;
65
75
  }
66
- Interpreter.prototype.digest = function () {
76
+ Interpreter.prototype.setTarget = function (target) {
77
+ var me = this;
78
+ me.target = target;
79
+ me.cps = new cps_1.default({
80
+ target: me.target,
81
+ resourceHandler: me.resourceHandler
82
+ });
83
+ me.apiContext = new context_1.OperationContext({
84
+ target: me.target,
85
+ isProtected: true,
86
+ debugger: me.debugger,
87
+ cps: me.cps
88
+ });
89
+ me.globalContext = me.apiContext.fork({
90
+ type: context_1.ContextType.GLOBAL,
91
+ state: context_1.ContextState.DEFAULT
92
+ });
93
+ return me;
94
+ };
95
+ Interpreter.prototype.setDebugger = function (dbgr) {
96
+ var me = this;
97
+ me.debugger = dbgr;
98
+ me.apiContext.debugger = dbgr;
99
+ me.globalContext.debugger = dbgr;
100
+ return me;
101
+ };
102
+ Interpreter.prototype.inject = function (code) {
67
103
  return __awaiter(this, void 0, void 0, function () {
68
- var me, code, _a, parser, chunk, cps, mainContext, topOperation, _b, _c;
69
- var _d;
70
- return __generator(this, function (_e) {
71
- switch (_e.label) {
104
+ var me, parser, chunk, body, context, err_1;
105
+ return __generator(this, function (_a) {
106
+ switch (_a.label) {
72
107
  case 0:
73
108
  me = this;
74
- _a = me.code;
109
+ parser = new greybel_core_1.Parser(code);
110
+ chunk = parser.parseChunk();
111
+ return [4 /*yield*/, me.cps.visit(chunk)];
112
+ case 1:
113
+ body = _a.sent();
114
+ _a.label = 2;
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
120
+ });
121
+ return [4 /*yield*/, body.run(context)];
122
+ case 3:
123
+ _a.sent();
124
+ return [3 /*break*/, 5];
125
+ case 4:
126
+ err_1 = _a.sent();
127
+ me.debugger.raise(err_1);
128
+ return [3 /*break*/, 5];
129
+ case 5: return [2 /*return*/, me];
130
+ }
131
+ });
132
+ });
133
+ };
134
+ Interpreter.prototype.digest = function (customCode) {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ var me, code, _a, parser, chunk, body, process_1, err_2;
137
+ return __generator(this, function (_b) {
138
+ switch (_b.label) {
139
+ case 0:
140
+ me = this;
141
+ if (me.apiContext.isPending()) {
142
+ return [2 /*return*/, Promise.reject(new Error('Process already running.'))];
143
+ }
144
+ _a = customCode;
75
145
  if (_a) return [3 /*break*/, 2];
76
146
  return [4 /*yield*/, me.resourceHandler.get(me.target)];
77
147
  case 1:
78
- _a = (_e.sent());
79
- _e.label = 2;
148
+ _a = (_b.sent());
149
+ _b.label = 2;
80
150
  case 2:
81
151
  code = _a;
82
152
  parser = new greybel_core_1.Parser(code);
83
153
  chunk = parser.parseChunk();
84
- cps = new cps_1.default({
85
- target: me.target,
86
- resourceHandler: me.resourceHandler
87
- });
88
- mainContext = new context_1.OperationContext({
89
- target: me.target,
90
- isProtected: true,
91
- debugger: me.debugger,
92
- cps: cps
93
- });
94
- _b = top_1.default.bind;
95
- _c = [void 0, null];
96
- _d = {};
97
- return [4 /*yield*/, cps.visit(chunk)];
154
+ return [4 /*yield*/, me.cps.visit(chunk)];
98
155
  case 3:
99
- topOperation = new (_b.apply(top_1.default, _c.concat([(_d.body = _e.sent(),
100
- _d)])))();
101
- mainContext.extend(me.api);
102
- mainContext.scope.value.set('params', (0, typer_1.cast)(me.params));
103
- me.context = mainContext;
104
- return [2 /*return*/, topOperation.run(mainContext)
105
- .catch(function (err) {
106
- console.error(err);
107
- throw err;
108
- })];
156
+ body = _b.sent();
157
+ me.apiContext.extend(me.api);
158
+ me.globalContext.scope.value.set('params', (0, typer_1.cast)(me.params));
159
+ me.emit('setup', me);
160
+ _b.label = 4;
161
+ case 4:
162
+ _b.trys.push([4, 6, 7, 8]);
163
+ me.apiContext.setPending(true);
164
+ process_1 = body.run(me.globalContext);
165
+ me.emit('start');
166
+ return [4 /*yield*/, process_1];
167
+ case 5:
168
+ _b.sent();
169
+ return [3 /*break*/, 8];
170
+ case 6:
171
+ err_2 = _b.sent();
172
+ me.debugger.raise(err_2);
173
+ return [3 /*break*/, 8];
174
+ case 7:
175
+ me.apiContext.setPending(false);
176
+ setImmediate(function () {
177
+ me.emit('exit', me);
178
+ });
179
+ return [7 /*endfinally*/];
180
+ case 8: return [2 /*return*/, me];
109
181
  }
110
182
  });
111
183
  });
112
184
  };
113
185
  Interpreter.prototype.exit = function () {
114
186
  var me = this;
115
- if (me.context == null) {
116
- throw new Error('Unexpected exit signal.');
187
+ try {
188
+ return me.apiContext.exit();
189
+ }
190
+ catch (err) {
191
+ me.debugger.raise(err);
117
192
  }
118
- me.context.processState.exit = true;
119
193
  };
120
194
  return Interpreter;
121
- }());
195
+ }(events_1.default));
122
196
  exports.default = Interpreter;
@@ -118,7 +118,7 @@ var BodyOperation = /** @class */ (function (_super) {
118
118
  _d.sent();
119
119
  _d.label = 8;
120
120
  case 8:
121
- if (isEOL() || operationContext.processState.exit)
121
+ if (isEOL() || operationContext.isExit())
122
122
  return [3 /*break*/, 10];
123
123
  _d.label = 9;
124
124
  case 9:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.3.0",
3
+ "version": "0.3.4",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -35,6 +35,8 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "greybel-core": "^0.1.9",
38
+ "greybel-interpreter": "^0.3.1",
39
+ "greybel-intrinsics": "^0.2.1",
38
40
  "greyscript-core": "^0.1.7",
39
41
  "uuid": "^8.3.2"
40
42
  },
@@ -1,12 +0,0 @@
1
- import { Operation } from '../types/operation';
2
- import { OperationContext } from '../context';
3
- import { ASTBase } from 'greybel-core';
4
- import BodyOperation from './body';
5
- export interface ReturnOperationOptions {
6
- body: BodyOperation;
7
- }
8
- export default class TopOperation extends Operation {
9
- body: BodyOperation;
10
- constructor(ast: ASTBase, options: ReturnOperationOptions);
11
- run(operationContext: OperationContext): Promise<void>;
12
- }
@@ -1,86 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
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
- Object.defineProperty(exports, "__esModule", { value: true });
54
- var operation_1 = require("../types/operation");
55
- var context_1 = require("../context");
56
- var TopOperation = /** @class */ (function (_super) {
57
- __extends(TopOperation, _super);
58
- function TopOperation(ast, options) {
59
- var _this = _super.call(this) || this;
60
- var me = _this;
61
- me.ast = ast;
62
- me.body = options.body;
63
- return _this;
64
- }
65
- TopOperation.prototype.run = function (operationContext) {
66
- return __awaiter(this, void 0, void 0, function () {
67
- var me, opc;
68
- return __generator(this, function (_a) {
69
- switch (_a.label) {
70
- case 0:
71
- me = this;
72
- opc = operationContext.fork({
73
- type: context_1.ContextType.GLOBAL,
74
- state: context_1.ContextState.DEFAULT
75
- });
76
- return [4 /*yield*/, me.body.run(opc)];
77
- case 1:
78
- _a.sent();
79
- return [2 /*return*/];
80
- }
81
- });
82
- });
83
- };
84
- return TopOperation;
85
- }(operation_1.Operation));
86
- exports.default = TopOperation;