greybel-interpreter 1.5.9 → 1.6.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.
@@ -3,10 +3,12 @@ import { ASTBase } from 'greyscript-core';
3
3
  import context from '../context';
4
4
  import CustomValue from '../types/base';
5
5
  import CustomString from '../types/string';
6
+ import Path from '../utils/path';
6
7
  import Operation, { CPSVisit } from './operation';
7
8
  export declare const MODULE_PROPERTY: CustomString;
8
9
  export declare const EXPORTS_PROPERTY: CustomString;
9
- export default class Include extends Operation {
10
+ export declare const EXPORTS_PATH: Path<CustomString>;
11
+ export default class Import extends Operation {
10
12
  readonly item: ASTFeatureImportExpression;
11
13
  code: string;
12
14
  chunk: ASTBase;
@@ -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.EXPORTS_PROPERTY = exports.MODULE_PROPERTY = void 0;
15
+ exports.EXPORTS_PATH = exports.EXPORTS_PROPERTY = exports.MODULE_PROPERTY = void 0;
16
16
  const greybel_core_1 = require("greybel-core");
17
17
  const context_1 = require("../context");
18
18
  const default_1 = __importDefault(require("../types/default"));
19
19
  const map_1 = __importDefault(require("../types/map"));
20
20
  const string_1 = __importDefault(require("../types/string"));
21
+ const path_1 = __importDefault(require("../utils/path"));
21
22
  const operation_1 = __importDefault(require("./operation"));
22
23
  exports.MODULE_PROPERTY = new string_1.default('module');
23
24
  exports.EXPORTS_PROPERTY = new string_1.default('exports');
24
- class Include extends operation_1.default {
25
+ exports.EXPORTS_PATH = new path_1.default([exports.MODULE_PROPERTY, exports.EXPORTS_PROPERTY]);
26
+ class Import extends operation_1.default {
25
27
  constructor(item, target, code) {
26
28
  super(null, target);
27
29
  this.item = item;
@@ -39,14 +41,13 @@ class Include extends operation_1.default {
39
41
  return __awaiter(this, void 0, void 0, function* () {
40
42
  const importCtx = ctx.fork({
41
43
  type: context_1.ContextType.External,
42
- state: context_1.ContextState.Temporary,
44
+ state: context_1.ContextState.Default,
43
45
  target: this.target
44
46
  });
45
- const moduleMap = new map_1.default();
46
- importCtx.set(exports.MODULE_PROPERTY, moduleMap);
47
+ importCtx.locals.scope.set(exports.MODULE_PROPERTY, new map_1.default());
47
48
  yield this.top.handle(importCtx);
48
- const item = moduleMap.has(exports.EXPORTS_PROPERTY)
49
- ? moduleMap.get(exports.EXPORTS_PROPERTY)
49
+ const item = importCtx.locals.scope.has(exports.EXPORTS_PATH)
50
+ ? importCtx.scope.get(exports.EXPORTS_PATH)
50
51
  : default_1.default.Void;
51
52
  const identifier = this.item.name;
52
53
  ctx.set(new string_1.default(identifier.name), item);
@@ -54,4 +55,4 @@ class Include extends operation_1.default {
54
55
  });
55
56
  }
56
57
  }
57
- exports.default = Include;
58
+ exports.default = Import;
@@ -3,6 +3,7 @@ export default abstract class CustomValue {
3
3
  abstract getCustomType(): string;
4
4
  abstract toNumber(): number;
5
5
  abstract toInt(): number;
6
+ abstract toJSON(depth?: number): string;
6
7
  abstract toString(): string;
7
8
  abstract toTruthy(): boolean;
8
9
  abstract fork(): CustomValue;
@@ -3,6 +3,7 @@ export default class CustomBoolean extends CustomValue {
3
3
  readonly value: boolean;
4
4
  constructor(value: boolean);
5
5
  getCustomType(): string;
6
+ toJSON(): string;
6
7
  toString(): string;
7
8
  fork(): CustomBoolean;
8
9
  toNumber(): number;
@@ -13,6 +13,9 @@ class CustomBoolean extends base_1.default {
13
13
  getCustomType() {
14
14
  return 'boolean';
15
15
  }
16
+ toJSON() {
17
+ return this.toNumber().toString();
18
+ }
16
19
  toString() {
17
20
  return this.value.toString();
18
21
  }
@@ -27,6 +27,7 @@ export default class CustomFunction extends CustomValue {
27
27
  getCustomType(): string;
28
28
  toNumber(): number;
29
29
  toInt(): number;
30
+ toJSON(): string;
30
31
  toString(): string;
31
32
  toTruthy(): boolean;
32
33
  instanceOf(v: CustomValue): boolean;
@@ -76,9 +76,12 @@ class CustomFunction extends base_1.default {
76
76
  toInt() {
77
77
  return 0;
78
78
  }
79
+ toJSON() {
80
+ return this.toString();
81
+ }
79
82
  toString() {
80
83
  const args = this.argumentDefs.map((item) => item.name);
81
- return `function ${this.name}(${args.join(', ')})`;
84
+ return `FUNCTION(${args.join(', ')})`;
82
85
  }
83
86
  toTruthy() {
84
87
  return true;
@@ -11,6 +11,7 @@ export default class CustomInterface extends CustomObject {
11
11
  readonly value: Object;
12
12
  constructor(type: string);
13
13
  getCustomType(): string;
14
+ toJSON(): string;
14
15
  toString(): string;
15
16
  fork(): CustomInterface;
16
17
  toNumber(): number;
@@ -27,6 +27,9 @@ class CustomInterface extends with_intrinsics_1.CustomObject {
27
27
  getCustomType() {
28
28
  return this.type;
29
29
  }
30
+ toJSON() {
31
+ return this.toString();
32
+ }
30
33
  toString() {
31
34
  return this.type;
32
35
  }
@@ -14,7 +14,8 @@ export default class CustomList extends CustomObject {
14
14
  readonly value: Array<CustomValue>;
15
15
  constructor(value?: Array<CustomValue>);
16
16
  getCustomType(): string;
17
- toString(): string;
17
+ toJSON(depth?: number): string;
18
+ toString(depth?: number): string;
18
19
  fork(): CustomList;
19
20
  toNumber(): number;
20
21
  toInt(): number;
@@ -46,8 +46,11 @@ class CustomList extends with_intrinsics_1.CustomObject {
46
46
  getCustomType() {
47
47
  return 'list';
48
48
  }
49
- toString() {
50
- return `[ ${this.value.join(', ')} ]`;
49
+ toJSON(depth = 0) {
50
+ return this.toString(depth);
51
+ }
52
+ toString(depth = 0) {
53
+ return `[${this.value.map((item) => item.toJSON(depth)).join(', ')}]`;
51
54
  }
52
55
  fork() {
53
56
  return new CustomList(this.value);
@@ -21,6 +21,7 @@ export default class CustomMap extends CustomObject {
21
21
  static createWithInitialValue(value: ObjectValue): CustomMap;
22
22
  constructor(value?: ObjectValue, isa?: CustomMap);
23
23
  getCustomType(): string;
24
+ toJSON(depth?: number): string;
24
25
  toString(depth?: number): string;
25
26
  fork(): CustomMap;
26
27
  toNumber(): number;
package/dist/types/map.js CHANGED
@@ -58,21 +58,23 @@ class CustomMap extends with_intrinsics_1.CustomObject {
58
58
  }
59
59
  return 'map';
60
60
  }
61
+ toJSON(depth = 0) {
62
+ return this.toString(depth);
63
+ }
61
64
  toString(depth = 0) {
62
- const json = {};
65
+ const fields = [];
63
66
  if (exports.CUSTOM_MAP_MAX_DEPTH < depth) {
64
67
  return exports.CUSTOM_MAP_MAX_DEPTH_VALUE;
65
68
  }
66
69
  if (this.isa) {
67
- json[exports.ISA_PROPERTY.toString()] = this.isa.toString(depth + 1);
70
+ fields.push(`${exports.ISA_PROPERTY.toJSON()}:${this.isa.toJSON(depth + 1)}`);
68
71
  }
69
72
  for (const [key, value] of this.value.entries()) {
70
- json[key.toString()] =
71
- value instanceof CustomMap
72
- ? value.toString(depth + 1)
73
- : value.toString();
73
+ fields.push(`${key instanceof CustomMap ? key.toJSON(depth + 1) : key.toJSON(depth)}:${value instanceof CustomMap
74
+ ? value.toJSON(depth + 1)
75
+ : value.toJSON(depth)}`);
74
76
  }
75
- return JSON.stringify(json);
77
+ return `{${fields.join(',')}}`;
76
78
  }
77
79
  fork() {
78
80
  return new CustomMap(this.value);
@@ -2,6 +2,7 @@ import CustomValue from './base';
2
2
  export default class CustomNil extends CustomValue {
3
3
  value: null;
4
4
  getCustomType(): string;
5
+ toJSON(): string;
5
6
  toString(): string;
6
7
  fork(): CustomNil;
7
8
  toNumber(): number;
package/dist/types/nil.js CHANGED
@@ -13,6 +13,9 @@ class CustomNil extends base_1.default {
13
13
  getCustomType() {
14
14
  return 'null';
15
15
  }
16
+ toJSON() {
17
+ return this.toString();
18
+ }
16
19
  toString() {
17
20
  return 'null';
18
21
  }
@@ -10,6 +10,7 @@ export default class CustomNumber extends CustomValueWithIntrinsics {
10
10
  readonly value: number;
11
11
  constructor(value: number);
12
12
  getCustomType(): string;
13
+ toJSON(): string;
13
14
  toString(): string;
14
15
  fork(): CustomNumber;
15
16
  toInt(): number;
@@ -26,6 +26,9 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
26
26
  getCustomType() {
27
27
  return 'number';
28
28
  }
29
+ toJSON() {
30
+ return this.value.toString();
31
+ }
29
32
  toString() {
30
33
  return this.value.toString();
31
34
  }
@@ -14,6 +14,7 @@ export default class CustomString extends CustomValueWithIntrinsics {
14
14
  readonly value: string;
15
15
  constructor(value: string);
16
16
  getCustomType(): string;
17
+ toJSON(): string;
17
18
  toString(): string;
18
19
  fork(): CustomString;
19
20
  isNumber(): boolean;
@@ -47,6 +47,9 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
47
47
  getCustomType() {
48
48
  return 'string';
49
49
  }
50
+ toJSON() {
51
+ return `"${this.value}"`;
52
+ }
50
53
  toString() {
51
54
  return this.value;
52
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "1.5.9",
3
+ "version": "1.6.1",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",