greybel-interpreter 3.5.1 → 4.0.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.
Files changed (116) hide show
  1. package/dist/byte-compiler/instruction.d.ts +129 -0
  2. package/dist/byte-compiler/instruction.js +60 -0
  3. package/dist/bytecode-generator.d.ts +61 -0
  4. package/dist/bytecode-generator.js +1165 -0
  5. package/dist/context.d.ts +20 -88
  6. package/dist/context.js +44 -273
  7. package/dist/index.d.ts +5 -29
  8. package/dist/index.js +14 -87
  9. package/dist/interpreter.d.ts +11 -12
  10. package/dist/interpreter.js +75 -87
  11. package/dist/types/function.d.ts +11 -25
  12. package/dist/types/function.js +41 -105
  13. package/dist/types/list.d.ts +4 -5
  14. package/dist/types/list.js +10 -52
  15. package/dist/types/map.d.ts +4 -5
  16. package/dist/types/map.js +15 -74
  17. package/dist/types/number.d.ts +4 -5
  18. package/dist/types/number.js +5 -12
  19. package/dist/types/string.d.ts +4 -5
  20. package/dist/types/string.js +6 -18
  21. package/dist/types/with-intrinsics.d.ts +4 -5
  22. package/dist/utils/error.d.ts +5 -5
  23. package/dist/utils/error.js +4 -5
  24. package/dist/utils/stack.d.ts +10 -0
  25. package/dist/utils/stack.js +30 -0
  26. package/dist/vm/evaluation.d.ts +26 -0
  27. package/dist/vm/evaluation.js +341 -0
  28. package/dist/vm.d.ts +66 -0
  29. package/dist/vm.js +662 -0
  30. package/package.json +1 -1
  31. package/dist/cps.d.ts +0 -16
  32. package/dist/cps.js +0 -202
  33. package/dist/operations/assign-globals.d.ts +0 -11
  34. package/dist/operations/assign-globals.js +0 -31
  35. package/dist/operations/assign-locals.d.ts +0 -11
  36. package/dist/operations/assign-locals.js +0 -31
  37. package/dist/operations/assign-outer.d.ts +0 -11
  38. package/dist/operations/assign-outer.js +0 -31
  39. package/dist/operations/assign-self.d.ts +0 -11
  40. package/dist/operations/assign-self.js +0 -41
  41. package/dist/operations/assign.d.ts +0 -13
  42. package/dist/operations/assign.js +0 -60
  43. package/dist/operations/block.d.ts +0 -13
  44. package/dist/operations/block.js +0 -48
  45. package/dist/operations/break.d.ts +0 -10
  46. package/dist/operations/break.js +0 -21
  47. package/dist/operations/call.d.ts +0 -13
  48. package/dist/operations/call.js +0 -64
  49. package/dist/operations/chunk.d.ts +0 -12
  50. package/dist/operations/chunk.js +0 -31
  51. package/dist/operations/continue.d.ts +0 -10
  52. package/dist/operations/continue.js +0 -21
  53. package/dist/operations/debugger-statement.d.ts +0 -10
  54. package/dist/operations/debugger-statement.js +0 -19
  55. package/dist/operations/envar.d.ts +0 -10
  56. package/dist/operations/envar.js +0 -21
  57. package/dist/operations/evaluate.d.ts +0 -45
  58. package/dist/operations/evaluate.js +0 -369
  59. package/dist/operations/for.d.ts +0 -14
  60. package/dist/operations/for.js +0 -87
  61. package/dist/operations/function-reference.d.ts +0 -12
  62. package/dist/operations/function-reference.js +0 -48
  63. package/dist/operations/function.d.ts +0 -19
  64. package/dist/operations/function.js +0 -84
  65. package/dist/operations/if-statement.d.ts +0 -19
  66. package/dist/operations/if-statement.js +0 -87
  67. package/dist/operations/import.d.ts +0 -20
  68. package/dist/operations/import.js +0 -56
  69. package/dist/operations/include.d.ts +0 -14
  70. package/dist/operations/include.js +0 -40
  71. package/dist/operations/list.d.ts +0 -11
  72. package/dist/operations/list.js +0 -40
  73. package/dist/operations/literal.d.ts +0 -11
  74. package/dist/operations/literal.js +0 -38
  75. package/dist/operations/map.d.ts +0 -11
  76. package/dist/operations/map.js +0 -42
  77. package/dist/operations/negated-binary.d.ts +0 -11
  78. package/dist/operations/negated-binary.js +0 -40
  79. package/dist/operations/new-instance.d.ts +0 -11
  80. package/dist/operations/new-instance.js +0 -37
  81. package/dist/operations/noop.d.ts +0 -7
  82. package/dist/operations/noop.js +0 -14
  83. package/dist/operations/not.d.ts +0 -11
  84. package/dist/operations/not.js +0 -33
  85. package/dist/operations/operation.d.ts +0 -15
  86. package/dist/operations/operation.js +0 -13
  87. package/dist/operations/reference-globals.d.ts +0 -7
  88. package/dist/operations/reference-globals.js +0 -13
  89. package/dist/operations/reference-locals.d.ts +0 -7
  90. package/dist/operations/reference-locals.js +0 -13
  91. package/dist/operations/reference-outer.d.ts +0 -7
  92. package/dist/operations/reference-outer.js +0 -13
  93. package/dist/operations/reference-self.d.ts +0 -7
  94. package/dist/operations/reference-self.js +0 -13
  95. package/dist/operations/reference.d.ts +0 -9
  96. package/dist/operations/reference.js +0 -17
  97. package/dist/operations/resolve-globals.d.ts +0 -5
  98. package/dist/operations/resolve-globals.js +0 -24
  99. package/dist/operations/resolve-locals.d.ts +0 -5
  100. package/dist/operations/resolve-locals.js +0 -24
  101. package/dist/operations/resolve-outer.d.ts +0 -5
  102. package/dist/operations/resolve-outer.js +0 -24
  103. package/dist/operations/resolve-self.d.ts +0 -5
  104. package/dist/operations/resolve-self.js +0 -24
  105. package/dist/operations/resolve.d.ts +0 -55
  106. package/dist/operations/resolve.js +0 -269
  107. package/dist/operations/return.d.ts +0 -11
  108. package/dist/operations/return.js +0 -43
  109. package/dist/operations/while.d.ts +0 -13
  110. package/dist/operations/while.js +0 -70
  111. package/dist/utils/create-assign.d.ts +0 -3
  112. package/dist/utils/create-assign.js +0 -25
  113. package/dist/utils/create-resolve.d.ts +0 -5
  114. package/dist/utils/create-resolve.js +0 -51
  115. package/dist/utils/path.d.ts +0 -10
  116. package/dist/utils/path.js +0 -33
