greybel-interpreter 3.4.3 → 3.5.0

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/index.d.ts CHANGED
@@ -44,3 +44,4 @@ export { PrepareError, RuntimeError } from './utils/error';
44
44
  export { ObjectValue } from './utils/object-value';
45
45
  export { Path } from './utils/path';
46
46
  export { deepEqual } from './utils/deep-equal';
47
+ export { deepHash } from './utils/deep-hash';
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.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;
4
+ exports.deepHash = 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; } });
@@ -127,3 +127,5 @@ var path_1 = require("./utils/path");
127
127
  Object.defineProperty(exports, "Path", { enumerable: true, get: function () { return path_1.Path; } });
128
128
  var deep_equal_1 = require("./utils/deep-equal");
129
129
  Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return deep_equal_1.deepEqual; } });
130
+ var deep_hash_1 = require("./utils/deep-hash");
131
+ Object.defineProperty(exports, "deepHash", { enumerable: true, get: function () { return deep_hash_1.deepHash; } });
@@ -1,5 +1,6 @@
1
1
  import { ContextTypeIntrinsics } from '../context/types';
2
2
  export declare abstract class CustomValue {
3
+ abstract readonly id: string;
3
4
  abstract value: any;
4
5
  abstract getCustomType(): string;
5
6
  abstract toNumber(): number;
@@ -22,6 +22,7 @@ export declare class CustomFunction extends CustomValue {
22
22
  readonly value: Callback;
23
23
  readonly argumentDefs: Array<Argument>;
24
24
  readonly assignOuter: boolean;
25
+ readonly id: string;
25
26
  private _nextContext;
26
27
  static createExternalAnonymous(callback: Callback): CustomFunction;
27
28
  static createExternal(name: string, callback: Callback): CustomFunction;
@@ -16,6 +16,7 @@ const operation_1 = require("../operations/operation");
16
16
  const reference_1 = require("../operations/reference");
17
17
  const hash_1 = require("../utils/hash");
18
18
  const object_value_1 = require("../utils/object-value");
19
+ const uuid_1 = require("../utils/uuid");
19
20
  const base_1 = require("./base");
20
21
  const default_1 = require("./default");
21
22
  const nil_1 = require("./nil");
@@ -53,6 +54,7 @@ class CustomFunction extends base_1.CustomValue {
53
54
  }
54
55
  constructor(scope, name, callback, argumentDefs = [], assignOuter = false) {
55
56
  super();
57
+ this.id = (0, uuid_1.uuid)();
56
58
  this.scope = scope;
57
59
  this.name = name;
58
60
  this.value = callback;
@@ -12,6 +12,7 @@ export declare class CustomListIterator implements Iterator<CustomValue> {
12
12
  export declare class CustomList extends CustomObject {
13
13
  static readonly intrinsics: ObjectValue;
14
14
  static getItemIndex(item: CustomList, index: number): number;
15
+ readonly id: string;
15
16
  readonly value: Array<CustomValue>;
16
17
  constructor(value?: Array<CustomValue>);
17
18
  getCustomType(): string;
@@ -21,6 +22,7 @@ export declare class CustomList extends CustomObject {
21
22
  toNumber(): number;
22
23
  toInt(): number;
23
24
  toTruthy(): boolean;
25
+ getSize(): number;
24
26
  instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
25
27
  slice(a: CustomValue, b: CustomValue): CustomList;
26
28
  extend(list: CustomList | Array<CustomValue>): CustomList;
@@ -4,6 +4,7 @@ exports.CustomList = exports.CustomListIterator = void 0;
4
4
  const hash_1 = require("../utils/hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const path_1 = require("../utils/path");
7
+ const uuid_1 = require("../utils/uuid");
7
8
  const base_1 = require("./base");
8
9
  const number_1 = require("./number");
9
10
  const with_intrinsics_1 = require("./with-intrinsics");
@@ -39,6 +40,7 @@ class CustomList extends with_intrinsics_1.CustomObject {
39
40
  }
40
41
  constructor(value = []) {
41
42
  super();
43
+ this.id = (0, uuid_1.uuid)();
42
44
  this.value = [...value];
43
45
  }
44
46
  getCustomType() {
@@ -62,6 +64,9 @@ class CustomList extends with_intrinsics_1.CustomObject {
62
64
  toTruthy() {
63
65
  return this.value.length > 0;
64
66
  }
67
+ getSize() {
68
+ return this.value.length;
69
+ }
65
70
  instanceOf(v, typeIntrinsics) {
66
71
  var _a;
67
72
  return v.value === ((_a = typeIntrinsics.list) !== null && _a !== void 0 ? _a : CustomList.intrinsics);
@@ -168,7 +173,7 @@ class CustomList extends with_intrinsics_1.CustomObject {
168
173
  }
169
174
  hash(recursionDepth = 0) {
170
175
  let result = (0, hash_1.getHashCode)(this.value.length);
171
- if (recursionDepth > 16)
176
+ if (recursionDepth > 4)
172
177
  return result;
173
178
  this.value.forEach((value) => {
174
179
  result = (0, hash_1.rotateBits)(result) ^ value.hash(recursionDepth + 1);
@@ -4,7 +4,6 @@ import { Path } from '../utils/path';
4
4
  import { CustomValue } from './base';
5
5
  import { CustomString } from './string';
6
6
  import { CustomObject, CustomValueWithIntrinsicsResult } from './with-intrinsics';
7
- export declare const CLASS_ID_PROPERTY: CustomString;
8
7
  export declare const ISA_PROPERTY: CustomString;
9
8
  export declare const CUSTOM_MAP_MAX_DEPTH = 2;
10
9
  export declare const CUSTOM_MAP_MAX_DEPTH_VALUE = "{...}";
@@ -16,6 +15,7 @@ export declare class CustomMapIterator implements Iterator<CustomValue> {
16
15
  }
17
16
  export declare class CustomMap extends CustomObject {
18
17
  static readonly intrinsics: ObjectValue;
18
+ readonly id: string;
19
19
  value: ObjectValue;
20
20
  private isInstance;
21
21
  static createWithInitialValue(value: ObjectValue): CustomMap;
@@ -27,6 +27,7 @@ export declare class CustomMap extends CustomObject {
27
27
  toNumber(): number;
28
28
  toInt(): number;
29
29
  toTruthy(): boolean;
30
+ getSize(): number;
30
31
  instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
31
32
  [Symbol.iterator](): CustomMapIterator;
32
33
  extend(map: CustomMap | ObjectValue): CustomMap;
package/dist/types/map.js CHANGED
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomMap = exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = exports.ISA_PROPERTY = exports.CLASS_ID_PROPERTY = void 0;
3
+ exports.CustomMap = exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = exports.ISA_PROPERTY = void 0;
4
4
  const hash_1 = require("../utils/hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const path_1 = require("../utils/path");
7
+ const uuid_1 = require("../utils/uuid");
7
8
  const base_1 = require("./base");
8
9
  const nil_1 = require("./nil");
9
10
  const string_1 = require("./string");
10
11
  const with_intrinsics_1 = require("./with-intrinsics");
11
- exports.CLASS_ID_PROPERTY = new string_1.CustomString('classID');
12
12
  exports.ISA_PROPERTY = new string_1.CustomString('__isa');
13
13
  exports.CUSTOM_MAP_MAX_DEPTH = 2;
14
14
  exports.CUSTOM_MAP_MAX_DEPTH_VALUE = '{...}';
@@ -47,12 +47,10 @@ class CustomMap extends with_intrinsics_1.CustomObject {
47
47
  constructor(value) {
48
48
  super();
49
49
  this.isInstance = false;
50
+ this.id = (0, uuid_1.uuid)();
50
51
  this.value = new object_value_1.ObjectValue(value);
51
52
  }
52
53
  getCustomType() {
53
- if (this.value.has(exports.CLASS_ID_PROPERTY)) {
54
- return this.value.get(exports.CLASS_ID_PROPERTY).toString();
55
- }
56
54
  return 'map';
57
55
  }
58
56
  toJSON(depth = 0) {
@@ -82,6 +80,9 @@ class CustomMap extends with_intrinsics_1.CustomObject {
82
80
  toTruthy() {
83
81
  return this.value.size > 0;
84
82
  }
83
+ getSize() {
84
+ return this.value.size;
85
+ }
85
86
  instanceOf(v, typeIntrinsics) {
86
87
  var _a;
87
88
  if (v instanceof CustomMap) {
@@ -228,7 +229,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
228
229
  }
229
230
  hash(recursionDepth = 0) {
230
231
  let result = (0, hash_1.getHashCode)(this.value.size);
231
- if (recursionDepth > 16)
232
+ if (recursionDepth > 4)
232
233
  return result;
233
234
  this.value.forEach((value, key) => {
234
235
  result ^= key.hash(recursionDepth + 1);
@@ -1,5 +1,6 @@
1
1
  import { CustomValue } from './base';
2
2
  export declare class CustomNil extends CustomValue {
3
+ readonly id: string;
3
4
  value: null;
4
5
  getCustomType(): string;
5
6
  toJSON(): string;
package/dist/types/nil.js CHANGED
@@ -5,6 +5,7 @@ const base_1 = require("./base");
5
5
  class CustomNil extends base_1.CustomValue {
6
6
  constructor() {
7
7
  super(...arguments);
8
+ this.id = 'null';
8
9
  this.value = null;
9
10
  }
10
11
  getCustomType() {
@@ -9,6 +9,7 @@ export declare class CustomNumberIterator implements Iterator<CustomValue> {
9
9
  }
10
10
  export declare class CustomNumber extends CustomValueWithIntrinsics {
11
11
  static readonly intrinsics: ObjectValue;
12
+ readonly id: string;
12
13
  readonly value: number;
13
14
  constructor(value: number);
14
15
  getCustomType(): string;
@@ -21,6 +21,7 @@ exports.CustomNumberIterator = CustomNumberIterator;
21
21
  class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
22
22
  constructor(value) {
23
23
  super();
24
+ this.id = `n:${value.toString()}`;
24
25
  this.value = value;
25
26
  }
26
27
  getCustomType() {
@@ -12,6 +12,7 @@ export declare class CustomStringIterator implements Iterator<CustomValue> {
12
12
  export declare class CustomString extends CustomValueWithIntrinsics {
13
13
  static readonly intrinsics: ObjectValue;
14
14
  static getCharIndex(item: CustomString, index: number): number;
15
+ readonly id: string;
15
16
  readonly value: string;
16
17
  constructor(value: string);
17
18
  getCustomType(): string;
@@ -39,6 +39,7 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
39
39
  }
40
40
  constructor(value) {
41
41
  super();
42
+ this.id = `s:${value}`;
42
43
  this.value = value;
43
44
  }
44
45
  getCustomType() {
@@ -22,4 +22,5 @@ export declare abstract class CustomValueWithIntrinsics extends CustomValue {
22
22
  getIntrinsics(): ObjectValue;
23
23
  }
24
24
  export declare abstract class CustomObject extends CustomValueWithIntrinsics {
25
+ abstract getSize(): number;
25
26
  }
@@ -1 +1,2 @@
1
- export declare function deepEqual(a: any, b: any, maxDepth?: number): boolean;
1
+ import { CustomValue } from '../types/base';
2
+ export declare function deepEqual(a: CustomValue, b: CustomValue): boolean;
@@ -1,43 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deepEqual = void 0;
4
- const list_1 = require("../types/list");
5
- const map_1 = require("../types/map");
6
4
  const with_intrinsics_1 = require("../types/with-intrinsics");
7
- function equalInner(a, b, maxDepth, depth = 0) {
8
- if (maxDepth <= depth)
9
- return a === b;
10
- if (a.value === b.value)
11
- return true;
12
- if (a && b && a instanceof with_intrinsics_1.CustomObject && b instanceof with_intrinsics_1.CustomObject) {
13
- if (a.constructor !== b.constructor)
14
- return false;
15
- if (a instanceof list_1.CustomList) {
16
- const length = a.value.length;
17
- if (length !== b.value.length)
5
+ function deepEqual(a, b) {
6
+ const stack = [];
7
+ const visited = new Set();
8
+ stack.push([a, b]);
9
+ while (stack.length > 0) {
10
+ const [a, b] = stack.pop();
11
+ visited.add(`${a.id}:${b.id}`);
12
+ if (a instanceof with_intrinsics_1.CustomObject) {
13
+ if (!(b instanceof with_intrinsics_1.CustomObject))
18
14
  return false;
19
- for (let i = length; i-- !== 0;)
20
- if (!equalInner(a.value[i], b.value[i], maxDepth, depth + 1))
15
+ if (Array.isArray(a.value)) {
16
+ if (!Array.isArray(b.value))
21
17
  return false;
22
- return true;
23
- }
24
- if (a instanceof map_1.CustomMap) {
25
- if (a.value.size !== b.value.size)
26
- return false;
27
- for (const i of a.value.keys()) {
28
- if (!b.value.has(i) ||
29
- !equalInner(a.value.get(i), b.value.get(i), maxDepth, depth + 1)) {
18
+ if (a.value.length !== b.value.length)
30
19
  return false;
20
+ for (let index = 0; index < a.value.length; index++) {
21
+ const valueA = a.value[index];
22
+ const valueB = b.value[index];
23
+ if (!visited.has(`${valueA.id}:${valueB.id}`)) {
24
+ stack.push([valueA, valueB]);
25
+ }
31
26
  }
32
27
  }
33
- return true;
28
+ else {
29
+ if (a.value.size !== b.value.size)
30
+ return false;
31
+ for (const key of a.value.keys()) {
32
+ if (!b.value.has(key))
33
+ return false;
34
+ const valueA = a.value.get(key);
35
+ const valueB = b.value.get(key);
36
+ if (!visited.has(`${valueA.id}:${valueB.id}`)) {
37
+ stack.push([valueA, valueB]);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ else if (a.value !== b.value) {
43
+ return false;
34
44
  }
35
- return false;
36
45
  }
37
- // true if both NaN, false otherwise
38
- return Number.isNaN(a.value) && Number.isNaN(b.value);
39
- }
40
- function deepEqual(a, b, maxDepth = 10) {
41
- return equalInner(a, b, maxDepth);
46
+ return true;
42
47
  }
43
48
  exports.deepEqual = deepEqual;
@@ -0,0 +1,2 @@
1
+ import { CustomValue } from '../types/base';
2
+ export declare function deepHash(value: CustomValue): number;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepHash = void 0;
4
+ const with_intrinsics_1 = require("../types/with-intrinsics");
5
+ const hash_1 = require("./hash");
6
+ function deepHash(value) {
7
+ let result = 0;
8
+ const stack = [];
9
+ const visited = new Set();
10
+ stack.push([value]);
11
+ while (stack.length > 0) {
12
+ const items = stack.pop();
13
+ for (const item of items) {
14
+ visited.add(item.id);
15
+ if (item instanceof with_intrinsics_1.CustomObject) {
16
+ if (Array.isArray(item.value)) {
17
+ result ^= (0, hash_1.getHashCode)(item.value.length);
18
+ for (let index = item.value.length - 1; index >= 0; index--) {
19
+ const child = item.value[index];
20
+ if (!(child instanceof with_intrinsics_1.CustomObject) || !visited.has(child.id))
21
+ stack.push([child]);
22
+ }
23
+ }
24
+ else {
25
+ result ^= (0, hash_1.getHashCode)(item.value.length);
26
+ const chunk = [];
27
+ item.value.forEach((child, key) => {
28
+ if (!(key instanceof with_intrinsics_1.CustomObject) || !visited.has(key.id))
29
+ chunk.push(key);
30
+ if (!(child instanceof with_intrinsics_1.CustomObject) || !visited.has(child.id))
31
+ chunk.push(child);
32
+ });
33
+ stack.push(chunk);
34
+ }
35
+ }
36
+ else {
37
+ result ^= item.hash();
38
+ }
39
+ }
40
+ result ^= (0, hash_1.rotateBits)(result);
41
+ }
42
+ return result;
43
+ }
44
+ exports.deepHash = deepHash;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ObjectValue = void 0;
4
4
  const nil_1 = require("../types/nil");
5
+ const deep_hash_1 = require("./deep-hash");
5
6
  class ObjectValue {
6
7
  constructor(entries) {
7
8
  if (entries == null) {
@@ -21,22 +22,22 @@ class ObjectValue {
21
22
  }
22
23
  }
23
24
  get(mapKey) {
24
- const hash = mapKey.hash();
25
+ const hash = (0, deep_hash_1.deepHash)(mapKey);
25
26
  if (!this.data.has(hash))
26
27
  return nil_1.Void;
27
28
  return this.data.get(hash)[1];
28
29
  }
29
30
  has(mapKey) {
30
- const hash = mapKey.hash();
31
+ const hash = (0, deep_hash_1.deepHash)(mapKey);
31
32
  return this.data.has(hash);
32
33
  }
33
34
  set(mapKey, mapValue) {
34
- const hash = mapKey.hash();
35
+ const hash = (0, deep_hash_1.deepHash)(mapKey);
35
36
  this.data.set(hash, [mapKey, mapValue]);
36
37
  return this;
37
38
  }
38
39
  delete(mapKey) {
39
- const hash = mapKey.hash();
40
+ const hash = (0, deep_hash_1.deepHash)(mapKey);
40
41
  return this.data.delete(hash);
41
42
  }
42
43
  values() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "3.4.3",
3
+ "version": "3.5.0",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",