greyscript-interpreter 0.6.0 → 1.0.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.
@@ -0,0 +1,8 @@
1
+ import { BytecodeGenerator as GreybelBytecodeGenerator } from 'greybel-interpreter';
2
+ import { ASTImportCodeExpression } from 'greyscript-core';
3
+ import { ASTBase } from 'miniscript-core';
4
+ export declare class BytecodeGenerator extends GreybelBytecodeGenerator {
5
+ parse(code: string): ASTBase | import("greyscript-core").ASTChunkGreyScript;
6
+ protected processNode(node: ASTBase): Promise<void>;
7
+ protected processImportCodeExpression(node: ASTImportCodeExpression): Promise<void>;
8
+ }
@@ -0,0 +1,68 @@
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.BytecodeGenerator = void 0;
13
+ const greybel_interpreter_1 = require("greybel-interpreter");
14
+ const greyscript_core_1 = require("greyscript-core");
15
+ const miniscript_core_1 = require("miniscript-core");
16
+ class BytecodeGenerator extends greybel_interpreter_1.BytecodeGenerator {
17
+ parse(code) {
18
+ const parser = new greyscript_core_1.Parser(code);
19
+ return parser.parseChunk();
20
+ }
21
+ processNode(node) {
22
+ const _super = Object.create(null, {
23
+ processNode: { get: () => super.processNode }
24
+ });
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ switch (node.type) {
27
+ case greyscript_core_1.ASTType.ImportCodeExpression:
28
+ yield this.processImportCodeExpression(node);
29
+ return;
30
+ }
31
+ return _super.processNode.call(this, node);
32
+ });
33
+ }
34
+ processImportCodeExpression(node) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const currentTarget = this.target.peek();
37
+ const importTarget = yield this.handler.resourceHandler.getTargetRelativeTo(currentTarget, node.directory);
38
+ if (this.target.includes(importTarget)) {
39
+ console.warn(`Found circular dependency between "${currentTarget}" and "${importTarget}" at line ${node.start.line}. Using noop instead to prevent overflow.`);
40
+ return;
41
+ }
42
+ const code = yield this.handler.resourceHandler.get(importTarget);
43
+ if (code == null) {
44
+ const range = new miniscript_core_1.ASTRange(node.start, node.end);
45
+ throw new greybel_interpreter_1.PrepareError(`Cannot find import "${currentTarget}"`, {
46
+ target: currentTarget,
47
+ range
48
+ });
49
+ }
50
+ try {
51
+ this.target.push(importTarget);
52
+ const childNodes = this.parse(code);
53
+ yield this.processNode(childNodes);
54
+ this.target.pop();
55
+ }
56
+ catch (err) {
57
+ if (err instanceof greybel_interpreter_1.PrepareError) {
58
+ throw err;
59
+ }
60
+ throw new greybel_interpreter_1.PrepareError(err.message, {
61
+ target: importTarget,
62
+ range: new miniscript_core_1.ASTRange(node.start, node.end)
63
+ }, err);
64
+ }
65
+ });
66
+ }
67
+ }
68
+ exports.BytecodeGenerator = BytecodeGenerator;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { CPS, defaultCPSVisit } from './cps';
1
+ import './overrides';
2
+ export { BytecodeGenerator } from './bytecode-generator';
2
3
  export { Interpreter } from './interpreter';
3
- export { Import } from './operations/import';
4
- export { Include } from './operations/include';
4
+ export { CLASS_ID_PROPERTY } from './overrides';
package/dist/index.js CHANGED
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Include = exports.Import = exports.Interpreter = exports.defaultCPSVisit = exports.CPS = void 0;
4
- var cps_1 = require("./cps");
5
- Object.defineProperty(exports, "CPS", { enumerable: true, get: function () { return cps_1.CPS; } });
6
- Object.defineProperty(exports, "defaultCPSVisit", { enumerable: true, get: function () { return cps_1.defaultCPSVisit; } });
3
+ exports.CLASS_ID_PROPERTY = exports.Interpreter = exports.BytecodeGenerator = void 0;
4
+ require("./overrides");
5
+ var bytecode_generator_1 = require("./bytecode-generator");
6
+ Object.defineProperty(exports, "BytecodeGenerator", { enumerable: true, get: function () { return bytecode_generator_1.BytecodeGenerator; } });
7
7
  var interpreter_1 = require("./interpreter");
