greybel-interpreter 2.2.20 → 2.3.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 (49) hide show
  1. package/dist/context.js +1 -4
  2. package/dist/cps.js +5 -2
  3. package/dist/interpreter.js +0 -1
  4. package/dist/operations/assign-globals.d.ts +11 -0
  5. package/dist/operations/assign-globals.js +31 -0
  6. package/dist/operations/assign-locals.d.ts +11 -0
  7. package/dist/operations/assign-locals.js +31 -0
  8. package/dist/operations/assign-outer.d.ts +11 -0
  9. package/dist/operations/assign-outer.js +31 -0
  10. package/dist/operations/assign-self.d.ts +11 -0
  11. package/dist/operations/assign-self.js +34 -0
  12. package/dist/operations/assign.js +5 -1
  13. package/dist/operations/call.js +7 -8
  14. package/dist/operations/chunk.js +0 -2
  15. package/dist/operations/function-reference.js +2 -1
  16. package/dist/operations/function.js +6 -3
  17. package/dist/operations/reference-globals.d.ts +7 -0
  18. package/dist/operations/reference-globals.js +13 -0
  19. package/dist/operations/reference-locals.d.ts +7 -0
  20. package/dist/operations/reference-locals.js +13 -0
  21. package/dist/operations/reference-outer.d.ts +7 -0
  22. package/dist/operations/reference-outer.js +13 -0
  23. package/dist/operations/reference-self.d.ts +7 -0
  24. package/dist/operations/reference-self.js +13 -0
  25. package/dist/operations/resolve-globals.d.ts +5 -0
  26. package/dist/operations/resolve-globals.js +24 -0
  27. package/dist/operations/resolve-locals.d.ts +5 -0
  28. package/dist/operations/resolve-locals.js +24 -0
  29. package/dist/operations/resolve-outer.d.ts +5 -0
  30. package/dist/operations/resolve-outer.js +24 -0
  31. package/dist/operations/resolve-self.d.ts +5 -0
  32. package/dist/operations/resolve-self.js +24 -0
  33. package/dist/operations/resolve.d.ts +1 -1
  34. package/dist/operations/resolve.js +9 -7
  35. package/dist/types/function.js +18 -24
  36. package/dist/types/map.js +2 -1
  37. package/dist/types/number.js +3 -3
  38. package/dist/types/string.js +2 -3
  39. package/dist/types/with-intrinsics.d.ts +1 -0
  40. package/dist/types/with-intrinsics.js +3 -0
  41. package/dist/utils/create-assign.d.ts +3 -0
  42. package/dist/utils/create-assign.js +25 -0
  43. package/dist/utils/create-resolve.d.ts +5 -0
  44. package/dist/utils/create-resolve.js +55 -0
  45. package/dist/utils/get-super.d.ts +3 -0
  46. package/dist/utils/get-super.js +12 -0
  47. package/dist/utils/lookup-path.d.ts +2 -0
  48. package/dist/utils/lookup-path.js +14 -0
  49. package/package.json +1 -1
package/dist/context.js CHANGED
@@ -61,12 +61,9 @@ class Scope extends map_1.CustomMap {
61
61
  else if ((_c = this.context.api) === null || _c === void 0 ? void 0 : _c.scope.has(path)) {
62
62
  return this.context.api.scope.get(path);
63
63
  }
64
- else if (path.count() === 1 && map_1.CustomMap.getIntrinsics().has(current)) {
64
+ else if (traversalPath.count() === 0 && map_1.CustomMap.getIntrinsics().has(current)) {
65
65
  return map_1.CustomMap.getIntrinsics().get(current);
66
66
  }
67
- else if (this.context.previous !== null) {
68
- return this.context.previous.get(path);
69
- }
70
67
  throw new Error(`Unknown path ${path.toString()}.`);
71
68
  }
72
69
  }
package/dist/cps.js CHANGED
@@ -12,7 +12,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CPS = exports.CPSContext = void 0;
13
13
  const greybel_core_1 = require("greybel-core");
14
14
  const greyscript_core_1 = require("greyscript-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");
@@ -37,6 +36,8 @@ const resolve_1 = require("./operations/resolve");
37
36
  const return_1 = require("./operations/return");
38
37
  const while_1 = require("./operations/while");
39
38
  const error_1 = require("./utils/error");
