greybel-interpreter 4.5.13 → 4.6.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 (42) hide show
  1. package/dist/bytecode-generator/context.d.ts +22 -0
  2. package/dist/bytecode-generator/context.js +31 -0
  3. package/dist/bytecode-generator/expression.d.ts +24 -0
  4. package/dist/bytecode-generator/expression.js +640 -0
  5. package/dist/{byte-compiler → bytecode-generator}/instruction.d.ts +17 -19
  6. package/dist/{byte-compiler → bytecode-generator}/instruction.js +13 -12
  7. package/dist/bytecode-generator/line.d.ts +9 -0
  8. package/dist/bytecode-generator/line.js +2 -0
  9. package/dist/bytecode-generator/models.d.ts +42 -0
  10. package/dist/bytecode-generator/models.js +2 -0
  11. package/dist/bytecode-generator/module.d.ts +25 -0
  12. package/dist/bytecode-generator/module.js +72 -0
  13. package/dist/bytecode-generator/statement.d.ts +31 -0
  14. package/dist/bytecode-generator/statement.js +825 -0
  15. package/dist/bytecode-generator/utils.d.ts +5 -0
  16. package/dist/bytecode-generator/utils.js +30 -0
  17. package/dist/bytecode-generator.d.ts +3 -57
  18. package/dist/bytecode-generator.js +31 -1219
  19. package/dist/context.d.ts +5 -2
  20. package/dist/context.js +15 -13
  21. package/dist/index.d.ts +6 -2
  22. package/dist/index.js +10 -2
  23. package/dist/types/function.d.ts +1 -1
  24. package/dist/types/function.js +2 -2
  25. package/dist/types/map.js +10 -10
  26. package/dist/types/string.js +1 -1
  27. package/dist/utils/error.d.ts +1 -1
  28. package/dist/utils/hash.js +8 -14
  29. package/dist/utils/object-value.d.ts +1 -1
  30. package/dist/utils/object-value.js +9 -9
  31. package/dist/utils/stack.d.ts +1 -0
  32. package/dist/utils/stack.js +3 -0
  33. package/dist/utils/uuid.d.ts +2 -1
  34. package/dist/utils/uuid.js +5 -33
  35. package/dist/utils/value-hash.d.ts +2 -0
  36. package/dist/utils/value-hash.js +15 -0
  37. package/dist/vm/call.js +1 -1
  38. package/dist/vm.d.ts +1 -1
  39. package/dist/vm.js +36 -18
  40. package/package.json +4 -4
  41. /package/dist/{byte-compiler → bytecode-generator}/keywords.d.ts +0 -0
  42. /package/dist/{byte-compiler → bytecode-generator}/keywords.js +0 -0
@@ -10,1239 +10,51 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.BytecodeGenerator = void 0;
13
+ const instruction_1 = require("./bytecode-generator/instruction");
14
+ const context_1 = require("./bytecode-generator/context");
15
+ const statement_1 = require("./bytecode-generator/statement");
13
16
  const greybel_core_1 = require("greybel-core");
14
- const miniscript_core_1 = require("miniscript-core");
15
17
  const error_1 = require("./utils/error");
