greybel-interpreter 1.4.4 → 1.4.5

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.
Files changed (93) hide show
  1. package/dist/context.d.ts +3 -2
  2. package/dist/context.js +113 -190
  3. package/dist/cps.js +135 -188
  4. package/dist/handler/error.js +6 -29
  5. package/dist/handler/output.js +15 -38
  6. package/dist/handler/resource.js +16 -39
  7. package/dist/handler-container.js +6 -8
  8. package/dist/index.d.ts +4 -3
  9. package/dist/index.js +10 -9
  10. package/dist/interpreter.d.ts +4 -4
  11. package/dist/interpreter.js +108 -203
  12. package/dist/operations/assign.d.ts +1 -1
  13. package/dist/operations/assign.js +28 -93
  14. package/dist/operations/block.d.ts +1 -1
  15. package/dist/operations/block.js +33 -89
  16. package/dist/operations/break.d.ts +1 -1
  17. package/dist/operations/break.js +11 -29
  18. package/dist/operations/call.d.ts +1 -1
  19. package/dist/operations/call.js +31 -106
  20. package/dist/operations/chunk.d.ts +1 -1
  21. package/dist/operations/chunk.js +15 -67
  22. package/dist/operations/continue.d.ts +1 -1
  23. package/dist/operations/continue.js +11 -29
  24. package/dist/operations/debugger-statement.d.ts +1 -1
  25. package/dist/operations/debugger-statement.js +11 -29
  26. package/dist/operations/evaluate.d.ts +1 -1
  27. package/dist/operations/evaluate.js +143 -247
  28. package/dist/operations/for.d.ts +1 -1
  29. package/dist/operations/for.js +62 -133
  30. package/dist/operations/function-reference.d.ts +1 -1
  31. package/dist/operations/function-reference.js +25 -84
  32. package/dist/operations/function.d.ts +1 -1
  33. package/dist/operations/function.js +49 -177
  34. package/dist/operations/if-statement.d.ts +1 -1
  35. package/dist/operations/if-statement.js +58 -154
  36. package/dist/operations/import.d.ts +1 -1
  37. package/dist/operations/import.js +35 -97
  38. package/dist/operations/include.d.ts +1 -1
  39. package/dist/operations/include.js +19 -73
  40. package/dist/operations/list.d.ts +1 -1
  41. package/dist/operations/list.js +24 -93
  42. package/dist/operations/literal.d.ts +1 -1
  43. package/dist/operations/literal.js +15 -33
  44. package/dist/operations/map.d.ts +1 -1
  45. package/dist/operations/map.js +26 -153
  46. package/dist/operations/negated-binary.d.ts +1 -1
  47. package/dist/operations/negated-binary.js +24 -91
  48. package/dist/operations/new-instance.d.ts +1 -1
  49. package/dist/operations/new-instance.js +21 -82
  50. package/dist/operations/noop.d.ts +1 -1
  51. package/dist/operations/noop.js +10 -27
  52. package/dist/operations/not.d.ts +1 -1
  53. package/dist/operations/not.js +17 -78
  54. package/dist/operations/operation.d.ts +2 -2
  55. package/dist/operations/operation.js +3 -5
  56. package/dist/operations/reference.d.ts +1 -1
  57. package/dist/operations/reference.js +10 -28
  58. package/dist/operations/resolve.d.ts +1 -1
  59. package/dist/operations/resolve.js +161 -281
  60. package/dist/operations/return.d.ts +1 -1
  61. package/dist/operations/return.js +27 -89
  62. package/dist/operations/while.d.ts +1 -1
  63. package/dist/operations/while.js +45 -116
  64. package/dist/types/base.d.ts +9 -0
  65. package/dist/types/base.js +5 -0
  66. package/dist/types/boolean.d.ts +3 -1
  67. package/dist/types/boolean.js +24 -36
  68. package/dist/types/default.d.ts +6 -9
  69. package/dist/types/default.js +11 -17
  70. package/dist/types/function.d.ts +1 -1
  71. package/dist/types/function.js +60 -130
  72. package/dist/types/interface.d.ts +2 -1
  73. package/dist/types/interface.js +44 -64
  74. package/dist/types/list.d.ts +4 -6
  75. package/dist/types/list.js +77 -128
  76. package/dist/types/map.d.ts +11 -15
  77. package/dist/types/map.js +102 -266
  78. package/dist/types/nil.d.ts +2 -1
  79. package/dist/types/nil.js +23 -36
  80. package/dist/types/number.d.ts +16 -2
  81. package/dist/types/number.js +60 -37
  82. package/dist/types/string.d.ts +4 -6
  83. package/dist/types/string.js +66 -90
  84. package/dist/types/{generics.d.ts → with-intrinsics.d.ts} +6 -9
  85. package/dist/types/with-intrinsics.js +19 -0
  86. package/dist/utils/deep-equal.js +9 -34
  87. package/dist/utils/object-value.d.ts +6 -0
  88. package/dist/utils/object-value.js +36 -0
  89. package/dist/utils/path.js +16 -43
  90. package/package.json +3 -3
  91. package/dist/intrinsics-container.d.ts +0 -7
  92. package/dist/intrinsics-container.js +0 -19
  93. package/dist/types/generics.js +0 -40