39
+ const create_resolve_1 = require("./utils/create-resolve");
40
+ const create_assign_1 = require("./utils/create-assign");
40
41
  class CPSContext {
41
42
  constructor(target, handler) {
42
43
  this.target = target;
@@ -53,9 +54,11 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
53
54
  case greyscript_core_1.ASTType.ListConstructorExpression:
54
55
  return new list_1.List(item, currentTarget).build(defaultVisit);
55
56
  case greyscript_core_1.ASTType.AssignmentStatement:
56
- return new assign_1.Assign(item, currentTarget).build(defaultVisit);
57
+ return (0, create_assign_1.createAssign)(item, currentTarget).build(defaultVisit);
57
58
  case greyscript_core_1.ASTType.MemberExpression:
59
+ return (0, create_resolve_1.createResolve)(item, currentTarget).build(defaultVisit);
58
60
  case greyscript_core_1.ASTType.Identifier:
61
+ return (0, create_resolve_1.createIdentifierResolve)(item, currentTarget).build(defaultVisit);
59
62
  case greyscript_core_1.ASTType.IndexExpression:
60
63
  case greyscript_core_1.ASTType.SliceExpression:
61
64
  return new resolve_1.Resolve(item, currentTarget).build(defaultVisit);
@@ -162,7 +162,6 @@ class Interpreter extends events_1.EventEmitter {
162
162
  const newParams = new list_1.CustomList(this.params.map((item) => new string_1.CustomString(item)));
163
163
  this.globalContext.scope.set(exports.IS_GREYBEL_PROPERTY, new boolean_1.CustomBoolean(true));
164
164
  this.globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
165
- this.globalContext.set(new string_1.CustomString('globals'), this.globalContext.scope);
166
165
  try {
167
166
  this.apiContext.setPending(true);
168
167
  const process = top.handle(this.globalContext);
@@ -0,0 +1,11 @@
1
+ import { ASTAssignmentStatement } from 'greyscript-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 'greyscript-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 'greyscript-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
+ }
@@ -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.AssignOuter = void 0;
13
+ const operation_1 = require("./operation");
14
+ class AssignOuter 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 outer.');
28
+ });
29
+ }
30
+ }
31
+ exports.AssignOuter = AssignOuter;
@@ -0,0 +1,11 @@
1
+ import { ASTAssignmentStatement } from 'greyscript-core';
2
+ import { OperationContext } from '../context';
3
+ import { CustomValue } from '../types/base';
4
+ import { CPSVisit, Operation } from './operation';
5
+ export declare class AssignSelf 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,34 @@
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.AssignSelf = void 0;
13
+ const default_1 = require("../types/default");
14
+ const operation_1 = require("./operation");
15
+ class AssignSelf extends operation_1.Operation {
16
+ constructor(item, target) {
17
+ super(null, target);
18
+ this.item = item;
19
+ }
20
+ build(visit) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ this.right = yield visit(this.item.init);
23
+ return this;
24
+ });
25
+ }
26
+ handle(ctx) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const rightValue = yield this.right.handle(ctx);
29
+ ctx.functionState.context = rightValue;
30
+ return default_1.DefaultType.Void;
31
+ });
32
+ }
33
+ }
34
+ exports.AssignSelf = AssignSelf;
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Assign = void 0;
13
13
  const default_1 = require("../types/default");
14
+ const create_resolve_1 = require("../utils/create-resolve");
14
15
  const operation_1 = require("./operation");
15
16
  const resolve_1 = require("./resolve");