16
- const instruction_1 = require("./byte-compiler/instruction");
17
- const number_1 = require("./types/number");
18
- const boolean_1 = require("./types/boolean");
19
- const string_1 = require("./types/string");
20
- const default_1 = require("./types/default");
21
- const stack_1 = require("./utils/stack");
22
- const keywords_1 = require("./byte-compiler/keywords");
23
- const path_1 = require("path");
24
- function generateCustomValueFromASTLiteral(node) {
25
- switch (node.type) {
26
- case miniscript_core_1.ASTType.BooleanLiteral:
27
- return new boolean_1.CustomBoolean(node.value);
28
- case miniscript_core_1.ASTType.StringLiteral:
29
- return new string_1.CustomString(node.value);
30
- case miniscript_core_1.ASTType.NumericLiteral:
31
- return new number_1.CustomNumber(node.value);
32
- case miniscript_core_1.ASTType.NilLiteral:
33
- return default_1.DefaultType.Void;
34
- default:
35
- throw new Error('Unexpected literal type.');
36
- }
37
- }
38
- function unwrap(node) {
39
- while (node instanceof miniscript_core_1.ASTParenthesisExpression) {
40
- node = node.expression;
18
+ const parse = function (code) {
19
+ try {
20
+ const parser = new greybel_core_1.Parser(code);
21
+ return parser.parseChunk();
22
+ }
23
+ catch (err) {
24
+ if (err instanceof error_1.PrepareError) {
25
+ this.handler.errorHandler.raise(err);
26
+ }
27
+ else {
28
+ this.handler.errorHandler.raise(new error_1.PrepareError(err.message, {
29
+ range: err.range,
30
+ target: this.context.target.peek()
31
+ }));
32
+ }
41
33
  }
42
- return node;
43
- }
34
+ };
44
35
  class BytecodeGenerator {
45
36
  constructor(options) {
46
- var _a;
47
- this.target = new stack_1.Stack(options.target);
48
37
  this.handler = options.handler;
49
- this.context = new stack_1.Stack({
50
- code: [],
51
- jumpPoints: []
38
+ this.context = new context_1.Context({
39
+ target: options.target,
40
+ handler: options.handler,
41
+ debugMode: options.debugMode
52
42
  });
53
- this.imports = new Map();
54
- this.debugMode = (_a = options.debugMode) !== null && _a !== void 0 ? _a : false;
55
- }
56
- parse(code) {
57
- try {
58
- const parser = new greybel_core_1.Parser(code);
59
- return parser.parseChunk();
60
- }
61
- catch (err) {
62
- if (err instanceof error_1.PrepareError) {
63
- this.handler.errorHandler.raise(err);
64
- }
65
- else {
66
- this.handler.errorHandler.raise(new error_1.PrepareError(err.message, {
67
- range: err.range,
68
- target: this.target.peek()
69
- }));
70
- }
71
- }
72
43
  }
73
44
  compile(code) {
74
45
  return __awaiter(this, void 0, void 0, function* () {
75
- const node = this.parse(code);
76
- yield this.processNode(node);
77
- this.push({
46
+ const statementGenerator = new statement_1.BytecodeStatementGenerator(this.context, parse.bind(this));
47
+ const node = parse.call(this, code);
48
+ yield statementGenerator.process(node);
49
+ const mod = this.context.module.peek();
50
+ mod.pushCode({
78
51
  op: instruction_1.OpCode.HALT,
79
- source: this.getSourceLocation(node)
52
+ source: mod.getSourceLocation(node)
80
53
  });
81
54
  return {
82
- code: this.context.peek().code,
83
- imports: this.imports
84
- };
85
- });
86
- }
87
- getCurrentPointer() {
88
- return this.context.peek().code.length - 1;
89
- }
90
- getSourceLocation(node, name) {
91
- const target = this.target.peek();
92
- return {
93
- name: name !== null && name !== void 0 ? name : node.type,
94
- path: target,
95
- start: node.start,
96
- end: node.end
97
- };
98
- }
99
- getInternalLocation() {
100
- return {
101
- name: 'internal',
102
- path: 'internal',
103
- start: { line: 0, character: 0 },
104
- end: { line: 0, character: 0 }
105
- };
106
- }
107
- pushContext() {
108
- this.context.push({
109
- code: [],
110
- jumpPoints: []
111
- });
112
- }
113
- popContext() {
114
- return this.context.pop();
115
- }
116
- getLastJumpPoint() {
117
- const jumpPoints = this.context.peek().jumpPoints;
118
- if (jumpPoints.length === 0) {
119
- return null;
120
- }
121
- return jumpPoints[jumpPoints.length - 1];
122
- }
123
- pushJumppoint(start, end) {
124
- this.context.peek().jumpPoints.push([start, end]);
125
- }
126
- popJumppoint() {
127
- return this.context.peek().jumpPoints.pop();
128
- }
129
- push(item) {
130
- item.ip = this.getCurrentPointer() + 1;
131
- this.context.peek().code.push(item);
132
- }
133
- processNode(node) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- if (this.debugMode) {
136
- this.push({
137
- op: instruction_1.OpCode.BREAKPOINT,
138
- source: this.getSourceLocation(node)
139
- });
140
- }
141
- switch (node.type) {
142
- case miniscript_core_1.ASTType.MemberExpression:
143
- yield this.processMemberExpression(node, { isCommand: true });
144
- this.push({
145
- op: instruction_1.OpCode.POP,
146
- source: this.getSourceLocation(node)
147
- });
148
- return;
149
- case miniscript_core_1.ASTType.IndexExpression:
150
- yield this.processIndexExpression(node, { isCommand: true });
151
- this.push({
152
- op: instruction_1.OpCode.POP,
153
- source: this.getSourceLocation(node)
154
- });
155
- return;
156
- case miniscript_core_1.ASTType.SliceExpression:
157
- yield this.processSliceExpression(node);
158
- this.push({
159
- op: instruction_1.OpCode.POP,
160
- source: this.getSourceLocation(node)
161
- });
162
- return;
163
- case miniscript_core_1.ASTType.Identifier:
164
- yield this.processIdentifier(node);
165
- this.push({
166
- op: instruction_1.OpCode.POP,
167
- source: this.getSourceLocation(node)
168
- });
169
- return;
170
- case miniscript_core_1.ASTType.AssignmentStatement:
171
- yield this.processAssignmentStatement(node);
172
- return;
173
- case miniscript_core_1.ASTType.Chunk:
174
- const chunk = node;
175
- for (const item of chunk.body) {
176
- yield this.processNode(item);
177
- }
178
- return;
179
- case miniscript_core_1.ASTType.BooleanLiteral:
180
- case miniscript_core_1.ASTType.StringLiteral:
181
- case miniscript_core_1.ASTType.NumericLiteral:
182
- case miniscript_core_1.ASTType.NilLiteral:
183
- yield this.processLiteral(node);
184
- this.push({
185
- op: instruction_1.OpCode.POP,
186
- source: this.getSourceLocation(node)
187
- });
188
- return;
189
- case miniscript_core_1.ASTType.IsaExpression:
190
- case miniscript_core_1.ASTType.BinaryExpression:
191
- case miniscript_core_1.ASTType.LogicalExpression:
192
- yield this.processEvaluationExpression(node);
193
- this.push({
194
- op: instruction_1.OpCode.POP,
195
- source: this.getSourceLocation(node)
196
- });
197
- return;
198
- case miniscript_core_1.ASTType.ReturnStatement:
199
- yield this.processReturn(node);
200
- return;
201
- case miniscript_core_1.ASTType.BreakStatement:
202
- yield this.processBreak(node);
203
- return;
204
- case miniscript_core_1.ASTType.ContinueStatement:
205
- yield this.processContinue(node);
206
- return;
207
- case miniscript_core_1.ASTType.MapConstructorExpression:
208
- yield this.processMapConstructorExpression(node);
209
- this.push({
210
- op: instruction_1.OpCode.POP,
211
- source: this.getSourceLocation(node)
212
- });
213
- return;
214
- case miniscript_core_1.ASTType.ListConstructorExpression:
215
- yield this.processListConstructorExpression(node);
216
- this.push({
217
- op: instruction_1.OpCode.POP,
218
- source: this.getSourceLocation(node)
219
- });
220
- return;
221
- case miniscript_core_1.ASTType.FunctionDeclaration:
222
- yield this.processFunctionDeclaration(node);
223
- this.push({
224
- op: instruction_1.OpCode.POP,
225
- source: this.getSourceLocation(node)
226
- });
227
- return;
228
- case miniscript_core_1.ASTType.WhileStatement:
229
- yield this.processWhileStatement(node);
230
- return;
231
- case miniscript_core_1.ASTType.ParenthesisExpression:
232
- yield this.processNode(node.expression);
233
- return;
234
- case miniscript_core_1.ASTType.BinaryNegatedExpression:
235
- case miniscript_core_1.ASTType.UnaryExpression:
236
- case miniscript_core_1.ASTType.NegationExpression:
237
- yield this.processUnaryExpression(node);
238
- this.push({
239
- op: instruction_1.OpCode.POP,
240
- source: this.getSourceLocation(node)
241
- });
242
- return;
243
- case miniscript_core_1.ASTType.CallStatement:
244
- yield this.processNode(node.expression);
245
- return;
246
- case miniscript_core_1.ASTType.CallExpression:
247
- yield this.processCallExpression(node);
248
- this.push({
249
- op: instruction_1.OpCode.POP,
250
- source: this.getSourceLocation(node)
251
- });
252
- return;
253
- case miniscript_core_1.ASTType.IfStatement:
254
- case miniscript_core_1.ASTType.IfShortcutStatement:
255
- yield this.processIfStatement(node);
256
- return;
257
- case miniscript_core_1.ASTType.ForGenericStatement:
258
- yield this.processForGenericStatement(node);
259
- return;
260
- case miniscript_core_1.ASTType.EmptyExpression:
261
- case miniscript_core_1.ASTType.Comment:
262
- return;
263
- case greybel_core_1.ASTType.FeatureEnvarExpression:
264
- yield this.processEnvarExpression(node);
265
- this.push({
266
- op: instruction_1.OpCode.POP,
267
- source: this.getSourceLocation(node)
268
- });
269
- return;
270
- case greybel_core_1.ASTType.FeatureImportExpression:
271
- yield this.processImportExpression(node);
272
- return;
273
- case greybel_core_1.ASTType.FeatureIncludeExpression:
274
- yield this.processIncludeExpression(node);
275
- return;
276
- case greybel_core_1.ASTType.FeatureDebuggerExpression:
277
- yield this.processDebuggerExpression(node);
278
- return;
279
- default: {
280
- const range = new miniscript_core_1.ASTRange(node.start, node.end);
281
- throw new error_1.PrepareError(`Unexpected AST type ${node.type}`, {
282
- target: this.target.peek(),
283
- range
284
- });
285
- }
286
- }
287
- });
288
- }
289
- processSubNode(node, context) {
290
- return __awaiter(this, void 0, void 0, function* () {
291
- switch (node.type) {
292
- case miniscript_core_1.ASTType.MemberExpression:
293
- yield this.processMemberExpression(node, context);
294
- return;
295
- case miniscript_core_1.ASTType.IndexExpression:
296
- yield this.processIndexExpression(node, context);
297
- return;
298
- case miniscript_core_1.ASTType.SliceExpression:
299
- yield this.processSliceExpression(node);
300
- return;
301
- case miniscript_core_1.ASTType.Identifier:
302
- yield this.processIdentifier(node);
303
- return;
304
- case miniscript_core_1.ASTType.BooleanLiteral:
305
- case miniscript_core_1.ASTType.StringLiteral:
306
- case miniscript_core_1.ASTType.NumericLiteral:
307
- case miniscript_core_1.ASTType.NilLiteral:
308
- yield this.processLiteral(node);
309
- return;
310
- case miniscript_core_1.ASTType.IsaExpression:
311
- case miniscript_core_1.ASTType.BinaryExpression:
312
- case miniscript_core_1.ASTType.LogicalExpression:
313
- yield this.processEvaluationExpression(node);
314
- return;
315
- case miniscript_core_1.ASTType.MapConstructorExpression:
316
- yield this.processMapConstructorExpression(node);
317
- return;
318
- case miniscript_core_1.ASTType.ListConstructorExpression:
319
- yield this.processListConstructorExpression(node);
320
- return;
321
- case miniscript_core_1.ASTType.FunctionDeclaration:
322
- yield this.processFunctionDeclaration(node, context);
323
- return;
324
- case miniscript_core_1.ASTType.ParenthesisExpression:
325
- yield this.processSubNode(node.expression);
326
- return;
327
- case miniscript_core_1.ASTType.BinaryNegatedExpression:
328
- case miniscript_core_1.ASTType.UnaryExpression:
329
- case miniscript_core_1.ASTType.NegationExpression:
330
- yield this.processUnaryExpression(node);
331
- return;
332
- case miniscript_core_1.ASTType.CallStatement:
333
- yield this.processNode(node.expression);
334
- return;
335
- case miniscript_core_1.ASTType.CallExpression:
336
- yield this.processCallExpression(node);
337
- return;
338
- case miniscript_core_1.ASTType.EmptyExpression:
339
- this.push({
340
- op: instruction_1.OpCode.PUSH,
341
- source: this.getSourceLocation(node),
342
- value: default_1.DefaultType.Void
343
- });
344
- return;
345
- case greybel_core_1.ASTType.FeatureEnvarExpression:
346
- yield this.processEnvarExpression(node);
347
- return;
348
- case greybel_core_1.ASTType.FeatureLineExpression:
349
- this.push({
350
- op: instruction_1.OpCode.PUSH,
351
- source: this.getSourceLocation(node),
352
- value: new number_1.CustomNumber(node.start.line)
353
- });
354
- return;
355
- case greybel_core_1.ASTType.FeatureFileExpression:
356
- this.push({
357
- op: instruction_1.OpCode.PUSH,
358
- source: this.getSourceLocation(node),
359
- value: new string_1.CustomString((0, path_1.basename)(this.target.peek()))
360
- });
361
- return;
362
- case miniscript_core_1.ASTType.Comment:
363
- return;
364
- default: {
365
- const range = new miniscript_core_1.ASTRange(node.start, node.end);
366
- throw new error_1.PrepareError(`Unexpected AST type ${node.type}`, {
367
- target: this.target.peek(),
368
- range
369
- });
370
- }
371
- }
372
- });
373
- }
374
- processMemberExpression(node, context) {
375
- return __awaiter(this, void 0, void 0, function* () {
376
- const base = unwrap(node.base);
377
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
378
- this.push({
379
- op: instruction_1.OpCode.PUSH,
380
- source: this.getSourceLocation(node.identifier),
381
- value: new string_1.CustomString(node.identifier.name)
382
- });
383
- this.push({
384
- op: instruction_1.OpCode.GET_SUPER_PROPERTY,
385
- source: this.getSourceLocation(node.identifier),
386
- invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
387
- });
388
- }
389
- else {
390
- yield this.processSubNode(base, { isCommand: !!(context === null || context === void 0 ? void 0 : context.isCommand) });
391
- if (node.identifier instanceof miniscript_core_1.ASTIdentifier) {
392
- yield this.processIdentifier(node.identifier, { isDescending: true, isReference: !!(context === null || context === void 0 ? void 0 : context.isReference) });
393
- }
394
- else {
395
- yield this.processSubNode(node.identifier, { isCommand: !!(context === null || context === void 0 ? void 0 : context.isCommand) });
396
- }
397
- }
398
- });
399
- }
400
- processIndexExpression(node, context) {
401
- return __awaiter(this, void 0, void 0, function* () {
402
- const base = unwrap(node.base);
403
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
404
- yield this.processSubNode(node.index);
405
- this.push({
406
- op: instruction_1.OpCode.GET_SUPER_PROPERTY,
407
- source: this.getSourceLocation(node.index, node.type),
408
- invoke: !(context === null || context === void 0 ? void 0 : context.isReference) && !!(context === null || context === void 0 ? void 0 : context.isCommand)
409
- });
410
- }
411
- else {
412
- yield this.processSubNode(base, { isCommand: !!(context === null || context === void 0 ? void 0 : context.isCommand) });
413
- yield this.processSubNode(node.index, { isCommand: !!(context === null || context === void 0 ? void 0 : context.isCommand) });
414
- this.push({
415
- op: instruction_1.OpCode.GET_PROPERTY,
416
- source: this.getSourceLocation(node.index, node.type),
417
- invoke: !(context === null || context === void 0 ? void 0 : context.isReference) && !!(context === null || context === void 0 ? void 0 : context.isCommand)
418
- });
419
- }
420
- });
421
- }
422
- processSliceExpression(node) {
423
- return __awaiter(this, void 0, void 0, function* () {
424
- yield this.processSubNode(node.base);
425
- yield this.processSubNode(node.left);
426
- yield this.processSubNode(node.right);
427
- this.push({
428
- op: instruction_1.OpCode.SLICE,
429
- source: this.getSourceLocation(node)
430
- });
431
- });
432
- }
433
- processIdentifier(node, context) {
434
- return __awaiter(this, void 0, void 0, function* () {
435
- if (!(context === null || context === void 0 ? void 0 : context.isDescending)) {
436
- switch (node.name) {
437
- case keywords_1.RuntimeKeyword.Self: {
438
- this.push({
439
- op: instruction_1.OpCode.GET_SELF,
440
- source: this.getSourceLocation(node)
441
- });
442
- return;
443
- }
444
- case keywords_1.RuntimeKeyword.Super: {
445
- this.push({
446
- op: instruction_1.OpCode.GET_SUPER,
447
- source: this.getSourceLocation(node)
448
- });
449
- return;
450
- }
451
- case keywords_1.RuntimeKeyword.Outer: {
452
- this.push({
453
- op: instruction_1.OpCode.GET_OUTER,
454
- source: this.getSourceLocation(node)
455
- });
456
- return;
457
- }
458
- case keywords_1.RuntimeKeyword.Locals: {
459
- this.push({
460
- op: instruction_1.OpCode.GET_LOCALS,
461
- source: this.getSourceLocation(node)
462
- });
463
- return;
464
- }
465
- case keywords_1.RuntimeKeyword.Globals: {
466
- this.push({
467
- op: instruction_1.OpCode.GET_GLOBALS,
468
- source: this.getSourceLocation(node)
469
- });
470
- return;
471
- }
472
- default: {
473
- this.push({
474
- op: instruction_1.OpCode.GET_VARIABLE,
475
- source: this.getSourceLocation(node),
476
- property: new string_1.CustomString(node.name),
477
- invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
478
- });
479
- }
480
- }
481
- }
482
- else {
483
- this.push({
484
- op: instruction_1.OpCode.PUSH,
485
- source: this.getSourceLocation(node),
486
- value: new string_1.CustomString(node.name)
487
- });
488
- this.push({
489
- op: instruction_1.OpCode.GET_PROPERTY,
490
- source: this.getSourceLocation(node),
491
- invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
492
- });
493
- }
494
- });
495
- }
496
- processAssignmentStatement(node) {
497
- return __awaiter(this, void 0, void 0, function* () {
498
- let variable = unwrap(node.variable);
499
- if (variable instanceof miniscript_core_1.ASTUnaryExpression) {
500
- variable = unwrap(variable.argument);
501
- }
502
- if (variable instanceof miniscript_core_1.ASTMemberExpression) {
503
- yield this.processSubNode(variable.base);
504
- this.push({
505
- op: instruction_1.OpCode.PUSH,
506
- source: this.getSourceLocation(variable.identifier),
507
- value: new string_1.CustomString(variable.identifier.name)
508
- });
509
- }
510
- else if (variable instanceof miniscript_core_1.ASTIndexExpression) {
511
- yield this.processSubNode(variable.base);
512
- yield this.processSubNode(variable.index);
513
- }
514
- else if (variable instanceof miniscript_core_1.ASTIdentifier) {
515
- this.push({
516
- op: instruction_1.OpCode.GET_LOCALS,
517
- source: this.getSourceLocation(variable)
518
- });
519
- this.push({
520
- op: instruction_1.OpCode.PUSH,
521
- source: this.getSourceLocation(variable),
522
- value: new string_1.CustomString(variable.name)
523
- });
524
- }
525
- else {
526
- yield this.processSubNode(variable);
527
- this.push({
528
- op: instruction_1.OpCode.PUSH,
529
- source: this.getSourceLocation(variable),
530
- value: default_1.DefaultType.Void
531
- });
532
- }
533
- yield this.processSubNode(node.init, { includeOuter: true });
534
- this.push({
535
- op: instruction_1.OpCode.ASSIGN,
536
- source: this.getSourceLocation(node)
537
- });
538
- });
539
- }
540
- processLiteral(node) {
541
- return __awaiter(this, void 0, void 0, function* () {
542
- const value = generateCustomValueFromASTLiteral(node);
543
- this.push({
544
- op: instruction_1.OpCode.PUSH,
545
- source: this.getSourceLocation(node),
546
- value
547
- });
548
- });
549
- }
550
- processEvaluationExpression(node) {
551
- return __awaiter(this, void 0, void 0, function* () {
552
- const skip = {
553
- op: instruction_1.OpCode.NOOP,
554
- source: this.getSourceLocation(node)
555
- };
556
- yield this.processSubNode(node.left);
557
- if (node.operator === miniscript_core_1.Operator.And) {
558
- this.push({
559
- op: instruction_1.OpCode.GOTO_A_IF_FALSE_AND_PUSH,
560
- source: this.getSourceLocation(node),
561
- goto: skip
562
- });
563
- }
564
- else if (node.operator === miniscript_core_1.Operator.Or) {
565
- this.push({
566
- op: instruction_1.OpCode.GOTO_A_IF_TRUE_AND_PUSH,
567
- source: this.getSourceLocation(node),
568
- goto: skip
569
- });
570
- }
571
- yield this.processSubNode(node.right);
572
- switch (node.operator) {
573
- case miniscript_core_1.Operator.Isa: {
574
- this.push({
575
- op: instruction_1.OpCode.ISA,
576
- source: this.getSourceLocation(node)
577
- });
578
- break;
579
- }
580
- case miniscript_core_1.Operator.Equal: {
581
- this.push({
582
- op: instruction_1.OpCode.EQUAL,
583
- source: this.getSourceLocation(node)
584
- });
585
- break;
586
- }
587
- case miniscript_core_1.Operator.NotEqual: {
588
- this.push({
589
- op: instruction_1.OpCode.NOT_EQUAL,
590
- source: this.getSourceLocation(node)
591
- });
592
- break;
593
- }
594
- case miniscript_core_1.Operator.LessThan: {
595
- this.push({
596
- op: instruction_1.OpCode.LESS_THAN,
597
- source: this.getSourceLocation(node)
598
- });
599
- break;
600
- }
601
- case miniscript_core_1.Operator.LessThanOrEqual: {
602
- this.push({
603
- op: instruction_1.OpCode.LESS_THAN_OR_EQUAL,
604
- source: this.getSourceLocation(node)
605
- });
606
- break;
607
- }
608
- case miniscript_core_1.Operator.GreaterThan: {
609
- this.push({
610
- op: instruction_1.OpCode.GREATER_THAN,
611
- source: this.getSourceLocation(node)
612
- });
613
- break;
614
- }
615
- case miniscript_core_1.Operator.GreaterThanOrEqual: {
616
- this.push({
617
- op: instruction_1.OpCode.GREATER_THAN_OR_EQUAL,
618
- source: this.getSourceLocation(node)
619
- });
620
- break;
621
- }
622
- case miniscript_core_1.Operator.And: {
623
- this.push({
624
- op: instruction_1.OpCode.AND,
625
- source: this.getSourceLocation(node)
626
- });
627
- this.push(skip);
628
- break;
629
- }
630
- case miniscript_core_1.Operator.Or: {
631
- this.push({
632
- op: instruction_1.OpCode.OR,
633
- source: this.getSourceLocation(node)
634
- });
635
- this.push(skip);
636
- break;
637
- }
638
- case miniscript_core_1.Operator.Plus: {
639
- this.push({
640
- op: instruction_1.OpCode.ADD,
641
- source: this.getSourceLocation(node)
642
- });
643
- break;
644
- }
645
- case miniscript_core_1.Operator.Minus: {
646
- this.push({
647
- op: instruction_1.OpCode.SUB,
648
- source: this.getSourceLocation(node)
649
- });
650
- break;
651
- }
652
- case miniscript_core_1.Operator.Asterik: {
653
- this.push({
654
- op: instruction_1.OpCode.MUL,
655
- source: this.getSourceLocation(node)
656
- });
657
- break;
658
- }
659
- case miniscript_core_1.Operator.Slash: {
660
- this.push({
661
- op: instruction_1.OpCode.DIV,
662
- source: this.getSourceLocation(node)
663
- });
664
- break;
665
- }
666
- case miniscript_core_1.Operator.Modulo: {
667
- this.push({
668
- op: instruction_1.OpCode.MOD,
669
- source: this.getSourceLocation(node)
670
- });
671
- break;
672
- }
673
- case miniscript_core_1.Operator.Power: {
674
- this.push({
675
- op: instruction_1.OpCode.POW,
676
- source: this.getSourceLocation(node)
677
- });
678
- break;
679
- }
680
- case greybel_core_1.Operator.BitwiseAnd: {
681
- this.push({
682
- op: instruction_1.OpCode.BITWISE_AND,
683
- source: this.getSourceLocation(node)
684
- });
685
- break;
686
- }
687
- case greybel_core_1.Operator.BitwiseOr: {
688
- this.push({
689
- op: instruction_1.OpCode.BITWISE_OR,
690
- source: this.getSourceLocation(node)
691
- });
692
- break;
693
- }
694
- case greybel_core_1.Operator.LeftShift: {
695
- this.push({
696
- op: instruction_1.OpCode.BITWISE_LEFT_SHIFT,
697
- source: this.getSourceLocation(node)
698
- });
699
- break;
700
- }
701
- case greybel_core_1.Operator.RightShift: {
702
- this.push({
703
- op: instruction_1.OpCode.BITWISE_RIGHT_SHIFT,
704
- source: this.getSourceLocation(node)
705
- });
706
- break;
707
- }
708
- case greybel_core_1.Operator.UnsignedRightShift: {
709
- this.push({
710
- op: instruction_1.OpCode.BITWISE_UNSIGNED_RIGHT_SHIFT,
711
- source: this.getSourceLocation(node)
712
- });
713
- break;
714
- }
715
- default:
716
- throw new Error(`Unexpected evaluation expression operator. ("${node.operator}")`);
717
- }
718
- });
719
- }
720
- processReturn(node) {
721
- return __awaiter(this, void 0, void 0, function* () {
722
- if (this.context.length === 1) {
723
- return;
724
- }
725
- if (node.argument) {
726
- yield this.processSubNode(node.argument);
727
- }
728
- else {
729
- this.push({
730
- op: instruction_1.OpCode.PUSH,
731
- source: this.getSourceLocation(node),
732
- value: default_1.DefaultType.Void
733
- });
734
- }
735
- this.push({
736
- op: instruction_1.OpCode.RETURN,
737
- source: this.getSourceLocation(node)
738
- });
739
- });
740
- }
741
- processBreak(node) {
742
- return __awaiter(this, void 0, void 0, function* () {
743
- const jumpPoint = this.getLastJumpPoint();
744
- if (jumpPoint === null)
745
- return;
746
- const [_, end] = jumpPoint;
747
- this.push({
748
- op: instruction_1.OpCode.GOTO_A,
749
- source: this.getSourceLocation(node),
750
- goto: end
751
- });
752
- });
753
- }
754
- processContinue(node) {
755
- return __awaiter(this, void 0, void 0, function* () {
756
- const jumpPoint = this.getLastJumpPoint();
757
- if (jumpPoint === null)
758
- return;
759
- const [start] = jumpPoint;
760
- this.push({
761
- op: instruction_1.OpCode.GOTO_A,
762
- source: this.getSourceLocation(node),
763
- goto: start
764
- });
765
- });
766
- }
767
- processMapConstructorExpression(node) {
768
- return __awaiter(this, void 0, void 0, function* () {
769
- for (const field of node.fields) {
770
- yield this.processSubNode(field.key);
771
- yield this.processSubNode(field.value);
772
- }
773
- this.push({
774
- op: instruction_1.OpCode.CONSTRUCT_MAP,
775
- source: this.getSourceLocation(node),
776
- length: node.fields.length
777
- });
778
- });
779
- }
780
- processListConstructorExpression(node) {
781
- return __awaiter(this, void 0, void 0, function* () {
782
- for (const field of node.fields) {
783
- yield this.processSubNode(field.value);
784
- }
785
- this.push({
786
- op: instruction_1.OpCode.CONSTRUCT_LIST,
787
- source: this.getSourceLocation(node),
788
- length: node.fields.length
789
- });
790
- });
791
- }
792
- processFunctionDeclaration(node, context) {
793
- return __awaiter(this, void 0, void 0, function* () {
794
- const args = [];
795
- for (const item of node.parameters) {
796
- if (item instanceof miniscript_core_1.ASTIdentifier) {
797
- args.push({
798
- name: new string_1.CustomString(item.name),
799
- defaultValue: default_1.DefaultType.Void
800
- });
801
- }
802
- else if (item instanceof miniscript_core_1.ASTAssignmentStatement) {
803
- let defaultValue = default_1.DefaultType.Void;
804
- if (item.init instanceof miniscript_core_1.ASTLiteral) {
805
- defaultValue = generateCustomValueFromASTLiteral(item.init);
806
- }
807
- args.push({
808
- name: new string_1.CustomString(item.variable.name),
809
- defaultValue
810
- });
811
- }
812
- }
813
- this.pushContext();
814
- for (const item of node.body) {
815
- yield this.processNode(item);
816
- }
817
- this.push({
818
- op: instruction_1.OpCode.PUSH,
819
- source: this.getInternalLocation(),
820
- value: default_1.DefaultType.Void
821
- });
822
- this.push({
823
- op: instruction_1.OpCode.RETURN,
824
- source: this.getInternalLocation()
825
- });
826
- const fnCode = this.popContext().code;
827
- this.push({
828
- op: instruction_1.OpCode.FUNCTION_DEFINITION,
829
- source: this.getSourceLocation(node),
830
- arguments: args,
831
- code: fnCode,
832
- /*
833
- Can be removed after MiniScript fixed outer context bug.
834
- */
835
- ignoreOuter: !(context === null || context === void 0 ? void 0 : context.includeOuter)
836
- });
837
- });
838
- }
839
- processWhileStatement(node) {
840
- return __awaiter(this, void 0, void 0, function* () {
841
- const start = {
842
- op: instruction_1.OpCode.NOOP,
843
- source: this.getSourceLocation(node.condition)
844
- };
845
- const end = {
846
- op: instruction_1.OpCode.NOOP,
847
- source: this.getSourceLocation(node)
55
+ code: mod.getCode(),
56
+ imports: this.context.imports
848
57
  };
849
- this.push(start);
850
- yield this.processSubNode(node.condition);
851
- this.push({
852
- op: instruction_1.OpCode.GOTO_A_IF_FALSE,
853
- source: this.getSourceLocation(node.condition),
854
- goto: end
855
- });
856
- this.pushJumppoint(start, end);
857
- for (const item of node.body) {
858
- yield this.processNode(item);
859
- }
860
- this.popJumppoint();
861
- this.push({
862
- op: instruction_1.OpCode.GOTO_A,
863
- source: this.getSourceLocation(node.condition),
864
- goto: start
865
- });
866
- this.push(end);
867
- });
868
- }
869
- processUnaryExpression(node) {
870
- return __awaiter(this, void 0, void 0, function* () {
871
- const arg = unwrap(node.argument);
872
- switch (node.operator) {
873
- case miniscript_core_1.Operator.Reference:
874
- if (arg instanceof miniscript_core_1.ASTMemberExpression) {
875
- yield this.processMemberExpression(arg, { isReference: true });
876
- }
877
- else if (arg instanceof miniscript_core_1.ASTIndexExpression) {
878
- yield this.processIndexExpression(arg, { isReference: true });
879
- }
880
- else if (arg instanceof miniscript_core_1.ASTIdentifier) {
881
- yield this.processIdentifier(arg, { isDescending: false, isReference: true });
882
- }
883
- else {
884
- yield this.processSubNode(arg);
885
- }
886
- return;
887
- case miniscript_core_1.Operator.Not: {
888
- yield this.processSubNode(arg);
889
- this.push({
890
- op: instruction_1.OpCode.FALSIFY,
891
- source: this.getSourceLocation(node)
892
- });
893
- return;
894
- }
895
- case miniscript_core_1.Operator.Minus: {
896
- yield this.processSubNode(arg);
897
- this.push({
898
- op: instruction_1.OpCode.NEGATE,
899
- source: this.getSourceLocation(node)
900
- });
901
- return;
902
- }
903
- case miniscript_core_1.Operator.New: {
904
- yield this.processSubNode(arg);
905
- this.push({
906
- op: instruction_1.OpCode.NEW,
907
- source: this.getSourceLocation(node)
908
- });
909
- return;
910
- }
911
- }
912
- });
913
- }
914
- processCallExpression(node) {
915
- return __awaiter(this, void 0, void 0, function* () {
916
- const pushArgs = () => __awaiter(this, void 0, void 0, function* () {
917
- for (const arg of node.arguments) {
918
- yield this.processSubNode(arg);
919
- }
920
- });
921
- const left = unwrap(node.base);
922
- if (left instanceof miniscript_core_1.ASTMemberExpression) {
923
- const base = unwrap(left.base);
924
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
925
- this.push({
926
- op: instruction_1.OpCode.PUSH,
927
- source: this.getSourceLocation(left.identifier),
928
- value: new string_1.CustomString(left.identifier.name)
929
- });
930
- yield pushArgs();
931
- this.push({
932
- op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
933
- source: this.getSourceLocation(node.base, node.type),
934
- length: node.arguments.length
935
- });
936
- }
937
- else {
938
- yield this.processSubNode(base);
939
- this.push({
940
- op: instruction_1.OpCode.PUSH,
941
- source: this.getSourceLocation(left.identifier),
942
- value: new string_1.CustomString(left.identifier.name)
943
- });
944
- yield pushArgs();
945
- this.push({
946
- op: instruction_1.OpCode.CALL_WITH_CONTEXT,
947
- source: this.getSourceLocation(left.identifier, node.type),
948
- length: node.arguments.length
949
- });
950
- }
951
- }
952
- else if (left instanceof miniscript_core_1.ASTIndexExpression) {
953
- const base = unwrap(left.base);
954
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
955
- yield this.processSubNode(left.index);
956
- yield pushArgs();
957
- this.push({
958
- op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
959
- source: this.getSourceLocation(left.index, node.type),
960
- length: node.arguments.length
961
- });
962
- }
963
- else {
964
- yield this.processSubNode(base);
965
- yield this.processSubNode(left.index);
966
- yield pushArgs();
967
- this.push({
968
- op: instruction_1.OpCode.CALL_WITH_CONTEXT,
969
- source: this.getSourceLocation(left.index, node.type),
970
- length: node.arguments.length
971
- });
972
- }
973
- }
974
- else if (left instanceof miniscript_core_1.ASTIdentifier) {
975
- yield this.processIdentifier(left, { isDescending: false, isReference: true });
976
- yield pushArgs();
977
- this.push({
978
- op: instruction_1.OpCode.CALL,
979
- source: this.getSourceLocation(left, node.type),
980
- length: node.arguments.length
981
- });
982
- }
983
- else {
984
- yield this.processSubNode(left);
985
- yield pushArgs();
986
- this.push({
987
- op: instruction_1.OpCode.CALL,
988
- source: this.getSourceLocation(left, node.type),
989
- length: node.arguments.length
990
- });
991
- }
992
- });
993
- }
994
- processIfStatement(node) {
995
- return __awaiter(this, void 0, void 0, function* () {
996
- const end = {
997
- op: instruction_1.OpCode.NOOP,
998
- source: this.getSourceLocation(node)
999
- };
1000
- for (const clause of node.clauses) {
1001
- if (clause instanceof miniscript_core_1.ASTIfClause) {
1002
- const next = {
1003
- op: instruction_1.OpCode.NOOP,
1004
- source: this.getSourceLocation(clause)
1005
- };
1006
- yield this.processSubNode(clause.condition);
1007
- this.push({
1008
- op: instruction_1.OpCode.GOTO_A_IF_FALSE,
1009
- source: this.getSourceLocation(node),
1010
- goto: next
1011
- });
1012
- for (const item of clause.body) {
1013
- yield this.processNode(item);
1014
- }
1015
- this.push({
1016
- op: instruction_1.OpCode.GOTO_A,
1017
- source: this.getSourceLocation(node),
1018
- goto: end
1019
- });
1020
- this.push(next);
1021
- }
1022
- else if (clause instanceof miniscript_core_1.ASTElseClause) {
1023
- for (const item of clause.body) {
1024
- yield this.processNode(item);
1025
- }
1026
- }
1027
- }
1028
- this.push(end);
1029
- });
1030
- }
1031
- processForGenericStatement(node) {
1032
- return __awaiter(this, void 0, void 0, function* () {
1033
- const variable = node.variable;
1034
- const idxVariable = new string_1.CustomString(`__${variable.name}_idx`);
1035
- const start = {
1036
- op: instruction_1.OpCode.NEXT,
1037
- source: this.getSourceLocation(node.iterator),
1038
- idxVariable,
1039
- variable: new string_1.CustomString(variable.name)
1040
- };
1041
- const end = {
1042
- op: instruction_1.OpCode.POP_ITERATOR,
1043
- source: this.getSourceLocation(node.iterator)
1044
- };
1045
- this.pushJumppoint(start, end);
1046
- this.push({
1047
- op: instruction_1.OpCode.GET_LOCALS,
1048
- source: this.getSourceLocation(node)
1049
- });
1050
- this.push({
1051
- op: instruction_1.OpCode.PUSH,
1052
- source: this.getSourceLocation(node),
1053
- value: idxVariable
1054
- });
1055
- this.push({
1056
- op: instruction_1.OpCode.PUSH,
1057
- source: this.getSourceLocation(node),
1058
- value: new number_1.CustomNumber(-1)
1059
- });
1060
- this.push({
1061
- op: instruction_1.OpCode.ASSIGN,
1062
- source: this.getSourceLocation(node)
1063
- });
1064
- yield this.processSubNode(node.iterator);
1065
- this.push({
1066
- op: instruction_1.OpCode.PUSH_ITERATOR,
1067
- source: this.getSourceLocation(node.iterator)
1068
- });
1069
- this.push(start);
1070
- this.push({
1071
- op: instruction_1.OpCode.GOTO_A_IF_FALSE,
1072
- source: this.getSourceLocation(node.iterator),
1073
- goto: end
1074
- });
1075
- for (const item of node.body) {
1076
- yield this.processNode(item);
1077
- }
1078
- this.push({
1079
- op: instruction_1.OpCode.GOTO_A,
1080
- source: this.getSourceLocation(node.iterator),
1081
- goto: start
1082
- });
1083
- this.push(end);
1084
- this.popJumppoint();
1085
- });
1086
- }
1087
- processEnvarExpression(node) {
1088
- return __awaiter(this, void 0, void 0, function* () {
1089
- this.push({
1090
- op: instruction_1.OpCode.PUSH,
1091
- source: this.getSourceLocation(node),
1092
- value: new string_1.CustomString(node.name)
1093
- });
1094
- this.push({
1095
- op: instruction_1.OpCode.GET_ENVAR,
1096
- source: this.getSourceLocation(node)
1097
- });
1098
- });
1099
- }
1100
- createImport(node, path, code) {
1101
- return __awaiter(this, void 0, void 0, function* () {
1102
- try {
1103
- this.target.push(path);
1104
- this.pushContext();
1105
- const childNodes = this.parse(code);
1106
- this.push({
1107
- op: instruction_1.OpCode.GET_LOCALS,
1108
- source: this.getInternalLocation()
1109
- });
1110
- this.push({
1111
- op: instruction_1.OpCode.PUSH,
1112
- source: this.getInternalLocation(),
1113
- value: new string_1.CustomString('module')
1114
- });
1115
- this.push({
1116
- op: instruction_1.OpCode.PUSH,
1117
- source: this.getInternalLocation(),
1118
- value: new string_1.CustomString('exports')
1119
- });
1120
- this.push({
1121
- op: instruction_1.OpCode.CONSTRUCT_MAP,
1122
- source: this.getInternalLocation(),
1123
- length: 0
1124
- });
1125
- this.push({
1126
- op: instruction_1.OpCode.CONSTRUCT_MAP,
1127
- source: this.getInternalLocation(),
1128
- length: 1
1129
- });
1130
- this.push({
1131
- op: instruction_1.OpCode.ASSIGN,
1132
- source: this.getInternalLocation(),
1133
- });
1134
- yield this.processNode(childNodes);
1135
- this.push({
1136
- op: instruction_1.OpCode.GET_VARIABLE,
1137
- source: this.getInternalLocation(),
1138
- property: new string_1.CustomString('module')
1139
- });
1140
- this.push({
1141
- op: instruction_1.OpCode.PUSH,
1142
- source: this.getInternalLocation(),
1143
- value: new string_1.CustomString('exports')
1144
- });
1145
- this.push({
1146
- op: instruction_1.OpCode.GET_PROPERTY,
1147
- source: this.getInternalLocation(),
1148
- });
1149
- const context = this.popContext();
1150
- this.target.pop();
1151
- this.imports.set(path, context.code);
1152
- }
1153
- catch (err) {
1154
- if (err instanceof error_1.PrepareError) {
1155
- throw err;
1156
- }
1157
- throw new error_1.PrepareError(err.message, {
1158
- target: path,
1159
- range: new miniscript_core_1.ASTRange(node.start, node.end)
1160
- }, err);
1161
- }
1162
- });
1163
- }
1164
- processImportExpression(node) {
1165
- return __awaiter(this, void 0, void 0, function* () {
1166
- const currentTarget = this.target.peek();
1167
- const importTarget = yield this.handler.resourceHandler.getTargetRelativeTo(currentTarget, node.path);
1168
- if (this.target.includes(importTarget)) {
1169
- console.warn(`Found circular dependency between "${currentTarget}" and "${importTarget}" at line ${node.start.line}. Using noop instead to prevent overflow.`);
1170
- return;
1171
- }
1172
- if (!this.imports.has(importTarget)) {
1173
- const code = yield this.handler.resourceHandler.get(importTarget);
1174
- if (code == null) {
1175
- const range = new miniscript_core_1.ASTRange(node.start, node.end);
1176
- throw new error_1.PrepareError(`Cannot find import "${currentTarget}"`, {
1177
- target: currentTarget,
1178
- range
1179
- });
1180
- }
1181
- yield this.createImport(node, importTarget, code);
1182
- }
1183
- this.push({
1184
- op: instruction_1.OpCode.GET_LOCALS,
1185
- source: this.getSourceLocation(node)
1186
- });
1187
- this.push({
1188
- op: instruction_1.OpCode.PUSH,
1189
- source: this.getSourceLocation(node),
1190
- value: new string_1.CustomString(node.name.name),
1191
- });
1192
- this.push({
1193
- op: instruction_1.OpCode.IMPORT,
1194
- source: this.getSourceLocation(node),
1195
- path: importTarget
1196
- });
1197
- this.push({
1198
- op: instruction_1.OpCode.ASSIGN,
1199
- source: this.getSourceLocation(node)
1200
- });
1201
- });
1202
- }
1203
- processIncludeExpression(node) {
1204
- return __awaiter(this, void 0, void 0, function* () {
1205
- const currentTarget = this.target.peek();
1206
- const importTarget = yield this.handler.resourceHandler.getTargetRelativeTo(currentTarget, node.path);
1207
- if (this.target.includes(importTarget)) {
1208
- console.warn(`Found circular dependency between "${currentTarget}" and "${importTarget}" at line ${node.start.line}. Using noop instead to prevent overflow.`);
1209
- return;
1210
- }
1211
- const code = yield this.handler.resourceHandler.get(importTarget);
1212
- if (code == null) {
1213
- const range = new miniscript_core_1.ASTRange(node.start, node.end);
1214
- throw new error_1.PrepareError(`Cannot find import "${currentTarget}"`, {
1215
- target: currentTarget,
1216
- range
1217
- });
1218
- }
1219
- try {
1220
- this.target.push(importTarget);
1221
- const childNodes = this.parse(code);
1222
- yield this.processNode(childNodes);
1223
- this.target.pop();
1224
- }
1225
- catch (err) {
1226
- if (err instanceof error_1.PrepareError) {
1227
- throw err;
1228
- }
1229
- throw new error_1.PrepareError(err.message, {
1230
- target: importTarget,
1231
- range: new miniscript_core_1.ASTRange(node.start, node.end)
1232
- }, err);
1233
- }
1234
- });
1235
- }
1236
- processDebuggerExpression(node) {
1237
- return __awaiter(this, void 0, void 0, function* () {
1238
- this.push({
1239
- op: instruction_1.OpCode.BREAKPOINT_ENABLE,
1240
- source: this.getSourceLocation(node)
1241
- });
1242
- this.push({
1243
- op: instruction_1.OpCode.BREAKPOINT,
1244
- source: this.getSourceLocation(node)
1245
- });
1246
58
  });
1247
59
  }
1248
60
  }