greybel-interpreter 0.1.2 → 0.1.6
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 +5 -0
- package/dist/context.js +5 -1
- package/dist/custom-types/list.d.ts +1 -1
- package/dist/custom-types/list.js +23 -12
- package/dist/expressions/logical-and-binary.js +7 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -1
- package/dist/interpreter.d.ts +3 -1
- package/dist/interpreter.js +9 -0
- package/dist/operations/body.js +1 -1
- package/dist/typer.js +13 -17
- package/package.json +1 -1
- package/test.src +0 -5
- package/test.ts +0 -62
- package/testA.src +0 -4
package/dist/context.d.ts
CHANGED
|
@@ -39,6 +39,9 @@ export declare class Debugger {
|
|
|
39
39
|
interact(operationContext: OperationContext, item: Operation | Expression): void;
|
|
40
40
|
run(code: string): Promise<void>;
|
|
41
41
|
}
|
|
42
|
+
export interface OperationContextProcessState {
|
|
43
|
+
exit: boolean;
|
|
44
|
+
}
|
|
42
45
|
export interface OperationContextOptions {
|
|
43
46
|
isProtected?: boolean;
|
|
44
47
|
type?: string;
|
|
@@ -46,6 +49,7 @@ export interface OperationContextOptions {
|
|
|
46
49
|
previous?: OperationContext;
|
|
47
50
|
debugger?: Debugger;
|
|
48
51
|
cps?: CPS;
|
|
52
|
+
processState?: OperationContextProcessState;
|
|
49
53
|
}
|
|
50
54
|
export declare class OperationContext {
|
|
51
55
|
debugger: Debugger;
|
|
@@ -56,6 +60,7 @@ export declare class OperationContext {
|
|
|
56
60
|
isProtected: boolean;
|
|
57
61
|
memory: Map<string, any>;
|
|
58
62
|
cps: CPS | null;
|
|
63
|
+
processState: OperationContextProcessState;
|
|
59
64
|
constructor(options: OperationContextOptions);
|
|
60
65
|
valueOf(): Map<string, any>;
|
|
61
66
|
extend(map: Map<string, any>): OperationContext;
|
package/dist/context.js
CHANGED
|
@@ -312,6 +312,9 @@ var OperationContext = /** @class */ (function () {
|
|
|
312
312
|
me.memory = new Map();
|
|
313
313
|
me.debugger = options.debugger || new Debugger();
|
|
314
314
|
me.cps = options.cps;
|
|
315
|
+
me.processState = options.processState || {
|
|
316
|
+
exit: false
|
|
317
|
+
};
|
|
315
318
|
}
|
|
316
319
|
OperationContext.prototype.valueOf = function () {
|
|
317
320
|
return this.scope.valueOf();
|
|
@@ -381,7 +384,8 @@ var OperationContext = /** @class */ (function () {
|
|
|
381
384
|
type: type,
|
|
382
385
|
state: state,
|
|
383
386
|
debugger: me.debugger,
|
|
384
|
-
cps: me.cps
|
|
387
|
+
cps: me.cps,
|
|
388
|
+
processState: me.processState
|
|
385
389
|
});
|
|
386
390
|
if (me.type === ContextType.FUNCTION || me.type === ContextType.GLOBAL) {
|
|
387
391
|
opc.scope.refs.set('locals', opc.scope);
|
|
@@ -13,7 +13,7 @@ export default class CustomList extends CustomObjectType {
|
|
|
13
13
|
[Symbol.iterator](): CustomListIterator;
|
|
14
14
|
concat(list: CustomList): CustomList;
|
|
15
15
|
slice: (a: CustomLiteralType, b: CustomLiteralType) => CustomList;
|
|
16
|
-
toIndex(value: string): number;
|
|
16
|
+
toIndex(value: string): number | null;
|
|
17
17
|
set(path: any[], value: any): Promise<void>;
|
|
18
18
|
get(path: any[]): Promise<any>;
|
|
19
19
|
getCallable(path: any[]): Promise<Callable>;
|
|
@@ -126,11 +126,15 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
126
126
|
CustomList.prototype.toIndex = function (value) {
|
|
127
127
|
var me = this;
|
|
128
128
|
var casted = Number(value);
|
|
129
|
-
|
|
129
|
+
var result = casted < 0 ? me.value.length + casted : casted;
|
|
130
|
+
if (result < 0) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
130
134
|
};
|
|
131
135
|
CustomList.prototype.set = function (path, value) {
|
|
132
136
|
return __awaiter(this, void 0, void 0, function () {
|
|
133
|
-
var me, traversalPath, refs, last, current, origin;
|
|
137
|
+
var me, traversalPath, refs, last, current, origin, index, setterIndex;
|
|
134
138
|
return __generator(this, function (_a) {
|
|
135
139
|
me = this;
|
|
136
140
|
traversalPath = [].concat(path);
|
|
@@ -139,8 +143,9 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
139
143
|
current = traversalPath.shift();
|
|
140
144
|
origin = refs;
|
|
141
145
|
if (current != null) {
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
index = me.toIndex(current);
|
|
147
|
+
if (origin.hasOwnProperty(index)) {
|
|
148
|
+
origin = origin[index];
|
|
144
149
|
if (origin instanceof custom_type_1.CustomObjectType) {
|
|
145
150
|
return [2 /*return*/, origin.set(traversalPath.concat([last]), value)];
|
|
146
151
|
}
|
|
@@ -150,11 +155,15 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
150
155
|
}
|
|
151
156
|
}
|
|
152
157
|
if (origin) {
|
|
158
|
+
setterIndex = me.toIndex(last);
|
|
159
|
+
if (!origin.hasOwnProperty(setterIndex)) {
|
|
160
|
+
throw new Error("Index error (list index ".concat(setterIndex, " out of range)"));
|
|
161
|
+
}
|
|
153
162
|
if (value instanceof function_1.default) {
|
|
154
|
-
origin[
|
|
163
|
+
origin[setterIndex] = value.fork(me);
|
|
155
164
|
}
|
|
156
165
|
else {
|
|
157
|
-
origin[
|
|
166
|
+
origin[setterIndex] = value;
|
|
158
167
|
}
|
|
159
168
|
}
|
|
160
169
|
else {
|
|
@@ -166,7 +175,7 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
166
175
|
};
|
|
167
176
|
CustomList.prototype.get = function (path) {
|
|
168
177
|
return __awaiter(this, void 0, void 0, function () {
|
|
169
|
-
var me, traversalPath, refs, current, currentValue, origin;
|
|
178
|
+
var me, traversalPath, refs, current, currentValue, origin, index;
|
|
170
179
|
return __generator(this, function (_a) {
|
|
171
180
|
me = this;
|
|
172
181
|
if (path.length === 0) {
|
|
@@ -178,8 +187,9 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
178
187
|
currentValue = current.valueOf();
|
|
179
188
|
origin = refs;
|
|
180
189
|
if (currentValue != null) {
|
|
181
|
-
|
|
182
|
-
|
|
190
|
+
index = me.toIndex(current);
|
|
191
|
+
if (origin.hasOwnProperty(index)) {
|
|
192
|
+
origin = origin[index];
|
|
183
193
|
if (traversalPath.length > 0 && origin instanceof custom_type_1.CustomObjectType) {
|
|
184
194
|
return [2 /*return*/, origin.get(traversalPath)];
|
|
185
195
|
}
|
|
@@ -200,7 +210,7 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
200
210
|
};
|
|
201
211
|
CustomList.prototype.getCallable = function (path) {
|
|
202
212
|
return __awaiter(this, void 0, void 0, function () {
|
|
203
|
-
var me, traversalPath, refs, current, origin, context;
|
|
213
|
+
var me, traversalPath, refs, current, origin, context, index;
|
|
204
214
|
return __generator(this, function (_a) {
|
|
205
215
|
me = this;
|
|
206
216
|
traversalPath = [].concat(path);
|
|
@@ -208,9 +218,10 @@ var CustomList = /** @class */ (function (_super) {
|
|
|
208
218
|
current = traversalPath.shift();
|
|
209
219
|
origin = refs;
|
|
210
220
|
if (current != null) {
|
|
211
|
-
|
|
221
|
+
index = me.toIndex(current);
|
|
222
|
+
if (origin.hasOwnProperty(index)) {
|
|
212
223
|
context = origin;
|
|
213
|
-
origin = origin[
|
|
224
|
+
origin = origin[index];
|
|
214
225
|
if (origin instanceof custom_type_1.CustomObjectType) {
|
|
215
226
|
return [2 /*return*/, origin.getCallable(traversalPath)];
|
|
216
227
|
}
|
|
@@ -71,7 +71,10 @@ var multiplyString = function (a, b) {
|
|
|
71
71
|
exports.multiplyString = multiplyString;
|
|
72
72
|
exports.OPERATIONS = (_a = {},
|
|
73
73
|
_a[greybel_core_1.Operator.Plus] = function (a, b) {
|
|
74
|
-
if ((0, typer_1.
|
|
74
|
+
if ((0, typer_1.isCustomMap)(a) && (0, typer_1.isCustomMap)(b)) {
|
|
75
|
+
return a.extend(b.value);
|
|
76
|
+
}
|
|
77
|
+
else if ((0, typer_1.isCustomList)(a) && (0, typer_1.isCustomList)(b)) {
|
|
75
78
|
return a.concat(b);
|
|
76
79
|
}
|
|
77
80
|
var aVal = (0, typer_1.isCustomString)(a) ? (a.valueOf() || '') : (0, exports.toPrimitive)(a);
|
|
@@ -140,7 +143,7 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
|
|
|
140
143
|
return value.get(operationContext);
|
|
141
144
|
};
|
|
142
145
|
var evaluate = function (node) { return __awaiter(_this, void 0, void 0, function () {
|
|
143
|
-
var left, right, _a;
|
|
146
|
+
var left, right, _a, result;
|
|
144
147
|
return __generator(this, function (_b) {
|
|
145
148
|
switch (_b.label) {
|
|
146
149
|
case 0:
|
|
@@ -156,7 +159,8 @@ var LogicalAndBinaryExpression = /** @class */ (function (_super) {
|
|
|
156
159
|
return [4 /*yield*/, resolve(node.right)];
|
|
157
160
|
case 3:
|
|
158
161
|
right = _b.sent();
|
|
159
|
-
|
|
162
|
+
result = exports.OPERATIONS[node.operator](left, right);
|
|
163
|
+
return [2 /*return*/, (0, typer_1.cast)(Number.isNaN(result) ? null : result)];
|
|
160
164
|
case 4: return [4 /*yield*/, resolve(node.left)];
|
|
161
165
|
case 5:
|
|
162
166
|
left = _b.sent();
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { default as BinaryNegatedExpression } from './expressions/binary-negated
|
|
|
8
8
|
export { default as ArgumentOperation } from './operations/argument';
|
|
9
9
|
export { default as WhileOperation } from './operations/while';
|
|
10
10
|
export { default as ForOperation } from './operations/for';
|
|
11
|
+
export { default as FunctionOperation } from './operations/function';
|
|
11
12
|
export { default as ReturnOperation } from './operations/return';
|
|
12
13
|
export { default as NewOperation } from './operations/new';
|
|
13
14
|
export { default as NotOperation } from './operations/not';
|
|
@@ -33,3 +34,4 @@ export * from './types/expression';
|
|
|
33
34
|
export * from './types/custom-type';
|
|
34
35
|
export { default as CPS, CPSMap, CPSMapContext, CPSMapType } from './cps';
|
|
35
36
|
export { default as Interpreter, InterpreterOptions } from './interpreter';
|
|
37
|
+
export { isCustomValue, isCustomMap, isCustomList, isCustomString, isCustomNumber, cast as toCustomValue } from './typer';
|
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.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.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.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;
|
|
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");
|
|
@@ -34,6 +34,8 @@ var while_1 = require("./operations/while");
|
|
|
34
34
|
Object.defineProperty(exports, "WhileOperation", { enumerable: true, get: function () { return __importDefault(while_1).default; } });
|
|
35
35
|
var for_1 = require("./operations/for");
|
|
36
36
|
Object.defineProperty(exports, "ForOperation", { enumerable: true, get: function () { return __importDefault(for_1).default; } });
|
|
37
|
+
var function_1 = require("./operations/function");
|
|
38
|
+
Object.defineProperty(exports, "FunctionOperation", { enumerable: true, get: function () { return __importDefault(function_1).default; } });
|
|
37
39
|
var return_1 = require("./operations/return");
|
|
38
40
|
Object.defineProperty(exports, "ReturnOperation", { enumerable: true, get: function () { return __importDefault(return_1).default; } });
|
|
39
41
|
var new_1 = require("./operations/new");
|
|
@@ -80,3 +82,10 @@ Object.defineProperty(exports, "CPS", { enumerable: true, get: function () { ret
|
|
|
80
82
|
Object.defineProperty(exports, "CPSMap", { enumerable: true, get: function () { return cps_1.CPSMap; } });
|
|
81
83
|
var interpreter_1 = require("./interpreter");
|
|
82
84
|
Object.defineProperty(exports, "Interpreter", { enumerable: true, get: function () { return __importDefault(interpreter_1).default; } });
|
|
85
|
+
var typer_1 = require("./typer");
|
|
86
|
+
Object.defineProperty(exports, "isCustomValue", { enumerable: true, get: function () { return typer_1.isCustomValue; } });
|
|
87
|
+
Object.defineProperty(exports, "isCustomMap", { enumerable: true, get: function () { return typer_1.isCustomMap; } });
|
|
88
|
+
Object.defineProperty(exports, "isCustomList", { enumerable: true, get: function () { return typer_1.isCustomList; } });
|
|
89
|
+
Object.defineProperty(exports, "isCustomString", { enumerable: true, get: function () { return typer_1.isCustomString; } });
|
|
90
|
+
Object.defineProperty(exports, "isCustomNumber", { enumerable: true, get: function () { return typer_1.isCustomNumber; } });
|
|
91
|
+
Object.defineProperty(exports, "toCustomValue", { enumerable: true, get: function () { return typer_1.cast; } });
|
package/dist/interpreter.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Debugger } from './context';
|
|
1
|
+
import { OperationContext, Debugger } from './context';
|
|
2
2
|
import { ResourceHandler } from './resource';
|
|
3
3
|
export interface InterpreterOptions {
|
|
4
4
|
target?: string;
|
|
@@ -15,6 +15,8 @@ export default class Interpreter {
|
|
|
15
15
|
params: any[];
|
|
16
16
|
resourceHandler: ResourceHandler;
|
|
17
17
|
debugger: Debugger;
|
|
18
|
+
context: OperationContext | null;
|
|
18
19
|
constructor(options: InterpreterOptions);
|
|
19
20
|
digest(): Promise<any>;
|
|
21
|
+
exit(): void;
|
|
20
22
|
}
|
package/dist/interpreter.js
CHANGED
|
@@ -17,6 +17,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
17
17
|
me.debugger = options.debugger || new context_1.Debugger();
|
|
18
18
|
me.api = options.api || new Map();
|
|
19
19
|
me.params = options.params || [];
|
|
20
|
+
me.context = null;
|
|
20
21
|
if (options.target) {
|
|
21
22
|
me.target = options.target;
|
|
22
23
|
me.code = me.resourceHandler.get(options.target);
|
|
@@ -44,12 +45,20 @@ var Interpreter = /** @class */ (function () {
|
|
|
44
45
|
});
|
|
45
46
|
mainContext.extend(me.api);
|
|
46
47
|
mainContext.scope.refs.set('params', (0, typer_1.cast)(me.params));
|
|
48
|
+
me.context = mainContext;
|
|
47
49
|
return topOperation.run(mainContext)
|
|
48
50
|
.catch(function (err) {
|
|
49
51
|
console.error(err);
|
|
50
52
|
throw err;
|
|
51
53
|
});
|
|
52
54
|
};
|
|
55
|
+
Interpreter.prototype.exit = function () {
|
|
56
|
+
var me = this;
|
|
57
|
+
if (me.context == null) {
|
|
58
|
+
throw new Error('Unexpected exit signal.');
|
|
59
|
+
}
|
|
60
|
+
me.context.processState.exit = true;
|
|
61
|
+
};
|
|
53
62
|
return Interpreter;
|
|
54
63
|
}());
|
|
55
64
|
exports.default = Interpreter;
|
package/dist/operations/body.js
CHANGED
package/dist/typer.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __read = (this && this.__read) || function (o, n) {
|
|
14
3
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
4
|
if (!m) return o;
|
|
@@ -84,12 +73,19 @@ var cast = function (value) {
|
|
|
84
73
|
value = value.map(exports.cast);
|
|
85
74
|
return new list_1.default(value);
|
|
86
75
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
76
|
+
var result_1 = new Map();
|
|
77
|
+
if (value instanceof Map) {
|
|
78
|
+
value.forEach(function (item, key) {
|
|
79
|
+
result_1.set(key, (0, exports.cast)(item));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
Object.entries(value).forEach(function (_a) {
|
|
84
|
+
var _b = __read(_a, 2), key = _b[0], item = _b[1];
|
|
85
|
+
result_1.set(key, (0, exports.cast)(item));
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return new map_1.default(result_1);
|
|
93
89
|
}
|
|
94
90
|
else if (type === 'function') {
|
|
95
91
|
return value;
|
package/package.json
CHANGED
package/test.src
DELETED
package/test.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
|
-
import { Interpreter, Debugger, OperationContext, Operation, Expression } from './src';
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const readline = require('readline');
|
|
7
|
-
|
|
8
|
-
const api = {
|
|
9
|
-
print: function(customValue) {
|
|
10
|
-
console.log(customValue.valueOf());
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
class TestDebugger extends Debugger {
|
|
15
|
-
debug(...args) {
|
|
16
|
-
console.warn(...args);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interact(operationContext: OperationContext, item: Operation | Expression) {
|
|
20
|
-
const me = this;
|
|
21
|
-
const ask = () => {
|
|
22
|
-
const rl = readline.createInterface({
|
|
23
|
-
input: process.stdin,
|
|
24
|
-
output: process.stdout
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
rl.question(`Line: ${item.ast.line} - next, exit or execute code\n> `, function (value) {
|
|
28
|
-
setImmediate(async () => {
|
|
29
|
-
rl.close();
|
|
30
|
-
|
|
31
|
-
if (value == 'next') {
|
|
32
|
-
me.next();
|
|
33
|
-
return;
|
|
34
|
-
} else if (value == 'exit') {
|
|
35
|
-
me.setBreakpoint(false);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
await me.run(value);
|
|
40
|
-
ask();
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
me.lastContext = operationContext;
|
|
46
|
-
ask();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const interpreter = new Interpreter({
|
|
51
|
-
target: path.resolve(__dirname, 'test.src'),
|
|
52
|
-
api,
|
|
53
|
-
debugger: new TestDebugger()
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
(async function() {
|
|
57
|
-
try {
|
|
58
|
-
await interpreter.digest();
|
|
59
|
-
} catch (err) {
|
|
60
|
-
console.error(err);
|
|
61
|
-
}
|
|
62
|
-
})();
|
package/testA.src
DELETED