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.
- package/dist/bytecode-generator/context.d.ts +22 -0
- package/dist/bytecode-generator/context.js +31 -0
- package/dist/bytecode-generator/expression.d.ts +24 -0
- package/dist/bytecode-generator/expression.js +640 -0
- package/dist/{byte-compiler → bytecode-generator}/instruction.d.ts +17 -19
- package/dist/{byte-compiler → bytecode-generator}/instruction.js +13 -12
- package/dist/bytecode-generator/line.d.ts +9 -0
- package/dist/bytecode-generator/line.js +2 -0
- package/dist/bytecode-generator/models.d.ts +42 -0
- package/dist/bytecode-generator/models.js +2 -0
- package/dist/bytecode-generator/module.d.ts +25 -0
- package/dist/bytecode-generator/module.js +72 -0
- package/dist/bytecode-generator/statement.d.ts +31 -0
- package/dist/bytecode-generator/statement.js +825 -0
- package/dist/bytecode-generator/utils.d.ts +5 -0
- package/dist/bytecode-generator/utils.js +30 -0
- package/dist/bytecode-generator.d.ts +3 -57
- package/dist/bytecode-generator.js +31 -1219
- package/dist/context.d.ts +5 -2
- package/dist/context.js +15 -13
- package/dist/index.d.ts +6 -2
- package/dist/index.js +10 -2
- package/dist/types/function.d.ts +1 -1
- package/dist/types/function.js +2 -2
- package/dist/types/map.js +10 -10
- package/dist/types/string.js +1 -1
- package/dist/utils/error.d.ts +1 -1
- package/dist/utils/hash.js +8 -14
- package/dist/utils/object-value.d.ts +1 -1
- package/dist/utils/object-value.js +9 -9
- package/dist/utils/stack.d.ts +1 -0
- package/dist/utils/stack.js +3 -0
- package/dist/utils/uuid.d.ts +2 -1
- package/dist/utils/uuid.js +5 -33
- package/dist/utils/value-hash.d.ts +2 -0
- package/dist/utils/value-hash.js +15 -0
- package/dist/vm/call.js +1 -1
- package/dist/vm.d.ts +1 -1
- package/dist/vm.js +36 -18
- package/package.json +4 -4
- /package/dist/{byte-compiler → bytecode-generator}/keywords.d.ts +0 -0
- /package/dist/{byte-compiler → bytecode-generator}/keywords.js +0 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BytecodeExpressionGenerator = void 0;
|
|
13
|
+
const greybel_core_1 = require("greybel-core");
|
|
14
|
+
const miniscript_core_1 = require("miniscript-core");
|
|
15
|
+
const path_1 = require("path");
|
|
16
|
+
const default_1 = require("../types/default");
|
|
17
|
+
const number_1 = require("../types/number");
|
|
18
|
+
const string_1 = require("../types/string");
|
|
19
|
+
const error_1 = require("../utils/error");
|
|
20
|
+
const instruction_1 = require("./instruction");
|
|
21
|
+
const keywords_1 = require("./keywords");
|
|
22
|
+
const utils_1 = require("./utils");
|
|
23
|
+
class BytecodeExpressionGenerator {
|
|
24
|
+
constructor(context, parseCodeFunction, stmtGenerator) {
|
|
25
|
+
this.context = context;
|
|
26
|
+
this.parseCode = parseCodeFunction;
|
|
27
|
+
this.stmtGenerator = stmtGenerator;
|
|
28
|
+
}
|
|
29
|
+
process(node, context) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const mod = this.context.module.peek();
|
|
32
|
+
switch (node.type) {
|
|
33
|
+
case miniscript_core_1.ASTType.MemberExpression:
|
|
34
|
+
yield this.processMemberExpression(node, context);
|
|
35
|
+
return;
|
|
36
|
+
case miniscript_core_1.ASTType.IndexExpression:
|
|
37
|
+
yield this.processIndexExpression(node);
|
|
38
|
+
return;
|
|
39
|
+
case miniscript_core_1.ASTType.SliceExpression:
|
|
40
|
+
yield this.processSliceExpression(node);
|
|
41
|
+
return;
|
|
42
|
+
case miniscript_core_1.ASTType.Identifier:
|
|
43
|
+
yield this.processIdentifier(node);
|
|
44
|
+
return;
|
|
45
|
+
case miniscript_core_1.ASTType.BooleanLiteral:
|
|
46
|
+
case miniscript_core_1.ASTType.StringLiteral:
|
|
47
|
+
case miniscript_core_1.ASTType.NumericLiteral:
|
|
48
|
+
case miniscript_core_1.ASTType.NilLiteral:
|
|
49
|
+
yield this.processLiteral(node);
|
|
50
|
+
return;
|
|
51
|
+
case miniscript_core_1.ASTType.IsaExpression:
|
|
52
|
+
case miniscript_core_1.ASTType.BinaryExpression:
|
|
53
|
+
case miniscript_core_1.ASTType.LogicalExpression:
|
|
54
|
+
yield this.processEvaluationExpression(node);
|
|
55
|
+
return;
|
|
56
|
+
case miniscript_core_1.ASTType.MapConstructorExpression:
|
|
57
|
+
yield this.processMapConstructorExpression(node);
|
|
58
|
+
return;
|
|
59
|
+
case miniscript_core_1.ASTType.ListConstructorExpression:
|
|
60
|
+
yield this.processListConstructorExpression(node);
|
|
61
|
+
return;
|
|
62
|
+
case miniscript_core_1.ASTType.FunctionDeclaration:
|
|
63
|
+
yield this.processFunctionDeclaration(node, context);
|
|
64
|
+
return;
|
|
65
|
+
case miniscript_core_1.ASTType.ParenthesisExpression:
|
|
66
|
+
yield this.process(node.expression);
|
|
67
|
+
return;
|
|
68
|
+
case miniscript_core_1.ASTType.BinaryNegatedExpression:
|
|
69
|
+
case miniscript_core_1.ASTType.UnaryExpression:
|
|
70
|
+
case miniscript_core_1.ASTType.NegationExpression:
|
|
71
|
+
yield this.processUnaryExpression(node);
|
|
72
|
+
return;
|
|
73
|
+
case miniscript_core_1.ASTType.CallExpression:
|
|
74
|
+
yield this.processCallExpression(node);
|
|
75
|
+
return;
|
|
76
|
+
case miniscript_core_1.ASTType.EmptyExpression:
|
|
77
|
+
mod.pushCode({
|
|
78
|
+
op: instruction_1.OpCode.PUSH,
|
|
79
|
+
source: mod.getSourceLocation(node),
|
|
80
|
+
value: default_1.DefaultType.Void
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
case greybel_core_1.ASTType.FeatureEnvarExpression:
|
|
84
|
+
yield this.processEnvarExpression(node);
|
|
85
|
+
return;
|
|
86
|
+
case greybel_core_1.ASTType.FeatureLineExpression:
|
|
87
|
+
mod.pushCode({
|
|
88
|
+
op: instruction_1.OpCode.PUSH,
|
|
89
|
+
source: mod.getSourceLocation(node),
|
|
90
|
+
value: new number_1.CustomNumber(node.start.line)
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
case greybel_core_1.ASTType.FeatureFileExpression:
|
|
94
|
+
mod.pushCode({
|
|
95
|
+
op: instruction_1.OpCode.PUSH,
|
|
96
|
+
source: mod.getSourceLocation(node),
|
|
97
|
+
value: new string_1.CustomString((0, path_1.basename)(this.context.target.peek()))
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
case miniscript_core_1.ASTType.Comment:
|
|
101
|
+
return;
|
|
102
|
+
default: {
|
|
103
|
+
const range = new miniscript_core_1.ASTRange(node.start, node.end);
|
|
104
|
+
throw new error_1.PrepareError(`Unexpected AST type ${node.type}`, {
|
|
105
|
+
target: this.context.target.peek(),
|
|
106
|
+
range
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
processMemberExpression(node, context) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const mod = this.context.module.peek();
|
|
115
|
+
const base = (0, utils_1.unwrap)(node.base);
|
|
116
|
+
if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
|
|
117
|
+
mod.pushCode({
|
|
118
|
+
op: instruction_1.OpCode.PUSH,
|
|
119
|
+
source: mod.getSourceLocation(node.identifier),
|
|
120
|
+
value: new string_1.CustomString(node.identifier.name)
|
|
121
|
+
});
|
|
122
|
+
mod.pushCode({
|
|
123
|
+
op: instruction_1.OpCode.GET_SUPER_PROPERTY,
|
|
124
|
+
source: mod.getSourceLocation(node.identifier),
|
|
125
|
+
invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
yield this.process(base);
|
|
130
|
+
yield this.processIdentifier(node.identifier, {
|
|
131
|
+
isDescending: true,
|
|
132
|
+
isReference: !!(context === null || context === void 0 ? void 0 : context.isReference)
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
processIndexExpression(node) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
const mod = this.context.module.peek();
|
|
140
|
+
const base = (0, utils_1.unwrap)(node.base);
|
|
141
|
+
if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
|
|
142
|
+
yield this.process(node.index);
|
|
143
|
+
mod.pushCode({
|
|
144
|
+
op: instruction_1.OpCode.GET_SUPER_PROPERTY,
|
|
145
|
+
source: mod.getSourceLocation(node.index, node.type),
|
|
146
|
+
invoke: false
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
yield this.process(base);
|
|
151
|
+
yield this.process(node.index);
|
|
152
|
+
mod.pushCode({
|
|
153
|
+
op: instruction_1.OpCode.GET_PROPERTY,
|
|
154
|
+
source: mod.getSourceLocation(node.index, node.type),
|
|
155
|
+
invoke: false
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
processSliceExpression(node) {
|
|
161
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
+
const mod = this.context.module.peek();
|
|
163
|
+
yield this.process(node.base);
|
|
164
|
+
yield this.process(node.left);
|
|
165
|
+
yield this.process(node.right);
|
|
166
|
+
mod.pushCode({
|
|
167
|
+
op: instruction_1.OpCode.SLICE,
|
|
168
|
+
source: mod.getSourceLocation(node)
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
processIdentifier(node, context) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const mod = this.context.module.peek();
|
|
175
|
+
if (!(context === null || context === void 0 ? void 0 : context.isDescending)) {
|
|
176
|
+
switch (node.name) {
|
|
177
|
+
case keywords_1.RuntimeKeyword.Self: {
|
|
178
|
+
mod.pushCode({
|
|
179
|
+
op: instruction_1.OpCode.GET_SELF,
|
|
180
|
+
source: mod.getSourceLocation(node)
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
case keywords_1.RuntimeKeyword.Super: {
|
|
185
|
+
mod.pushCode({
|
|
186
|
+
op: instruction_1.OpCode.GET_SUPER,
|
|
187
|
+
source: mod.getSourceLocation(node)
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
case keywords_1.RuntimeKeyword.Outer: {
|
|
192
|
+
mod.pushCode({
|
|
193
|
+
op: instruction_1.OpCode.GET_OUTER,
|
|
194
|
+
source: mod.getSourceLocation(node)
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
case keywords_1.RuntimeKeyword.Locals: {
|
|
199
|
+
mod.pushCode({
|
|
200
|
+
op: instruction_1.OpCode.GET_LOCALS,
|
|
201
|
+
source: mod.getSourceLocation(node)
|
|
202
|
+
});
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
case keywords_1.RuntimeKeyword.Globals: {
|
|
206
|
+
mod.pushCode({
|
|
207
|
+
op: instruction_1.OpCode.GET_GLOBALS,
|
|
208
|
+
source: mod.getSourceLocation(node)
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
default: {
|
|
213
|
+
mod.pushCode({
|
|
214
|
+
op: instruction_1.OpCode.GET_VARIABLE,
|
|
215
|
+
source: mod.getSourceLocation(node),
|
|
216
|
+
property: new string_1.CustomString(node.name),
|
|
217
|
+
invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
mod.pushCode({
|
|
224
|
+
op: instruction_1.OpCode.PUSH,
|
|
225
|
+
source: mod.getSourceLocation(node),
|
|
226
|
+
value: new string_1.CustomString(node.name)
|
|
227
|
+
});
|
|
228
|
+
mod.pushCode({
|
|
229
|
+
op: instruction_1.OpCode.GET_PROPERTY,
|
|
230
|
+
source: mod.getSourceLocation(node),
|
|
231
|
+
invoke: !(context === null || context === void 0 ? void 0 : context.isReference)
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
processLiteral(node) {
|
|
237
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
238
|
+
const mod = this.context.module.peek();
|
|
239
|
+
const value = (0, utils_1.generateCustomValueFromASTLiteral)(node);
|
|
240
|
+
mod.pushCode({
|
|
241
|
+
op: instruction_1.OpCode.PUSH,
|
|
242
|
+
source: mod.getSourceLocation(node),
|
|
243
|
+
value
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
processEvaluationExpression(node) {
|
|
248
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
+
const mod = this.context.module.peek();
|
|
250
|
+
const skip = {
|
|
251
|
+
op: instruction_1.OpCode.NOOP,
|
|
252
|
+
source: mod.getSourceLocation(node)
|
|
253
|
+
};
|
|
254
|
+
yield this.process(node.left);
|
|
255
|
+
if (node.operator === miniscript_core_1.Operator.And) {
|
|
256
|
+
mod.pushCode({
|
|
257
|
+
op: instruction_1.OpCode.GOTO_A_IF_FALSE_AND_PUSH,
|
|
258
|
+
source: mod.getSourceLocation(node),
|
|
259
|
+
goto: skip
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
else if (node.operator === miniscript_core_1.Operator.Or) {
|
|
263
|
+
mod.pushCode({
|
|
264
|
+
op: instruction_1.OpCode.GOTO_A_IF_TRUE_AND_PUSH,
|
|
265
|
+
source: mod.getSourceLocation(node),
|
|
266
|
+
goto: skip
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
yield this.process(node.right);
|
|
270
|
+
switch (node.operator) {
|
|
271
|
+
case miniscript_core_1.Operator.Isa: {
|
|
272
|
+
mod.pushCode({
|
|
273
|
+
op: instruction_1.OpCode.ISA,
|
|
274
|
+
source: mod.getSourceLocation(node)
|
|
275
|
+
});
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case miniscript_core_1.Operator.Equal: {
|
|
279
|
+
mod.pushCode({
|
|
280
|
+
op: instruction_1.OpCode.EQUAL,
|
|
281
|
+
source: mod.getSourceLocation(node)
|
|
282
|
+
});
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case miniscript_core_1.Operator.NotEqual: {
|
|
286
|
+
mod.pushCode({
|
|
287
|
+
op: instruction_1.OpCode.NOT_EQUAL,
|
|
288
|
+
source: mod.getSourceLocation(node)
|
|
289
|
+
});
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
case miniscript_core_1.Operator.LessThan: {
|
|
293
|
+
mod.pushCode({
|
|
294
|
+
op: instruction_1.OpCode.LESS_THAN,
|
|
295
|
+
source: mod.getSourceLocation(node)
|
|
296
|
+
});
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
case miniscript_core_1.Operator.LessThanOrEqual: {
|
|
300
|
+
mod.pushCode({
|
|
301
|
+
op: instruction_1.OpCode.LESS_THAN_OR_EQUAL,
|
|
302
|
+
source: mod.getSourceLocation(node)
|
|
303
|
+
});
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case miniscript_core_1.Operator.GreaterThan: {
|
|
307
|
+
mod.pushCode({
|
|
308
|
+
op: instruction_1.OpCode.GREATER_THAN,
|
|
309
|
+
source: mod.getSourceLocation(node)
|
|
310
|
+
});
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
case miniscript_core_1.Operator.GreaterThanOrEqual: {
|
|
314
|
+
mod.pushCode({
|
|
315
|
+
op: instruction_1.OpCode.GREATER_THAN_OR_EQUAL,
|
|
316
|
+
source: mod.getSourceLocation(node)
|
|
317
|
+
});
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case miniscript_core_1.Operator.And: {
|
|
321
|
+
mod.pushCode({
|
|
322
|
+
op: instruction_1.OpCode.AND,
|
|
323
|
+
source: mod.getSourceLocation(node)
|
|
324
|
+
});
|
|
325
|
+
mod.pushCode(skip);
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
case miniscript_core_1.Operator.Or: {
|
|
329
|
+
mod.pushCode({
|
|
330
|
+
op: instruction_1.OpCode.OR,
|
|
331
|
+
source: mod.getSourceLocation(node)
|
|
332
|
+
});
|
|
333
|
+
mod.pushCode(skip);
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case miniscript_core_1.Operator.Plus: {
|
|
337
|
+
mod.pushCode({
|
|
338
|
+
op: instruction_1.OpCode.ADD,
|
|
339
|
+
source: mod.getSourceLocation(node)
|
|
340
|
+
});
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case miniscript_core_1.Operator.Minus: {
|
|
344
|
+
mod.pushCode({
|
|
345
|
+
op: instruction_1.OpCode.SUB,
|
|
346
|
+
source: mod.getSourceLocation(node)
|
|
347
|
+
});
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
case miniscript_core_1.Operator.Asterik: {
|
|
351
|
+
mod.pushCode({
|
|
352
|
+
op: instruction_1.OpCode.MUL,
|
|
353
|
+
source: mod.getSourceLocation(node)
|
|
354
|
+
});
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
case miniscript_core_1.Operator.Slash: {
|
|
358
|
+
mod.pushCode({
|
|
359
|
+
op: instruction_1.OpCode.DIV,
|
|
360
|
+
source: mod.getSourceLocation(node)
|
|
361
|
+
});
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
case miniscript_core_1.Operator.Modulo: {
|
|
365
|
+
mod.pushCode({
|
|
366
|
+
op: instruction_1.OpCode.MOD,
|
|
367
|
+
source: mod.getSourceLocation(node)
|
|
368
|
+
});
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case miniscript_core_1.Operator.Power: {
|
|
372
|
+
mod.pushCode({
|
|
373
|
+
op: instruction_1.OpCode.POW,
|
|
374
|
+
source: mod.getSourceLocation(node)
|
|
375
|
+
});
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case greybel_core_1.Operator.BitwiseAnd: {
|
|
379
|
+
mod.pushCode({
|
|
380
|
+
op: instruction_1.OpCode.BITWISE_AND,
|
|
381
|
+
source: mod.getSourceLocation(node)
|
|
382
|
+
});
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
case greybel_core_1.Operator.BitwiseOr: {
|
|
386
|
+
mod.pushCode({
|
|
387
|
+
op: instruction_1.OpCode.BITWISE_OR,
|
|
388
|
+
source: mod.getSourceLocation(node)
|
|
389
|
+
});
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case greybel_core_1.Operator.LeftShift: {
|
|
393
|
+
mod.pushCode({
|
|
394
|
+
op: instruction_1.OpCode.BITWISE_LEFT_SHIFT,
|
|
395
|
+
source: mod.getSourceLocation(node)
|
|
396
|
+
});
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
case greybel_core_1.Operator.RightShift: {
|
|
400
|
+
mod.pushCode({
|
|
401
|
+
op: instruction_1.OpCode.BITWISE_RIGHT_SHIFT,
|
|
402
|
+
source: mod.getSourceLocation(node)
|
|
403
|
+
});
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case greybel_core_1.Operator.UnsignedRightShift: {
|
|
407
|
+
mod.pushCode({
|
|
408
|
+
op: instruction_1.OpCode.BITWISE_UNSIGNED_RIGHT_SHIFT,
|
|
409
|
+
source: mod.getSourceLocation(node)
|
|
410
|
+
});
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
default:
|
|
414
|
+
throw new Error(`Unexpected evaluation expression operator. ("${node.operator}")`);
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
processMapConstructorExpression(node) {
|
|
419
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
420
|
+
const mod = this.context.module.peek();
|
|
421
|
+
for (const field of node.fields) {
|
|
422
|
+
yield this.process(field.key);
|
|
423
|
+
yield this.process(field.value);
|
|
424
|
+
}
|
|
425
|
+
mod.pushCode({
|
|
426
|
+
op: instruction_1.OpCode.CONSTRUCT_MAP,
|
|
427
|
+
source: mod.getSourceLocation(node),
|
|
428
|
+
length: node.fields.length
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
processListConstructorExpression(node) {
|
|
433
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
434
|
+
const mod = this.context.module.peek();
|
|
435
|
+
for (const field of node.fields) {
|
|
436
|
+
yield this.process(field.value);
|
|
437
|
+
}
|
|
438
|
+
mod.pushCode({
|
|
439
|
+
op: instruction_1.OpCode.CONSTRUCT_LIST,
|
|
440
|
+
source: mod.getSourceLocation(node),
|
|
441
|
+
length: node.fields.length
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
processFunctionDeclaration(node, context) {
|
|
446
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
447
|
+
const args = [];
|
|
448
|
+
for (const item of node.parameters) {
|
|
449
|
+
if (item instanceof miniscript_core_1.ASTIdentifier) {
|
|
450
|
+
args.push({
|
|
451
|
+
name: new string_1.CustomString(item.name),
|
|
452
|
+
defaultValue: default_1.DefaultType.Void
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
else if (item instanceof miniscript_core_1.ASTAssignmentStatement) {
|
|
456
|
+
let defaultValue = default_1.DefaultType.Void;
|
|
457
|
+
if (item.init instanceof miniscript_core_1.ASTLiteral) {
|
|
458
|
+
defaultValue = (0, utils_1.generateCustomValueFromASTLiteral)(item.init);
|
|
459
|
+
}
|
|
460
|
+
args.push({
|
|
461
|
+
name: new string_1.CustomString(item.variable.name),
|
|
462
|
+
defaultValue
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
const mod = this.context.module.peek();
|
|
467
|
+
mod.pushContext();
|
|
468
|
+
for (const item of node.body) {
|
|
469
|
+
yield this.stmtGenerator.process(item);
|
|
470
|
+
}
|
|
471
|
+
mod.pushCode({
|
|
472
|
+
op: instruction_1.OpCode.PUSH,
|
|
473
|
+
source: mod.getInternalLocation(),
|
|
474
|
+
value: default_1.DefaultType.Void
|
|
475
|
+
});
|
|
476
|
+
mod.pushCode({
|
|
477
|
+
op: instruction_1.OpCode.RETURN,
|
|
478
|
+
source: mod.getInternalLocation()
|
|
479
|
+
});
|
|
480
|
+
const fnCode = mod.popContext().code;
|
|
481
|
+
mod.pushCode({
|
|
482
|
+
op: instruction_1.OpCode.FUNCTION_DEFINITION,
|
|
483
|
+
source: mod.getSourceLocation(node),
|
|
484
|
+
arguments: args,
|
|
485
|
+
code: fnCode,
|
|
486
|
+
/*
|
|
487
|
+
Can be removed after MiniScript fixed outer context bug.
|
|
488
|
+
*/
|
|
489
|
+
ignoreOuter: !(context === null || context === void 0 ? void 0 : context.includeOuter)
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
processUnaryExpression(node) {
|
|
494
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
495
|
+
const mod = this.context.module.peek();
|
|
496
|
+
const arg = (0, utils_1.unwrap)(node.argument);
|
|
497
|
+
switch (node.operator) {
|
|
498
|
+
case miniscript_core_1.Operator.Reference:
|
|
499
|
+
if (arg instanceof miniscript_core_1.ASTMemberExpression) {
|
|
500
|
+
yield this.processMemberExpression(arg, { isReference: true });
|
|
501
|
+
}
|
|
502
|
+
else if (arg instanceof miniscript_core_1.ASTIndexExpression) {
|
|
503
|
+
yield this.processIndexExpression(arg);
|
|
504
|
+
}
|
|
505
|
+
else if (arg instanceof miniscript_core_1.ASTIdentifier) {
|
|
506
|
+
yield this.processIdentifier(arg, {
|
|
507
|
+
isDescending: false,
|
|
508
|
+
isReference: true
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
yield this.process(arg);
|
|
513
|
+
}
|
|
514
|
+
return;
|
|
515
|
+
case miniscript_core_1.Operator.Not: {
|
|
516
|
+
yield this.process(arg);
|
|
517
|
+
mod.pushCode({
|
|
518
|
+
op: instruction_1.OpCode.FALSIFY,
|
|
519
|
+
source: mod.getSourceLocation(node)
|
|
520
|
+
});
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
case miniscript_core_1.Operator.Minus: {
|
|
524
|
+
yield this.process(arg);
|
|
525
|
+
mod.pushCode({
|
|
526
|
+
op: instruction_1.OpCode.NEGATE,
|
|
527
|
+
source: mod.getSourceLocation(node)
|
|
528
|
+
});
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
case miniscript_core_1.Operator.New: {
|
|
532
|
+
yield this.process(arg);
|
|
533
|
+
mod.pushCode({
|
|
534
|
+
op: instruction_1.OpCode.NEW,
|
|
535
|
+
source: mod.getSourceLocation(node)
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
processCallExpression(node) {
|
|
542
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
543
|
+
const mod = this.context.module.peek();
|
|
544
|
+
const pushArgs = () => __awaiter(this, void 0, void 0, function* () {
|
|
545
|
+
for (const arg of node.arguments) {
|
|
546
|
+
yield this.process(arg);
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
const left = (0, utils_1.unwrap)(node.base);
|
|
550
|
+
if (left instanceof miniscript_core_1.ASTMemberExpression) {
|
|
551
|
+
const base = (0, utils_1.unwrap)(left.base);
|
|
552
|
+
if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
|
|
553
|
+
mod.pushCode({
|
|
554
|
+
op: instruction_1.OpCode.PUSH,
|
|
555
|
+
source: mod.getSourceLocation(left.identifier),
|
|
556
|
+
value: new string_1.CustomString(left.identifier.name)
|
|
557
|
+
});
|
|
558
|
+
yield pushArgs();
|
|
559
|
+
mod.pushCode({
|
|
560
|
+
op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
|
|
561
|
+
source: mod.getSourceLocation(node.base, node.type),
|
|
562
|
+
length: node.arguments.length
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
yield this.process(base);
|
|
567
|
+
mod.pushCode({
|
|
568
|
+
op: instruction_1.OpCode.PUSH,
|
|
569
|
+
source: mod.getSourceLocation(left.identifier),
|
|
570
|
+
value: new string_1.CustomString(left.identifier.name)
|
|
571
|
+
});
|
|
572
|
+
yield pushArgs();
|
|
573
|
+
mod.pushCode({
|
|
574
|
+
op: instruction_1.OpCode.CALL_WITH_CONTEXT,
|
|
575
|
+
source: mod.getSourceLocation(left.identifier, node.type),
|
|
576
|
+
length: node.arguments.length
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
else if (left instanceof miniscript_core_1.ASTIndexExpression) {
|
|
581
|
+
const base = (0, utils_1.unwrap)(left.base);
|
|
582
|
+
if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
|
|
583
|
+
yield this.process(left.index);
|
|
584
|
+
yield pushArgs();
|
|
585
|
+
mod.pushCode({
|
|
586
|
+
op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
|
|
587
|
+
source: mod.getSourceLocation(left.index, node.type),
|
|
588
|
+
length: node.arguments.length
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
yield this.process(base);
|
|
593
|
+
yield this.process(left.index);
|
|
594
|
+
yield pushArgs();
|
|
595
|
+
mod.pushCode({
|
|
596
|
+
op: instruction_1.OpCode.CALL_WITH_CONTEXT,
|
|
597
|
+
source: mod.getSourceLocation(left.index, node.type),
|
|
598
|
+
length: node.arguments.length
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
else if (left instanceof miniscript_core_1.ASTIdentifier) {
|
|
603
|
+
yield this.processIdentifier(left, {
|
|
604
|
+
isDescending: false,
|
|
605
|
+
isReference: true
|
|
606
|
+
});
|
|
607
|
+
yield pushArgs();
|
|
608
|
+
mod.pushCode({
|
|
609
|
+
op: instruction_1.OpCode.CALL,
|
|
610
|
+
source: mod.getSourceLocation(left, node.type),
|
|
611
|
+
length: node.arguments.length
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
yield this.process(left);
|
|
616
|
+
yield pushArgs();
|
|
617
|
+
mod.pushCode({
|
|
618
|
+
op: instruction_1.OpCode.CALL,
|
|
619
|
+
source: mod.getSourceLocation(left, node.type),
|
|
620
|
+
length: node.arguments.length
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
processEnvarExpression(node) {
|
|
626
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
627
|
+
const mod = this.context.module.peek();
|
|
628
|
+
mod.pushCode({
|
|
629
|
+
op: instruction_1.OpCode.PUSH,
|
|
630
|
+
source: mod.getSourceLocation(node),
|
|
631
|
+
value: new string_1.CustomString(node.name)
|
|
632
|
+
});
|
|
633
|
+
mod.pushCode({
|
|
634
|
+
op: instruction_1.OpCode.GET_ENVAR,
|
|
635
|
+
source: mod.getSourceLocation(node)
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
exports.BytecodeExpressionGenerator = BytecodeExpressionGenerator;
|