8
8
  Object.defineProperty(exports, "Interpreter", { enumerable: true, get: function () { return interpreter_1.Interpreter; } });
9
- var import_1 = require("./operations/import");
10
- Object.defineProperty(exports, "Import", { enumerable: true, get: function () { return import_1.Import; } });
11
- var include_1 = require("./operations/include");
12
- Object.defineProperty(exports, "Include", { enumerable: true, get: function () { return include_1.Include; } });
9
+ var overrides_1 = require("./overrides");
10
+ Object.defineProperty(exports, "CLASS_ID_PROPERTY", { enumerable: true, get: function () { return overrides_1.CLASS_ID_PROPERTY; } });
@@ -1,6 +1,7 @@
1
- import { Interpreter as GreybelInterpreter } from 'greybel-interpreter';
2
- import { CPS } from './cps';
1
+ import { Interpreter as GreybelInterpreter, OperationContext } from 'greybel-interpreter';
2
+ import { InterpreterRunOptions } from 'greybel-interpreter/dist/interpreter';
3
3
  export declare class Interpreter extends GreybelInterpreter {
4
- createCPS(): CPS;
5
4
  parse(code: string): import("miniscript-core").ASTBase | import("greyscript-core").ASTChunkGreyScript;
5
+ inject(code: string, context?: OperationContext): Promise<Interpreter>;
6
+ run({ customCode }?: InterpreterRunOptions): Promise<Interpreter>;
6
7
  }
@@ -1,17 +1,69 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.Interpreter = void 0;
4
13
  const greybel_interpreter_1 = require("greybel-interpreter");
5
- const cps_1 = require("./cps");
6
14
  const greyscript_core_1 = require("greyscript-core");
15
+ const bytecode_generator_1 = require("./bytecode-generator");
7
16
  class Interpreter extends greybel_interpreter_1.Interpreter {
8
- createCPS() {
9
- const cpsCtx = new greybel_interpreter_1.CPSContext(this.target, this.handler);
10
- return new cps_1.CPS(cpsCtx);
11
- }
12
17
  parse(code) {
13
18
  const parser = new greyscript_core_1.Parser(code);
14
19
  return parser.parseChunk();
15
20
  }
21
+ inject(code, context) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const bytecodeGenerator = new bytecode_generator_1.BytecodeGenerator({
24
+ target: 'injected',
25
+ handler: this.handler
26
+ });
27
+ const result = yield bytecodeGenerator.compile(code);
28
+ const vm = new greybel_interpreter_1.VM({
29
+ target: this.target,
30
+ debugger: this.debugger,
31
+ handler: this.handler,
32
+ environmentVariables: this.environmentVariables,
33
+ contextTypeIntrinsics: this.vm.contextTypeIntrinsics,
34
+ globals: (context !== null && context !== void 0 ? context : this.globalContext).fork({
35
+ code: result.code,
36
+ type: greybel_interpreter_1.ContextType.Injected
37
+ }),
38
+ imports: result.imports
39
+ });
40
+ try {
41
+ yield vm.exec();
42
+ }
43
+ catch (err) {
44
+ if (err instanceof greybel_interpreter_1.PrepareError || err instanceof greybel_interpreter_1.RuntimeError) {
45
+ this.handler.errorHandler.raise(err);
46
+ }
47
+ else {
48
+ this.handler.errorHandler.raise(new greybel_interpreter_1.RuntimeError(err.message, vm, err));
49
+ }
50
+ }
51
+ return this;
52
+ });
53
+ }
54
+ run({ customCode } = {}) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
57
+ const bytecodeConverter = new bytecode_generator_1.BytecodeGenerator({
58
+ target: this.target,
59
+ handler: this.handler,
60
+ debugMode: this.debugMode
61
+ });
62
+ const bytecode = yield bytecodeConverter.compile(code);
63
+ this.initVM(bytecode);
64
+ yield this.start();
65
+ return this;
66
+ });
67
+ }
16
68
  }
17
69
  exports.Interpreter = Interpreter;
