greybel-interpreter 1.7.7 → 1.7.9

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 CHANGED
@@ -54,6 +54,7 @@ export declare class FunctionState {
54
54
  }
55
55
  export interface ContextOptions {
56
56
  target?: string;
57
+ stackTrace?: Operation[];
57
58
  previous?: OperationContext;
58
59
  type?: ContextType;
59
60
  state?: ContextState;
@@ -73,7 +74,7 @@ export interface ContextForkOptions {
73
74
  }
74
75
  export declare class OperationContext {
75
76
  target: string;
76
- stackItem: ASTBase;
77
+ stackTrace: Operation[];
77
78
  debugger: Debugger;
78
79
  environmentVariables: Map<string, string>;
79
80
  handler: HandlerContainer;
@@ -94,7 +95,7 @@ export declare class OperationContext {
94
95
  private static readonly lookupGlobalsType;
95
96
  private static readonly lookupLocalsType;
96
97
  constructor(options?: ContextOptions);
97
- step(op: Operation): Promise<void>;
98
+ step(op: Operation): Promise<CustomValue>;
98
99
  setLastActive(ctx: OperationContext): OperationContext;
99
100
  getLastActive(): OperationContext;
100
101
  isExit(): boolean;
package/dist/context.js CHANGED
@@ -1,7 +1,17 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.OperationContext = exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
4
13
  const handler_container_1 = require("./handler-container");
14
+ const operation_1 = require("./operations/operation");
5
15
  const base_1 = require("./types/base");
6
16
  const default_1 = require("./types/default");
7
17
  const map_1 = require("./types/map");
@@ -132,7 +142,7 @@ exports.FunctionState = FunctionState;
132
142
  class OperationContext {
133
143
  constructor(options = {}) {
134
144
  this.target = options.target || 'unknown';
135
- this.stackItem = null;
145
+ this.stackTrace = options.stackTrace || [];
136
146
  this.previous = options.previous || null;
137
147
  this.type = options.type || ContextType.Api;
138
148
  this.state = options.state || ContextState.Default;
@@ -149,16 +159,26 @@ class OperationContext {
149
159
  this.locals = this.lookupLocals() || this;
150
160
  }
151
161
  step(op) {
152
- if (!this.injected) {
153
- this.stackItem = op.item;
154
- this.target = op.target || this.target;
155
- this.setLastActive(this);
156
- if (this.debugger.getBreakpoint(this)) {
157
- this.debugger.interact(this, op.item, op);
158
- return this.debugger.resume();
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ if (!this.injected) {
164
+ this.target = op.target || this.target;
165
+ this.setLastActive(this);
166
+ if (this.debugger.getBreakpoint(this)) {
167
+ this.debugger.interact(this, op.item, op);
168
+ yield this.debugger.resume();
169
+ }
159
170
  }
160
- }
161
- return Promise.resolve();
171
+ let result;
172
+ if (op instanceof operation_1.OperationBlock) {
173
+ result = yield op.handle(this);
174
+ }
175
+ else {
176
+ this.stackTrace.unshift(op);
177
+ result = yield op.handle(this);
178
+ this.stackTrace.shift();
179
+ }
180
+ return result;
181
+ });
162
182
  }
163
183
  setLastActive(ctx) {
164
184
  if (!ctx.injected) {
@@ -276,6 +296,7 @@ class OperationContext {
276
296
  fork(options) {
277
297
  const newContext = new OperationContext({
278
298
  target: options.target || this.target,
299
+ stackTrace: this.stackTrace,
279
300
  previous: this,
280
301
  type: options.type,
281
302
  state: options.state,
package/dist/cps.js CHANGED
@@ -95,7 +95,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
95
95
  }
96
96
  try {
97
97
  const subVisit = visit.bind(null, context, [...stack, target]);
98
- const importStatement = yield new import_1.Import(importExpr, target, code).build(subVisit);
98
+ const importStatement = yield new import_1.Import(importExpr, currentTarget, target, code).build(subVisit);
99
99
  return importStatement;
100
100
  }
101
101
  catch (err) {
@@ -124,7 +124,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
124
124
  }
125
125
  try {
126
126
  const subVisit = visit.bind(null, context, [...stack, target]);
127
- const importStatement = yield new include_1.Include(includeExpr, target, code).build(subVisit);
127
+ const importStatement = yield new include_1.Include(includeExpr, currentTarget, target, code).build(subVisit);
128
128
  return importStatement;
129
129
  }
130
130
  catch (err) {
@@ -157,7 +157,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
157
157
  }
158
158
  try {
159
159
  const subVisit = visit.bind(null, context, [...stack, target]);
160
- const importStatement = yield new include_1.Include(importExpr, target, code).build(subVisit);
160
+ const importStatement = yield new include_1.Include(importExpr, currentTarget, target, code).build(subVisit);
161
161
  return importStatement;
162
162
  }
163
163
  catch (err) {
@@ -36,7 +36,6 @@ class Block extends operation_1.Operation {
36
36
  }
37
37
  const op = this.stack[index];
38
38
  yield ctx.step(op);
39
- yield op.handle(ctx);
40
39
  }
41
40
  return default_1.DefaultType.Void;
42
41
  });
@@ -2,8 +2,8 @@ import { ASTChunk } from 'greyscript-core';
2
2
  import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
4
  import { Block } from './block';
5
- import { CPSVisit, Operation } from './operation';
6
- export declare class Chunk extends Operation {
5
+ import { CPSVisit, Operation, OperationBlock } from './operation';
6
+ export declare class Chunk extends OperationBlock {
7
7
  readonly item: ASTChunk;
8
8
  block: Block;
9
9
  constructor(item: ASTChunk, target?: string);
@@ -13,7 +13,7 @@ exports.Chunk = void 0;
13
13
  const string_1 = require("../types/string");
14
14
  const block_1 = require("./block");
15
15
  const operation_1 = require("./operation");
16
- class Chunk extends operation_1.Operation {
16
+ class Chunk extends operation_1.OperationBlock {
17
17
  constructor(item, target) {
18
18
  super(null, target);
19
19
  this.item = item;
@@ -2,8 +2,8 @@ import { ASTForGenericStatement, ASTIdentifier } from 'greyscript-core';
2
2
  import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
4
  import { Block } from './block';
5
- import { CPSVisit, Operation } from './operation';
6
- export declare class For extends Operation {
5
+ import { CPSVisit, Operation, OperationBlock } from './operation';
6
+ export declare class For extends OperationBlock {
7
7
  readonly item: ASTForGenericStatement;
8
8
  block: Block;
9
9
  variable: ASTIdentifier;
@@ -16,7 +16,7 @@ const number_1 = require("../types/number");
16
16
  const string_1 = require("../types/string");
17
17
  const block_1 = require("./block");
18
18
  const operation_1 = require("./operation");
19
- class For extends operation_1.Operation {
19
+ class For extends operation_1.OperationBlock {
20
20
  constructor(item, target) {
21
21
  super(null, target);
22
22
  this.item = item;
@@ -2,13 +2,13 @@ import { ASTElseClause, ASTIfClause, ASTIfStatement } from 'greyscript-core';
2
2
  import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
4
  import { Block } from './block';
5
- import { CPSVisit, Operation } from './operation';
5
+ import { CPSVisit, Operation, OperationBlock } from './operation';
6
6
  export declare class Clause {
7
7
  readonly condition: Operation;
8
8
  readonly block: Block;
9
9
  constructor(condition: Operation, block: Block);
10
10
  }
11
- export declare class IfStatement extends Operation {
11
+ export declare class IfStatement extends OperationBlock {
12
12
  readonly item: ASTIfStatement;
13
13
  clauses: Array<Clause>;
14
14
  constructor(item: ASTIfStatement, target?: string);
@@ -23,7 +23,7 @@ class Clause {
23
23
  }
24
24
  }
25
25
  exports.Clause = Clause;
26
- class IfStatement extends operation_1.Operation {
26
+ class IfStatement extends operation_1.OperationBlock {
27
27
  constructor(item, target) {
28
28
  super(null, target);
29
29
  this.item = item;
@@ -69,7 +69,7 @@ class IfStatement extends operation_1.Operation {
69
69
  return __awaiter(this, void 0, void 0, function* () {
70
70
  for (let index = 0; index < this.clauses.length; index++) {
71
71
  const clause = this.clauses[index];
72
- const clauseResult = yield clause.condition.handle(ctx);
72
+ const clauseResult = yield ctx.step(clause.condition);
73
73
  if (clauseResult.toTruthy()) {
74
74
  yield clause.block.handle(ctx);
75
75
  break;
@@ -10,10 +10,11 @@ export declare const EXPORTS_PROPERTY: CustomString;
10
10
  export declare const EXPORTS_PATH: Path<CustomString>;
11
11
  export declare class Import extends Operation {
12
12
  readonly item: ASTFeatureImportExpression;
13
+ newTarget: string;
13
14
  code: string;
14
15
  chunk: ASTBase;
15
16
  top: Operation;
16
- constructor(item: ASTFeatureImportExpression, target: string, code: string);
17
+ constructor(item: ASTFeatureImportExpression, target: string, newTarget: string, code: string);
17
18
  build(visit: CPSVisit): Promise<Operation>;
18
19
  handle(ctx: OperationContext): Promise<CustomValue>;
19
20
  }
@@ -21,8 +21,9 @@ exports.MODULE_PROPERTY = new string_1.CustomString('module');
21
21
  exports.EXPORTS_PROPERTY = new string_1.CustomString('exports');
22
22
  exports.EXPORTS_PATH = new path_1.Path([exports.MODULE_PROPERTY, exports.EXPORTS_PROPERTY]);
23
23
  class Import extends operation_1.Operation {
24
- constructor(item, target, code) {
24
+ constructor(item, target, newTarget, code) {
25
25
  super(null, target);
26
+ this.newTarget = newTarget;
26
27
  this.item = item;
27
28
  this.code = code;
28
29
  }
@@ -39,7 +40,7 @@ class Import extends operation_1.Operation {
39
40
  const importCtx = ctx.fork({
40
41
  type: context_1.ContextType.External,
41
42
  state: context_1.ContextState.Default,
42
- target: this.target
43
+ target: this.newTarget
43
44
  });
44
45
  importCtx.locals.scope.set(exports.MODULE_PROPERTY, new map_1.CustomMap());
45
46
  yield this.top.handle(importCtx);
@@ -4,10 +4,11 @@ import { CustomValue } from '../types/base';
4
4
  import { CPSVisit, Operation } from './operation';
5
5
  export declare class Include extends Operation {
6
6
  readonly item: ASTBase;
7
+ newTarget: string;
7
8
  code: string;
8
9
  chunk: ASTBase;
9
10
  top: Operation;
10
- constructor(item: ASTBase, target: string, code: string);
11
+ constructor(item: ASTBase, target: string, newTarget: string, code: string);
11
12
  build(visit: CPSVisit): Promise<Operation>;
12
13
  handle(ctx: OperationContext): Promise<CustomValue>;
13
14
  }
@@ -14,8 +14,9 @@ const greybel_core_1 = require("greybel-core");
14
14
  const context_1 = require("../context");
15
15
  const operation_1 = require("./operation");
16
16
  class Include extends operation_1.Operation {
17
- constructor(item, target, code) {
17
+ constructor(item, target, newTarget, code) {
18
18
  super(null, target);
19
+ this.newTarget = newTarget;
19
20
  this.item = item;
20
21
  this.code = code;
21
22
  }
@@ -31,7 +32,7 @@ class Include extends operation_1.Operation {
31
32
  const importCtx = ctx.fork({
32
33
  type: context_1.ContextType.External,
33
34
  state: context_1.ContextState.Temporary,
34
- target: this.target
35
+ target: this.newTarget
35
36
  });
36
37
  return this.top.handle(importCtx);
37
38
  }
@@ -11,3 +11,5 @@ export declare abstract class Operation {
11
11
  abstract build(visit: CPSVisit): Promise<Operation>;
12
12
  abstract handle(ctx: OperationContext): Promise<CustomValue>;
13
13
  }
14
+ export declare abstract class OperationBlock extends Operation {
15
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Operation = void 0;
3
+ exports.OperationBlock = exports.Operation = void 0;
4
4
  class Operation {
5
5
  constructor(item, target = null) {
6
6
  this.item = item;
@@ -8,3 +8,6 @@ class Operation {
8
8
  }
9
9
  }
10
10
  exports.Operation = Operation;
11
+ class OperationBlock extends Operation {
12
+ }
13
+ exports.OperationBlock = OperationBlock;
@@ -2,8 +2,8 @@ import { ASTWhileStatement } from 'greyscript-core';
2
2
  import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
4
  import { Block } from './block';
5
- import { CPSVisit, Operation } from './operation';
6
- export declare class While extends Operation {
5
+ import { CPSVisit, Operation, OperationBlock } from './operation';
6
+ export declare class While extends OperationBlock {
7
7
  readonly item: ASTWhileStatement;
8
8
  block: Block;
9
9
  condition: Operation;
@@ -14,7 +14,7 @@ const context_1 = require("../context");
14
14
  const default_1 = require("../types/default");
15
15
  const block_1 = require("./block");
16
16
  const operation_1 = require("./operation");
17
- class While extends operation_1.Operation {
17
+ class While extends operation_1.OperationBlock {
18
18
  constructor(item, target) {
19
19
  super(null, target);
20
20
  this.item = item;
@@ -38,7 +38,7 @@ class While extends operation_1.Operation {
38
38
  return new Promise((resolve, reject) => {
39
39
  const iteration = () => __awaiter(this, void 0, void 0, function* () {
40
40
  try {
41
- const conditionResult = yield this.condition.handle(whileCtx);
41
+ const conditionResult = yield whileCtx.step(this.condition);
42
42
  if (!conditionResult.toTruthy()) {
43
43
  resolve(default_1.DefaultType.Void);
44
44
  return;
@@ -1,12 +1,12 @@
1
1
  import { ASTBase } from 'greyscript-core';
2
+ import { Operation } from '../operations/operation';
2
3
  interface RuntimeContext {
3
- previous?: RuntimeContext;
4
- stackItem?: ASTBase;
4
+ stackTrace?: Operation[];
5
5
  target: string;
6
6
  }
7
7
  export declare class RuntimeError extends Error {
8
- relatedItem: ASTBase | null;
9
8
  relatedTarget: string;
9
+ stackTrace: Operation[];
10
10
  source?: Error;
11
11
  constructor(message: string, context: RuntimeContext, source?: Error);
12
12
  private createTrace;
@@ -4,24 +4,18 @@ exports.PrepareError = exports.RuntimeError = void 0;
4
4
  class RuntimeError extends Error {
5
5
  constructor(message, context, source) {
6
6
  super(message);
7
- this.relatedItem = context.stackItem || null;
8
7
  this.relatedTarget = context.target;
9
- this.stack = this.createTrace(context);
8
+ this.stackTrace = context.stackTrace || [];
9
+ this.stack = this.createTrace();
10
10
  this.source = source;
11
11
  }
12
- createTrace(context) {
13
- var _a, _b;
14
- const related = new Map();
15
- let item = context;
16
- while (item) {
17
- related.set(item.stackItem, item.target);
18
- item = item.previous;
19
- }
20
- const lines = [];
21
- for (const [item, target] of related) {
22
- lines.push(`${target} at ${(_a = item === null || item === void 0 ? void 0 : item.start.line) !== null && _a !== void 0 ? _a : 0}:${(_b = item === null || item === void 0 ? void 0 : item.start.character) !== null && _b !== void 0 ? _b : 0}`);
23
- }
24
- return lines.join('\n');
12
+ createTrace() {
13
+ return this.stackTrace
14
+ .map((op) => {
15
+ var _a, _b, _c, _d;
16
+ return `${op.target} at ${(_b = (_a = op.item) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : 0}:${(_d = (_c = op.item) === null || _c === void 0 ? void 0 : _c.start.character) !== null && _d !== void 0 ? _d : 0}`;
17
+ })
18
+ .join('\n');
25
19
  }
26
20
  }
27
21
  exports.RuntimeError = RuntimeError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "1.7.7",
3
+ "version": "1.7.9",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",