greybel-interpreter 3.0.4 → 3.1.1

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 (83) hide show
  1. package/dist/context/types.d.ts +8 -0
  2. package/dist/context/types.js +2 -0
  3. package/dist/context.d.ts +17 -5
  4. package/dist/context.js +47 -28
  5. package/dist/cps.js +6 -5
  6. package/dist/handler/output.d.ts +2 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +3 -3
  9. package/dist/interpreter.d.ts +10 -3
  10. package/dist/interpreter.js +51 -46
  11. package/dist/operations/assign-globals.d.ts +11 -0
  12. package/dist/operations/assign-globals.js +31 -0
  13. package/dist/operations/assign-locals.d.ts +11 -0
  14. package/dist/operations/assign-locals.js +31 -0
  15. package/dist/operations/assign-outer.d.ts +11 -0
  16. package/dist/operations/assign-outer.js +31 -0
  17. package/dist/operations/assign-self.d.ts +11 -0
  18. package/dist/operations/assign-self.js +41 -0
  19. package/dist/operations/assign.js +16 -3
  20. package/dist/operations/block.js +3 -1
  21. package/dist/operations/call.js +16 -8
  22. package/dist/operations/chunk.js +0 -2
  23. package/dist/operations/evaluate.js +1 -1
  24. package/dist/operations/for.js +5 -1
  25. package/dist/operations/function-reference.js +6 -3
  26. package/dist/operations/function.d.ts +1 -1
  27. package/dist/operations/function.js +10 -6
  28. package/dist/operations/if-statement.js +5 -0
  29. package/dist/operations/import.js +1 -1
  30. package/dist/operations/reference-globals.d.ts +7 -0
  31. package/dist/operations/reference-globals.js +13 -0
  32. package/dist/operations/reference-locals.d.ts +7 -0
  33. package/dist/operations/reference-locals.js +13 -0
  34. package/dist/operations/reference-outer.d.ts +7 -0
  35. package/dist/operations/reference-outer.js +13 -0
  36. package/dist/operations/reference-self.d.ts +7 -0
  37. package/dist/operations/reference-self.js +13 -0
  38. package/dist/operations/resolve-globals.d.ts +5 -0
  39. package/dist/operations/resolve-globals.js +24 -0
  40. package/dist/operations/resolve-locals.d.ts +5 -0
  41. package/dist/operations/resolve-locals.js +24 -0
  42. package/dist/operations/resolve-outer.d.ts +5 -0
  43. package/dist/operations/resolve-outer.js +24 -0
  44. package/dist/operations/resolve-self.d.ts +5 -0
  45. package/dist/operations/resolve-self.js +24 -0
  46. package/dist/operations/resolve.d.ts +1 -1
  47. package/dist/operations/resolve.js +30 -18
  48. package/dist/operations/while.js +5 -1
  49. package/dist/types/base.d.ts +3 -1
  50. package/dist/types/function.d.ts +12 -6
  51. package/dist/types/function.js +47 -33
  52. package/dist/types/list.d.ts +6 -3
  53. package/dist/types/list.js +26 -7
  54. package/dist/types/map.d.ts +6 -3
  55. package/dist/types/map.js +64 -8
  56. package/dist/types/nil.d.ts +1 -0
  57. package/dist/types/nil.js +3 -0
  58. package/dist/types/number.d.ts +6 -3
  59. package/dist/types/number.js +20 -8
  60. package/dist/types/string.d.ts +6 -3
  61. package/dist/types/string.js +20 -9
  62. package/dist/types/with-intrinsics.d.ts +9 -1
  63. package/dist/types/with-intrinsics.js +6 -0
  64. package/dist/utils/create-assign.d.ts +3 -0
  65. package/dist/utils/create-assign.js +25 -0
  66. package/dist/utils/create-resolve.d.ts +5 -0
  67. package/dist/utils/create-resolve.js +51 -0
  68. package/dist/utils/deep-equal.js +9 -4
  69. package/dist/utils/error.d.ts +1 -1
  70. package/dist/utils/error.js +2 -2
  71. package/dist/utils/get-super.d.ts +3 -0
  72. package/dist/utils/get-super.js +12 -0
  73. package/dist/utils/hash.d.ts +3 -0
  74. package/dist/utils/hash.js +41 -0
  75. package/dist/utils/lookup-path.d.ts +3 -0
  76. package/dist/utils/lookup-path.js +16 -0
  77. package/dist/utils/next-tick.d.ts +1 -0
  78. package/dist/utils/next-tick.js +10 -0
  79. package/dist/utils/object-value.d.ts +11 -1
  80. package/dist/utils/object-value.js +54 -27
  81. package/dist/utils/uuid.d.ts +1 -0
  82. package/dist/utils/uuid.js +36 -0
  83. package/package.json +3 -2
