greybel-interpreter 1.5.3 → 1.5.4

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/cps.js CHANGED
@@ -121,6 +121,7 @@ const visit = (context, currentTarget, item) => __awaiter(void 0, void 0, void 0
121
121
  return new literal_1.default(item, currentTarget).build(defaultVisit);
122
122
  case greyscript_core_1.ASTType.EmptyExpression:
123
123
  return new noop_1.default(item, currentTarget).build(defaultVisit);
124
+ case greyscript_core_1.ASTType.IsaExpression:
124
125
  case greyscript_core_1.ASTType.BinaryExpression:
125
126
  case greyscript_core_1.ASTType.LogicalExpression:
126
127
  return new evaluate_1.default(item, currentTarget).build(defaultVisit);
@@ -316,6 +316,11 @@ class Evaluate extends operation_1.default {
316
316
  if (op instanceof Evaluate) {
317
317
  const expr = op;
318
318
  switch (expr.type) {
319
+ case greyscript_core_1.ASTType.IsaExpression: {
320
+ const left = yield this.resolve(ctx, expr.left);
321
+ const right = yield this.resolve(ctx, expr.right);
322
+ return new boolean_1.default(left.instanceOf(right));
323
+ }
319
324
  case greyscript_core_1.ASTType.BinaryExpression:
320
325
  return this.resolveBinaryExpression(ctx, expr);
321
326
  case greyscript_core_1.ASTType.LogicalExpression:
@@ -4,6 +4,7 @@ import CustomValue from '../types/base';
4
4
  import CustomString from '../types/string';
5
5
  import Operation, { CPSVisit } from './operation';
6
6
  export declare const SELF_PROPERTY: CustomString;
7
+ export declare const SUPER_PROPERTY: CustomString;
7
8
  export default class FunctionOperation extends Operation {
8
9
  readonly item: ASTFunctionStatement;
9
10
  block: Operation;
@@ -12,16 +12,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SELF_PROPERTY = void 0;
15
+ exports.SUPER_PROPERTY = exports.SELF_PROPERTY = void 0;
16
16
  const greyscript_core_1 = require("greyscript-core");
17
17
  const context_1 = require("../context");
18
18
  const default_1 = __importDefault(require("../types/default"));
19
19
  const function_1 = __importDefault(require("../types/function"));
20
+ const map_1 = __importDefault(require("../types/map"));
20
21
  const string_1 = __importDefault(require("../types/string"));
21
22
  const block_1 = __importDefault(require("./block"));
22
23
  const operation_1 = __importDefault(require("./operation"));
23
24
  const reference_1 = __importDefault(require("./reference"));
24
25
  exports.SELF_PROPERTY = new string_1.default('self');
26
+ exports.SUPER_PROPERTY = new string_1.default('super');
25
27
  class FunctionOperation extends operation_1.default {
26
28
  constructor(item, target) {
27
29
  super(null, target);
@@ -57,6 +59,9 @@ class FunctionOperation extends operation_1.default {
57
59
  const func = new function_1.default(ctx, 'anonymous', (fnCtx, self, args) => __awaiter(this, void 0, void 0, function* () {
58
60
  fnCtx.functionState = new context_1.FunctionState();
59
61
  fnCtx.set(exports.SELF_PROPERTY, self);
62
+ if (self instanceof map_1.default && self.isa !== null) {
63
+ fnCtx.set(exports.SUPER_PROPERTY, self.isa);
64
+ }
60
65
  fnCtx.set(new string_1.default('locals'), fnCtx.locals.scope);
61
66
  fnCtx.set(new string_1.default('outer'), fnCtx.previous.locals.scope);
62
67
  for (const [key, value] of args) {
@@ -6,4 +6,5 @@ export default abstract class CustomValue {
6
6
  abstract toString(): string;
7
7
  abstract toTruthy(): boolean;
8
8
  abstract fork(): CustomValue;
9
+ abstract instanceOf(value: CustomValue): boolean;
9
10
  }
@@ -8,6 +8,7 @@ export default class CustomBoolean extends CustomValue {
8
8
  toNumber(): number;
9
9
  toInt(): number;
10
10
  toTruthy(): boolean;
11
+ instanceOf(v: CustomValue): boolean;
11
12
  }
12
13
  export declare const DefaultTrue: CustomBoolean;
13
14
  export declare const DefaultFalse: CustomBoolean;
@@ -8,7 +8,7 @@ const base_1 = __importDefault(require("./base"));
8
8
  class CustomBoolean extends base_1.default {
9
9
  constructor(value) {
10
10
  super();
11
- this.value = value;
11
+ this.value = !!value;
12
12
  }
13
13
  getCustomType() {
14
14
  return 'boolean';
@@ -28,6 +28,9 @@ class CustomBoolean extends base_1.default {
28
28
  toTruthy() {
29
29
  return this.value;
30
30
  }
31
+ instanceOf(v) {
32
+ return v instanceof CustomBoolean;
33
+ }
31
34
  }
32
35
  exports.default = CustomBoolean;
33
36
  exports.DefaultTrue = new CustomBoolean(true);
@@ -28,5 +28,6 @@ export default class CustomFunction extends CustomValue {
28
28
  toInt(): number;
29
29
  toString(): string;
30
30
  toTruthy(): boolean;
31
+ instanceOf(v: CustomValue): boolean;
31
32
  run(self: CustomValue, args: Array<CustomValue>, callContext: OperationContext): Promise<CustomValue>;
32
33
  }
@@ -82,6 +82,9 @@ class CustomFunction extends base_1.default {
82
82
  toTruthy() {
83
83
  return true;
84
84
  }
85
+ instanceOf(v) {
86
+ return v instanceof CustomFunction;
87
+ }
85
88
  run(self, args, callContext) {
86
89
  var _a;
87
90
  return __awaiter(this, void 0, void 0, function* () {
@@ -16,6 +16,7 @@ export default class CustomInterface extends CustomObject {
16
16
  toNumber(): number;
17
17
  toInt(): number;
18
18
  toTruthy(): boolean;
19
+ instanceOf(v: CustomValue): boolean;
19
20
  [Symbol.iterator](): CustomInterfaceIterator;
20
21
  has(path: Path<CustomValue> | CustomValue): boolean;
21
22
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
@@ -42,6 +42,9 @@ class CustomInterface extends with_intrinsics_1.CustomObject {
42
42
  toTruthy() {
43
43
  return true;
44
44
  }
45
+ instanceOf(v) {
46
+ return (v instanceof CustomInterface && v.getCustomType() === this.getCustomType());
47
+ }
45
48
  [Symbol.iterator]() {
46
49
  return new CustomInterfaceIterator();
47
50
  }
@@ -19,6 +19,7 @@ export default class CustomList extends CustomObject {
19
19
  toNumber(): number;
20
20
  toInt(): number;
21
21
  toTruthy(): boolean;
22
+ instanceOf(v: CustomValue): boolean;
22
23
  slice(a: CustomValue, b: CustomValue): CustomList;
23
24
  extend(list: CustomList | Array<CustomValue>): CustomList;
24
25
  [Symbol.iterator](): CustomListIterator;
@@ -61,6 +61,9 @@ class CustomList extends with_intrinsics_1.CustomObject {
61
61
  toTruthy() {
62
62
  return this.value.length > 0;
63
63
  }
64
+ instanceOf(v) {
65
+ return v.value === CustomList.intrinsics;
66
+ }
64
67
  slice(a, b) {
65
68
  return new CustomList(this.value.slice(a.toNumber(), b.toNumber()));
66
69
  }
@@ -5,6 +5,8 @@ import CustomString from './string';
5
5
  import { CustomObject } from './with-intrinsics';
6
6
  export declare const CLASS_ID_PROPERTY: CustomString;
7
7
  export declare const ISA_PROPERTY: CustomString;
8
+ export declare const CUSTOM_MAP_MAX_DEPTH = 2;
9
+ export declare const CUSTOM_MAP_MAX_DEPTH_VALUE = "{...}";
8
10
  export declare class CustomMapIterator implements Iterator<CustomValue> {
9
11
  value: ObjectValue;
10
12
  index: number;
@@ -14,16 +16,17 @@ export declare class CustomMapIterator implements Iterator<CustomValue> {
14
16
  export default class CustomMap extends CustomObject {
15
17
  static readonly intrinsics: ObjectValue;
16
18
  value: ObjectValue;
17
- readonly isa: ObjectValue;
19
+ readonly isa: CustomMap | null;
18
20
  private isInstance;
19
21
  static createWithInitialValue(value: ObjectValue): CustomMap;
20
- constructor(value?: ObjectValue, isa?: ObjectValue);
22
+ constructor(value?: ObjectValue, isa?: CustomMap);
21
23
  getCustomType(): string;
22
- toString(): string;
24
+ toString(depth?: number): string;
23
25
  fork(): CustomMap;
24
26
  toNumber(): number;
25
27
  toInt(): number;
26
28
  toTruthy(): boolean;
29
+ instanceOf(v: CustomValue): boolean;
27
30
  [Symbol.iterator](): CustomMapIterator;
28
31
  extend(map: CustomMap | ObjectValue): CustomMap;
29
32
  has(path: Path<CustomValue> | CustomValue): boolean;
package/dist/types/map.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CustomMapIterator = exports.ISA_PROPERTY = exports.CLASS_ID_PROPERTY = void 0;
6
+ exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = exports.ISA_PROPERTY = exports.CLASS_ID_PROPERTY = void 0;
7
7
  const object_value_1 = __importDefault(require("../utils/object-value"));
8
8
  const path_1 = __importDefault(require("../utils/path"));
9
9
  const base_1 = __importDefault(require("./base"));
@@ -12,6 +12,8 @@ const string_1 = __importDefault(require("./string"));
12
12
  const with_intrinsics_1 = require("./with-intrinsics");
13
13
  exports.CLASS_ID_PROPERTY = new string_1.default('classID');
14
14
  exports.ISA_PROPERTY = new string_1.default('__isa');
15
+ exports.CUSTOM_MAP_MAX_DEPTH = 2;
16
+ exports.CUSTOM_MAP_MAX_DEPTH_VALUE = '{...}';
15
17
  class CustomMapIterator {
16
18
  constructor(value) {
17
19
  const me = this;
@@ -39,11 +41,11 @@ class CustomMapIterator {
39
41
  }
40
42
  exports.CustomMapIterator = CustomMapIterator;
41
43
  class CustomMap extends with_intrinsics_1.CustomObject {
42
- constructor(value, isa) {
44
+ constructor(value, isa = null) {
43
45
  super();
44
46
  this.isInstance = false;
45
47
  this.value = new object_value_1.default(value);
46
- this.isa = new object_value_1.default(isa);
48
+ this.isa = isa;
47
49
  }
48
50
  static createWithInitialValue(value) {
49
51
  const map = new CustomMap();
@@ -56,13 +58,19 @@ class CustomMap extends with_intrinsics_1.CustomObject {
56
58
  }
57
59
  return 'map';
58
60
  }
59
- toString() {
60
- const json = { [exports.ISA_PROPERTY.toString()]: {} };
61
- for (const [key, value] of this.isa.entries()) {
62
- json.__isa[key.toString()] = value.toString();
61
+ toString(depth = 0) {
62
+ const json = {};
63
+ if (exports.CUSTOM_MAP_MAX_DEPTH < depth) {
64
+ return exports.CUSTOM_MAP_MAX_DEPTH_VALUE;
65
+ }
66
+ if (this.isa) {
67
+ json[exports.ISA_PROPERTY.toString()] = this.isa.toString(depth + 1);
63
68
  }
64
69
  for (const [key, value] of this.value.entries()) {
65
- json[key.toString()] = value.toString();
70
+ json[key.toString()] =
71
+ value instanceof CustomMap
72
+ ? value.toString(depth + 1)
73
+ : value.toString();
66
74
  }
67
75
  return JSON.stringify(json);
68
76
  }
@@ -78,6 +86,18 @@ class CustomMap extends with_intrinsics_1.CustomObject {
78
86
  toTruthy() {
79
87
  return this.value.size > 0;
80
88
  }
89
+ instanceOf(v) {
90
+ if (v instanceof CustomMap) {
91
+ let current = this;
92
+ while ((current = current.isa)) {
93
+ if (current === v) {
94
+ return true;
95
+ }
96
+ }
97
+ return v.value === CustomMap.intrinsics;
98
+ }
99
+ return false;
100
+ }
81
101
  [Symbol.iterator]() {
82
102
  return new CustomMapIterator(this.value);
83
103
  }
@@ -103,14 +123,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
103
123
  }
104
124
  return traversalPath.count() === 0;
105
125
  }
106
- else if (this.isa.has(current)) {
107
- const sub = this.isa.get(current);
108
- if (traversalPath.count() > 0 &&
109
- sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
110
- return sub.has(traversalPath);
111
- }
112
- return traversalPath.count() === 0;
113
- }
126
+ return this.isa ? this.isa.has(current) : false;
114
127
  }
115
128
  return false;
116
129
  }
@@ -134,6 +147,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
134
147
  this.value.set(last, newValue);
135
148
  }
136
149
  get(path) {
150
+ var _a, _b;
137
151
  if (path instanceof base_1.default) {
138
152
  return this.get(new path_1.default([path]));
139
153
  }
@@ -151,33 +165,17 @@ class CustomMap extends with_intrinsics_1.CustomObject {
151
165
  return sub;
152
166
  }
153
167
  }
154
- else if (this.isa.has(current)) {
155
- const sub = this.isa.get(current);
156
- if (traversalPath.count() > 0) {
157
- if (sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
158
- return sub.get(traversalPath);
159
- }
160
- }
161
- else if (traversalPath.count() === 0) {
162
- return sub;
163
- }
168
+ else if ((_a = this.isa) === null || _a === void 0 ? void 0 : _a.has(current)) {
169
+ return this.isa.get(current);
164
170
  }
165
171
  else if (current.toString() === exports.ISA_PROPERTY.toString()) {
166
172
  if (path.count() === 1) {
167
- return new CustomMap(this.isa);
173
+ return this.isa;
168
174
  }
169
175
  else {
170
176
  const ahead = traversalPath.next();
171
- if (this.isa.has(ahead)) {
172
- const sub = this.isa.get(ahead);
173
- if (traversalPath.count() > 0) {
174
- if (sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
175
- return sub.get(traversalPath);
176
- }
177
- }
178
- else if (traversalPath.count() === 0) {
179
- return sub;
180
- }
177
+ if ((_b = this.isa) === null || _b === void 0 ? void 0 : _b.has(ahead)) {
178
+ return this.isa.get(ahead);
181
179
  }
182
180
  }
183
181
  }
@@ -188,10 +186,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
188
186
  throw new Error(`Unknown path in map ${path.toString()}.`);
189
187
  }
190
188
  createInstance() {
191
- const newInstance = new CustomMap(new object_value_1.default(), new object_value_1.default(this.isa));
192
- for (const [k, v] of this.value.entries()) {
193
- newInstance.isa.set(k, v);
194
- }
189
+ const newInstance = new CustomMap(new object_value_1.default(), this);
195
190
  newInstance.isInstance = true;
196
191
  return newInstance;
197
192
  }
@@ -7,5 +7,6 @@ export default class CustomNil extends CustomValue {
7
7
  toNumber(): number;
8
8
  toInt(): number;
9
9
  toTruthy(): boolean;
10
+ instanceOf(v: CustomValue): boolean;
10
11
  }
11
12
  export declare const Void: CustomNil;
package/dist/types/nil.js CHANGED
@@ -28,6 +28,9 @@ class CustomNil extends base_1.default {
28
28
  toTruthy() {
29
29
  return false;
30
30
  }
31
+ instanceOf(v) {
32
+ return v instanceof CustomNil;
33
+ }
31
34
  }
32
35
  exports.default = CustomNil;
33
36
  exports.Void = new CustomNil();
@@ -15,6 +15,7 @@ export default class CustomNumber extends CustomValueWithIntrinsics {
15
15
  toInt(): number;
16
16
  toNumber(): number;
17
17
  toTruthy(): boolean;
18
+ instanceOf(v: CustomValue): boolean;
18
19
  [Symbol.iterator](): CustomNumberIterator;
19
20
  has(_path: Path<CustomValue> | CustomValue): boolean;
20
21
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
@@ -41,6 +41,9 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
41
41
  toTruthy() {
42
42
  return !!this.value;
43
43
  }
44
+ instanceOf(v) {
45
+ return v.value === CustomNumber.intrinsics;
46
+ }
44
47
  [Symbol.iterator]() {
45
48
  return new CustomNumberIterator();
46
49
  }
@@ -22,6 +22,7 @@ export default class CustomString extends CustomValueWithIntrinsics {
22
22
  toNumber(): number;
23
23
  toInt(): number;
24
24
  toTruthy(): boolean;
25
+ instanceOf(v: CustomValue): boolean;
25
26
  slice(a: CustomValue, b: CustomValue): CustomString;
26
27
  [Symbol.iterator](): CustomStringIterator;
27
28
  getCharIndex(index: number): number;
@@ -72,6 +72,9 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
72
72
  toTruthy() {
73
73
  return this.value.length > 0;
74
74
  }
75
+ instanceOf(v) {
76
+ return v.value === CustomString.intrinsics;
77
+ }
75
78
  slice(a, b) {
76
79
  return new CustomString(this.value.slice(a.toNumber(), b.toNumber()));
77
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -48,8 +48,8 @@
48
48
  "typescript": "^4.5.4"
49
49
  },
50
50
  "dependencies": {
51
- "greybel-core": "^0.6.1",
52
- "greyscript-core": "^0.5.6"
51
+ "greybel-core": "^0.6.3",
52
+ "greyscript-core": "^0.5.8"
53
53
  },
54
54
  "keywords": [
55
55
  "greyscript",