package/dist/vm.js ADDED
@@ -0,0 +1,662 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.VM = exports.VMState = exports.Debugger = void 0;
16
+ const context_1 = require("./context");
17
+ const instruction_1 = require("./byte-compiler/instruction");
18
+ const default_1 = require("./types/default");
19
+ const stack_1 = require("./utils/stack");
20
+ const nil_1 = require("./types/nil");
21
+ const error_1 = require("./utils/error");
22
+ const with_intrinsics_1 = require("./types/with-intrinsics");
23
+ const boolean_1 = require("./types/boolean");
24
+ const function_1 = require("./types/function");
25
+ const evaluation_1 = require("./vm/evaluation");
26
+ const number_1 = require("./types/number");
27
+ const map_1 = require("./types/map");
28
+ const list_1 = require("./types/list");
29
+ const next_tick_1 = require("./utils/next-tick");
30
+ const string_1 = require("./types/string");
31
+ const events_1 = __importDefault(require("events"));
32
+ const object_value_1 = require("./utils/object-value");
33
+ class Debugger {
34
+ constructor() {
35
+ this.breakpoint = false;
36
+ this.nextStep = false;
37
+ }
38
+ debug(...segments) {
39
+ console.debug(...segments);
40
+ return default_1.DefaultType.Void;
41
+ }
42
+ setBreakpoint(breakpoint) {
43
+ this.breakpoint = breakpoint;
44
+ return this;
45
+ }
46
+ getBreakpoint(vm) {
47
+ return this.breakpoint;
48
+ }
49
+ next() {
50
+ this.nextStep = true;
51
+ return this;
52
+ }
53
+ resume() {
54
+ if (!this.breakpoint) {
55
+ return Promise.resolve();
56
+ }
57
+ return new Promise((resolve) => {
58
+ const check = () => {
59
+ if (!this.breakpoint) {
60
+ resolve();
61
+ }
62
+ else if (this.nextStep) {
63
+ this.nextStep = false;
64
+ resolve();
65
+ }
66
+ else {
67
+ setImmediate(check);
68
+ }
69
+ };
70
+ check();
71
+ });
72
+ }
73
+ interact(vm, _source) {
74
+ const me = this;
75
+ console.warn('Debugger is not setup.');
76
+ console.info(vm);
77
+ me.breakpoint = false;
78
+ }
79
+ }
80
+ exports.Debugger = Debugger;
81
+ var VMState;
82
+ (function (VMState) {
83
+ VMState[VMState["PREPARATION"] = 0] = "PREPARATION";
84
+ VMState[VMState["PENDING"] = 1] = "PENDING";
85
+ VMState[VMState["FINISHED"] = 2] = "FINISHED";
86
+ })(VMState = exports.VMState || (exports.VMState = {}));
87
+ class VM {
88
+ constructor(options) {
89
+ var _a, _b;
90
+ this.ACTIONS_PER_LOOP = 120;
91
+ this.STACK_LIMIT = 512;
92
+ this.stack = new Array(this.STACK_LIMIT);
93
+ this.signal = new events_1.default();
94
+ this.state = VMState.PREPARATION;
95
+ this.actionCount = 0;
96
+ this.sp = 0;
97
+ this.time = -1;
98
+ this.target = options.target;
99
+ this.frames = new stack_1.Stack(options.globals);
100
+ this.contextTypeIntrinsics = options.contextTypeIntrinsics;
101
+ this.handler = options.handler;
102
+ this.debugger = options.debugger;
103
+ this.environmentVariables = (_a = options.environmentVariables) !== null && _a !== void 0 ? _a : new Map();
104
+ this.iterators = new stack_1.Stack();
105
+ this.imports = (_b = options.imports) !== null && _b !== void 0 ? _b : new Map();
106
+ }
107
+ getTime() {
108
+ return this.time;
109
+ }
110
+ isPending() {
111
+ return this.state === VMState.PENDING;
112
+ }
113
+ getStacktrace() {
114
+ const frames = this.frames.values();
115
+ const stacktrace = [];
116
+ for (let index = frames.length - 1; index >= 0; index--) {
117
+ stacktrace.push(frames[index].getCurrentInstruction());
118
+ }
119
+ return stacktrace;
120
+ }
121
+ getOpenHandles() {
122
+ return this.sp;
123
+ }
124
+ exit() {
125
+ this.signal.emit('exit');
126
+ }
127
+ getFrame() {
128
+ return this.frames.peek();
129
+ }
130
+ pushStack(value) {
131
+ this.stack[this.sp++] = value;
132
+ }
133
+ popStack() {
134
+ return this.stack[--this.sp];
135
+ }
136
+ createFrame(options) {
137
+ const ctx = this.getFrame().fork({
138
+ code: options.code,
139
+ type: context_1.ContextType.Function,
140
+ self: options.self,
141
+ super: options.super,
142
+ outer: options.outer
143
+ });
144
+ this.frames.push(ctx);
145
+ return ctx;
146
+ }
147
+ popFrame() {
148
+ return this.frames.pop();
149
+ }
150
+ exec() {
151
+ var _a, _b, _c, _d, _e, _f, _g, _h;
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ let shouldContinue = true;
154
+ const exitCallback = () => {
155
+ shouldContinue = false;
156
+ };
157
+ this.time = Date.now();
158
+ this.state = VMState.PENDING;
159
+ this.signal.once('exit', exitCallback);
160
+ while (shouldContinue) {
161
+ const frame = this.getFrame();
162
+ if (frame.code.length === frame.ip) {
163
+ this.popFrame();
164
+ continue;
165
+ }
166
+ const instruction = frame.code[frame.ip++];
167
+ switch (instruction.op) {
168
+ case instruction_1.OpCode.NOOP: {
169
+ continue;
170
+ }
171
+ case instruction_1.OpCode.PUSH: {
172
+ const pushInstruction = instruction;
173
+ this.pushStack(pushInstruction.value);
174
+ break;
175
+ }
176
+ case instruction_1.OpCode.POP: {
177
+ this.popStack();
178
+ break;
179
+ }
180
+ case instruction_1.OpCode.GET_GLOBALS: {
181
+ this.pushStack(frame.globals.scope);
182
+ break;
183
+ }
184
+ case instruction_1.OpCode.GET_OUTER: {
185
+ this.pushStack(((_a = frame.outer) !== null && _a !== void 0 ? _a : frame.globals).scope);
186
+ break;
187
+ }
188
+ case instruction_1.OpCode.GET_LOCALS: {
189
+ this.pushStack(frame.locals.scope);
190
+ break;
191
+ }
192
+ case instruction_1.OpCode.GET_SELF: {
193
+ this.pushStack((_b = frame.self) !== null && _b !== void 0 ? _b : default_1.DefaultType.Void);
194
+ break;
195
+ }
196
+ case instruction_1.OpCode.GET_SUPER: {
197
+ this.pushStack((_c = frame.super) !== null && _c !== void 0 ? _c : default_1.DefaultType.Void);
198
+ break;
199
+ }
200
+ case instruction_1.OpCode.IMPORT: {
201
+ const importInstruction = instruction;
202
+ const code = this.imports.get(importInstruction.path);
203
+ this.createFrame({ code });
204
+ break;
205
+ }
206
+ case instruction_1.OpCode.ASSIGN: {
207
+ const value = this.popStack();
208
+ const key = this.popStack();
209
+ const base = this.popStack();
210
+ if (!(base instanceof with_intrinsics_1.CustomValueWithIntrinsics)) {
211
+ throw new error_1.RuntimeError(`Base left side must be a value with intrinsics!`, this);
212
+ }
213
+ else if (key instanceof nil_1.CustomNil) {
214
+ throw new error_1.RuntimeError(`Key left side cannot be null!`, this);
215
+ }
216
+ base.set(key, value);
217
+ break;
218
+ }
219
+ case instruction_1.OpCode.FUNCTION_DEFINITION: {
220
+ const functionInstruction = instruction;
221
+ const fn = new function_1.CustomFunction('anonymous', functionInstruction.code, functionInstruction.arguments, functionInstruction.ignoreOuter ? frame.globals : frame);
222
+ this.pushStack(fn);
223
+ break;
224
+ }
225
+ case instruction_1.OpCode.CALL: {
226
+ const callInstruction = instruction;
227
+ const args = new Array(callInstruction.length);
228
+ for (let i = 0; i < callInstruction.length; i++) {
229
+ args[i] = this.popStack();
230
+ }
231
+ const fn = this.popStack();
232
+ if (fn instanceof function_1.CustomFunction) {
233
+ if (callInstruction.length > fn.arguments.length) {
234
+ throw new Error('Too many arguments.');
235
+ }
236
+ const newFrame = this.createFrame({ code: fn.value, outer: fn.outer });
237
+ const argsCount = args.length;
238
+ for (let index = 0; index < argsCount; index++) {
239
+ const argument = args.shift();
240
+ const paramNum = argsCount - 1 - index;
241
+ if (paramNum >= fn.arguments.length) {
242
+ throw new Error('Too many arguments.');
243
+ }
244
+ const param = fn.arguments[paramNum].name;
245
+ if (param.toString() == "self") {
246
+ newFrame.self = argument;
247
+ newFrame.super = argument instanceof map_1.CustomMap ? ((_d = argument.getIsa()) !== null && _d !== void 0 ? _d : new map_1.CustomMap()) : null;
248
+ }
249
+ else {
250
+ newFrame.set(param, argument);
251
+ }
252
+ }
253
+ for (let paramNum = argsCount; paramNum < fn.arguments.length; paramNum++) {
254
+ newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
255
+ }
256
+ newFrame.injectContext();
257
+ }
258
+ break;
259
+ }
260
+ case instruction_1.OpCode.CALL_WITH_CONTEXT: {
261
+ const callInstruction = instruction;
262
+ const args = new Array(callInstruction.length);
263
+ for (let i = 0; i < callInstruction.length; i++) {
264
+ args[i] = this.popStack();
265
+ }
266
+ const propertyName = this.popStack();
267
+ const context = this.popStack();
268
+ const fn = context.get(propertyName, this.contextTypeIntrinsics);
269
+ if (fn instanceof function_1.CustomFunction) {
270
+ const newFrame = this.createFrame({
271
+ code: fn.value,
272
+ self: context,
273
+ super: context instanceof map_1.CustomMap ? ((_e = context.getIsa()) !== null && _e !== void 0 ? _e : new map_1.CustomMap()) : null,
274
+ outer: fn.outer
275
+ });
276
+ const argsCount = args.length;
277
+ let selfParam = fn.arguments.length > 0 && fn.arguments[0].name.toString() == "self" ? 1 : 0;
278
+ for (let index = 0; index < argsCount; index++) {
279
+ const argument = args.shift();
280
+ const paramNum = argsCount - 1 - index + selfParam;
281
+ if (paramNum >= fn.arguments.length) {
282
+ throw new Error('Too many arguments.');
283
+ }
284
+ const param = fn.arguments[paramNum].name;
285
+ if (param.toString() !== "self")
286
+ newFrame.set(param, argument);
287
+ }
288
+ for (let paramNum = argsCount + selfParam; paramNum < fn.arguments.length; paramNum++) {
289
+ newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
290
+ }
291
+ newFrame.injectContext();
292
+ }
293
+ break;
294
+ }
295
+ case instruction_1.OpCode.CALL_SUPER_PROPERTY: {
296
+ const callInstruction = instruction;
297
+ const args = new Array(callInstruction.length);
298
+ for (let i = 0; i < callInstruction.length; i++) {
299
+ args[i] = this.popStack();
300
+ }
301
+ const property = this.popStack();
302
+ const context = frame.super;
303
+ if (!(context instanceof with_intrinsics_1.CustomValueWithIntrinsics)) {
304
+ throw new error_1.RuntimeError(`Unknown path ${property.toString()}.`, this);
305
+ }
306
+ const ret = context.getWithOrigin(property, this.contextTypeIntrinsics);
307
+ const fn = ret.value;
308
+ if (fn instanceof function_1.CustomFunction) {
309
+ const newFrame = this.createFrame({
310
+ code: fn.value,
311
+ self: frame.self,
312
+ super: ret.origin instanceof map_1.CustomMap ? ((_f = ret.origin.getIsa()) !== null && _f !== void 0 ? _f : new map_1.CustomMap()) : null,
313
+ outer: fn.outer
314
+ });
315
+ let selfParam = fn.arguments.length > 0 && fn.arguments[0].name.toString() == "self" ? 1 : 0;
316
+ const argsCount = args.length;
317
+ for (let index = 0; index < argsCount; index++) {
318
+ const argument = args.shift();
319
+ const paramNum = argsCount - 1 - index + selfParam;
320
+ if (paramNum >= fn.arguments.length) {
321
+ throw new Error('Too many arguments.');
322
+ }
323
+ const param = fn.arguments[paramNum].name;
324
+ if (param.toString() !== "self")
325
+ newFrame.set(param, argument);
326
+ }
327
+ for (let paramNum = argsCount + selfParam; paramNum < fn.arguments.length; paramNum++) {
328
+ newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
329
+ }
330
+ newFrame.injectContext();
331
+ }
332
+ break;
333
+ }
334
+ case instruction_1.OpCode.CONSTRUCT_MAP: {
335
+ const mapConstructInstruction = instruction;
336
+ const map = [];
337
+ for (let index = 0; index < mapConstructInstruction.length; index++) {
338
+ const value = this.popStack();
339
+ const key = this.popStack();
340
+ map.unshift([key, value]);
341
+ }
342
+ this.pushStack(new map_1.CustomMap(new object_value_1.ObjectValue(map)));
343
+ break;
344
+ }
345
+ case instruction_1.OpCode.CONSTRUCT_LIST: {
346
+ const listConstructInstruction = instruction;
347
+ const list = [];
348
+ for (let index = 0; index < listConstructInstruction.length; index++) {
349
+ const value = this.popStack();
350
+ list.unshift(value);
351
+ }
352
+ this.pushStack(new list_1.CustomList(list));
353
+ break;
354
+ }
355
+ case instruction_1.OpCode.GOTO_A_IF_FALSE: {
356
+ const condition = this.popStack();
357
+ if (condition.toTruthy()) {
358
+ break;
359
+ }
360
+ const gotoAInstruction = instruction;
361
+ frame.ip = gotoAInstruction.goto.ip;
362
+ break;
363
+ }
364
+ case instruction_1.OpCode.GOTO_A: {
365
+ const gotoAInstruction = instruction;
366
+ frame.ip = gotoAInstruction.goto.ip;
367
+ break;
368
+ }
369
+ case instruction_1.OpCode.GOTO_A_IF_FALSE: {
370
+ const condition = this.popStack();
371
+ if (condition.toTruthy()) {
372
+ break;
373
+ }
374
+ const gotoAInstruction = instruction;
375
+ frame.ip = gotoAInstruction.goto.ip;
376
+ break;
377
+ }
378
+ case instruction_1.OpCode.PUSH_ITERATOR: {
379
+ const value = this.popStack();
380
+ const iterator = value[Symbol.iterator]();
381
+ this.iterators.push(iterator);
382
+ break;
383
+ }
384
+ case instruction_1.OpCode.POP_ITERATOR: {
385
+ this.iterators.pop();
386
+ break;
387
+ }
388
+ case instruction_1.OpCode.NEXT: {
389
+ const nextInstruction = instruction;
390
+ let idx = frame.get(nextInstruction.idxVariable, this.contextTypeIntrinsics).toNumber();
391
+ const iterator = this.iterators.peek();
392
+ iterator.index = ++idx;
393
+ const iteratorResult = iterator.next();
394
+ this.pushStack(new boolean_1.CustomBoolean(!iteratorResult.done));
395
+ if (!iteratorResult.done) {
396
+ frame.set(nextInstruction.variable, iteratorResult.value);
397
+ frame.set(nextInstruction.idxVariable, new number_1.CustomNumber(idx));
398
+ }
399
+ break;
400
+ }
401
+ case instruction_1.OpCode.GET_VARIABLE: {
402
+ const getVariableInstroduction = instruction;
403
+ const ret = frame.locals.get(getVariableInstroduction.property, this.contextTypeIntrinsics);
404
+ if (ret instanceof function_1.CustomFunction && getVariableInstroduction.invoke) {
405
+ const newFrame = this.createFrame({
406
+ code: ret.value,
407
+ outer: ret.outer
408
+ });
409
+ for (let paramNum = 0; paramNum < ret.arguments.length; paramNum++) {
410
+ newFrame.set(ret.arguments[paramNum].name, ret.arguments[paramNum].defaultValue);
411
+ }
412
+ newFrame.injectContext();
413
+ break;
414
+ }
415
+ this.pushStack(ret);
416
+ break;
417
+ }
418
+ case instruction_1.OpCode.GET_PROPERTY: {
419
+ const getPropertyInstruction = instruction;
420
+ const property = this.popStack();
421
+ const context = this.popStack();
422
+ if (!(context instanceof with_intrinsics_1.CustomValueWithIntrinsics)) {
423
+ throw new error_1.RuntimeError(`Unknown path ${property.toString()}.`, this);
424
+ }
425
+ const ret = context.getWithOrigin(property, this.contextTypeIntrinsics);
426
+ if (ret.value instanceof function_1.CustomFunction && getPropertyInstruction.invoke) {
427
+ const newFrame = this.createFrame({
428
+ code: ret.value.value,
429
+ self: context,
430
+ super: ret.origin instanceof map_1.CustomMap ? ((_g = ret.origin.getIsa()) !== null && _g !== void 0 ? _g : new map_1.CustomMap()) : null,
431
+ outer: ret.value.outer
432
+ });
433
+ for (let paramNum = 0; paramNum < ret.value.arguments.length; paramNum++) {
434
+ newFrame.set(ret.value.arguments[paramNum].name, ret.value.arguments[paramNum].defaultValue);
435
+ }
436
+ newFrame.injectContext();
437
+ break;
438
+ }
439
+ this.pushStack(ret.value);
440
+ break;
441
+ }
442
+ case instruction_1.OpCode.GET_SUPER_PROPERTY: {
443
+ const getPropertyInstruction = instruction;
444
+ const property = this.popStack();
445
+ const ret = frame.super.getWithOrigin(property, this.contextTypeIntrinsics);
446
+ if (ret.value instanceof function_1.CustomFunction && getPropertyInstruction.invoke) {
447
+ const newFrame = this.createFrame({
448
+ code: ret.value.value,
449
+ self: frame.self,
450
+ super: ret.origin instanceof map_1.CustomMap ? ((_h = ret.origin.getIsa()) !== null && _h !== void 0 ? _h : new map_1.CustomMap()) : null,
451
+ outer: ret.value.outer
452
+ });
453
+ for (let paramNum = 0; paramNum < ret.value.arguments.length; paramNum++) {
454
+ newFrame.set(ret.value.arguments[paramNum].name, ret.value.arguments[paramNum].defaultValue);
455
+ }
456
+ newFrame.injectContext();
457
+ break;
458
+ }
459
+ this.pushStack(ret.value);
460
+ break;
461
+ }
462
+ case instruction_1.OpCode.CALL_INTERNAL: {
463
+ const callInstruction = instruction;
464
+ const args = new Map();
465
+ const callback = callInstruction.callback;
466
+ for (const arg of callInstruction.arguments) {
467
+ const value = frame.scope.value.get(arg.name);
468
+ args.set(arg.name.toString(), value);
469
+ }
470
+ this.pushStack(yield callback(this, frame.self, args));
471
+ break;
472
+ }
473
+ case instruction_1.OpCode.SLICE: {
474
+ const b = this.popStack();
475
+ const a = this.popStack();
476
+ const value = this.popStack();
477
+ if (value instanceof list_1.CustomList || value instanceof string_1.CustomString) {
478
+ this.pushStack(value.slice(a, b));
479
+ }
480
+ else {
481
+ this.pushStack(default_1.DefaultType.Void);
482
+ }
483
+ break;
484
+ }
485
+ case instruction_1.OpCode.NEW: {
486
+ const value = this.popStack();
487
+ if (value instanceof map_1.CustomMap) {
488
+ this.pushStack(value.createInstance());
489
+ }
490
+ else {
491
+ this.pushStack(value);
492
+ }
493
+ break;
494
+ }
495
+ case instruction_1.OpCode.NEGATE: {
496
+ const value = this.popStack();
497
+ this.pushStack(new number_1.CustomNumber(-value.toNumber()));
498
+ break;
499
+ }
500
+ case instruction_1.OpCode.FALSIFY: {
501
+ const value = this.popStack();
502
+ this.pushStack(new boolean_1.CustomBoolean(!value.toTruthy()));
503
+ break;
504
+ }
505
+ case instruction_1.OpCode.ISA: {
506
+ const b = this.popStack();
507
+ const a = this.popStack();
508
+ this.pushStack(new boolean_1.CustomBoolean(a.instanceOf(b, this.contextTypeIntrinsics)));
509
+ break;
510
+ }
511
+ case instruction_1.OpCode.BITWISE_AND: {
512
+ const b = this.popStack();
513
+ const a = this.popStack();
514
+ this.pushStack((0, evaluation_1.evalBitwiseAnd)(a, b));
515
+ break;
516
+ }
517
+ case instruction_1.OpCode.BITWISE_OR: {
518
+ const b = this.popStack();
519
+ const a = this.popStack();
520
+ this.pushStack((0, evaluation_1.evalBitwiseOr)(a, b));
521
+ break;
522
+ }
523
+ case instruction_1.OpCode.BITWISE_LEFT_SHIFT: {
524
+ const b = this.popStack();
525
+ const a = this.popStack();
526
+ this.pushStack((0, evaluation_1.evalBitwiseLeftShift)(a, b));
527
+ break;
528
+ }
529
+ case instruction_1.OpCode.BITWISE_RIGHT_SHIFT: {
530
+ const b = this.popStack();
531
+ const a = this.popStack();
532
+ this.pushStack((0, evaluation_1.evalBitwiseRightShift)(a, b));
533
+ break;
534
+ }
535
+ case instruction_1.OpCode.BITWISE_UNSIGNED_RIGHT_SHIFT: {
536
+ const b = this.popStack();
537
+ const a = this.popStack();
538
+ this.pushStack((0, evaluation_1.evalBitwiseUnsignedRightShift)(a, b));
539
+ break;
540
+ }
541
+ case instruction_1.OpCode.ADD: {
542
+ const b = this.popStack();
543
+ const a = this.popStack();
544
+ this.pushStack((0, evaluation_1.evalAdd)(a, b));
545
+ break;
546
+ }
547
+ case instruction_1.OpCode.SUB: {
548
+ const b = this.popStack();
549
+ const a = this.popStack();
550
+ this.pushStack((0, evaluation_1.evalSub)(a, b));
551
+ break;
552
+ }
553
+ case instruction_1.OpCode.MUL: {
554
+ const b = this.popStack();
555
+ const a = this.popStack();
556
+ this.pushStack((0, evaluation_1.evalMul)(a, b));
557
+ break;
558
+ }
559
+ case instruction_1.OpCode.DIV: {
560
+ const b = this.popStack();
561
+ const a = this.popStack();
562
+ this.pushStack((0, evaluation_1.evalDiv)(a, b));
563
+ break;
564
+ }
565
+ case instruction_1.OpCode.MOD: {
566
+ const b = this.popStack();
567
+ const a = this.popStack();
568
+ this.pushStack((0, evaluation_1.evalMod)(a, b));
569
+ break;
570
+ }
571
+ case instruction_1.OpCode.POW: {
572
+ const b = this.popStack();
573
+ const a = this.popStack();
574
+ this.pushStack((0, evaluation_1.evalPow)(a, b));
575
+ break;
576
+ }
577
+ case instruction_1.OpCode.EQUAL: {
578
+ const b = this.popStack();
579
+ const a = this.popStack();
580
+ this.pushStack((0, evaluation_1.evalEqual)(a, b));
581
+ break;
582
+ }
583
+ case instruction_1.OpCode.NOT_EQUAL: {
584
+ const b = this.popStack();
585
+ const a = this.popStack();
586
+ this.pushStack((0, evaluation_1.evalNotEqual)(a, b));
587
+ break;
588
+ }
589
+ case instruction_1.OpCode.GREATER_THAN: {
590
+ const b = this.popStack();
591
+ const a = this.popStack();
592
+ this.pushStack((0, evaluation_1.evalGreaterThan)(a, b));
593
+ break;
594
+ }
595
+ case instruction_1.OpCode.GREATER_THAN_OR_EQUAL: {
596
+ const b = this.popStack();
597
+ const a = this.popStack();
598
+ this.pushStack((0, evaluation_1.evalGreaterThanOrEqual)(a, b));
599
+ break;
600
+ }
601
+ case instruction_1.OpCode.LESS_THAN: {
602
+ const b = this.popStack();
603
+ const a = this.popStack();
604
+ this.pushStack((0, evaluation_1.evalLessThan)(a, b));
605
+ break;
606
+ }
607
+ case instruction_1.OpCode.LESS_THAN_OR_EQUAL: {
608
+ const b = this.popStack();
609
+ const a = this.popStack();
610
+ this.pushStack((0, evaluation_1.evalLessThanOrEqual)(a, b));
611
+ break;
612
+ }
613
+ case instruction_1.OpCode.AND: {
614
+ const b = this.popStack();
615
+ const a = this.popStack();
616
+ this.pushStack((0, evaluation_1.evalAnd)(a, b));
617
+ break;
618
+ }
619
+ case instruction_1.OpCode.OR: {
620
+ const b = this.popStack();
621
+ const a = this.popStack();
622
+ this.pushStack((0, evaluation_1.evalOr)(a, b));
623
+ break;
624
+ }
625
+ case instruction_1.OpCode.RETURN: {
626
+ const value = this.popStack();
627
+ this.popFrame();
628
+ this.pushStack(value);
629
+ break;
630
+ }
631
+ case instruction_1.OpCode.GET_ENVAR: {
632
+ const key = this.popStack();
633
+ const value = this.environmentVariables.get(key.toString());
634
+ this.pushStack(new string_1.CustomString(value));
635
+ break;
636
+ }
637
+ case instruction_1.OpCode.BREAKPOINT: {
638
+ if (this.debugger.getBreakpoint(this)) {
639
+ this.debugger.interact(this, instruction.source);
640
+ yield this.debugger.resume();
641
+ }
642
+ break;
643
+ }
644
+ case instruction_1.OpCode.BREAKPOINT_ENABLE: {
645
+ this.debugger.setBreakpoint(true);
646
+ break;
647
+ }
648
+ case instruction_1.OpCode.HALT: {
649
+ this.state = VMState.FINISHED;
650
+ this.signal.off('exit', exitCallback);
651
+ return;
652
+ }
653
+ }
654
+ if (this.actionCount++ === this.ACTIONS_PER_LOOP) {
655
+ this.actionCount = 0;
656
+ yield (0, next_tick_1.nextTick)();
657
+ }
658
+ }
659
+ });
660
+ }
661
+ }
662
+ exports.VM = VM;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "3.5.1",
3
+ "version": "4.0.1",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
package/dist/cps.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { ASTBase } from 'miniscript-core';
2
- import { HandlerContainer } from './handler-container';
3
- import { Operation } from './operations/operation';
4
- export declare class CPSContext {
5
- readonly target: string;
6
- readonly handler: HandlerContainer;
7
- constructor(target: string, handler: HandlerContainer);
8
- }
9
- export type CPSVisitCallback = (cpsVisit: CPSVisitCallback, context: CPSContext, stack: string[], item: ASTBase) => Promise<Operation>;
10
- export declare const defaultCPSVisit: CPSVisitCallback;
11
- export declare class CPS {
12
- private readonly context;
13
- private __visit;
14
- constructor(context: CPSContext, cpsVisit?: CPSVisitCallback);
15
- visit(item: ASTBase): Promise<Operation>;
16
- }