greybel-interpreter 0.7.2 → 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.
Files changed (137) hide show
  1. package/.editorconfig +15 -0
  2. package/.eslintrc +48 -0
  3. package/LICENSE +21 -21
  4. package/README.md +2 -2
  5. package/dist/context.d.ts +107 -98
  6. package/dist/context.js +358 -456
  7. package/dist/cps.d.ts +15 -17
  8. package/dist/cps.js +220 -872
  9. package/dist/handler/error.d.ts +6 -0
  10. package/dist/{operations/debugger.js → handler/error.js} +35 -32
  11. package/dist/handler/output.d.ts +6 -0
  12. package/dist/{operations/if.js → handler/output.js} +35 -31
  13. package/dist/handler/resource.d.ts +12 -0
  14. package/dist/handler/resource.js +51 -0
  15. package/dist/handler-container.d.ts +14 -0
  16. package/dist/handler-container.js +16 -0
  17. package/dist/index.d.ts +44 -36
  18. package/dist/index.js +129 -93
  19. package/dist/interpreter.d.ts +39 -29
  20. package/dist/interpreter.js +292 -210
  21. package/dist/intrinsics-container.d.ts +7 -0
  22. package/dist/intrinsics-container.js +19 -0
  23. package/dist/operations/assign.d.ts +13 -0
  24. package/dist/operations/assign.js +111 -0
  25. package/dist/operations/block.d.ts +12 -0
  26. package/dist/operations/{body.js → block.js} +108 -140
  27. package/dist/operations/break.d.ts +10 -7
  28. package/dist/operations/break.js +41 -34
  29. package/dist/operations/call.d.ts +13 -0
  30. package/dist/operations/call.js +124 -0
  31. package/dist/operations/chunk.d.ts +12 -0
  32. package/dist/operations/chunk.js +85 -0
  33. package/dist/operations/continue.d.ts +10 -7
  34. package/dist/operations/continue.js +41 -34
  35. package/dist/operations/debugger-statement.d.ts +10 -0
  36. package/dist/operations/{else-if.js → debugger-statement.js} +39 -31
  37. package/dist/operations/evaluate.d.ts +39 -0
  38. package/dist/operations/evaluate.js +366 -0
  39. package/dist/operations/for.d.ts +15 -16
  40. package/dist/operations/for.js +146 -143
  41. package/dist/operations/function-reference.d.ts +12 -0
  42. package/dist/operations/function-reference.js +103 -0
  43. package/dist/operations/function.d.ts +12 -26
  44. package/dist/operations/function.js +198 -145
  45. package/dist/operations/if-statement.d.ts +19 -8
  46. package/dist/operations/if-statement.js +181 -169
  47. package/dist/operations/import.d.ts +14 -0
  48. package/dist/{expressions/include.js → operations/import.js} +115 -132
  49. package/dist/operations/include.d.ts +13 -0
  50. package/dist/operations/include.js +95 -0
  51. package/dist/operations/list.d.ts +11 -0
  52. package/dist/{expressions → operations}/list.js +111 -142
  53. package/dist/operations/literal.d.ts +11 -0
  54. package/dist/operations/literal.js +58 -0
  55. package/dist/operations/map.d.ts +11 -0
  56. package/dist/operations/{argument.js → map.js} +167 -145
  57. package/dist/operations/negated-binary.d.ts +11 -0
  58. package/dist/{expressions/binary-negated-expression.js → operations/negated-binary.js} +109 -127
  59. package/dist/operations/new-instance.d.ts +11 -0
  60. package/dist/operations/{new.js → new-instance.js} +100 -97
  61. package/dist/operations/noop.d.ts +9 -0
  62. package/dist/operations/{else.js → noop.js} +36 -30
  63. package/dist/operations/not.d.ts +11 -11
  64. package/dist/operations/not.js +96 -96
  65. package/dist/operations/operation.d.ts +13 -0
  66. package/dist/operations/operation.js +11 -0
  67. package/dist/operations/reference.d.ts +9 -11
  68. package/dist/operations/reference.js +37 -103
  69. package/dist/operations/resolve.d.ts +38 -0
  70. package/dist/operations/resolve.js +330 -0
  71. package/dist/operations/return.d.ts +11 -11
  72. package/dist/operations/return.js +101 -106
  73. package/dist/operations/while.d.ts +13 -14
  74. package/dist/operations/while.js +129 -145
  75. package/dist/types/boolean.d.ts +11 -0
  76. package/dist/{custom-types → types}/boolean.js +46 -47
  77. package/dist/types/default.d.ts +11 -0
  78. package/dist/types/default.js +20 -0
  79. package/dist/types/function.d.ts +32 -0
  80. package/dist/types/function.js +174 -0
  81. package/dist/types/generics.d.ts +17 -0
  82. package/dist/types/{operation.js → generics.js} +40 -38
  83. package/dist/types/interface.d.ts +22 -0
  84. package/dist/types/interface.js +102 -0
  85. package/dist/types/list.d.ts +31 -0
  86. package/dist/types/list.js +227 -0
  87. package/dist/types/map.d.ts +30 -0
  88. package/dist/types/map.js +224 -0
  89. package/dist/types/nil.d.ts +9 -0
  90. package/dist/{custom-types → types}/nil.js +44 -44
  91. package/dist/types/number.d.ts +11 -0
  92. package/dist/{custom-types → types}/number.js +46 -47
  93. package/dist/types/string.d.ts +33 -0
  94. package/dist/types/string.js +149 -0
  95. package/dist/utils/path.d.ts +10 -0
  96. package/dist/utils/path.js +59 -0
  97. package/package.json +58 -44
  98. package/dist/custom-types/boolean.d.ts +0 -12
  99. package/dist/custom-types/list.d.ts +0 -26
  100. package/dist/custom-types/list.js +0 -236
  101. package/dist/custom-types/map.d.ts +0 -26
  102. package/dist/custom-types/map.js +0 -245
  103. package/dist/custom-types/nil.d.ts +0 -10
  104. package/dist/custom-types/number.d.ts +0 -12
  105. package/dist/custom-types/string.d.ts +0 -25
  106. package/dist/custom-types/string.js +0 -172
  107. package/dist/expressions/assign.d.ts +0 -13
  108. package/dist/expressions/assign.js +0 -174
  109. package/dist/expressions/binary-negated-expression.d.ts +0 -18
  110. package/dist/expressions/call.d.ts +0 -14
  111. package/dist/expressions/call.js +0 -209
  112. package/dist/expressions/import.d.ts +0 -14
  113. package/dist/expressions/import.js +0 -155
  114. package/dist/expressions/include.d.ts +0 -13
  115. package/dist/expressions/list.d.ts +0 -14
  116. package/dist/expressions/logical-and-binary.d.ts +0 -22
  117. package/dist/expressions/logical-and-binary.js +0 -222
  118. package/dist/expressions/map.d.ts +0 -19
  119. package/dist/expressions/map.js +0 -197
  120. package/dist/expressions/path.d.ts +0 -31
  121. package/dist/expressions/path.js +0 -384
  122. package/dist/operations/argument.d.ts +0 -8
  123. package/dist/operations/body.d.ts +0 -8
  124. package/dist/operations/debugger.d.ts +0 -7
  125. package/dist/operations/else-if.d.ts +0 -12
  126. package/dist/operations/else.d.ts +0 -10
  127. package/dist/operations/if.d.ts +0 -12
  128. package/dist/operations/new.d.ts +0 -11
  129. package/dist/resource.d.ts +0 -9
  130. package/dist/resource.js +0 -29
  131. package/dist/typer.d.ts +0 -6
  132. package/dist/typer.js +0 -95
  133. package/dist/types/custom-type.d.ts +0 -22
  134. package/dist/types/custom-type.js +0 -67
  135. package/dist/types/expression.d.ts +0 -7
  136. package/dist/types/expression.js +0 -9
  137. package/dist/types/operation.d.ts +0 -10
