greybel-interpreter 1.8.5 → 1.8.6

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.
@@ -5,10 +5,14 @@ import { CustomString } from '../types/string';
5
5
  import { CPSVisit, Operation } from './operation';
6
6
  export declare const SELF_PROPERTY: CustomString;
7
7
  export declare const SUPER_PROPERTY: CustomString;
8
+ export interface FunctionOperationArgument {
9
+ name: string;
10
+ op: Operation;
11
+ }
8
12
  export declare class FunctionOperation extends Operation {
9
13
  readonly item: ASTFunctionStatement;
10
14
  block: Operation;
11
- args: Map<string, Operation>;
15
+ args: FunctionOperationArgument[];
12
16
  constructor(item: ASTFunctionStatement, target?: string);
13
17
  build(visit: CPSVisit): Promise<Operation>;
14
18
  handle(ctx: OperationContext): Promise<CustomValue>;
@@ -18,8 +18,8 @@ const string_1 = require("../types/string");
18
18
  const block_1 = require("./block");
19
19
  const operation_1 = require("./operation");
20
20
  const reference_1 = require("./reference");
21
- exports.SELF_PROPERTY = new string_1.CustomString('self');
22
- exports.SUPER_PROPERTY = new string_1.CustomString('super');
21
+ exports.SELF_PROPERTY = new string_1.CustomString(function_1.SELF_NAMESPACE);
22
+ exports.SUPER_PROPERTY = new string_1.CustomString(function_1.SUPER_NAMESPACE);
23
23
  class FunctionOperation extends operation_1.Operation {
24
24
  constructor(item, target) {
25
25
  super(null, target);
@@ -29,18 +29,24 @@ class FunctionOperation extends operation_1.Operation {
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
30
  const stack = yield Promise.all(this.item.body.map((child) => visit(child)));
31
31
  this.block = new block_1.Block(this.item, stack);
32
- this.args = new Map();
32
+ this.args = [];
33
33
  const defers = this.item.parameters.map((child) => __awaiter(this, void 0, void 0, function* () {
34
34
  switch (child.type) {
35
35
  case greyscript_core_1.ASTType.AssignmentStatement: {
36
36
  const assignStatement = child;
37
37
  const assignKey = assignStatement.variable;
38
- this.args.set(assignKey.name, yield visit(assignStatement.init));
38
+ this.args.push({
39
+ name: assignKey.name,
40
+ op: yield visit(assignStatement.init)
41
+ });
39
42
  break;
40
43
  }
41
44
  case greyscript_core_1.ASTType.Identifier: {
42
45
  const identifierKey = child;
43
- this.args.set(identifierKey.name, new reference_1.Reference(default_1.DefaultType.Void));
46
+ this.args.push({
47
+ name: identifierKey.name,
48
+ op: new reference_1.Reference(default_1.DefaultType.Void)
49
+ });
44
50
  break;
45
51
  }
46
52
  default:
@@ -69,8 +75,8 @@ class FunctionOperation extends operation_1.Operation {
69
75
  yield this.block.handle(fnCtx);
70
76
  return functionState.value;
71
77
  }));
72
- for (const [key, value] of this.args) {
73
- func.addArgument(key, value);
78
+ for (const item of this.args) {
79
+ func.addArgument(item.name, item.op);
74
80
  }
75
81
  return Promise.resolve(func);
76
82
  }
@@ -79,7 +79,7 @@ class SegmentContainer {
79
79
  isSuper() {
80
80
  return (this.path.length === 2 &&
81
81
  this.path[0] instanceof IdentifierSegment &&
82
- this.path[0].value === 'super');
82
+ this.path[0].value === function_1.SUPER_NAMESPACE);
83
83
  }
84
84
  getLast() {
85
85
  return this.path[this.path.length - 1];
@@ -177,7 +177,7 @@ class Resolve extends operation_1.Operation {
177
177
  }
178
178
  if (handle instanceof function_1.CustomFunction) {
179
179
  if (index === 1 &&
180
- traversedPath.toString() === 'super' &&
180
+ traversedPath.toString() === function_1.SUPER_NAMESPACE &&
181
181
  ctx.functionState.context &&
182
182
  previous instanceof map_1.CustomMap) {
183
183
  handle = yield handle.run(ctx.functionState.context, [], ctx, previous.isa);
@@ -5,6 +5,9 @@ import { CustomMap } from './map';
5
5
  export interface Callback {
6
6
  (ctx: OperationContext, self: CustomValue, args: Map<string, CustomValue>, next?: CustomValue): Promise<NonNullable<CustomValue>>;
7
7
  }
8
+ export declare const DEFAULT_FUNCTION_NAME = "anonymous";
9
+ export declare const SELF_NAMESPACE = "self";
10
+ export declare const SUPER_NAMESPACE = "super";
8
11
  export declare class Argument {
9
12
  readonly name: string;
10
13
  readonly defaultValue: Operation;
@@ -15,13 +18,11 @@ export declare class CustomFunction extends CustomValue {
15
18
  readonly scope?: OperationContext;
16
19
  readonly name: string;
17
20
  readonly value: Callback;
18
- private injectSelf;
19
21
  readonly argumentDefs: Array<Argument>;
20
22
  static createExternalAnonymous(callback: Callback): CustomFunction;
21
23
  static createExternal(name: string, callback: Callback): CustomFunction;
22
24
  static createExternalWithSelf(name: string, callback: Callback): CustomFunction;
23
- constructor(scope: OperationContext, name: string, callback: Callback, injectSelf?: boolean);
24
- setInjectSelf(injectSelf: boolean): CustomFunction;
25
+ constructor(scope: OperationContext, name: string, callback: Callback);
25
26
  addArgument(name: string, defaultValue?: Operation | CustomValue): CustomFunction;
26
27
  fork(): CustomValue;
27
28
  getCustomType(): string;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CustomFunction = exports.Argument = void 0;
12
+ exports.CustomFunction = exports.Argument = exports.SUPER_NAMESPACE = exports.SELF_NAMESPACE = exports.DEFAULT_FUNCTION_NAME = void 0;
13
13
  const context_1 = require("../context");
14
14
  const operation_1 = require("../operations/operation");
15
15
  const reference_1 = require("../operations/reference");
@@ -17,6 +17,9 @@ const base_1 = require("./base");
17
17
  const default_1 = require("./default");
18
18
  const map_1 = require("./map");
19
19
  const nil_1 = require("./nil");
20
+ exports.DEFAULT_FUNCTION_NAME = 'anonymous';
21
+ exports.SELF_NAMESPACE = 'self';
22
+ exports.SUPER_NAMESPACE = 'super';
20
23
  class Argument {
21
24
  static createWithCustomValue(name, defaultValue) {
22
25
  return new Argument(name, new reference_1.Reference(defaultValue));
@@ -37,26 +40,21 @@ class Argument {
37
40
  exports.Argument = Argument;
38
41
  class CustomFunction extends base_1.CustomValue {
39
42
  static createExternalAnonymous(callback) {
40
- return new CustomFunction(null, 'anonymous', callback);
43
+ return new CustomFunction(null, exports.DEFAULT_FUNCTION_NAME, callback);
41
44
  }
42
45
  static createExternal(name, callback) {
43
46
  return new CustomFunction(null, name, callback);
44
47
  }
45
48
  static createExternalWithSelf(name, callback) {
46
- return new CustomFunction(null, name, callback, true).addArgument('self');
49
+ return new CustomFunction(null, name, callback).addArgument(exports.SELF_NAMESPACE);
47
50
  }
48
- constructor(scope, name, callback, injectSelf = false) {
51
+ constructor(scope, name, callback) {
49
52
  super();
50
53
  this.scope = scope;
51
54
  this.name = name;
52
55
  this.value = callback;
53
- this.injectSelf = injectSelf;
54
56
  this.argumentDefs = [];
55
57
  }
56
- setInjectSelf(injectSelf) {
57
- this.injectSelf = injectSelf;
58
- return this;
59
- }
60
58
  addArgument(name, defaultValue = default_1.DefaultType.Void) {
61
59
  this.argumentDefs.push(new Argument(name, defaultValue));
62
60
  return this;
@@ -94,12 +92,16 @@ class CustomFunction extends base_1.CustomValue {
94
92
  state: context_1.ContextState.Default
95
93
  });
96
94
  const argMap = new Map();
97
- if (this.injectSelf && !(self instanceof nil_1.CustomNil)) {
98
- args.unshift(self);
99
- }
95
+ const isSelfNull = self instanceof nil_1.CustomNil;
96
+ let argIndex = 0;
100
97
  for (let index = 0; index < this.argumentDefs.length; index++) {
101
98
  const item = this.argumentDefs[index];
102
- argMap.set(item.name, args[index] || (yield item.defaultValue.handle(fnCtx)));
99
+ if (!isSelfNull && item.name === exports.SELF_NAMESPACE)
100
+ continue;
101
+ argMap.set(item.name, args[argIndex++] || (yield item.defaultValue.handle(fnCtx)));
102
+ }
103
+ if (!isSelfNull) {
104
+ argMap.set(exports.SELF_NAMESPACE, self);
103
105
  }
104
106
  const isa = next || (self instanceof map_1.CustomMap ? self.isa : null);
105
107
  return this.value(fnCtx || callContext, self, argMap, isa);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "1.8.5",
3
+ "version": "1.8.6",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",