greybel-interpreter 1.7.2 → 1.7.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/cps.js +66 -15
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -1
- package/dist/interpreter.js +28 -4
- package/dist/utils/error.d.ts +25 -0
- package/dist/utils/error.js +31 -0
- package/package.json +1 -1
package/dist/cps.js
CHANGED
|
@@ -36,6 +36,7 @@ const not_1 = require("./operations/not");
|
|
|
36
36
|
const resolve_1 = require("./operations/resolve");
|
|
37
37
|
const return_1 = require("./operations/return");
|
|
38
38
|
const while_1 = require("./operations/while");
|
|
39
|
+
const error_1 = require("./utils/error");
|
|
39
40
|
class CPSContext {
|
|
40
41
|
constructor(target, handler) {
|
|
41
42
|
this.target = target;
|
|
@@ -87,11 +88,25 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
|
|
|
87
88
|
}
|
|
88
89
|
const code = yield context.handler.resourceHandler.get(target);
|
|
89
90
|
if (code == null) {
|
|
90
|
-
throw new
|
|
91
|
+
throw new error_1.PrepareError(`Cannot find import ${currentTarget}.`, {
|
|
92
|
+
target: currentTarget,
|
|
93
|
+
item
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
98
|
+
const importStatement = yield new import_1.Import(importExpr, target, code).build(subVisit);
|
|
99
|
+
return importStatement;
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
if (err instanceof error_1.PrepareError) {
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
throw new error_1.PrepareError(err.message, {
|
|
106
|
+
target,
|
|
107
|
+
item
|
|
108
|
+
}, err);
|
|
91
109
|
}
|
|
92
|
-
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
93
|
-
const importStatement = yield new import_1.Import(importExpr, target, code).build(subVisit);
|
|
94
|
-
return importStatement;
|
|
95
110
|
}
|
|
96
111
|
case greybel_core_1.ASTType.FeatureIncludeExpression: {
|
|
97
112
|
const includeExpr = item;
|
|
@@ -102,26 +117,58 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
|
|
|
102
117
|
}
|
|
103
118
|
const code = yield context.handler.resourceHandler.get(target);
|
|
104
119
|
if (code == null) {
|
|
105
|
-
throw new
|
|
120
|
+
throw new error_1.PrepareError(`Cannot find include ${currentTarget}.`, {
|
|
121
|
+
target: currentTarget,
|
|
122
|
+
item
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
127
|
+
const importStatement = yield new include_1.Include(includeExpr, target, code).build(subVisit);
|
|
128
|
+
return importStatement;
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
if (err instanceof error_1.PrepareError) {
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
throw new error_1.PrepareError(err.message, {
|
|
135
|
+
target,
|
|
136
|
+
item
|
|
137
|
+
}, err);
|
|
106
138
|
}
|
|
107
|
-
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
108
|
-
const importStatement = yield new include_1.Include(includeExpr, target, code).build(subVisit);
|
|
109
|
-
return importStatement;
|
|
110
139
|
}
|
|
111
140
|
case greyscript_core_1.ASTType.ImportCodeExpression: {
|
|
112
141
|
const importExpr = item;
|
|
142
|
+
if (importExpr.fileSystemDirectory === null) {
|
|
143
|
+
console.warn(`Ignoring dependency in ${currentTarget}. Using noop operation.`);
|
|
144
|
+
return new noop_1.Noop(item, currentTarget);
|
|
145
|
+
}
|
|
113
146
|
const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, importExpr.fileSystemDirectory);
|
|
114
147
|
if (stack.includes(target)) {
|
|
115
|
-
console.warn(
|
|
148
|
+
console.warn(`Found circluar dependency in ${currentTarget}. Using noop operation.`);
|
|
116
149
|
return new noop_1.Noop(item, target);
|
|
117
150
|
}
|
|
118
151
|
const code = yield context.handler.resourceHandler.get(target);
|
|
119
152
|
if (code == null) {
|
|
120
|
-
throw new
|
|
153
|
+
throw new error_1.PrepareError(`Cannot find native import ${currentTarget}.`, {
|
|
154
|
+
target: currentTarget,
|
|
155
|
+
item
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
160
|
+
const importStatement = yield new include_1.Include(importExpr, target, code).build(subVisit);
|
|
161
|
+
return importStatement;
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
if (err instanceof error_1.PrepareError) {
|
|
165
|
+
throw err;
|
|
166
|
+
}
|
|
167
|
+
throw new error_1.PrepareError(err.message, {
|
|
168
|
+
target,
|
|
169
|
+
item
|
|
170
|
+
}, err);
|
|
121
171
|
}
|
|
122
|
-
const subVisit = visit.bind(null, context, [...stack, target]);
|
|
123
|
-
const importStatement = yield new include_1.Include(importExpr, target, code).build(subVisit);
|
|
124
|
-
return importStatement;
|
|
125
172
|
}
|
|
126
173
|
case greybel_core_1.ASTType.FeatureDebuggerExpression:
|
|
127
174
|
return new debugger_statement_1.DebuggerStatement(item, currentTarget);
|
|
@@ -150,7 +197,9 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
|
|
|
150
197
|
else if (unaryExpr.operator === '@') {
|
|
151
198
|
return new function_reference_1.FunctionReference(unaryExpr).build(defaultVisit);
|
|
152
199
|
}
|
|
153
|
-
throw new
|
|
200
|
+
throw new error_1.RuntimeError('Unknown unary expression.', {
|
|
201
|
+
target: currentTarget
|
|
202
|
+
});
|
|
154
203
|
}
|
|
155
204
|
case greyscript_core_1.ASTType.Chunk:
|
|
156
205
|
return new chunk_1.Chunk(item, currentTarget).build(defaultVisit);
|
|
@@ -159,7 +208,9 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
|
|
|
159
208
|
case greyscript_core_1.ASTType.ParenthesisExpression:
|
|
160
209
|
return defaultVisit(item.expression);
|
|
161
210
|
default:
|
|
162
|
-
throw new
|
|
211
|
+
throw new error_1.RuntimeError(`Unexpected AST type ${item.type}.`, {
|
|
212
|
+
target: currentTarget
|
|
213
|
+
});
|
|
163
214
|
}
|
|
164
215
|
});
|
|
165
216
|
class CPS {
|
package/dist/index.d.ts
CHANGED
|
@@ -42,5 +42,6 @@ export { CustomNumber } from './types/number';
|
|
|
42
42
|
export { CustomString, CustomStringIterator } from './types/string';
|
|
43
43
|
export { CustomObject, CustomValueWithIntrinsics } from './types/with-intrinsics';
|
|
44
44
|
export { deepEqual } from './utils/deep-equal';
|
|
45
|
+
export { PrepareError, RuntimeError } from './utils/error';
|
|
45
46
|
export { ObjectValue } from './utils/object-value';
|
|
46
47
|
export { Path } from './utils/path';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Not = exports.Noop = exports.NewInstance = exports.NegatedBinary = exports.MapOperation = exports.Literal = exports.List = exports.Include = exports.Import = exports.IfStatement = exports.Clause = exports.FunctionOperation = exports.For = exports.StringProcessorHandler = exports.NumberProcessorHandler = exports.MapProcessorHandler = exports.ListProcessorHandler = exports.handleString = exports.handleNumber = exports.handleMap = exports.handleList = exports.handleInterface = exports.handle = exports.GenericProcessorHandler = exports.Evaluate = exports.DebuggerStatement = exports.Continue = exports.Chunk = exports.Call = exports.Break = exports.Block = exports.Assign = exports.Interpreter = exports.HandlerContainer = exports.ResourceHandler = exports.DefaultResourceHandler = exports.OutputHandler = exports.DefaultOutputHandler = exports.ErrorHandler = exports.DefaultErrorHandler = exports.CPSContext = exports.CPS = exports.Scope = exports.ProcessState = exports.OperationContext = exports.LoopState = exports.FunctionState = exports.Debugger = exports.ContextType = exports.ContextState = void 0;
|
|
4
|
-
exports.Path = exports.ObjectValue = exports.deepEqual = exports.CustomValueWithIntrinsics = exports.CustomObject = exports.CustomStringIterator = exports.CustomString = exports.CustomNumber = exports.CustomNil = exports.CustomMapIterator = exports.CustomMap = exports.CustomListIterator = exports.CustomList = exports.CustomInterfaceIterator = exports.CustomInterface = exports.CustomFunction = exports.Argument = exports.DefaultType = exports.CustomBoolean = exports.CustomValue = exports.While = exports.Return = exports.ResolveResult = exports.Resolve = exports.OperationSegment = exports.IndexSegment = exports.IdentifierSegment = exports.Reference = exports.Operation = void 0;
|
|
4
|
+
exports.Path = exports.ObjectValue = exports.RuntimeError = exports.PrepareError = exports.deepEqual = exports.CustomValueWithIntrinsics = exports.CustomObject = exports.CustomStringIterator = exports.CustomString = exports.CustomNumber = exports.CustomNil = exports.CustomMapIterator = exports.CustomMap = exports.CustomListIterator = exports.CustomList = exports.CustomInterfaceIterator = exports.CustomInterface = exports.CustomFunction = exports.Argument = exports.DefaultType = exports.CustomBoolean = exports.CustomValue = exports.While = exports.Return = exports.ResolveResult = exports.Resolve = exports.OperationSegment = exports.IndexSegment = exports.IdentifierSegment = exports.Reference = exports.Operation = void 0;
|
|
5
5
|
var context_1 = require("./context");
|
|
6
6
|
Object.defineProperty(exports, "ContextState", { enumerable: true, get: function () { return context_1.ContextState; } });
|
|
7
7
|
Object.defineProperty(exports, "ContextType", { enumerable: true, get: function () { return context_1.ContextType; } });
|
|
@@ -123,6 +123,9 @@ Object.defineProperty(exports, "CustomObject", { enumerable: true, get: function
|
|
|
123
123
|
Object.defineProperty(exports, "CustomValueWithIntrinsics", { enumerable: true, get: function () { return with_intrinsics_1.CustomValueWithIntrinsics; } });
|
|
124
124
|
var deep_equal_1 = require("./utils/deep-equal");
|
|
125
125
|
Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return deep_equal_1.deepEqual; } });
|
|
126
|
+
var error_2 = require("./utils/error");
|
|
127
|
+
Object.defineProperty(exports, "PrepareError", { enumerable: true, get: function () { return error_2.PrepareError; } });
|
|
128
|
+
Object.defineProperty(exports, "RuntimeError", { enumerable: true, get: function () { return error_2.RuntimeError; } });
|
|
126
129
|
var object_value_1 = require("./utils/object-value");
|
|
127
130
|
Object.defineProperty(exports, "ObjectValue", { enumerable: true, get: function () { return object_value_1.ObjectValue; } });
|
|
128
131
|
var path_1 = require("./utils/path");
|
package/dist/interpreter.js
CHANGED
|
@@ -21,6 +21,7 @@ const list_1 = require("./types/list");
|
|
|
21
21
|
const map_1 = require("./types/map");
|
|
22
22
|
const number_1 = require("./types/number");
|
|
23
23
|
const string_1 = require("./types/string");
|
|
24
|
+
const error_1 = require("./utils/error");
|
|
24
25
|
const object_value_1 = require("./utils/object-value");
|
|
25
26
|
exports.PARAMS_PROPERTY = new string_1.CustomString('params');
|
|
26
27
|
class Interpreter extends events_1.EventEmitter {
|
|
@@ -88,7 +89,15 @@ class Interpreter extends events_1.EventEmitter {
|
|
|
88
89
|
return this.cps.visit(chunk);
|
|
89
90
|
}
|
|
90
91
|
catch (err) {
|
|
91
|
-
|
|
92
|
+
if (err instanceof error_1.PrepareError) {
|
|
93
|
+
this.handler.errorHandler.raise(err);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.handler.errorHandler.raise(new error_1.PrepareError(err.message, {
|
|
97
|
+
item: null,
|
|
98
|
+
target: this.target
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
92
101
|
}
|
|
93
102
|
return Promise.resolve(new noop_1.Noop(null));
|
|
94
103
|
}
|
|
@@ -104,7 +113,12 @@ class Interpreter extends events_1.EventEmitter {
|
|
|
104
113
|
yield top.handle(injectionCtx);
|
|
105
114
|
}
|
|
106
115
|
catch (err) {
|
|
107
|
-
|
|
116
|
+
if (err instanceof error_1.PrepareError || err instanceof error_1.RuntimeError) {
|
|
117
|
+
this.handler.errorHandler.raise(err);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.handler.errorHandler.raise(new error_1.RuntimeError(err.message, this.apiContext.getLastActive(), err));
|
|
121
|
+
}
|
|
108
122
|
}
|
|
109
123
|
return this;
|
|
110
124
|
});
|
|
@@ -149,7 +163,12 @@ class Interpreter extends events_1.EventEmitter {
|
|
|
149
163
|
yield process;
|
|
150
164
|
}
|
|
151
165
|
catch (err) {
|
|
152
|
-
|
|
166
|
+
if (err instanceof error_1.PrepareError || err instanceof error_1.RuntimeError) {
|
|
167
|
+
this.handler.errorHandler.raise(err);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
this.handler.errorHandler.raise(new error_1.RuntimeError(err.message, this.apiContext.getLastActive(), err));
|
|
171
|
+
}
|
|
153
172
|
}
|
|
154
173
|
finally {
|
|
155
174
|
this.apiContext.setPending(false);
|
|
@@ -175,7 +194,12 @@ class Interpreter extends events_1.EventEmitter {
|
|
|
175
194
|
return this.apiContext.exit();
|
|
176
195
|
}
|
|
177
196
|
catch (err) {
|
|
178
|
-
|
|
197
|
+
if (err instanceof error_1.PrepareError || err instanceof error_1.RuntimeError) {
|
|
198
|
+
this.handler.errorHandler.raise(err);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
this.handler.errorHandler.raise(new error_1.RuntimeError(err.message, this.apiContext.getLastActive(), err));
|
|
202
|
+
}
|
|
179
203
|
}
|
|
180
204
|
}
|
|
181
205
|
setGlobalVariable(path, value) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ASTBase } from 'greyscript-core';
|
|
2
|
+
interface RuntimeContext {
|
|
3
|
+
previous?: RuntimeContext;
|
|
4
|
+
stackItem?: ASTBase;
|
|
5
|
+
target: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class RuntimeError extends Error {
|
|
8
|
+
relatedItem: ASTBase | null;
|
|
9
|
+
relatedTarget: string;
|
|
10
|
+
stackTrace: Set<ASTBase>;
|
|
11
|
+
source?: Error;
|
|
12
|
+
constructor(message: string, context: RuntimeContext, source?: Error);
|
|
13
|
+
private createTrace;
|
|
14
|
+
}
|
|
15
|
+
interface PrepareContext {
|
|
16
|
+
item: ASTBase;
|
|
17
|
+
target: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class PrepareError extends Error {
|
|
20
|
+
relatedItem: ASTBase | null;
|
|
21
|
+
relatedTarget: string;
|
|
22
|
+
source?: Error;
|
|
23
|
+
constructor(message: string, context: PrepareContext, source?: Error);
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrepareError = exports.RuntimeError = void 0;
|
|
4
|
+
class RuntimeError extends Error {
|
|
5
|
+
constructor(message, context, source) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.relatedItem = context.stackItem || null;
|
|
8
|
+
this.relatedTarget = context.target;
|
|
9
|
+
this.stackTrace = this.createTrace(context);
|
|
10
|
+
this.source = source;
|
|
11
|
+
}
|
|
12
|
+
createTrace(context) {
|
|
13
|
+
const result = new Set();
|
|
14
|
+
let item = context;
|
|
15
|
+
while (item) {
|
|
16
|
+
result.add(item.stackItem);
|
|
17
|
+
item = item.previous;
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.RuntimeError = RuntimeError;
|
|
23
|
+
class PrepareError extends Error {
|
|
24
|
+
constructor(message, context, source) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.relatedItem = context.item;
|
|
27
|
+
this.relatedTarget = context.target;
|
|
28
|
+
this.source = source;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.PrepareError = PrepareError;
|