@@ -0,0 +1,8 @@
1
+ import { ObjectValue } from '../utils/object-value';
2
+ export type ContextTypeIntrinsics = {
3
+ map: ObjectValue;
4
+ list: ObjectValue;
5
+ number: ObjectValue;
6
+ string: ObjectValue;
7
+ function: ObjectValue;
8
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/context.d.ts CHANGED
@@ -6,9 +6,9 @@ import { HandlerContainer } from './handler-container';
6
6
  import { Operation } from './operations/operation';
7
7
  import { CustomValue } from './types/base';
8
8
  import { CustomMap } from './types/map';
9
- import { CustomNil } from './types/nil';
10
9
  import { ObjectValue } from './utils/object-value';
11
10
  import { Path } from './utils/path';
11
+ import { ContextTypeIntrinsics } from './context/types';
12
12
  export declare enum ContextType {
13
13
  Api = 0,
14
14
  Global = 1,
@@ -25,24 +25,31 @@ export declare enum ContextState {
25
25
  export declare class Scope extends CustomMap {
26
26
  private readonly context;
27
27
  constructor(context: OperationContext);
28
- get(path: Path<CustomValue> | CustomValue): CustomValue;
28
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
29
29
  }
30
30
  export declare class Debugger {
31
31
  private breakpoint;
32
32
  private nextStep;
33
33
  private lastContext;
34
34
  getLastContext(): OperationContext;
35
- debug(...segments: any[]): CustomNil;
35
+ debug(...segments: any[]): CustomValue;
36
36
  setBreakpoint(breakpoint: boolean): Debugger;
37
37
  getBreakpoint(_ctx: OperationContext): boolean;
38
38
  next(): Debugger;
39
39
  resume(): Promise<void>;
40
40
  interact(ctx: OperationContext, _ast: ASTBase, _op: Operation): void;
41
41
  }
42
+ export interface ExitObserver {
43
+ occured: () => boolean;
44
+ close: () => void;
45
+ }
42
46
  export declare class ProcessState extends EventEmitter {
43
- isExit: boolean;
44
47
  isPending: boolean;
45
48
  last: OperationContext;
49
+ private observer;
50
+ private observerWithExitOccurence;
51
+ constructor();
52
+ createExitObserver(): ExitObserver;
46
53
  }
47
54
  export declare class LoopState {
48
55
  isBreak: boolean;
@@ -67,12 +74,16 @@ export interface ContextOptions {
67
74
  cps?: CPS;
68
75
  processState?: ProcessState;
69
76
  environmentVariables?: Map<string, string>;
77
+ ignoreOuter?: boolean;
78
+ contextTypeIntrinsics?: ContextTypeIntrinsics;
70
79
  }
71
80
  export interface ContextForkOptions {
72
81
  type: ContextType;
73
82
  state: ContextState;
74
83
  target?: string;
75
84
  injected?: boolean;
85
+ ignoreOuter?: boolean;
86
+ processState?: ProcessState;
76
87
  }
77
88
  export declare class OperationContext {
78
89
  target: string;
@@ -80,6 +91,7 @@ export declare class OperationContext {
80
91
  debugger: Debugger;
81
92
  environmentVariables: Map<string, string>;
82
93
  handler: HandlerContainer;
94
+ contextTypeIntrinsics: ContextTypeIntrinsics;
83
95
  previous: OperationContext;
84
96
  readonly type: ContextType;
85
97
  readonly state: ContextState;
@@ -102,7 +114,6 @@ export declare class OperationContext {
102
114
  step(op: Operation): Promise<CustomValue>;
103
115
  setLastActive(ctx: OperationContext): OperationContext;
104
116
  getLastActive(): OperationContext;
105
- isExit(): boolean;
106
117
  isPending(): boolean;
107
118
  setPending(pending: boolean): OperationContext;
108
119
  lookupAllOfType(validate: (type: ContextType) => boolean): OperationContext[];
@@ -112,6 +123,7 @@ export declare class OperationContext {
112
123
  lookupApi(): OperationContext;
113
124
  lookupGlobals(): OperationContext;
114
125
  lookupLocals(): OperationContext;
126
+ lookupOuter(): OperationContext;
115
127
  extend(map: ObjectValue): OperationContext;
116
128
  set(path: Path<CustomValue> | CustomValue, value: CustomValue): void;
117
129
  get(path: Path<CustomValue> | CustomValue): CustomValue;
package/dist/context.js CHANGED
@@ -19,6 +19,7 @@ const default_1 = require("./types/default");
19
19
  const map_1 = require("./types/map");
20
20
  const path_1 = require("./utils/path");
21
21
  const set_immediate_1 = require("./utils/set-immediate");
22
+ const uuid_1 = require("./utils/uuid");
22
23
  var ContextType;
23
24
  (function (ContextType) {
24
25
  ContextType[ContextType["Api"] = 0] = "Api";
@@ -39,10 +40,10 @@ class Scope extends map_1.CustomMap {
39
40
  super();
40
41
  this.context = context;
41
42
  }
42
- get(path) {
43
- var _a, _b, _c;
43
+ get(path, typeIntrinsics) {
44
+ var _a, _b, _c, _d;
44
45
  if (path instanceof base_1.CustomValue) {
45
- return this.get(new path_1.Path([path]));
46
+ return this.get(new path_1.Path([path]), typeIntrinsics);
46
47
  }
47
48
  if (path.count() === 0) {
48
49
  return this;
@@ -50,22 +51,20 @@ class Scope extends map_1.CustomMap {
50
51
  const traversalPath = path.clone();
51
52
  const current = traversalPath.next();
52
53
  if (this.has(path)) {
53
- return super.get(path);
54
+ return super.get(path, typeIntrinsics);
54
55
  }
55
56
  else if ((_a = this.context.outer) === null || _a === void 0 ? void 0 : _a.scope.has(path)) {
56
- return this.context.outer.scope.get(path);
57
+ return this.context.outer.scope.get(path, typeIntrinsics);
57
58
  }
58
59
  else if ((_b = this.context.globals) === null || _b === void 0 ? void 0 : _b.scope.has(path)) {
59
- return this.context.globals.scope.get(path);
60
+ return this.context.globals.scope.get(path, typeIntrinsics);
60
61
  }
61
62
  else if ((_c = this.context.api) === null || _c === void 0 ? void 0 : _c.scope.has(path)) {
62
- return this.context.api.scope.get(path);
63
- }
64
- else if (path.count() === 1 && map_1.CustomMap.getIntrinsics().has(current)) {
65
- return map_1.CustomMap.getIntrinsics().get(current);
63
+ return this.context.api.scope.get(path, typeIntrinsics);
66
64
  }
67
- else if (this.context.previous !== null) {
68
- return this.context.previous.get(path);
65
+ const intrinsics = (_d = typeIntrinsics.map) !== null && _d !== void 0 ? _d : map_1.CustomMap.getIntrinsics();
66
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
67
+ return intrinsics.get(current);
69
68
  }
70
69
  throw new Error(`Unknown path ${path.toString()}.`);
71
70
  }
@@ -126,11 +125,23 @@ class Debugger {
126
125
  exports.Debugger = Debugger;
127
126
  class ProcessState extends events_1.EventEmitter {
128
127
  constructor() {
129
- super(...arguments);
130
- this.isExit = false;
128
+ super();
131
129
  this.isPending = false;
132
130
  /* eslint-disable no-use-before-define */
133
131
  this.last = null;
132
+ this.observer = new Set();
133
+ this.observerWithExitOccurence = new Set();
134
+ this.once('exit', () => {
135
+ this.observerWithExitOccurence = new Set(this.observer);
136
+ });
137
+ }
138
+ createExitObserver() {
139
+ const id = (0, uuid_1.uuid)();
140
+ this.observer.add(id);
141
+ return {
142
+ occured: () => this.observerWithExitOccurence.has(id),
143
+ close: () => this.observer.delete(id)
144
+ };
134
145
  }
135
146
  }
136
147
  exports.ProcessState = ProcessState;
@@ -163,15 +174,22 @@ class OperationContext {
163
174
  this.injected = (_g = options.injected) !== null && _g !== void 0 ? _g : false;
164
175
  this.debugger = (_h = options.debugger) !== null && _h !== void 0 ? _h : new Debugger();
165
176
  this.handler = (_j = options.handler) !== null && _j !== void 0 ? _j : new handler_container_1.HandlerContainer();
166
- this.cps = (_k = options.cps) !== null && _k !== void 0 ? _k : null;
167
- this.processState = (_l = options.processState) !== null && _l !== void 0 ? _l : new ProcessState();
168
- this.environmentVariables = (_m = options.environmentVariables) !== null && _m !== void 0 ? _m : new Map();
177
+ this.contextTypeIntrinsics = (_k = options.contextTypeIntrinsics) !== null && _k !== void 0 ? _k : {
178
+ string: null,
179
+ number: null,
180
+ list: null,
181
+ map: null,
182
+ function: null
183
+ };
184
+ this.cps = (_l = options.cps) !== null && _l !== void 0 ? _l : null;
185
+ this.processState = (_m = options.processState) !== null && _m !== void 0 ? _m : new ProcessState();
186
+ this.environmentVariables = (_o = options.environmentVariables) !== null && _o !== void 0 ? _o : new Map();
169
187
  this.functionState = new FunctionState();
170
188
  this.loopState = new LoopState();
171
189
  this.api = this.lookupApi();
172
190
  this.globals = this.lookupGlobals();
173
- this.locals = (_o = this.lookupLocals()) !== null && _o !== void 0 ? _o : this;
174
- this.outer = (_q = (_p = this.locals.previous) === null || _p === void 0 ? void 0 : _p.lookupLocals()) !== null && _q !== void 0 ? _q : null;
191
+ this.locals = (_p = this.lookupLocals()) !== null && _p !== void 0 ? _p : this;
192
+ this.outer = (_q = (options.ignoreOuter ? null : this.lookupOuter())) !== null && _q !== void 0 ? _q : this.globals;
175
193
  }
176
194
  isIgnoredInDebugging(op) {
177
195
  return op instanceof operation_1.OperationBlock || op instanceof noop_1.Noop;
@@ -206,9 +224,6 @@ class OperationContext {
206
224
  getLastActive() {
207
225
  return this.processState.last;
208
226
  }
209
- isExit() {
210
- return this.processState.isExit;
211
- }
212
227
  isPending() {
213
228
  return this.processState.isPending;
214
229
  }
@@ -233,12 +248,10 @@ class OperationContext {
233
248
  }
234
249
  exit() {
235
250
  if (this.processState.isPending) {
236
- this.processState.isExit = true;
237
251
  this.processState.emit('exit');
238
252
  return new Promise((resolve) => {
239
253
  const check = () => {
240
254
  if (!this.processState.isPending) {
241
- this.processState.isExit = false;
242
255
  resolve(this);
243
256
  }
244
257
  else {
@@ -275,6 +288,10 @@ class OperationContext {
275
288
  lookupLocals() {
276
289
  return this.lookupType(OperationContext.lookupLocalsType);
277
290
  }
291
+ lookupOuter() {
292
+ var _a, _b;
293
+ return (_b = (_a = this.locals.previous) === null || _a === void 0 ? void 0 : _a.lookupLocals()) !== null && _b !== void 0 ? _b : null;
294
+ }
278
295
  extend(map) {
279
296
  var _a;
280
297
  if (this.state === ContextState.Temporary) {
@@ -309,23 +326,25 @@ class OperationContext {
309
326
  if (this.state === ContextState.Temporary) {
310
327
  return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
311
328
  }
312
- return this.locals.scope.get(path);
329
+ return this.locals.scope.get(path, this.contextTypeIntrinsics);
313
330
  }
314
331
  fork(options) {
315
- var _a;
332
+ var _a, _b;
316
333
  const newContext = new OperationContext({
317
334
  target: (_a = options.target) !== null && _a !== void 0 ? _a : this.target,
318
335
  stackTrace: this.stackTrace,
319
336
  previous: this,
320
337
  type: options.type,
321
338
  state: options.state,
339
+ ignoreOuter: options.ignoreOuter,
322
340
  isProtected: false,
323
341
  injected: this.injected,
324
342
  debugger: this.debugger,
325
343
  handler: this.handler,
326
344
  cps: this.cps,
327
- processState: this.processState,
328
- environmentVariables: this.environmentVariables
345
+ processState: (_b = options.processState) !== null && _b !== void 0 ? _b : this.processState,
346
+ environmentVariables: this.environmentVariables,
347
+ contextTypeIntrinsics: this.contextTypeIntrinsics
329
348
  });
330
349
  if (options.type !== ContextType.Function) {
331
350
  if (options.type !== ContextType.Loop) {
package/dist/cps.js CHANGED
@@ -12,7 +12,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CPS = exports.defaultCPSVisit = exports.CPSContext = void 0;
13
13
  const greybel_core_1 = require("greybel-core");
14
14
  const miniscript_core_1 = require("miniscript-core");
15
- const assign_1 = require("./operations/assign");
16
15
  const break_1 = require("./operations/break");
17
16
  const call_1 = require("./operations/call");
18
17
  const chunk_1 = require("./operations/chunk");
@@ -33,10 +32,11 @@ const negated_binary_1 = require("./operations/negated-binary");
33
32
  const new_instance_1 = require("./operations/new-instance");
34
33
  const noop_1 = require("./operations/noop");
35
34
  const not_1 = require("./operations/not");
36
- const resolve_1 = require("./operations/resolve");
37
35
  const return_1 = require("./operations/return");
38
36
  const while_1 = require("./operations/while");
39
37
  const error_1 = require("./utils/error");
38
+ const create_resolve_1 = require("./utils/create-resolve");
39
+ const create_assign_1 = require("./utils/create-assign");
40
40
  class CPSContext {
41
41
  constructor(target, handler) {
42
42
  this.target = target;
@@ -53,12 +53,13 @@ const defaultCPSVisit = (cpsVisit, context, stack, item) => __awaiter(void 0, vo
53
53
  case miniscript_core_1.ASTType.ListConstructorExpression:
54
54
  return new list_1.List(item, currentTarget).build(defaultVisit);
55
55
  case miniscript_core_1.ASTType.AssignmentStatement:
56
- return new assign_1.Assign(item, currentTarget).build(defaultVisit);
56
+ return (0, create_assign_1.createAssign)(item, currentTarget).build(defaultVisit);
57
57
  case miniscript_core_1.ASTType.MemberExpression:
58
- case miniscript_core_1.ASTType.Identifier:
59
58
  case miniscript_core_1.ASTType.IndexExpression:
60
59
  case miniscript_core_1.ASTType.SliceExpression:
61
- return new resolve_1.Resolve(item, currentTarget).build(defaultVisit);
60
+ return (0, create_resolve_1.createResolve)(item, currentTarget).build(defaultVisit);
61
+ case miniscript_core_1.ASTType.Identifier:
62
+ return (0, create_resolve_1.createIdentifierResolve)(item, currentTarget).build(defaultVisit);
62
63
  case miniscript_core_1.ASTType.FunctionDeclaration:
63
64
  return new function_1.FunctionOperation(item, currentTarget).build(defaultVisit);
64
65
  case miniscript_core_1.ASTType.InvalidCodeExpression:
@@ -1,6 +1,7 @@
1
1
  import { OperationContext } from '../context';
2
2
  export interface KeyEvent {
3
- keyCode: number;
3
+ keyCode?: number;
4
+ charCode?: number;
4
5
  code: string;
5
6
  }
6
7
  export interface PrintOptions {
package/dist/index.d.ts CHANGED
@@ -40,7 +40,7 @@ export { CustomNil } from './types/nil';
40
40
  export { CustomNumber } from './types/number';
41
41
  export { CustomString, CustomStringIterator } from './types/string';
42
42
  export { CustomObject, CustomValueWithIntrinsics } from './types/with-intrinsics';
43
- export { deepEqual } from './utils/deep-equal';
44
43
  export { PrepareError, RuntimeError } from './utils/error';
45
44
  export { ObjectValue } from './utils/object-value';
46
45
  export { Path } from './utils/path';
46
+ export { deepEqual } from './utils/deep-equal';
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.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.defaultCPSVisit = 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.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.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.deepEqual = exports.Path = exports.ObjectValue = exports.RuntimeError = exports.PrepareError = exports.CustomValueWithIntrinsics = exports.CustomObject = exports.CustomStringIterator = exports.CustomString = exports.CustomNumber = exports.CustomNil = exports.CustomMapIterator = exports.CustomMap = exports.CustomListIterator = exports.CustomList = 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; } });
@@ -118,8 +118,6 @@ Object.defineProperty(exports, "CustomStringIterator", { enumerable: true, get:
118
118
  var with_intrinsics_1 = require("./types/with-intrinsics");
119
119
  Object.defineProperty(exports, "CustomObject", { enumerable: true, get: function () { return with_intrinsics_1.CustomObject; } });
120
120
  Object.defineProperty(exports, "CustomValueWithIntrinsics", { enumerable: true, get: function () { return with_intrinsics_1.CustomValueWithIntrinsics; } });
121
- var deep_equal_1 = require("./utils/deep-equal");
122
- Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return deep_equal_1.deepEqual; } });
123
121
  var error_2 = require("./utils/error");
124
122
  Object.defineProperty(exports, "PrepareError", { enumerable: true, get: function () { return error_2.PrepareError; } });
125
123
  Object.defineProperty(exports, "RuntimeError", { enumerable: true, get: function () { return error_2.RuntimeError; } });
@@ -127,3 +125,5 @@ var object_value_1 = require("./utils/object-value");
127
125
  Object.defineProperty(exports, "ObjectValue", { enumerable: true, get: function () { return object_value_1.ObjectValue; } });
128
126
  var path_1 = require("./utils/path");
129
127
  Object.defineProperty(exports, "Path", { enumerable: true, get: function () { return path_1.Path; } });
128
+ var deep_equal_1 = require("./utils/deep-equal");
129
+ Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return deep_equal_1.deepEqual; } });
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { Debugger, OperationContext } from './context';
3
+ import { ContextOptions, Debugger, OperationContext } from './context';
4
4
  import { CPS } from './cps';
5
5
  import { HandlerContainer } from './handler-container';
6
6
  import { Operation } from './operations/operation';
@@ -17,6 +17,10 @@ export interface InterpreterOptions {
17
17
  debugger?: Debugger;
18
18
  environmentVariables?: Map<string, string>;
19
19
  }
20
+ export interface InterpreterRunOptions {
21
+ customCode?: string;
22
+ ctxOptions?: ContextOptions;
23
+ }
20
24
  export declare class Interpreter extends EventEmitter {
21
25
  target: string;
22
26
  api: ObjectValue;
@@ -32,11 +36,14 @@ export declare class Interpreter extends EventEmitter {
32
36
  setDebugger(dbgr: Debugger): Interpreter;
33
37
  setApi(newApi: ObjectValue): Interpreter;
34
38
  setHandler(handler: HandlerContainer): Interpreter;
39
+ parse(code: string): import("miniscript-core").ASTBase | import("greybel-core").ASTChunkAdvanced;
35
40
  prepare(code: string): Promise<Operation>;
36
41
  inject(code: string, context?: OperationContext): Promise<Interpreter>;
37
42
  injectInLastContext(code: string): Promise<Interpreter>;
38
- run(customCode?: string): Promise<Interpreter>;
39
- start(top: Operation): Promise<Interpreter>;
43
+ createCPS(): CPS;
44
+ private initScopes;
45
+ private start;
46
+ run({ customCode, ctxOptions }?: InterpreterRunOptions): Promise<Interpreter>;
40
47
  resume(): Interpreter;
41
48
  pause(): Interpreter;
42
49
  exit(): Promise<OperationContext>;
@@ -45,51 +45,36 @@ class Interpreter extends events_1.EventEmitter {
45
45
  throw new Error('You cannot set a target while a process is running.');
46
46
  }
47
47
  this.target = target;
48
- const cpsCtx = new cps_1.CPSContext(target, this.handler);
49
- this.cps = new cps_1.CPS(cpsCtx);
50
- this.apiContext = new context_1.OperationContext({
51
- target,
52
- isProtected: true,
53
- debugger: this.debugger,
54
- handler: this.handler,
55
- cps: this.cps,
56
- environmentVariables: this.environmentVariables
57
- });
58
- this.globalContext = this.apiContext.fork({
59
- type: context_1.ContextType.Global,
60
- state: context_1.ContextState.Default
61
- });
62
48
  return this;
63
49
  }
64
50
  setDebugger(dbgr) {
65
51
  if (this.apiContext !== null && this.apiContext.isPending()) {
66
- throw new Error('You cannot set a target while a process is running.');
52
+ throw new Error('You cannot set a debugger while a process is running.');
67
53
  }
68
54
  this.debugger = dbgr;
69
- this.apiContext.debugger = dbgr;
70
- this.globalContext.debugger = dbgr;
71
55
  return this;
72
56
  }
73
57
  setApi(newApi) {
74
58
  if (this.apiContext !== null && this.apiContext.isPending()) {
75
- throw new Error('You cannot set a target while a process is running.');
59
+ throw new Error('You cannot set an api object while a process is running.');
76
60
  }
77
61
  this.api = newApi;
78
62
  return this;
79
63
  }
80
64
  setHandler(handler) {
81
65
  if (this.apiContext !== null && this.apiContext.isPending()) {
82
- throw new Error('You cannot set a target while a process is running.');
66
+ throw new Error('You cannot set a handler while a process is running.');
83
67
  }
84
68
  this.handler = handler;
85
- this.apiContext.handler = handler;
86
- this.globalContext.handler = handler;
87
69
  return this;
88
70
  }
71
+ parse(code) {
72
+ const parser = new greybel_core_1.Parser(code);
73
+ return parser.parseChunk();
74
+ }
89
75
  prepare(code) {
90
76
  try {
91
- const parser = new greybel_core_1.Parser(code);
92
- const chunk = parser.parseChunk();
77
+ const chunk = this.parse(code);
93
78
  return this.cps.visit(chunk);
94
79
  }
95
80
  catch (err) {
@@ -136,33 +121,42 @@ class Interpreter extends events_1.EventEmitter {
136
121
  throw new Error('Unable to inject into last context.');
137
122
  });
138
123
  }
139
- run(customCode) {
140
- return __awaiter(this, void 0, void 0, function* () {
141
- const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
142
- const top = yield this.prepare(code);
143
- return this.start(top);
124
+ createCPS() {
125
+ const cpsCtx = new cps_1.CPSContext(this.target, this.handler);
126
+ return new cps_1.CPS(cpsCtx);
127
+ }
128
+ initScopes(ctxOptions) {
129
+ this.cps = this.createCPS();
130
+ const apiContext = new context_1.OperationContext(Object.assign({ target: this.target, isProtected: true, debugger: this.debugger, handler: this.handler, cps: this.cps, environmentVariables: this.environmentVariables, contextTypeIntrinsics: {
131
+ string: string_1.CustomString.getIntrinsics().fork(),
132
+ number: number_1.CustomNumber.getIntrinsics().fork(),
133
+ list: list_1.CustomList.getIntrinsics().fork(),
134
+ map: map_1.CustomMap.getIntrinsics().fork(),
135
+ function: function_1.CustomFunction.intrinsics.fork()
136
+ } }, ctxOptions));
137
+ const stringIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.string);
138
+ const numberIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.number);
139
+ const listIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.list);
140
+ const mapIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.map);
141
+ const funcRefIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.function);
142
+ apiContext.scope.set(new string_1.CustomString('string'), stringIntrinsics);
143
+ apiContext.scope.set(new string_1.CustomString('number'), numberIntrinsics);
144
+ apiContext.scope.set(new string_1.CustomString('list'), listIntrinsics);
145
+ apiContext.scope.set(new string_1.CustomString('map'), mapIntrinsics);
146
+ apiContext.scope.set(new string_1.CustomString('funcRef'), funcRefIntrinsics);
147
+ apiContext.scope.extend(this.api);
148
+ const globalContext = apiContext.fork({
149
+ type: context_1.ContextType.Global,
150
+ state: context_1.ContextState.Default
144
151
  });
152
+ const newParams = new list_1.CustomList(this.params.map((item) => new string_1.CustomString(item)));
153
+ globalContext.scope.set(exports.IS_GREYBEL_PROPERTY, new boolean_1.CustomBoolean(true));
154
+ globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
155
+ this.apiContext = apiContext;
156
+ this.globalContext = globalContext;
145
157
  }
146
158
  start(top) {
147
159
  return __awaiter(this, void 0, void 0, function* () {
148
- if (this.apiContext !== null && this.apiContext.isPending()) {
149
- throw new Error('Process already running.');
150
- }
151
- const stringIntrinsics = map_1.CustomMap.createWithInitialValue(string_1.CustomString.intrinsics);
152
- const numberIntrinsics = map_1.CustomMap.createWithInitialValue(number_1.CustomNumber.intrinsics);
153
- const listIntrinsics = map_1.CustomMap.createWithInitialValue(list_1.CustomList.intrinsics);
154
- const mapIntrinsics = map_1.CustomMap.createWithInitialValue(map_1.CustomMap.intrinsics);
155
- const funcRefIntrinsics = map_1.CustomMap.createWithInitialValue(function_1.CustomFunction.intrinsics);
156
- this.apiContext.set(new string_1.CustomString('string'), stringIntrinsics);
157
- this.apiContext.set(new string_1.CustomString('number'), numberIntrinsics);
158
- this.apiContext.set(new string_1.CustomString('list'), listIntrinsics);
159
- this.apiContext.set(new string_1.CustomString('map'), mapIntrinsics);
160
- this.apiContext.set(new string_1.CustomString('funcRef'), funcRefIntrinsics);
161
- this.apiContext.extend(this.api);
162
- const newParams = new list_1.CustomList(this.params.map((item) => new string_1.CustomString(item)));
163
- this.globalContext.scope.set(exports.IS_GREYBEL_PROPERTY, new boolean_1.CustomBoolean(true));
164
- this.globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
165
- this.globalContext.set(new string_1.CustomString('globals'), this.globalContext.scope);
166
160
  try {
167
161
  this.apiContext.setPending(true);
168
162
  const process = top.handle(this.globalContext);
@@ -184,6 +178,17 @@ class Interpreter extends events_1.EventEmitter {
184
178
  return this;
185
179
  });
186
180
  }
181
+ run({ customCode, ctxOptions } = {}) {
182
+ return __awaiter(this, void 0, void 0, function* () {
183
+ if (this.apiContext !== null && this.apiContext.isPending()) {
184
+ throw new Error('Process already running.');
185
+ }
186
+ this.initScopes(ctxOptions);
187
+ const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
188
+ const top = yield this.prepare(code);
189
+ return this.start(top);
190
+ });
191
+ }
187
192
  resume() {
188
193
  if (this.apiContext !== null && this.apiContext.isPending()) {
189
194
  this.debugger.setBreakpoint(false);
@@ -0,0 +1,11 @@
1
+ import { ASTAssignmentStatement } from 'miniscript-core';
2
+ import { OperationContext } from '../context';
3
+ import { CustomValue } from '../types/base';
4
+ import { CPSVisit, Operation } from './operation';
5
+ export declare class AssignGlobals extends Operation {
6
+ readonly item: ASTAssignmentStatement;
7
+ right: Operation;
8
+ constructor(item: ASTAssignmentStatement, target?: string);
9
+ build(visit: CPSVisit): Promise<Operation>;
10
+ handle(_ctx: OperationContext): Promise<CustomValue>;
11
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AssignGlobals = void 0;
13
+ const operation_1 = require("./operation");
14
+ class AssignGlobals extends operation_1.Operation {
15
+ constructor(item, target) {
16
+ super(null, target);
17
+ this.item = item;
18
+ }
19
+ build(visit) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ this.right = yield visit(this.item.init);
22
+ return this;
23
+ });
24
+ }
25
+ handle(_ctx) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('Cannot assign to globals.');
28
+ });
29
+ }
30
+ }
31
+ exports.AssignGlobals = AssignGlobals;
@@ -0,0 +1,11 @@
1
+ import { ASTAssignmentStatement } from 'miniscript-core';
2
+ import { OperationContext } from '../context';
3
+ import { CustomValue } from '../types/base';
4
+ import { CPSVisit, Operation } from './operation';
5
+ export declare class AssignLocals extends Operation {
6
+ readonly item: ASTAssignmentStatement;
7
+ right: Operation;
8
+ constructor(item: ASTAssignmentStatement, target?: string);
9
+ build(visit: CPSVisit): Promise<Operation>;
10
+ handle(_ctx: OperationContext): Promise<CustomValue>;
11
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AssignLocals = void 0;
13
+ const operation_1 = require("./operation");
14
+ class AssignLocals extends operation_1.Operation {
15
+ constructor(item, target) {
16
+ super(null, target);
17
+ this.item = item;
18
+ }
19
+ build(visit) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ this.right = yield visit(this.item.init);
22
+ return this;
23
+ });
24
+ }
25
+ handle(_ctx) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ throw new Error('Cannot assign to locals.');
28
+ });
29
+ }
30
+ }
31
+ exports.AssignLocals = AssignLocals;
@@ -0,0 +1,11 @@
1
+ import { ASTAssignmentStatement } from 'miniscript-core';
2
+ import { OperationContext } from '../context';
3
+ import { CustomValue } from '../types/base';
4
+ import { CPSVisit, Operation } from './operation';
5
+ export declare class AssignOuter extends Operation {
6
+ readonly item: ASTAssignmentStatement;
7
+ right: Operation;
8
+ constructor(item: ASTAssignmentStatement, target?: string);
9
+ build(visit: CPSVisit): Promise<Operation>;
10
+ handle(_ctx: OperationContext): Promise<CustomValue>;
11
+ }