@@ -0,0 +1,2 @@
1
+ import { CustomString } from 'greybel-interpreter';
2
+ export declare const CLASS_ID_PROPERTY: CustomString;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLASS_ID_PROPERTY = void 0;
4
+ const greybel_interpreter_1 = require("greybel-interpreter");
5
+ exports.CLASS_ID_PROPERTY = new greybel_interpreter_1.CustomString('classID');
6
+ const defaultCustomMapToString = greybel_interpreter_1.CustomMap.prototype.toString;
7
+ greybel_interpreter_1.CustomMap.prototype.toString = function (depth = 0) {
8
+ if (this.value.has(exports.CLASS_ID_PROPERTY)) {
9
+ return this.value.get(exports.CLASS_ID_PROPERTY).toString();
10
+ }
11
+ return defaultCustomMapToString.call(this, depth);
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greyscript-interpreter",
3
- "version": "0.6.0",
3
+ "version": "1.0.0",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "greyscript-core": "~1.4.2",
52
- "greybel-interpreter": "~3.5.0"
52
+ "greybel-interpreter": "~4.0.3"
53
53
  },
54
54
  "keywords": [
55
55
  "greyscript",
package/dist/cps.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { CPSContext, CPSVisitCallback, CPS as GreybelCPS } from 'greybel-interpreter';
2
- export declare const defaultCPSVisit: CPSVisitCallback;
3
- export declare class CPS extends GreybelCPS {
4
- constructor(context: CPSContext, cpsVisit?: CPSVisitCallback);
5
- }
package/dist/cps.js DELETED
@@ -1,126 +0,0 @@
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.CPS = exports.defaultCPSVisit = void 0;
13
- const greybel_interpreter_1 = require("greybel-interpreter");
14
- const greybel_core_1 = require("greybel-core");
15
- const greyscript_core_1 = require("greyscript-core");
16
- const miniscript_core_1 = require("miniscript-core");
17
- const include_1 = require("./operations/include");
18
- const import_1 = require("./operations/import");
19
- const defaultCPSVisit = (cpsVisit, context, stack, item) => __awaiter(void 0, void 0, void 0, function* () {
20
- const currentTarget = stack[stack.length - 1];
21
- switch (item.type) {
22
- case greyscript_core_1.ASTType.ImportCodeExpression: {
23
- const importExpr = item;
24
- const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, importExpr.directory);
25
- if (stack.includes(target)) {
26
- console.warn(`Found circular dependency between "${currentTarget}" and "${target}" at line ${item.start.line}. Using noop instead to prevent overflow.`);
27
- return new greybel_interpreter_1.Noop(item, target);
28
- }
29
- const code = yield context.handler.resourceHandler.get(target);
30
- if (code == null) {
31
- const range = new miniscript_core_1.ASTRange(item.start, item.end);
32
- throw new greybel_interpreter_1.PrepareError(`Cannot find native import "${currentTarget}"`, {
33
- target: currentTarget,
34
- range
35
- });
36
- }
37
- try {
38
- const subVisit = cpsVisit.bind(null, cpsVisit, context, [
39
- ...stack,
40
- target
41
- ]);
42
- const importStatement = yield new include_1.Include(importExpr, currentTarget, target, code).build(subVisit);
43
- return importStatement;
44
- }
45
- catch (err) {
46
- if (err instanceof greybel_interpreter_1.PrepareError) {
47
- throw err;
48
- }
49
- throw new greybel_interpreter_1.PrepareError(err.message, {
50
- target,
51
- range: new miniscript_core_1.ASTRange(item.start, item.end)
52
- }, err);
53
- }
54
- }
55
- case greybel_core_1.ASTType.FeatureImportExpression: {
56
- const importExpr = item;
57
- const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, importExpr.path);
58
- if (stack.includes(target)) {
59
- console.warn(`Found circular dependency between "${currentTarget}" and "${target}" at line ${item.start.line}. Using noop instead to prevent overflow.`);
60
- return new greybel_interpreter_1.Noop(item, target);
61
- }
62
- const code = yield context.handler.resourceHandler.get(target);
63
- if (code == null) {
64
- const range = new miniscript_core_1.ASTRange(item.start, item.end);
65
- throw new greybel_interpreter_1.PrepareError(`Cannot find import "${currentTarget}"`, {
66
- target: currentTarget,
67
- range
68
- });
69
- }
70
- try {
71
- const subVisit = cpsVisit.bind(null, cpsVisit, context, [...stack, target]);
72
- const importStatement = yield new import_1.Import(importExpr, currentTarget, target, code).build(subVisit);
73
- return importStatement;
74
- }
75
- catch (err) {
76
- if (err instanceof greybel_interpreter_1.PrepareError) {
77
- throw err;
78
- }
79
- throw new greybel_interpreter_1.PrepareError(err.message, {
80
- target,
81
- range: new miniscript_core_1.ASTRange(item.start, item.end)
82
- }, err);
83
- }
84
- }
85
- case greybel_core_1.ASTType.FeatureIncludeExpression: {
86
- const includeExpr = item;
87
- const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, includeExpr.path);
88
- if (stack.includes(target)) {
89
- console.warn(`Found circular dependency between "${currentTarget}" and "${target}" at line ${item.start.line}. Using noop instead to prevent overflow.`);
90
- return new greybel_interpreter_1.Noop(item, target);
91
- }
92
- const code = yield context.handler.resourceHandler.get(target);
93
- if (code == null) {
94
- const range = new miniscript_core_1.ASTRange(item.start, item.end);
95
- throw new greybel_interpreter_1.PrepareError(`Cannot find include "${currentTarget}"`, {
96
- target: currentTarget,
97
- range
98
- });
99
- }
100
- try {
101
- const subVisit = cpsVisit.bind(null, cpsVisit, context, [...stack, target]);
102
- const importStatement = yield new include_1.Include(includeExpr, currentTarget, target, code).build(subVisit);
103
- return importStatement;
104
- }
105
- catch (err) {
106
- if (err instanceof greybel_interpreter_1.PrepareError) {
107
- throw err;
108
- }
109
- throw new greybel_interpreter_1.PrepareError(err.message, {
110
- target,
111
- range: new miniscript_core_1.ASTRange(item.start, item.end)
112
- }, err);
113
- }
114
- }
115
- default: {
116
- return (0, greybel_interpreter_1.defaultCPSVisit)(cpsVisit, context, stack, item);
117
- }
118
- }
119
- });
120
- exports.defaultCPSVisit = defaultCPSVisit;
121
- class CPS extends greybel_interpreter_1.CPS {
122
- constructor(context, cpsVisit = exports.defaultCPSVisit) {
123
- super(context, cpsVisit);
124
- }
125
- }
126
- exports.CPS = CPS;
@@ -1,4 +0,0 @@
1
- import { CPSVisit, Import as GreybelImport, Operation } from 'greybel-interpreter';
2
- export declare class Import extends GreybelImport {
3
- build(visit: CPSVisit): Promise<Operation>;
4
- }
@@ -1,25 +0,0 @@
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.Import = void 0;
13
- const greybel_interpreter_1 = require("greybel-interpreter");
14
- const greyscript_core_1 = require("greyscript-core");
15
- class Import extends greybel_interpreter_1.Import {
16
- build(visit) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- const parser = new greyscript_core_1.Parser(this.code);
19
- this.chunk = parser.parseChunk();
20
- this.top = yield visit(this.chunk);
21
- return this;
22
- });
23
- }
24
- }
25
- exports.Import = Import;
@@ -1,4 +0,0 @@
1
- import { CPSVisit, Include as GreybelInclude, Operation } from 'greybel-interpreter';
2
- export declare class Include extends GreybelInclude {
3
- build(visit: CPSVisit): Promise<Operation>;
4
- }
@@ -1,25 +0,0 @@
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.Include = void 0;
13
- const greybel_interpreter_1 = require("greybel-interpreter");
14
- const greyscript_core_1 = require("greyscript-core");
15
- class Include extends greybel_interpreter_1.Include {
16
- build(visit) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- const parser = new greyscript_core_1.Parser(this.code);
19
- this.chunk = parser.parseChunk();
20
- this.top = yield visit(this.chunk);
21
- return this;
22
- });
23
- }
24
- }
25
- exports.Include = Include;