greybel-interpreter 0.3.6 → 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 +9 -7
- package/dist/context.js +30 -78
- package/dist/interpreter.d.ts +2 -1
- package/dist/interpreter.js +20 -7
- package/dist/operations/body.js +17 -22
- package/package.json +1 -1
package/dist/context.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
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';
|
|
3
|
+
import { ASTBase } from 'greybel-core';
|
|
5
4
|
import CPS from './cps';
|
|
6
5
|
export declare enum ContextType {
|
|
7
6
|
API = "API",
|
|
@@ -35,12 +34,12 @@ export declare class Debugger {
|
|
|
35
34
|
getBreakpoint(operationContext: OperationContext): boolean;
|
|
36
35
|
next(): Debugger;
|
|
37
36
|
resume(): Promise<void>;
|
|
38
|
-
interact(operationContext: OperationContext, item:
|
|
39
|
-
run(code: string): Promise<void>;
|
|
37
|
+
interact(operationContext: OperationContext, item: ASTBase): void;
|
|
40
38
|
}
|
|
41
39
|
export interface OperationContextProcessState {
|
|
42
40
|
exit: boolean;
|
|
43
41
|
pending: boolean;
|
|
42
|
+
last: OperationContext | null;
|
|
44
43
|
}
|
|
45
44
|
export interface OperationContextOptions {
|
|
46
45
|
target?: string;
|
|
@@ -59,7 +58,7 @@ export interface OperationContextForkOptions {
|
|
|
59
58
|
}
|
|
60
59
|
export declare class OperationContext {
|
|
61
60
|
target: string;
|
|
62
|
-
|
|
61
|
+
stackItem: ASTBase | null;
|
|
63
62
|
debugger: Debugger;
|
|
64
63
|
previous: OperationContext | null;
|
|
65
64
|
type: ContextType;
|
|
@@ -73,10 +72,13 @@ export declare class OperationContext {
|
|
|
73
72
|
locals: Scope | null;
|
|
74
73
|
globals: Scope | null;
|
|
75
74
|
constructor(options: OperationContextOptions);
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
step(item: ASTBase): Promise<void>;
|
|
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;
|
|
@@ -292,7 +222,7 @@ var OperationContext = /** @class */ (function () {
|
|
|
292
222
|
function OperationContext(options) {
|
|
293
223
|
var me = this;
|
|
294
224
|
me.target = options.target || 'unknown';
|
|
295
|
-
me.
|
|
225
|
+
me.stackItem = null;
|
|
296
226
|
me.previous = options.previous || null;
|
|
297
227
|
me.type = options.type || ContextType.API;
|
|
298
228
|
me.state = options.state || ContextState.DEFAULT;
|
|
@@ -303,12 +233,37 @@ 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();
|
|
310
241
|
me.locals = me.lookupLocals();
|
|
311
242
|
}
|
|
243
|
+
OperationContext.prototype.step = function (item) {
|
|
244
|
+
var me = this;
|
|
245
|
+
var dbgr = me.debugger;
|
|
246
|
+
me.stackItem = item;
|
|
247
|
+
me.setLastActive(me);
|
|
248
|
+
if (dbgr.getBreakpoint(me)) {
|
|
249
|
+
dbgr.interact(me, item);
|
|
250
|
+
return dbgr.resume();
|
|
251
|
+
}
|
|
252
|
+
return Promise.resolve();
|
|
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
|
+
};
|
|
312
267
|
OperationContext.prototype.exit = function () {
|
|
313
268
|
var me = this;
|
|
314
269
|
var state = me.processState;
|
|
@@ -325,17 +280,14 @@ var OperationContext = /** @class */ (function () {
|
|
|
325
280
|
}
|
|
326
281
|
return Promise.reject(new Error('No running process found.'));
|
|
327
282
|
};
|
|
283
|
+
OperationContext.prototype.isPending = function () {
|
|
284
|
+
return this.processState.pending;
|
|
285
|
+
};
|
|
328
286
|
OperationContext.prototype.setPending = function (pending) {
|
|
329
287
|
var me = this;
|
|
330
288
|
me.processState.pending = pending;
|
|
331
289
|
return me;
|
|
332
290
|
};
|
|
333
|
-
OperationContext.prototype.isExit = function () {
|
|
334
|
-
return this.processState.exit;
|
|
335
|
-
};
|
|
336
|
-
OperationContext.prototype.isPending = function () {
|
|
337
|
-
return this.processState.exit;
|
|
338
|
-
};
|
|
339
291
|
OperationContext.prototype.lookupType = function (validate) {
|
|
340
292
|
var me = this;
|
|
341
293
|
if (validate(me.type)) {
|
package/dist/interpreter.d.ts
CHANGED
|
@@ -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
|
}
|
package/dist/interpreter.js
CHANGED
|
@@ -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,
|
|
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.
|
|
117
|
-
|
|
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/dist/operations/body.js
CHANGED
|
@@ -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
|
|
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*/,
|
|
101
|
+
if (!!_b.done) return [3 /*break*/, 9];
|
|
102
102
|
entity = _b.value;
|
|
103
|
-
operationContext.
|
|
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
|
-
|
|
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
|
|
108
|
+
case 4:
|
|
114
109
|
_d.sent();
|
|
115
|
-
return [3 /*break*/,
|
|
116
|
-
case
|
|
117
|
-
case
|
|
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
|
|
128
|
-
case
|
|
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*/,
|
|
132
|
-
case
|
|
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
|
|
133
|
+
case 12: return [2 /*return*/];
|
|
139
134
|
}
|
|
140
135
|
});
|
|
141
136
|
});
|