package/dist/context.d.ts CHANGED
@@ -2,9 +2,10 @@ import { ASTBase } from 'greyscript-core';
2
2
  import CPS from './cps';
3
3
  import HandlerContainer from './handler-container';
4
4
  import Operation from './operations/operation';
5
- import { CustomValue } from './types/generics';
5
+ import CustomValue from './types/base';
6
6
  import CustomMap from './types/map';
7
7
  import CustomNil from './types/nil';
8
+ import ObjectValue from './utils/object-value';
8
9
  import Path from './utils/path';
9
10
  export declare enum ContextType {
10
11
  Api = 0,
@@ -102,7 +103,7 @@ export default class OperationContext {
102
103
  lookupApi(): OperationContext;
103
104
  lookupGlobals(): OperationContext;
104
105
  lookupLocals(): OperationContext;
105
- extend(map: Map<CustomValue, CustomValue>): OperationContext;
106
+ extend(map: ObjectValue): OperationContext;
106
107
  set(path: Path<CustomValue> | CustomValue, value: CustomValue): void;
107
108
  get(path: Path<CustomValue> | CustomValue): CustomValue;
108
109
  fork(options: ContextForkOptions): OperationContext;
package/dist/context.js CHANGED
@@ -1,54 +1,14 @@
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
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
43
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
44
4
  };
45
5
  Object.defineProperty(exports, "__esModule", { value: true });
46
6
  exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
- var handler_container_1 = __importDefault(require("./handler-container"));
48
- var default_1 = __importDefault(require("./types/default"));
49
- var generics_1 = require("./types/generics");
50
- var map_1 = __importDefault(require("./types/map"));
51
- var path_1 = __importDefault(require("./utils/path"));
7
+ const handler_container_1 = __importDefault(require("./handler-container"));
8
+ const base_1 = __importDefault(require("./types/base"));
9
+ const default_1 = __importDefault(require("./types/default"));
10
+ const map_1 = __importDefault(require("./types/map"));
11
+ const path_1 = __importDefault(require("./utils/path"));
52
12
  var ContextType;
53
13
  (function (ContextType) {
54
14
  ContextType[ContextType["Api"] = 0] = "Api";
@@ -64,84 +24,72 @@ var ContextState;
64
24
  ContextState[ContextState["Temporary"] = 0] = "Temporary";
65
25
  ContextState[ContextState["Default"] = 1] = "Default";
66
26
  })(ContextState = exports.ContextState || (exports.ContextState = {}));
67
- var Scope = /** @class */ (function (_super) {
68
- __extends(Scope, _super);
69
- function Scope(context) {
70
- var _this = _super.call(this) || this;
71
- _this.context = context;
72
- return _this;
27
+ class Scope extends map_1.default {
28
+ constructor(context) {
29
+ super();
30
+ this.context = context;
73
31
  }
74
- Scope.prototype.get = function (path) {
75
- if (path instanceof generics_1.CustomValue) {
32
+ get(path) {
33
+ if (path instanceof base_1.default) {
76
34
  return this.get(new path_1.default([path]));
77
35
  }
78
36
  if (path.count() === 0) {
79
37
  return this;
80
38
  }
81
- var traversalPath = path.clone();
82
- var current = traversalPath.next();
83
- if (current.value === 'locals' || current.value === 'globals') {
84
- return this.context.get(traversalPath);
85
- }
86
- else if (this.has(path)) {
87
- return _super.prototype.get.call(this, path);
39
+ const traversalPath = path.clone();
40
+ const current = traversalPath.next();
41
+ if (this.has(path)) {
42
+ return super.get(path);
88
43
  }
89
44
  else if (this.context.api.scope.has(path)) {
90
45
  return this.context.api.scope.get(path);
91
46
  }
92
- else if (path.count() === 1 &&
93
- map_1.default.getIntrinsics().has(current.toString())) {
94
- return map_1.default.getIntrinsics().get(current.toString());
47
+ else if (path.count() === 1 && map_1.default.getIntrinsics().has(current)) {
48
+ return map_1.default.getIntrinsics().get(current);
95
49
  }
96
50
  else if (this.context.previous !== null) {
97
51
  return this.context.previous.get(path);
98
52
  }
99
- throw new Error("Unknown path ".concat(path.toString(), "."));
100
- };
101
- return Scope;
102
- }(map_1.default));
53
+ throw new Error(`Unknown path ${path.toString()}.`);
54
+ }
55
+ }
103
56
  exports.Scope = Scope;
104
- var Debugger = /** @class */ (function () {
105
- function Debugger() {
57
+ class Debugger {
58
+ constructor() {
106
59
  this.breakpoint = false;
107
60
  this.nextStep = false;
108
61
  /* eslint-disable no-use-before-define */
109
62
  this.lastContext = null;
110
63
  }
111
- Debugger.prototype.getLastContext = function () {
64
+ getLastContext() {
112
65
  return this.lastContext;
113
- };
114
- Debugger.prototype.debug = function () {
115
- var segments = [];
116
- for (var _i = 0; _i < arguments.length; _i++) {
117
- segments[_i] = arguments[_i];
118
- }
119
- console.debug.apply(console, __spreadArray([], __read(segments), false));
66
+ }
67
+ debug(...segments) {
68
+ console.debug(...segments);
120
69
  return default_1.default.Void;
121
- };
122
- Debugger.prototype.setBreakpoint = function (breakpoint) {
70
+ }
71
+ setBreakpoint(breakpoint) {
123
72
  this.breakpoint = breakpoint;
124
73
  return this;
125
- };
126
- Debugger.prototype.getBreakpoint = function (_ctx) {
74
+ }
75
+ getBreakpoint(_ctx) {
127
76
  return this.breakpoint;
128
- };
129
- Debugger.prototype.next = function () {
77
+ }
78
+ next() {
130
79
  this.nextStep = true;
131
80
  return this;
132
- };
133
- Debugger.prototype.resume = function () {
134
- var _this = this;
81
+ }
82
+ resume() {
135
83
  if (!this.breakpoint) {
136
84
  return Promise.resolve();
137
85
  }
138
- return new Promise(function (resolve) {
139
- var check = function () {
140
- if (!_this.breakpoint) {
86
+ return new Promise((resolve) => {
87
+ const check = () => {
88
+ if (!this.breakpoint) {
141
89
  resolve();
142
90
  }
143
- else if (_this.nextStep) {
144
- _this.nextStep = false;
91
+ else if (this.nextStep) {
92
+ this.nextStep = false;
145
93
  resolve();
146
94
  }
147
95
  else {
@@ -150,45 +98,40 @@ var Debugger = /** @class */ (function () {
150
98
  };
151
99
  check();
152
100
  });
153
- };
154
- Debugger.prototype.interact = function (ctx, _ast, _op) {
155
- var me = this;
101
+ }
102
+ interact(ctx, _ast, _op) {
103
+ const me = this;
156
104
  console.warn('Debugger is not setup.');
157
105
  console.info(ctx);
158
106
  me.breakpoint = false;
159
- };
160
- return Debugger;
161
- }());
107
+ }
108
+ }
162
109
  exports.Debugger = Debugger;
163
- var ProcessState = /** @class */ (function () {
164
- function ProcessState() {
110
+ class ProcessState {
111
+ constructor() {
165
112
  this.isExit = false;
166
113
  this.isPending = false;
167
114
  /* eslint-disable no-use-before-define */
168
115
  this.last = null;
169
116
  }
170
- return ProcessState;
171
- }());
117
+ }
172
118
  exports.ProcessState = ProcessState;
173
- var LoopState = /** @class */ (function () {
174
- function LoopState() {
119
+ class LoopState {
120
+ constructor() {
175
121
  this.isBreak = false;
176
122
  this.isContinue = false;
177
123
  }
178
- return LoopState;
179
- }());
124
+ }
180
125
  exports.LoopState = LoopState;
181
- var FunctionState = /** @class */ (function () {
182
- function FunctionState() {
126
+ class FunctionState {
127
+ constructor() {
183
128
  this.value = default_1.default.Void;
184
129
  this.isReturn = false;
185
130
  }
186
- return FunctionState;
187
- }());
131
+ }
188
132
  exports.FunctionState = FunctionState;
189
- var OperationContext = /** @class */ (function () {
190
- function OperationContext(options) {
191
- if (options === void 0) { options = {}; }
133
+ class OperationContext {
134
+ constructor(options = {}) {
192
135
  this.target = options.target || 'unknown';
193
136
  this.stackItem = null;
194
137
  this.previous = options.previous || null;
@@ -205,7 +148,7 @@ var OperationContext = /** @class */ (function () {
205
148
  this.globals = this.lookupGlobals();
206
149
  this.locals = this.lookupLocals() || this;
207
150
  }
208
- OperationContext.prototype.step = function (op) {
151
+ step(op) {
209
152
  if (!this.injected) {
210
153
  this.stackItem = op.item;
211
154
  this.target = op.target || this.target;
@@ -216,33 +159,33 @@ var OperationContext = /** @class */ (function () {
216
159
  }
217
160
  }
218
161
  return Promise.resolve();
219
- };
220
- OperationContext.prototype.setLastActive = function (ctx) {
162
+ }
163
+ setLastActive(ctx) {
221
164
  if (!ctx.injected) {
222
165
  this.processState.last = ctx;
223
166
  }
224
167
  return this;
225
- };
226
- OperationContext.prototype.getLastActive = function () {
168
+ }
169
+ getLastActive() {
227
170
  return this.processState.last;
228
- };
229
- OperationContext.prototype.isExit = function () {
171
+ }
172
+ isExit() {
230
173
  return this.processState.isExit;
231
- };
232
- OperationContext.prototype.isPending = function () {
174
+ }
175
+ isPending() {
233
176
  return this.processState.isPending;
234
- };
235
- OperationContext.prototype.setPending = function (pending) {
177
+ }
178
+ setPending(pending) {
236
179
  this.processState.isPending = pending;
237
180
  return this;
238
- };
239
- OperationContext.prototype.lookupAllOfType = function (validate) {
240
- var me = this;
241
- var result = [];
181
+ }
182
+ lookupAllOfType(validate) {
183
+ const me = this;
184
+ const result = [];
242
185
  if (validate(me.type)) {
243
186
  result.push(me);
244
187
  }
245
- var current = me.previous;
188
+ let current = me.previous;
246
189
  while (current) {
247
190
  if (validate(current.type)) {
248
191
  result.push(current);
@@ -250,16 +193,15 @@ var OperationContext = /** @class */ (function () {
250
193
  current = current.previous;
251
194
  }
252
195
  return result;
253
- };
254
- OperationContext.prototype.exit = function () {
255
- var _this = this;
196
+ }
197
+ exit() {
256
198
  if (this.processState.isPending) {
257
199
  this.processState.isExit = true;
258
- return new Promise(function (resolve) {
259
- var check = function () {
260
- if (!_this.processState.isPending) {
261
- _this.processState.isExit = false;
262
- resolve(_this);
200
+ return new Promise((resolve) => {
201
+ const check = () => {
202
+ if (!this.processState.isPending) {
203
+ this.processState.isExit = false;
204
+ resolve(this);
263
205
  }
264
206
  else {
265
207
  setTimeout(check);
@@ -269,12 +211,12 @@ var OperationContext = /** @class */ (function () {
269
211
  });
270
212
  }
271
213
  return Promise.reject(new Error('No running process found.'));
272
- };
273
- OperationContext.prototype.lookupType = function (allowedTypes) {
214
+ }
215
+ lookupType(allowedTypes) {
274
216
  if (allowedTypes.includes(this.type)) {
275
217
  return this;
276
218
  }
277
- var current = this.previous;
219
+ let current = this.previous;
278
220
  while (current !== null) {
279
221
  if (allowedTypes.includes(current.type)) {
280
222
  return current;
@@ -282,22 +224,20 @@ var OperationContext = /** @class */ (function () {
282
224
  current = current.previous;
283
225
  }
284
226
  return null;
285
- };
286
- OperationContext.prototype.lookupAllScopes = function () {
287
- return this.lookupAllOfType(function (type) {
288
- return [ContextType.Global, ContextType.Function].includes(type);
289
- });
290
- };
291
- OperationContext.prototype.lookupApi = function () {
227
+ }
228
+ lookupAllScopes() {
229
+ return this.lookupAllOfType((type) => [ContextType.Global, ContextType.Function].includes(type));
230
+ }
231
+ lookupApi() {
292
232
  return this.lookupType(OperationContext.lookupApiType);
293
- };
294
- OperationContext.prototype.lookupGlobals = function () {
233
+ }
234
+ lookupGlobals() {
295
235
  return this.lookupType(OperationContext.lookupGlobalsType);
296
- };
297
- OperationContext.prototype.lookupLocals = function () {
236
+ }
237
+ lookupLocals() {
298
238
  return this.lookupType(OperationContext.lookupLocalsType);
299
- };
300
- OperationContext.prototype.extend = function (map) {
239
+ }
240
+ extend(map) {
301
241
  var _a;
302
242
  if (this.state === ContextState.Temporary) {
303
243
  (_a = this.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
@@ -306,51 +246,35 @@ var OperationContext = /** @class */ (function () {
306
246
  this.scope.extend(map);
307
247
  }
308
248
  return this;
309
- };
310
- OperationContext.prototype.set = function (path, value) {
249
+ }
250
+ set(path, value) {
311
251
  var _a;
312
- if (path instanceof generics_1.CustomValue) {
252
+ if (path instanceof base_1.default) {
313
253
  this.set(new path_1.default([path]), value);
314
254
  return;
315
255
  }
316
- var traversalPath = path.clone();
317
- var current = traversalPath.next();
318
- if (current.value === 'locals') {
319
- this.locals.set(traversalPath, value);
320
- }
321
- else if (current.value === 'globals') {
322
- this.globals.set(traversalPath, value);
323
- }
324
- else if (this.state === ContextState.Temporary) {
256
+ if (this.state === ContextState.Temporary) {
325
257
  (_a = this.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
326
258
  }
327
259
  else {
328
260
  this.locals.scope.set(path, value);
329
261
  }
330
- };
331
- OperationContext.prototype.get = function (path) {
262
+ }
263
+ get(path) {
332
264
  var _a;
333
- if (path instanceof generics_1.CustomValue) {
265
+ if (path instanceof base_1.default) {
334
266
  return this.get(new path_1.default([path]));
335
267
  }
336
268
  if (path.count() === 0) {
337
269
  return this.scope;
338
270
  }
339
- var traversalPath = path.clone();
340
- var current = traversalPath.next();
341
- if (current.value === 'locals') {
342
- return this.locals.get(traversalPath);
343
- }
344
- else if (current.value === 'globals') {
345
- return this.globals.get(traversalPath);
346
- }
347
- else if (this.state === ContextState.Temporary) {
271
+ if (this.state === ContextState.Temporary) {
348
272
  return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
349
273
  }
350
274
  return this.locals.scope.get(path);
351
- };
352
- OperationContext.prototype.fork = function (options) {
353
- var newContext = new OperationContext({
275
+ }
276
+ fork(options) {
277
+ const newContext = new OperationContext({
354
278
  target: options.target || this.target,
355
279
  previous: this,
356
280
  type: options.type,
@@ -369,15 +293,14 @@ var OperationContext = /** @class */ (function () {
369
293
  newContext.functionState = this.functionState;
370
294
  }
371
295
  return newContext;
372
- };
373
- OperationContext.lookupApiType = [ContextType.Api];
374
- OperationContext.lookupGlobalsType = [
375
- ContextType.Global
376
- ];
377
- OperationContext.lookupLocalsType = [
378
- ContextType.Global,
379
- ContextType.Function
380
- ];
381
- return OperationContext;
382
- }());
296
+ }
297
+ }
383
298
  exports.default = OperationContext;
299
+ OperationContext.lookupApiType = [ContextType.Api];
300
+ OperationContext.lookupGlobalsType = [
301
+ ContextType.Global
302
+ ];
303
+ OperationContext.lookupLocalsType = [
304
+ ContextType.Global,
305
+ ContextType.Function
306
+ ];