16
17
  class Assign extends operation_1.Operation {
@@ -20,7 +21,7 @@ class Assign extends operation_1.Operation {
20
21
  }
21
22
  build(visit) {
22
23
  return __awaiter(this, void 0, void 0, function* () {
23
- this.left = new resolve_1.Resolve(this.item.variable);
24
+ this.left = (0, create_resolve_1.createResolve)(this.item.variable, this.target);
24
25
  yield this.left.build(visit);
25
26
  this.right = yield visit(this.item.init);
26
27
  return this;
@@ -33,6 +34,9 @@ class Assign extends operation_1.Operation {
33
34
  return default_1.DefaultType.Void;
34
35
  }
35
36
  const rightValue = yield this.right.handle(ctx);
37
+ if (resolveResult.path.count() === 0) {
38
+ throw new Error('Resolve path cannot be empty.');
39
+ }
36
40
  if (!(resolveResult.handle instanceof resolve_1.ResolveNil)) {
37
41
  const resultValueCtx = resolveResult.handle;
38
42
  resultValueCtx.set(resolveResult.path, rightValue);
@@ -12,9 +12,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Call = void 0;
13
13
  const default_1 = require("../types/default");
14
14
  const function_1 = require("../types/function");
15
- const map_1 = require("../types/map");
15
+ const create_resolve_1 = require("../utils/create-resolve");
16
+ const get_super_1 = require("../utils/get-super");
16
17
  const operation_1 = require("./operation");
17
- const resolve_1 = require("./resolve");
18
18
  class Call extends operation_1.Operation {
19
19
  constructor(item, target) {
20
20
  super(null, target);
@@ -22,7 +22,7 @@ class Call extends operation_1.Operation {
22
22
  }
23
23
  build(visit) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
- this.fnRef = new resolve_1.Resolve(this.item.base);
25
+ this.fnRef = (0, create_resolve_1.createResolve)(this.item.base, this.target);
26
26
  yield this.fnRef.build(visit);
27
27
  const args = this.item.arguments.map((arg) => visit(arg));
28
28
  this.args = yield Promise.all(args);
@@ -42,12 +42,11 @@ class Call extends operation_1.Operation {
42
42
  }
43
43
  if (valueRef instanceof function_1.CustomFunction) {
44
44
  const func = valueRef;
45
- if (this.fnRef.path.isSuper() &&
46
- ctx.functionState.context &&
47
- resolveResult.handle instanceof map_1.CustomMap) {
48
- return func.run(ctx.functionState.context, fnArgs, ctx, resolveResult.handle.getIsa());
45
+ const next = (0, get_super_1.getSuper)(resolveResult.handle);
46
+ if (this.fnRef.path.isSuper() && ctx.functionState.context && next) {
47
+ return func.run(ctx.functionState.context, fnArgs, ctx, next);
49
48
  }
50
- return func.run(resolveResult.handle, fnArgs, ctx);
49
+ return func.run(resolveResult.handle, fnArgs, ctx, next);
51
50
  }
52
51
  return default_1.DefaultType.Void;
53
52
  });
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Chunk = void 0;
13
- const string_1 = require("../types/string");
14
13
  const block_1 = require("./block");
15
14
  const operation_1 = require("./operation");
16
15
  class Chunk extends operation_1.OperationBlock {
@@ -26,7 +25,6 @@ class Chunk extends operation_1.OperationBlock {
26
25
  });
27
26
  }
28
27
  handle(ctx) {
29
- ctx.set(new string_1.CustomString('locals'), ctx.locals.scope);
30
28
  return this.block.handle(ctx);
31
29
  }
32
30
  }
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.FunctionReference = void 0;
13
13
  const default_1 = require("../types/default");
14
+ const create_resolve_1 = require("../utils/create-resolve");
14
15
  const operation_1 = require("./operation");
15
16
  const resolve_1 = require("./resolve");
16
17
  class FunctionReference extends operation_1.Operation {
@@ -20,7 +21,7 @@ class FunctionReference extends operation_1.Operation {
20
21
  }
21
22
  build(visit) {
22
23
  return __awaiter(this, void 0, void 0, function* () {
23
- this.ref = new resolve_1.Resolve(this.item.argument);
24
+ this.ref = (0, create_resolve_1.createResolve)(this.item.argument, this.target);
24
25
  yield this.ref.build(visit);
25
26
  return this;
26
27
  });
@@ -15,6 +15,7 @@ const context_1 = require("../context");
15
15
  const default_1 = require("../types/default");
16
16
  const function_1 = require("../types/function");
17
17
  const string_1 = require("../types/string");
18
+ const with_intrinsics_1 = require("../types/with-intrinsics");
18
19
  const block_1 = require("./block");
19
20
  const operation_1 = require("./operation");
20
21
  const reference_1 = require("./reference");
@@ -56,15 +57,17 @@ class FunctionOperation extends operation_1.Operation {
56
57
  handle(ctx) {
57
58
  const func = new function_1.CustomFunction(ctx, 'anonymous', (fnCtx, self, args, next) => __awaiter(this, void 0, void 0, function* () {
58
59
  const functionState = new context_1.FunctionState();
59
- fnCtx.set(exports.SELF_PROPERTY, self);
60
60
  functionState.context = self;
61
+ if (self instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
62
+ fnCtx.set(exports.SELF_PROPERTY, self);
63
+ }
61
64
  if (next) {
62
65
  fnCtx.set(exports.SUPER_PROPERTY, next);
63
66
  functionState.super = next;
64
67
  }
65
- fnCtx.set(new string_1.CustomString('locals'), fnCtx.locals.scope);
66
- fnCtx.set(new string_1.CustomString('outer'), fnCtx.previous.locals.scope);
67
68
  for (const [key, value] of args) {
69
+ if (key === function_1.SELF_NAMESPACE)
70
+ continue;
68
71
  fnCtx.set(new string_1.CustomString(key), value);
69
72
  }
70
73
  fnCtx.functionState = functionState;
@@ -0,0 +1,7 @@
1
+ import { OperationContext } from '../context';
2
+ import { CustomValue } from '../types/base';
3
+ import { CPSVisit, Operation } from './operation';
4
+ export declare class ReferenceGlobals extends Operation {
5
+ build(_visit: CPSVisit): Promise<ReferenceGlobals>;
6
+ handle(ctx: OperationContext): Promise<CustomValue>;
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReferenceGlobals = void 0;
4
+ const operation_1 = require("./operation");
5
+ class ReferenceGlobals extends operation_1.Operation {
6
+ build(_visit) {
7
+ return Promise.resolve(this);
8
+ }
9
+ handle(ctx) {
10
+ return Promise.resolve(ctx.globals.scope);
11
+ }
12
+ }
13
+ exports.ReferenceGlobals = ReferenceGlobals;
@@ -0,0 +1,7 @@
1
+ import { OperationContext } from '../context';
2
+ import { CustomValue } from '../types/base';
3
+ import { CPSVisit, Operation } from './operation';
4
+ export declare class ReferenceLocals extends Operation {
5
+ build(_visit: CPSVisit): Promise<ReferenceLocals>;
6
+ handle(ctx: OperationContext): Promise<CustomValue>;
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReferenceLocals = void 0;
4
+ const operation_1 = require("./operation");
5
+ class ReferenceLocals extends operation_1.Operation {
6
+ build(_visit) {
7
+ return Promise.resolve(this);
8
+ }
9
+ handle(ctx) {
10
+ return Promise.resolve(ctx.locals.scope);
11
+ }
12
+ }
13
+ exports.ReferenceLocals = ReferenceLocals;
@@ -0,0 +1,7 @@
1
+ import { OperationContext } from '../context';
2
+ import { CustomValue } from '../types/base';
3
+ import { CPSVisit, Operation } from './operation';
4
+ export declare class ReferenceOuter extends Operation {
5
+ build(_visit: CPSVisit): Promise<ReferenceOuter>;
6
+ handle(ctx: OperationContext): Promise<CustomValue>;
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReferenceOuter = void 0;
4
+ const operation_1 = require("./operation");
5
+ class ReferenceOuter extends operation_1.Operation {
6
+ build(_visit) {
7
+ return Promise.resolve(this);
8
+ }
9
+ handle(ctx) {
10
+ return Promise.resolve(ctx.outer.scope);
11
+ }
12
+ }
13
+ exports.ReferenceOuter = ReferenceOuter;
@@ -0,0 +1,7 @@
1
+ import { OperationContext } from '../context';
2
+ import { CustomValue } from '../types/base';
3
+ import { CPSVisit, Operation } from './operation';
4
+ export declare class ReferenceSelf extends Operation {
5
+ build(_visit: CPSVisit): Promise<ReferenceSelf>;
6
+ handle(ctx: OperationContext): Promise<CustomValue>;
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReferenceSelf = void 0;
4
+ const operation_1 = require("./operation");
5
+ class ReferenceSelf extends operation_1.Operation {
6
+ build(_visit) {
7
+ return Promise.resolve(this);
8
+ }
9
+ handle(ctx) {
10
+ return Promise.resolve(ctx.functionState.context);
11
+ }
12
+ }
13
+ exports.ReferenceSelf = ReferenceSelf;
@@ -0,0 +1,5 @@
1
+ import { OperationContext } from '../context';
2
+ import { Resolve, ResolveResult } from './resolve';
3
+ export declare class ResolveGlobals extends Resolve {
4
+ getResult(ctx: OperationContext): Promise<ResolveResult | null>;
5
+ }
@@ -0,0 +1,24 @@
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.ResolveGlobals = void 0;
13
+ const resolve_1 = require("./resolve");
14
+ class ResolveGlobals extends resolve_1.Resolve {
15
+ getResult(ctx) {
16
+ const _super = Object.create(null, {
17
+ getResult: { get: () => super.getResult }
18
+ });
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return _super.getResult.call(this, ctx, ctx.globals.scope);
21
+ });
22
+ }
23
+ }
24
+ exports.ResolveGlobals = ResolveGlobals;
@@ -0,0 +1,5 @@
1
+ import { OperationContext } from '../context';
2
+ import { Resolve, ResolveResult } from './resolve';
3
+ export declare class ResolveLocals extends Resolve {
4
+ getResult(ctx: OperationContext): Promise<ResolveResult | null>;
5
+ }
@@ -0,0 +1,24 @@
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.ResolveLocals = void 0;
13
+ const resolve_1 = require("./resolve");
14
+ class ResolveLocals extends resolve_1.Resolve {
15
+ getResult(ctx) {
16
+ const _super = Object.create(null, {
17
+ getResult: { get: () => super.getResult }
18
+ });
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return _super.getResult.call(this, ctx, ctx.locals.scope);
21
+ });
22
+ }
23
+ }
24
+ exports.ResolveLocals = ResolveLocals;
@@ -0,0 +1,5 @@
1
+ import { OperationContext } from '../context';
2
+ import { Resolve, ResolveResult } from './resolve';
3
+ export declare class ResolveOuter extends Resolve {
4
+ getResult(ctx: OperationContext): Promise<ResolveResult | null>;
5
+ }
@@ -0,0 +1,24 @@
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.ResolveOuter = void 0;
13
+ const resolve_1 = require("./resolve");
14
+ class ResolveOuter extends resolve_1.Resolve {
15
+ getResult(ctx) {
16
+ const _super = Object.create(null, {
17
+ getResult: { get: () => super.getResult }
18
+ });
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return _super.getResult.call(this, ctx, ctx.outer.scope);
21
+ });
22
+ }
23
+ }
24
+ exports.ResolveOuter = ResolveOuter;
@@ -0,0 +1,5 @@
1
+ import { OperationContext } from '../context';
2
+ import { Resolve, ResolveResult } from './resolve';
3
+ export declare class ResolveSelf extends Resolve {
4
+ getResult(ctx: OperationContext): Promise<ResolveResult | null>;
5
+ }
@@ -0,0 +1,24 @@
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.ResolveSelf = void 0;
13
+ const resolve_1 = require("./resolve");
14
+ class ResolveSelf extends resolve_1.Resolve {
15
+ getResult(ctx) {
16
+ const _super = Object.create(null, {
17
+ getResult: { get: () => super.getResult }
18
+ });
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return _super.getResult.call(this, ctx, ctx.functionState.context);
21
+ });
22
+ }
23
+ }
24
+ exports.ResolveSelf = ResolveSelf;
@@ -50,6 +50,6 @@ export declare class Resolve extends Operation {
50
50
  constructor(item: ASTBase, target?: string);
51
51
  buildProcessor(node: ASTBase, visit: CPSVisit): Promise<void>;
52
52
  build(visit: CPSVisit): Promise<Resolve>;
53
- getResult(ctx: OperationContext): Promise<ResolveResult | null>;
53
+ getResult(ctx: OperationContext, handle?: CustomValue): Promise<ResolveResult | null>;
54
54
  handle(ctx: OperationContext, result?: ResolveResult, autoCall?: boolean): Promise<CustomValue>;
55
55
  }
@@ -18,6 +18,7 @@ const map_1 = require("../types/map");
18
18
  const nil_1 = require("../types/nil");
19
19
  const string_1 = require("../types/string");
20
20
  const with_intrinsics_1 = require("../types/with-intrinsics");
21
+ const get_super_1 = require("../utils/get-super");
21
22
  const path_1 = require("../utils/path");
22
23
  const operation_1 = require("./operation");
23
24
  class SliceSegment {
@@ -149,15 +150,15 @@ class Resolve extends operation_1.Operation {
149
150
  return this;
150
151
  });
151
152
  }
152
- getResult(ctx) {
153
+ getResult(ctx, handle = new ResolveNil()) {
153
154
  return __awaiter(this, void 0, void 0, function* () {
154
155
  let traversedPath = new path_1.Path();
155
- let handle = new ResolveNil();
156
+ let index = 0;
156
157
  const maxIndex = this.path.count();
157
158
  const lastIndex = maxIndex - 1;
158
- for (let index = 0; index < maxIndex; index++) {
159
+ for (; index < maxIndex; index++) {
159
160
  if (ctx.isExit()) {
160
- return new ResolveResult(null, default_1.DefaultType.Void);
161
+ return new ResolveResult(null, new ResolveNil());
161
162
  }
162
163
  const current = this.path.at(index);
163
164
  if (current instanceof OperationSegment) {
@@ -228,19 +229,20 @@ class Resolve extends operation_1.Operation {
228
229
  }
229
230
  if (result.handle instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
230
231
  const customValueCtx = result.handle;
232
+ const next = (0, get_super_1.getSuper)(customValueCtx);
231
233
  if (this.path.isSuper() && customValueCtx instanceof map_1.CustomMap) {
232
234
  const superChild = (_a = customValueCtx.getIsa()) === null || _a === void 0 ? void 0 : _a.get(result.path);
233
235
  if (autoCall && superChild instanceof function_1.CustomFunction) {
234
236
  if (ctx.functionState.context) {
235
- return superChild.run(ctx.functionState.context, [], ctx, customValueCtx.getIsa());
237
+ return superChild.run(ctx.functionState.context, [], ctx, next);
236
238
  }
237
- return superChild.run(customValueCtx, [], ctx);
239
+ return superChild.run(customValueCtx, [], ctx, next);
238
240
  }
239
241
  return superChild;
240
242
  }
241
243
  const child = customValueCtx.get(result.path);
242
244
  if (autoCall && child instanceof function_1.CustomFunction) {
243
- return child.run(customValueCtx, [], ctx);
245
+ return child.run(customValueCtx, [], ctx, next);
244
246
  }
245
247
  return child;
246
248
  }
@@ -104,40 +104,34 @@ class CustomFunction extends base_1.CustomValue {
104
104
  return v.value === CustomFunction.intrinsics;
105
105
  }
106
106
  run(self, args, callContext, next) {
107
- var _a, _b;
107
+ var _a, _b, _c, _d;
108
108
  return __awaiter(this, void 0, void 0, function* () {
109
+ if (args.length > this.argumentDefs.length) {
110
+ throw new Error('Too many arguments.');
111
+ }
109
112
  const fnCtx = (_a = this.scope) === null || _a === void 0 ? void 0 : _a.fork({
110
113
  type: context_1.ContextType.Function,
111
114
  state: context_1.ContextState.Default
112
115
  });
113
116
  const argMap = new Map();
114
- const isSelfNull = self instanceof nil_1.CustomNil;
115
- let index = 0;
116
- if (!isSelfNull) {
117
- for (; index < this.argumentDefs.length; index++) {
118
- const item = this.argumentDefs[index];
119
- if (item.name !== exports.SELF_NAMESPACE)
120
- break;
121
- }
122
- }
123
- let argIndex = 0;
124
- for (; index < this.argumentDefs.length; index++) {
125
- const item = this.argumentDefs[index];
126
- if (!isSelfNull && item.name === exports.SELF_NAMESPACE) {
127
- argIndex++;
117
+ const hasSelf = !(self instanceof nil_1.CustomNil);
118
+ let selfWithinArgs = default_1.DefaultType.Void;
119
+ let argIndex = this.argumentDefs.length - 1;
120
+ const selfParam = hasSelf && ((_b = this.argumentDefs[0]) === null || _b === void 0 ? void 0 : _b.name) === exports.SELF_NAMESPACE ? 1 : 0;
121
+ for (; argIndex >= selfParam; argIndex--) {
122
+ const item = this.argumentDefs[argIndex];
123
+ if (item.name === exports.SELF_NAMESPACE) {
124
+ selfWithinArgs = (_c = args[argIndex - selfParam]) !== null && _c !== void 0 ? _c : default_1.DefaultType.Void;
128
125
  continue;
129
126
  }
130
- if (argMap.has(item.name)) {
131
- argIndex++;
132
- continue;
133
- }
134
- argMap.set(item.name, (_b = args[argIndex++]) !== null && _b !== void 0 ? _b : (yield item.defaultValue.handle(fnCtx)));
135
- }
136
- if (!isSelfNull) {
137
- argMap.set(exports.SELF_NAMESPACE, self);
127
+ argMap.set(item.name, (_d = args[argIndex - selfParam]) !== null && _d !== void 0 ? _d : (yield item.defaultValue.handle(fnCtx)));
138
128
  }
129
+ const selfValue = hasSelf ? self : selfWithinArgs;
139
130
  const isa = next !== null && next !== void 0 ? next : (self instanceof map_1.CustomMap ? self.getIsa() : null);
140
- return this.value(fnCtx !== null && fnCtx !== void 0 ? fnCtx : callContext, self, argMap, isa);
131
+ if (selfValue) {
132
+ argMap.set(exports.SELF_NAMESPACE, selfValue);
133
+ }
134
+ return this.value(fnCtx !== null && fnCtx !== void 0 ? fnCtx : callContext, selfValue, argMap, isa);
141
135
  });
142
136
  }
143
137
  }
package/dist/types/map.js CHANGED
@@ -164,7 +164,8 @@ class CustomMap extends with_intrinsics_1.CustomObject {
164
164
  else if (isa === null || isa === void 0 ? void 0 : isa.has(current)) {
165
165
  return isa.get(current);
166
166
  }
167
- else if (path.count() === 1 && CustomMap.getIntrinsics().has(current)) {
167
+ else if (traversalPath.count() === 0 &&
168
+ CustomMap.getIntrinsics().has(current)) {
168
169
  return CustomMap.getIntrinsics().get(current);
169
170
  }
170
171
  }
@@ -4,7 +4,6 @@ exports.Zero = exports.PositiveOne = exports.NegativeOne = exports.CustomNumber
4
4
  const object_value_1 = require("../utils/object-value");
5
5
  const path_1 = require("../utils/path");
6
6
  const base_1 = require("./base");
7
- const nil_1 = require("./nil");
8
7
  const with_intrinsics_1 = require("./with-intrinsics");
9
8
  class CustomNumberIterator {
10
9
  constructor() {
@@ -62,10 +61,11 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
62
61
  }
63
62
  const traversalPath = path.clone();
64
63
  const current = traversalPath.next();
65
- if (path.count() === 1 && CustomNumber.getIntrinsics().has(current)) {
64
+ if (traversalPath.count() === 0 &&
65
+ CustomNumber.getIntrinsics().has(current)) {
66
66
  return CustomNumber.intrinsics.get(current);
67
67
  }
68
- return nil_1.Void;
68
+ throw new Error(`Unknown path in number ${path.toString()}.`);
69
69
  }
70
70
  }
71
71
  CustomNumber.intrinsics = new object_value_1.ObjectValue();
@@ -4,7 +4,6 @@ exports.CustomString = exports.CustomStringIterator = void 0;
4
4
  const object_value_1 = require("../utils/object-value");
5
5
  const path_1 = require("../utils/path");
6
6
  const base_1 = require("./base");
7
- const default_1 = require("./default");
8
7
  const number_1 = require("./number");
9
8
  const with_intrinsics_1 = require("./with-intrinsics");
10
9
  class CustomStringIterator {
@@ -113,11 +112,11 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
113
112
  }
114
113
  throw new Error(`Index error (string index ${currentIndex} out of range).`);
115
114
  }
116
- else if (path.count() === 1 &&
115
+ else if (traversalPath.count() === 0 &&
117
116
  CustomString.getIntrinsics().has(current)) {
118
117
  return CustomString.intrinsics.get(current);
119
118
  }
120
- return default_1.DefaultType.Void;
119
+ throw new Error(`Unknown path in string ${path.toString()}.`);
121
120
  }
122
121
  }
123
122
  CustomString.intrinsics = new object_value_1.ObjectValue();
@@ -12,6 +12,7 @@ export declare abstract class CustomValueWithIntrinsics extends CustomValue {
12
12
  static readonly intrinsics: ObjectValue;
13
13
  static getIntrinsics(): ObjectValue;
14
14
  static addIntrinsic(key: CustomValue, fn: CustomFunction): void;
15
+ getIntrinsics(): ObjectValue;
15
16
  }
16
17
  export declare abstract class CustomObject extends CustomValueWithIntrinsics {
17
18
  }
@@ -9,6 +9,9 @@ class CustomValueWithIntrinsics extends base_1.CustomValue {
9
9
  static addIntrinsic(key, fn) {
10
10
  this.intrinsics.set(key, fn);
11
11
  }
12
+ getIntrinsics() {
13
+ return this.constructor.intrinsics;
14
+ }
12
15
  }
13
16
  exports.CustomValueWithIntrinsics = CustomValueWithIntrinsics;
14
17
  class CustomObject extends CustomValueWithIntrinsics {
@@ -0,0 +1,3 @@
1
+ import { ASTAssignmentStatement } from 'greyscript-core';
2
+ import { Operation } from '../operations/operation';
3
+ export declare function createAssign(item: ASTAssignmentStatement, target?: string): Operation;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAssign = void 0;
4
+ const greyscript_core_1 = require("greyscript-core");
5
+ const assign_1 = require("../operations/assign");
6
+ const assign_globals_1 = require("../operations/assign-globals");
7
+ const assign_locals_1 = require("../operations/assign-locals");
8
+ const assign_outer_1 = require("../operations/assign-outer");
9
+ const assign_self_1 = require("../operations/assign-self");
10
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
11
+ const optAssignMap = {
12
+ self: assign_self_1.AssignSelf,
13
+ globals: assign_globals_1.AssignGlobals,
14
+ locals: assign_locals_1.AssignLocals,
15
+ outer: assign_outer_1.AssignOuter
16
+ };
17
+ function createAssign(item, target) {
18
+ if (item.variable instanceof greyscript_core_1.ASTIdentifier &&
19
+ hasOwnProperty.call(optAssignMap, item.variable.name)) {
20
+ const OptAssign = optAssignMap[item.variable.name];
21
+ return new OptAssign(item, target);
22
+ }
23
+ return new assign_1.Assign(item, target);
24
+ }
25
+ exports.createAssign = createAssign;
@@ -0,0 +1,5 @@
1
+ import { ASTBase, ASTIdentifier } from 'greyscript-core';
2
+ import { Operation } from '../operations/operation';
3
+ import { Resolve } from '../operations/resolve';
4
+ export declare function createIdentifierResolve(item: ASTIdentifier, target?: string): Operation;
5
+ export declare function createResolve(item: ASTBase, target?: string): Resolve;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createResolve = exports.createIdentifierResolve = void 0;
4
+ const greyscript_core_1 = require("greyscript-core");
5
+ const reference_globals_1 = require("../operations/reference-globals");
6
+ const reference_locals_1 = require("../operations/reference-locals");
7
+ const reference_outer_1 = require("../operations/reference-outer");
8
+ const reference_self_1 = require("../operations/reference-self");
9
+ const resolve_1 = require("../operations/resolve");
10
+ const resolve_globals_1 = require("../operations/resolve-globals");
11
+ const resolve_locals_1 = require("../operations/resolve-locals");
12
+ const resolve_outer_1 = require("../operations/resolve-outer");
13
+ const resolve_self_1 = require("../operations/resolve-self");
14
+ const lookup_path_1 = require("./lookup-path");
15
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
16
+ const optIdentifierResolveMap = {
17
+ self: reference_self_1.ReferenceSelf,
18
+ globals: reference_globals_1.ReferenceGlobals,
19
+ locals: reference_locals_1.ReferenceLocals,
20
+ outer: reference_outer_1.ReferenceOuter
21
+ };
22
+ function createIdentifierResolve(item, target) {
23
+ if (hasOwnProperty.call(optIdentifierResolveMap, item.name)) {
24
+ const OptResolve = optIdentifierResolveMap[item.name];
25
+ return new OptResolve(item, target);
26
+ }
27
+ return new resolve_1.Resolve(item, target);
28
+ }
29
+ exports.createIdentifierResolve = createIdentifierResolve;
30
+ const optResolveMap = {
31
+ self: resolve_self_1.ResolveSelf,
32
+ globals: resolve_globals_1.ResolveGlobals,
33
+ locals: resolve_locals_1.ResolveLocals,
34
+ outer: resolve_outer_1.ResolveOuter
35
+ };
36
+ function createResolve(item, target) {
37
+ if (item instanceof greyscript_core_1.ASTMemberExpression) {
38
+ const memberExpr = item;
39
+ const path = (0, lookup_path_1.lookupPath)(memberExpr);
40
+ if (path.length > 0 &&
41
+ path[0].base instanceof greyscript_core_1.ASTIdentifier &&
42
+ hasOwnProperty.call(optResolveMap, path[0].base.name)) {
43
+ const OptResolve = optResolveMap[path[0].base.name];
44
+ const right = path[0].identifier;
45
+ if (path.length === 1) {
46
+ return new OptResolve(right, target);
47
+ }
48
+ const newBase = path[1];
49
+ newBase.base = right;
50
+ return new OptResolve(memberExpr, target);
51
+ }
52
+ }
53
+ return new resolve_1.Resolve(item, target);
54
+ }
55
+ exports.createResolve = createResolve;
@@ -0,0 +1,3 @@
1
+ import { CustomValue } from '../types/base';
2
+ import { CustomMap } from '../types/map';
3
+ export declare function getSuper(item: CustomValue): CustomMap;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSuper = void 0;
4
+ const map_1 = require("../types/map");
5
+ function getSuper(item) {
6
+ var _a;
7
+ if (item instanceof map_1.CustomMap) {
8
+ return (_a = item.getIsa()) !== null && _a !== void 0 ? _a : new map_1.CustomMap(item.getIntrinsics());
9
+ }
10
+ return null;
11
+ }
12
+ exports.getSuper = getSuper;
@@ -0,0 +1,2 @@
1
+ import { ASTMemberExpression } from 'greyscript-core';
2
+ export declare function lookupPath(item: ASTMemberExpression): ASTMemberExpression[];
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lookupPath = void 0;
4
+ const greyscript_core_1 = require("greyscript-core");
5
+ function lookupPath(item) {
6
+ const path = [item];
7
+ let current = item.base;
8
+ while (current instanceof greyscript_core_1.ASTMemberExpression) {
9
+ path.unshift(current);
10
+ current = current.base;
11
+ }
12
+ return path;
13
+ }
14
+ exports.lookupPath = lookupPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.2.20",
3
+ "version": "2.3.1",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",