@@ -1,29 +1,39 @@
1
- /// <reference types="node" />
2
- import CPS from './cps';
3
- import { OperationContext, Debugger } from './context';
4
- import { ResourceHandler } from './resource';
5
- import EventEmitter from 'events';
6
- export interface InterpreterOptions {
7
- target?: string;
8
- api?: Map<string, any>;
9
- params?: any[];
10
- resourceHandler?: ResourceHandler;
11
- debugger?: Debugger;
12
- }
13
- export default class Interpreter extends EventEmitter {
14
- target: string;
15
- api: Map<string, any>;
16
- params: any[];
17
- resourceHandler: ResourceHandler;
18
- debugger: Debugger;
19
- apiContext: OperationContext;
20
- globalContext: OperationContext;
21
- cps: CPS;
22
- constructor(options: InterpreterOptions);
23
- setTarget(target: string): Interpreter;
24
- setDebugger(dbgr: Debugger): Interpreter;
25
- inject(code: string, context?: OperationContext): Promise<Interpreter>;
26
- injectInLastContext(code: string): Promise<Interpreter>;
27
- digest(customCode?: string): Promise<Interpreter>;
28
- exit(): Promise<OperationContext>;
29
- }
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import OperationContext, { Debugger } from './context';
4
+ import CPS from './cps';
5
+ import HandlerContainer from './handler-container';
6
+ import Operation from './operations/operation';
7
+ import { CustomValue } from './types/generics';
8
+ export interface InterpreterOptions {
9
+ target?: string;
10
+ api?: Map<string, CustomValue>;
11
+ params?: Array<string>;
12
+ handler?: HandlerContainer;
13
+ debugger?: Debugger;
14
+ }
15
+ export default class Interpreter extends EventEmitter {
16
+ target: string;
17
+ api: Map<string, CustomValue>;
18
+ params: Array<string>;
19
+ handler: HandlerContainer;
20
+ debugger: Debugger;
21
+ apiContext: OperationContext;
22
+ globalContext: OperationContext;
23
+ cps: CPS;
24
+ constructor(options: InterpreterOptions);
25
+ setTarget(target: string): Interpreter;
26
+ setDebugger(dbgr: Debugger): Interpreter;
27
+ setApi(newApi: Map<string, CustomValue>): Interpreter;
28
+ setHandler(handler: HandlerContainer): Interpreter;
29
+ prepare(code: string): Promise<Operation>;
30
+ inject(code: string, context?: OperationContext): Promise<Interpreter>;
31
+ injectInLastContext(code: string): Promise<Interpreter>;
32
+ run(customCode?: string): Promise<Interpreter>;
33
+ start(top: Operation): Promise<Interpreter>;
34
+ resume(): Interpreter;
35
+ pause(): Interpreter;
36
+ exit(): Promise<OperationContext>;
37
+ setGlobalVariable(path: string, value: CustomValue): Interpreter;
38
+ getGlobalVariable(path: string): CustomValue;
39
+ }
@@ -1,210 +1,292 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- var __importDefault = (this && this.__importDefault) || function (mod) {
54
- return (mod && mod.__esModule) ? mod : { "default": mod };
55
- };
56
- Object.defineProperty(exports, "__esModule", { value: true });
57
- var cps_1 = __importDefault(require("./cps"));
58
- var context_1 = require("./context");
59
- var greybel_core_1 = require("greybel-core");
60
- var resource_1 = require("./resource");
61
- var typer_1 = require("./typer");
62
- var events_1 = __importDefault(require("events"));
63
- ;
64
- var Interpreter = /** @class */ (function (_super) {
65
- __extends(Interpreter, _super);
66
- function Interpreter(options) {
67
- var _this = _super.call(this) || this;
68
- var me = _this;
69
- me.resourceHandler = options.resourceHandler || new resource_1.ResourceProvider().getHandler();
70
- me.debugger = options.debugger || new context_1.Debugger();
71
- me.api = options.api || new Map();
72
- me.params = options.params || [];
73
- me.setTarget(options.target || 'unknown');
74
- return _this;
75
- }
76
- Interpreter.prototype.setTarget = function (target) {
77
- var me = this;
78
- me.target = target;
79
- me.cps = new cps_1.default({
80
- target: me.target,
81
- resourceHandler: me.resourceHandler
82
- });
83
- me.apiContext = new context_1.OperationContext({
84
- target: me.target,
85
- isProtected: true,
86
- debugger: me.debugger,
87
- cps: me.cps
88
- });
89
- me.globalContext = me.apiContext.fork({
90
- type: context_1.ContextType.GLOBAL,
91
- state: context_1.ContextState.DEFAULT
92
- });
93
- return me;
94
- };
95
- Interpreter.prototype.setDebugger = function (dbgr) {
96
- var me = this;
97
- me.debugger = dbgr;
98
- me.apiContext.debugger = dbgr;
99
- me.globalContext.debugger = dbgr;
100
- return me;
101
- };
102
- Interpreter.prototype.inject = function (code, context) {
103
- return __awaiter(this, void 0, void 0, function () {
104
- var me, parser, chunk, body, injectionCtx, err_1;
105
- return __generator(this, function (_a) {
106
- switch (_a.label) {
107
- case 0:
108
- me = this;
109
- _a.label = 1;
110
- case 1:
111
- _a.trys.push([1, 4, , 5]);
112
- parser = new greybel_core_1.Parser(code);
113
- chunk = parser.parseChunk();
114
- return [4 /*yield*/, me.cps.visit(chunk)];
115
- case 2:
116
- body = _a.sent();
117
- injectionCtx = (context || me.globalContext).fork({
118
- type: context_1.ContextType.CALL,
119
- state: context_1.ContextState.TEMPORARY,
120
- injected: true
121
- });
122
- return [4 /*yield*/, body.run(injectionCtx)];
123
- case 3:
124
- _a.sent();
125
- return [3 /*break*/, 5];
126
- case 4:
127
- err_1 = _a.sent();
128
- me.debugger.raise(err_1);
129
- return [3 /*break*/, 5];
130
- case 5: return [2 /*return*/, me];
131
- }
132
- });
133
- });
134
- };
135
- Interpreter.prototype.injectInLastContext = function (code) {
136
- return __awaiter(this, void 0, void 0, function () {
137
- var me, last;
138
- return __generator(this, function (_a) {
139
- me = this;
140
- last = me.apiContext.getLastActive();
141
- if (me.apiContext.isPending()) {
142
- return [2 /*return*/, me.inject(code, last)];
143
- }
144
- return [2 /*return*/, me];
145
- });
146
- });
147
- };
148
- Interpreter.prototype.digest = function (customCode) {
149
- return __awaiter(this, void 0, void 0, function () {
150
- var me, code, _a, parser, chunk, body, process_1, err_2;
151
- return __generator(this, function (_b) {
152
- switch (_b.label) {
153
- case 0:
154
- me = this;
155
- if (me.apiContext.isPending()) {
156
- return [2 /*return*/, Promise.reject(new Error('Process already running.'))];
157
- }
158
- _a = customCode;
159
- if (_a) return [3 /*break*/, 2];
160
- return [4 /*yield*/, me.resourceHandler.get(me.target)];
161
- case 1:
162
- _a = (_b.sent());
163
- _b.label = 2;
164
- case 2:
165
- code = _a;
166
- parser = new greybel_core_1.Parser(code);
167
- chunk = parser.parseChunk();
168
- return [4 /*yield*/, me.cps.visit(chunk)];
169
- case 3:
170
- body = _b.sent();
171
- me.apiContext.extend(me.api);
172
- me.globalContext.scope.value.set('params', (0, typer_1.cast)(me.params));
173
- me.emit('setup', me);
174
- _b.label = 4;
175
- case 4:
176
- _b.trys.push([4, 6, 7, 8]);
177
- me.apiContext.setPending(true);
178
- process_1 = body.run(me.globalContext);
179
- me.emit('start');
180
- return [4 /*yield*/, process_1];
181
- case 5:
182
- _b.sent();
183
- return [3 /*break*/, 8];
184
- case 6:
185
- err_2 = _b.sent();
186
- me.debugger.raise(err_2);
187
- return [3 /*break*/, 8];
188
- case 7:
189
- setTimeout(function () {
190
- me.apiContext.setPending(false);
191
- me.emit('exit', me);
192
- }, 100);
193
- return [7 /*endfinally*/];
194
- case 8: return [2 /*return*/, me];
195
- }
196
- });
197
- });
198
- };
199
- Interpreter.prototype.exit = function () {
200
- var me = this;
201
- try {
202
- return me.apiContext.exit();
203
- }
204
- catch (err) {
205
- me.debugger.raise(err);
206
- }
207
- };
208
- return Interpreter;
209
- }(events_1.default));
210
- exports.default = Interpreter;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
+ return new (P || (P = Promise))(function (resolve, reject) {
43
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
47
+ });
48
+ };
49
+ var __generator = (this && this.__generator) || function (thisArg, body) {
50
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
51
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
52
+ function verb(n) { return function (v) { return step([n, v]); }; }
53
+ function step(op) {
54
+ if (f) throw new TypeError("Generator is already executing.");
55
+ while (_) try {
56
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
57
+ if (y = 0, t) op = [op[0] & 2, t.value];
58
+ switch (op[0]) {
59
+ case 0: case 1: t = op; break;
60
+ case 4: _.label++; return { value: op[1], done: false };
61
+ case 5: _.label++; y = op[1]; op = [0]; continue;
62
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
63
+ default:
64
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
65
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
66
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
67
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
68
+ if (t[2]) _.ops.pop();
69
+ _.trys.pop(); continue;
70
+ }
71
+ op = body.call(thisArg, _);
72
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
73
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
74
+ }
75
+ };
76
+ var __importDefault = (this && this.__importDefault) || function (mod) {
77
+ return (mod && mod.__esModule) ? mod : { "default": mod };
78
+ };
79
+ Object.defineProperty(exports, "__esModule", { value: true });
80
+ var events_1 = __importDefault(require("events"));
81
+ var greybel_core_1 = require("greybel-core");
82
+ var context_1 = __importStar(require("./context"));
83
+ var cps_1 = __importStar(require("./cps"));
84
+ var handler_container_1 = __importDefault(require("./handler-container"));
85
+ var noop_1 = __importDefault(require("./operations/noop"));
86
+ var default_1 = __importDefault(require("./types/default"));
87
+ var list_1 = __importDefault(require("./types/list"));
88
+ var string_1 = __importDefault(require("./types/string"));
89
+ var Interpreter = /** @class */ (function (_super) {
90
+ __extends(Interpreter, _super);
91
+ function Interpreter(options) {
92
+ var _this = _super.call(this) || this;
93
+ _this.handler = options.handler || new handler_container_1.default();
94
+ _this.debugger = options.debugger || new context_1.Debugger();
95
+ _this.api = options.api || new Map();
96
+ _this.params = options.params || [];
97
+ _this.apiContext = null;
98
+ _this.globalContext = null;
99
+ _this.setTarget(options.target || 'unknown');
100
+ return _this;
101
+ }
102
+ Interpreter.prototype.setTarget = function (target) {
103
+ if (this.apiContext !== null && this.apiContext.isPending()) {
104
+ throw new Error('You cannot set a target while a process is running.');
105
+ }
106
+ this.target = target;
107
+ var cpsCtx = new cps_1.CPSContext(target, this.handler);
108
+ this.cps = new cps_1.default(cpsCtx);
109
+ this.apiContext = new context_1.default({
110
+ target: target,
111
+ isProtected: true,
112
+ debugger: this.debugger,
113
+ handler: this.handler,
114
+ cps: this.cps
115
+ });
116
+ this.globalContext = this.apiContext.fork({
117
+ type: context_1.ContextType.Global,
118
+ state: context_1.ContextState.Default
119
+ });
120
+ return this;
121
+ };
122
+ Interpreter.prototype.setDebugger = function (dbgr) {
123
+ if (this.apiContext !== null && this.apiContext.isPending()) {
124
+ throw new Error('You cannot set a target while a process is running.');
125
+ }
126
+ this.debugger = dbgr;
127
+ this.apiContext.debugger = dbgr;
128
+ this.globalContext.debugger = dbgr;
129
+ return this;
130
+ };
131
+ Interpreter.prototype.setApi = function (newApi) {
132
+ if (this.apiContext !== null && this.apiContext.isPending()) {
133
+ throw new Error('You cannot set a target while a process is running.');
134
+ }
135
+ this.api = newApi;
136
+ return this;
137
+ };
138
+ Interpreter.prototype.setHandler = function (handler) {
139
+ if (this.apiContext !== null && this.apiContext.isPending()) {
140
+ throw new Error('You cannot set a target while a process is running.');
141
+ }
142
+ this.handler = handler;
143
+ this.apiContext.handler = handler;
144
+ this.globalContext.handler = handler;
145
+ return this;
146
+ };
147
+ Interpreter.prototype.prepare = function (code) {
148
+ try {
149
+ var parser = new greybel_core_1.Parser(code);
150
+ var chunk = parser.parseChunk();
151
+ return this.cps.visit(chunk);
152
+ }
153
+ catch (err) {
154
+ this.handler.errorHandler.raise(err);
155
+ }
156
+ finally {
157
+ this.apiContext.setPending(false);
158
+ }
159
+ return Promise.resolve(new noop_1.default());
160
+ };
161
+ Interpreter.prototype.inject = function (code, context) {
162
+ return __awaiter(this, void 0, void 0, function () {
163
+ var top, injectionCtx, err_1;
164
+ return __generator(this, function (_a) {
165
+ switch (_a.label) {
166
+ case 0:
167
+ _a.trys.push([0, 3, , 4]);
168
+ return [4 /*yield*/, this.prepare(code)];
169
+ case 1:
170
+ top = _a.sent();
171
+ injectionCtx = (context || this.globalContext).fork({
172
+ type: context_1.ContextType.Call,
173
+ state: context_1.ContextState.Temporary,
174
+ injected: true
175
+ });
176
+ return [4 /*yield*/, top.handle(injectionCtx)];
177
+ case 2:
178
+ _a.sent();
179
+ return [3 /*break*/, 4];
180
+ case 3:
181
+ err_1 = _a.sent();
182
+ this.handler.errorHandler.raise(err_1);
183
+ return [3 /*break*/, 4];
184
+ case 4: return [2 /*return*/, this];
185
+ }
186
+ });
187
+ });
188
+ };
189
+ Interpreter.prototype.injectInLastContext = function (code) {
190
+ return __awaiter(this, void 0, void 0, function () {
191
+ var last;
192
+ return __generator(this, function (_a) {
193
+ last = this.apiContext.getLastActive();
194
+ if (this.apiContext !== null && this.apiContext.isPending()) {
195
+ return [2 /*return*/, this.inject(code, last)];
196
+ }
197
+ throw new Error('Unable to inject into last context.');
198
+ });
199
+ });
200
+ };
201
+ Interpreter.prototype.run = function (customCode) {
202
+ return __awaiter(this, void 0, void 0, function () {
203
+ var code, _a, top;
204
+ return __generator(this, function (_b) {
205
+ switch (_b.label) {
206
+ case 0:
207
+ _a = customCode;
208
+ if (_a) return [3 /*break*/, 2];
209
+ return [4 /*yield*/, this.handler.resourceHandler.get(this.target)];
210
+ case 1:
211
+ _a = (_b.sent());
212
+ _b.label = 2;
213
+ case 2:
214
+ code = _a;
215
+ return [4 /*yield*/, this.prepare(code)];
216
+ case 3:
217
+ top = _b.sent();
218
+ return [2 /*return*/, this.start(top)];
219
+ }
220
+ });
221
+ });
222
+ };
223
+ Interpreter.prototype.start = function (top) {
224
+ return __awaiter(this, void 0, void 0, function () {
225
+ var newParams, process_1, err_2;
226
+ return __generator(this, function (_a) {
227
+ switch (_a.label) {
228
+ case 0:
229
+ if (this.apiContext !== null && this.apiContext.isPending()) {
230
+ throw new Error('Process already running.');
231
+ }
232
+ this.apiContext.extend(this.api);
233
+ newParams = new list_1.default(this.params.map(function (item) { return new string_1.default(item); }));
234
+ this.globalContext.scope.set('params', newParams);
235
+ _a.label = 1;
236
+ case 1:
237
+ _a.trys.push([1, 3, 4, 5]);
238
+ this.apiContext.setPending(true);
239
+ process_1 = top.handle(this.globalContext);
240
+ this.emit('start', this);
241
+ return [4 /*yield*/, process_1];
242
+ case 2:
243
+ _a.sent();
244
+ return [3 /*break*/, 5];
245
+ case 3:
246
+ err_2 = _a.sent();
247
+ this.handler.errorHandler.raise(err_2);
248
+ return [3 /*break*/, 5];
249
+ case 4:
250
+ this.apiContext.setPending(false);
251
+ this.emit('exit', this);
252
+ return [7 /*endfinally*/];
253
+ case 5: return [2 /*return*/, this];
254
+ }
255
+ });
256
+ });
257
+ };
258
+ Interpreter.prototype.resume = function () {
259
+ if (this.apiContext !== null && this.apiContext.isPending()) {
260
+ this.debugger.setBreakpoint(false);
261
+ }
262
+ return this;
263
+ };
264
+ Interpreter.prototype.pause = function () {
265
+ if (this.apiContext !== null && this.apiContext.isPending()) {
266
+ this.debugger.setBreakpoint(true);
267
+ }
268
+ return this;
269
+ };
270
+ Interpreter.prototype.exit = function () {
271
+ try {
272
+ return this.apiContext.exit();
273
+ }
274
+ catch (err) {
275
+ this.handler.errorHandler.raise(err);
276
+ }
277
+ };
278
+ Interpreter.prototype.setGlobalVariable = function (path, value) {
279
+ if (this.globalContext != null) {
280
+ this.globalContext.set(path, value);
281
+ }
282
+ return this;
283
+ };
284
+ Interpreter.prototype.getGlobalVariable = function (path) {
285
+ if (this.globalContext != null) {
286
+ this.globalContext.get(path);
287
+ }
288
+ return default_1.default.Void;
289
+ };
290
+ return Interpreter;
291
+ }(events_1.default));
292
+ exports.default = Interpreter;
@@ -0,0 +1,7 @@
1
+ import CustomFunction from './types/function';
2
+ export default class IntrinsicsContainer {
3
+ private readonly intrinsics;
4
+ add(name: string, fn: CustomFunction): IntrinsicsContainer;
5
+ has(name: string): boolean;
6
+ get(name: string): CustomFunction;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var IntrinsicsContainer = /** @class */ (function () {
4
+ function IntrinsicsContainer() {
5
+ this.intrinsics = new Map();
6
+ }
7
+ IntrinsicsContainer.prototype.add = function (name, fn) {
8
+ this.intrinsics.set(name, fn);
9
+ return this;
10
+ };
11
+ IntrinsicsContainer.prototype.has = function (name) {
12
+ return this.intrinsics.has(name);
13
+ };
14
+ IntrinsicsContainer.prototype.get = function (name) {
15
+ return this.intrinsics.get(name) || null;
16
+ };
17
+ return IntrinsicsContainer;
18
+ }());
19
+ exports.default = IntrinsicsContainer;
@@ -0,0 +1,13 @@
1
+ import { ASTAssignmentStatement } from 'greyscript-core';
2
+ import OperationContext from '../context';
3
+ import { CustomValue } from '../types/generics';
4
+ import Operation, { CPSVisit } from './operation';
5
+ import Resolve from './resolve';
6
+ export default class Assign extends Operation {
7
+ readonly item: ASTAssignmentStatement;
8
+ left: Resolve;
9
+ right: Operation;
10
+ constructor(item: ASTAssignmentStatement, target?: string);
11
+ build(visit: CPSVisit): Promise<Operation>;
12
+ handle(ctx: OperationContext): Promise<CustomValue>;
13
+ }