redscript-mc 1.2.0 → 1.2.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.
- package/CHANGELOG.md +5 -0
- package/README.md +53 -10
- package/README.zh.md +53 -10
- package/dist/__tests__/dce.test.d.ts +1 -0
- package/dist/__tests__/dce.test.js +137 -0
- package/dist/__tests__/lexer.test.js +19 -2
- package/dist/__tests__/lowering.test.js +8 -0
- package/dist/__tests__/mc-syntax.test.js +12 -0
- package/dist/__tests__/parser.test.js +10 -0
- package/dist/__tests__/runtime.test.js +13 -0
- package/dist/__tests__/typechecker.test.js +30 -0
- package/dist/ast/types.d.ts +22 -2
- package/dist/cli.js +15 -10
- package/dist/codegen/structure/index.d.ts +4 -1
- package/dist/codegen/structure/index.js +4 -2
- package/dist/compile.d.ts +1 -0
- package/dist/compile.js +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -1
- package/dist/lexer/index.d.ts +2 -1
- package/dist/lexer/index.js +89 -1
- package/dist/lowering/index.js +37 -1
- package/dist/optimizer/dce.d.ts +23 -0
- package/dist/optimizer/dce.js +591 -0
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.js +81 -16
- package/dist/typechecker/index.d.ts +2 -0
- package/dist/typechecker/index.js +49 -0
- package/docs/ARCHITECTURE.zh.md +1088 -0
- package/editors/vscode/.vscodeignore +3 -0
- package/editors/vscode/icon.png +0 -0
- package/editors/vscode/package-lock.json +2 -2
- package/editors/vscode/package.json +1 -1
- package/editors/vscode/syntaxes/redscript.tmLanguage.json +6 -2
- package/examples/spiral.mcrs +79 -0
- package/logo.png +0 -0
- package/package.json +1 -1
- package/src/__tests__/dce.test.ts +129 -0
- package/src/__tests__/lexer.test.ts +21 -2
- package/src/__tests__/lowering.test.ts +9 -0
- package/src/__tests__/mc-syntax.test.ts +14 -0
- package/src/__tests__/parser.test.ts +11 -0
- package/src/__tests__/runtime.test.ts +16 -0
- package/src/__tests__/typechecker.test.ts +33 -0
- package/src/ast/types.ts +14 -1
- package/src/cli.ts +24 -10
- package/src/codegen/structure/index.ts +13 -2
- package/src/compile.ts +5 -1
- package/src/index.ts +5 -1
- package/src/lexer/index.ts +102 -1
- package/src/lowering/index.ts +38 -2
- package/src/optimizer/dce.ts +618 -0
- package/src/parser/index.ts +97 -17
- package/src/typechecker/index.ts +65 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeadCodeEliminator = void 0;
|
|
4
|
+
exports.eliminateDeadCode = eliminateDeadCode;
|
|
5
|
+
function copySpan(target, source) {
|
|
6
|
+
const descriptor = Object.getOwnPropertyDescriptor(source, 'span');
|
|
7
|
+
if (descriptor) {
|
|
8
|
+
Object.defineProperty(target, 'span', descriptor);
|
|
9
|
+
}
|
|
10
|
+
return target;
|
|
11
|
+
}
|
|
12
|
+
function isConstantBoolean(expr) {
|
|
13
|
+
if (expr.kind === 'bool_lit') {
|
|
14
|
+
return expr.value;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
function isPureExpr(expr) {
|
|
19
|
+
switch (expr.kind) {
|
|
20
|
+
case 'int_lit':
|
|
21
|
+
case 'float_lit':
|
|
22
|
+
case 'byte_lit':
|
|
23
|
+
case 'short_lit':
|
|
24
|
+
case 'long_lit':
|
|
25
|
+
case 'double_lit':
|
|
26
|
+
case 'rel_coord':
|
|
27
|
+
case 'local_coord':
|
|
28
|
+
case 'bool_lit':
|
|
29
|
+
case 'str_lit':
|
|
30
|
+
case 'mc_name':
|
|
31
|
+
case 'range_lit':
|
|
32
|
+
case 'selector':
|
|
33
|
+
case 'ident':
|
|
34
|
+
case 'blockpos':
|
|
35
|
+
return true;
|
|
36
|
+
case 'str_interp':
|
|
37
|
+
return expr.parts.every(part => typeof part === 'string' || isPureExpr(part));
|
|
38
|
+
case 'f_string':
|
|
39
|
+
return expr.parts.every(part => part.kind === 'text' || isPureExpr(part.expr));
|
|
40
|
+
case 'binary':
|
|
41
|
+
return isPureExpr(expr.left) && isPureExpr(expr.right);
|
|
42
|
+
case 'is_check':
|
|
43
|
+
return isPureExpr(expr.expr);
|
|
44
|
+
case 'unary':
|
|
45
|
+
return isPureExpr(expr.operand);
|
|
46
|
+
case 'member':
|
|
47
|
+
return isPureExpr(expr.obj);
|
|
48
|
+
case 'index':
|
|
49
|
+
return isPureExpr(expr.obj) && isPureExpr(expr.index);
|
|
50
|
+
case 'array_lit':
|
|
51
|
+
return expr.elements.every(isPureExpr);
|
|
52
|
+
case 'struct_lit':
|
|
53
|
+
return expr.fields.every(field => isPureExpr(field.value));
|
|
54
|
+
case 'lambda':
|
|
55
|
+
return true;
|
|
56
|
+
case 'assign':
|
|
57
|
+
case 'member_assign':
|
|
58
|
+
case 'call':
|
|
59
|
+
case 'invoke':
|
|
60
|
+
case 'static_call':
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
class DeadCodeEliminator {
|
|
65
|
+
constructor() {
|
|
66
|
+
this.functionMap = new Map();
|
|
67
|
+
this.reachableFunctions = new Set();
|
|
68
|
+
this.usedConstants = new Set();
|
|
69
|
+
this.localReads = new Set();
|
|
70
|
+
this.localDeclIds = new WeakMap();
|
|
71
|
+
this.localIdCounter = 0;
|
|
72
|
+
}
|
|
73
|
+
eliminate(program) {
|
|
74
|
+
this.functionMap.clear();
|
|
75
|
+
this.reachableFunctions.clear();
|
|
76
|
+
this.usedConstants.clear();
|
|
77
|
+
this.localReads.clear();
|
|
78
|
+
this.localIdCounter = 0;
|
|
79
|
+
for (const fn of program.declarations) {
|
|
80
|
+
this.functionMap.set(fn.name, fn);
|
|
81
|
+
}
|
|
82
|
+
const entryPoints = this.findEntryPoints(program);
|
|
83
|
+
if (entryPoints.length === 0) {
|
|
84
|
+
for (const fn of program.declarations) {
|
|
85
|
+
this.markReachable(fn.name);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
for (const fnName of entryPoints) {
|
|
90
|
+
this.markReachable(fnName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
for (const global of program.globals) {
|
|
94
|
+
this.collectExprRefs(global.init, []);
|
|
95
|
+
}
|
|
96
|
+
for (const implBlock of program.implBlocks) {
|
|
97
|
+
for (const method of implBlock.methods) {
|
|
98
|
+
this.collectFunctionRefs(method);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
...program,
|
|
103
|
+
declarations: program.declarations
|
|
104
|
+
.filter(fn => this.reachableFunctions.has(fn.name))
|
|
105
|
+
.map(fn => this.transformFunction(fn)),
|
|
106
|
+
consts: program.consts.filter(constDecl => this.usedConstants.has(constDecl.name)),
|
|
107
|
+
implBlocks: program.implBlocks.map(implBlock => ({
|
|
108
|
+
...implBlock,
|
|
109
|
+
methods: implBlock.methods.map(method => this.transformFunction(method)),
|
|
110
|
+
})),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
findEntryPoints(program) {
|
|
114
|
+
const entries = new Set();
|
|
115
|
+
for (const fn of program.declarations) {
|
|
116
|
+
if (fn.name === 'main') {
|
|
117
|
+
entries.add(fn.name);
|
|
118
|
+
}
|
|
119
|
+
if (fn.decorators.some(decorator => [
|
|
120
|
+
'tick',
|
|
121
|
+
'load',
|
|
122
|
+
'on',
|
|
123
|
+
'on_trigger',
|
|
124
|
+
'on_advancement',
|
|
125
|
+
'on_craft',
|
|
126
|
+
'on_death',
|
|
127
|
+
'on_login',
|
|
128
|
+
'on_join_team',
|
|
129
|
+
].includes(decorator.name))) {
|
|
130
|
+
entries.add(fn.name);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return [...entries];
|
|
134
|
+
}
|
|
135
|
+
markReachable(fnName) {
|
|
136
|
+
if (this.reachableFunctions.has(fnName)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const fn = this.functionMap.get(fnName);
|
|
140
|
+
if (!fn) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this.reachableFunctions.add(fnName);
|
|
144
|
+
this.collectFunctionRefs(fn);
|
|
145
|
+
}
|
|
146
|
+
collectFunctionRefs(fn) {
|
|
147
|
+
const scope = [fn.params.map(param => ({ id: `param:${fn.name}:${param.name}`, name: param.name }))];
|
|
148
|
+
for (const param of fn.params) {
|
|
149
|
+
if (param.default) {
|
|
150
|
+
this.collectExprRefs(param.default, scope);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
this.collectStmtRefs(fn.body, scope);
|
|
154
|
+
}
|
|
155
|
+
collectStmtRefs(block, scope) {
|
|
156
|
+
scope.push([]);
|
|
157
|
+
for (const stmt of block) {
|
|
158
|
+
this.collectStmtRef(stmt, scope);
|
|
159
|
+
}
|
|
160
|
+
scope.pop();
|
|
161
|
+
}
|
|
162
|
+
collectStmtRef(stmt, scope) {
|
|
163
|
+
switch (stmt.kind) {
|
|
164
|
+
case 'let': {
|
|
165
|
+
this.collectExprRefs(stmt.init, scope);
|
|
166
|
+
const id = `local:${stmt.name}:${this.localIdCounter++}:${(stmt.span?.line ?? 0)}:${(stmt.span?.col ?? 0)}`;
|
|
167
|
+
this.localDeclIds.set(stmt, id);
|
|
168
|
+
scope[scope.length - 1].push({ id, name: stmt.name });
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case 'expr':
|
|
172
|
+
this.collectExprRefs(stmt.expr, scope);
|
|
173
|
+
break;
|
|
174
|
+
case 'return':
|
|
175
|
+
if (stmt.value) {
|
|
176
|
+
this.collectExprRefs(stmt.value, scope);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
case 'if': {
|
|
180
|
+
this.collectExprRefs(stmt.cond, scope);
|
|
181
|
+
const constant = isConstantBoolean(stmt.cond);
|
|
182
|
+
if (constant === true) {
|
|
183
|
+
this.collectStmtRefs(stmt.then, scope);
|
|
184
|
+
}
|
|
185
|
+
else if (constant === false) {
|
|
186
|
+
if (stmt.else_) {
|
|
187
|
+
this.collectStmtRefs(stmt.else_, scope);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.collectStmtRefs(stmt.then, scope);
|
|
192
|
+
if (stmt.else_) {
|
|
193
|
+
this.collectStmtRefs(stmt.else_, scope);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case 'while':
|
|
199
|
+
this.collectExprRefs(stmt.cond, scope);
|
|
200
|
+
this.collectStmtRefs(stmt.body, scope);
|
|
201
|
+
break;
|
|
202
|
+
case 'for':
|
|
203
|
+
scope.push([]);
|
|
204
|
+
if (stmt.init) {
|
|
205
|
+
this.collectStmtRef(stmt.init, scope);
|
|
206
|
+
}
|
|
207
|
+
this.collectExprRefs(stmt.cond, scope);
|
|
208
|
+
this.collectExprRefs(stmt.step, scope);
|
|
209
|
+
this.collectStmtRefs(stmt.body, scope);
|
|
210
|
+
scope.pop();
|
|
211
|
+
break;
|
|
212
|
+
case 'foreach':
|
|
213
|
+
this.collectExprRefs(stmt.iterable, scope);
|
|
214
|
+
scope.push([{ id: `foreach:${stmt.binding}:${stmt.span?.line ?? 0}:${stmt.span?.col ?? 0}`, name: stmt.binding }]);
|
|
215
|
+
this.collectStmtRefs(stmt.body, scope);
|
|
216
|
+
scope.pop();
|
|
217
|
+
break;
|
|
218
|
+
case 'for_range':
|
|
219
|
+
this.collectExprRefs(stmt.start, scope);
|
|
220
|
+
this.collectExprRefs(stmt.end, scope);
|
|
221
|
+
scope.push([{ id: `range:${stmt.varName}:${stmt.span?.line ?? 0}:${stmt.span?.col ?? 0}`, name: stmt.varName }]);
|
|
222
|
+
this.collectStmtRefs(stmt.body, scope);
|
|
223
|
+
scope.pop();
|
|
224
|
+
break;
|
|
225
|
+
case 'match':
|
|
226
|
+
this.collectExprRefs(stmt.expr, scope);
|
|
227
|
+
for (const arm of stmt.arms) {
|
|
228
|
+
if (arm.pattern) {
|
|
229
|
+
this.collectExprRefs(arm.pattern, scope);
|
|
230
|
+
}
|
|
231
|
+
this.collectStmtRefs(arm.body, scope);
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
case 'as_block':
|
|
235
|
+
case 'at_block':
|
|
236
|
+
case 'as_at':
|
|
237
|
+
case 'execute':
|
|
238
|
+
this.collectNestedStmtRefs(stmt, scope);
|
|
239
|
+
break;
|
|
240
|
+
case 'raw':
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
collectNestedStmtRefs(stmt, scope) {
|
|
245
|
+
if (stmt.kind === 'execute') {
|
|
246
|
+
for (const sub of stmt.subcommands) {
|
|
247
|
+
if ('varName' in sub && sub.varName) {
|
|
248
|
+
const resolved = this.resolveLocal(sub.varName, scope);
|
|
249
|
+
if (resolved) {
|
|
250
|
+
this.localReads.add(resolved.id);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
this.collectStmtRefs(stmt.body, scope);
|
|
256
|
+
}
|
|
257
|
+
collectExprRefs(expr, scope) {
|
|
258
|
+
switch (expr.kind) {
|
|
259
|
+
case 'ident': {
|
|
260
|
+
const resolved = this.resolveLocal(expr.name, scope);
|
|
261
|
+
if (resolved) {
|
|
262
|
+
this.localReads.add(resolved.id);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
this.usedConstants.add(expr.name);
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case 'call':
|
|
270
|
+
{
|
|
271
|
+
const resolved = this.resolveLocal(expr.fn, scope);
|
|
272
|
+
if (resolved) {
|
|
273
|
+
this.localReads.add(resolved.id);
|
|
274
|
+
}
|
|
275
|
+
else if (this.functionMap.has(expr.fn)) {
|
|
276
|
+
this.markReachable(expr.fn);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
for (const arg of expr.args) {
|
|
280
|
+
this.collectExprRefs(arg, scope);
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
case 'static_call':
|
|
284
|
+
for (const arg of expr.args) {
|
|
285
|
+
this.collectExprRefs(arg, scope);
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
case 'invoke':
|
|
289
|
+
this.collectExprRefs(expr.callee, scope);
|
|
290
|
+
for (const arg of expr.args) {
|
|
291
|
+
this.collectExprRefs(arg, scope);
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
case 'member':
|
|
295
|
+
this.collectExprRefs(expr.obj, scope);
|
|
296
|
+
break;
|
|
297
|
+
case 'member_assign':
|
|
298
|
+
this.collectExprRefs(expr.obj, scope);
|
|
299
|
+
this.collectExprRefs(expr.value, scope);
|
|
300
|
+
break;
|
|
301
|
+
case 'index':
|
|
302
|
+
this.collectExprRefs(expr.obj, scope);
|
|
303
|
+
this.collectExprRefs(expr.index, scope);
|
|
304
|
+
break;
|
|
305
|
+
case 'array_lit':
|
|
306
|
+
expr.elements.forEach(element => this.collectExprRefs(element, scope));
|
|
307
|
+
break;
|
|
308
|
+
case 'struct_lit':
|
|
309
|
+
expr.fields.forEach(field => this.collectExprRefs(field.value, scope));
|
|
310
|
+
break;
|
|
311
|
+
case 'binary':
|
|
312
|
+
this.collectExprRefs(expr.left, scope);
|
|
313
|
+
this.collectExprRefs(expr.right, scope);
|
|
314
|
+
break;
|
|
315
|
+
case 'is_check':
|
|
316
|
+
this.collectExprRefs(expr.expr, scope);
|
|
317
|
+
break;
|
|
318
|
+
case 'unary':
|
|
319
|
+
this.collectExprRefs(expr.operand, scope);
|
|
320
|
+
break;
|
|
321
|
+
case 'assign': {
|
|
322
|
+
this.collectExprRefs(expr.value, scope);
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
case 'str_interp':
|
|
326
|
+
expr.parts.forEach(part => {
|
|
327
|
+
if (typeof part !== 'string') {
|
|
328
|
+
this.collectExprRefs(part, scope);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
break;
|
|
332
|
+
case 'f_string':
|
|
333
|
+
expr.parts.forEach(part => {
|
|
334
|
+
if (part.kind === 'expr') {
|
|
335
|
+
this.collectExprRefs(part.expr, scope);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
break;
|
|
339
|
+
case 'lambda': {
|
|
340
|
+
const lambdaScope = [
|
|
341
|
+
...scope.map(entries => [...entries]),
|
|
342
|
+
expr.params.map(param => ({ id: `lambda:${param.name}:${expr.span?.line ?? 0}:${expr.span?.col ?? 0}`, name: param.name })),
|
|
343
|
+
];
|
|
344
|
+
if (Array.isArray(expr.body)) {
|
|
345
|
+
this.collectStmtRefs(expr.body, lambdaScope);
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
this.collectExprRefs(expr.body, lambdaScope);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case 'blockpos':
|
|
353
|
+
case 'bool_lit':
|
|
354
|
+
case 'byte_lit':
|
|
355
|
+
case 'double_lit':
|
|
356
|
+
case 'float_lit':
|
|
357
|
+
case 'int_lit':
|
|
358
|
+
case 'long_lit':
|
|
359
|
+
case 'mc_name':
|
|
360
|
+
case 'range_lit':
|
|
361
|
+
case 'selector':
|
|
362
|
+
case 'short_lit':
|
|
363
|
+
case 'str_lit':
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
resolveLocal(name, scope) {
|
|
368
|
+
for (let i = scope.length - 1; i >= 0; i--) {
|
|
369
|
+
for (let j = scope[i].length - 1; j >= 0; j--) {
|
|
370
|
+
if (scope[i][j].name === name) {
|
|
371
|
+
return scope[i][j];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
transformFunction(fn) {
|
|
378
|
+
const scope = [fn.params.map(param => ({ id: `param:${fn.name}:${param.name}`, name: param.name }))];
|
|
379
|
+
const body = this.transformBlock(fn.body, scope);
|
|
380
|
+
return body === fn.body ? fn : copySpan({ ...fn, body }, fn);
|
|
381
|
+
}
|
|
382
|
+
transformBlock(block, scope) {
|
|
383
|
+
scope.push([]);
|
|
384
|
+
const transformed = [];
|
|
385
|
+
for (const stmt of block) {
|
|
386
|
+
const next = this.transformStmt(stmt, scope);
|
|
387
|
+
transformed.push(...next);
|
|
388
|
+
}
|
|
389
|
+
scope.pop();
|
|
390
|
+
return transformed;
|
|
391
|
+
}
|
|
392
|
+
transformStmt(stmt, scope) {
|
|
393
|
+
switch (stmt.kind) {
|
|
394
|
+
case 'let': {
|
|
395
|
+
const init = this.transformExpr(stmt.init, scope);
|
|
396
|
+
const id = this.localDeclIds.get(stmt) ?? `local:${stmt.name}:${stmt.span?.line ?? 0}:${stmt.span?.col ?? 0}`;
|
|
397
|
+
scope[scope.length - 1].push({ id, name: stmt.name });
|
|
398
|
+
if (this.localReads.has(id)) {
|
|
399
|
+
if (init === stmt.init) {
|
|
400
|
+
return [stmt];
|
|
401
|
+
}
|
|
402
|
+
return [copySpan({ ...stmt, init }, stmt)];
|
|
403
|
+
}
|
|
404
|
+
if (isPureExpr(init)) {
|
|
405
|
+
return [];
|
|
406
|
+
}
|
|
407
|
+
return [copySpan({ kind: 'expr', expr: init }, stmt)];
|
|
408
|
+
}
|
|
409
|
+
case 'expr': {
|
|
410
|
+
const expr = this.transformExpr(stmt.expr, scope);
|
|
411
|
+
if (expr.kind === 'assign') {
|
|
412
|
+
const resolved = this.resolveLocal(expr.target, scope);
|
|
413
|
+
if (resolved && !this.localReads.has(resolved.id)) {
|
|
414
|
+
if (isPureExpr(expr.value)) {
|
|
415
|
+
return [];
|
|
416
|
+
}
|
|
417
|
+
return [copySpan({ kind: 'expr', expr: expr.value }, stmt)];
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (expr === stmt.expr) {
|
|
421
|
+
return [stmt];
|
|
422
|
+
}
|
|
423
|
+
return [copySpan({ ...stmt, expr }, stmt)];
|
|
424
|
+
}
|
|
425
|
+
case 'return': {
|
|
426
|
+
if (!stmt.value) {
|
|
427
|
+
return [stmt];
|
|
428
|
+
}
|
|
429
|
+
const value = this.transformExpr(stmt.value, scope);
|
|
430
|
+
if (value === stmt.value) {
|
|
431
|
+
return [stmt];
|
|
432
|
+
}
|
|
433
|
+
return [copySpan({ ...stmt, value }, stmt)];
|
|
434
|
+
}
|
|
435
|
+
case 'if': {
|
|
436
|
+
const cond = this.transformExpr(stmt.cond, scope);
|
|
437
|
+
const constant = isConstantBoolean(cond);
|
|
438
|
+
if (constant === true) {
|
|
439
|
+
return this.transformBlock(stmt.then, scope);
|
|
440
|
+
}
|
|
441
|
+
if (constant === false) {
|
|
442
|
+
return stmt.else_ ? this.transformBlock(stmt.else_, scope) : [];
|
|
443
|
+
}
|
|
444
|
+
const thenBlock = this.transformBlock(stmt.then, scope);
|
|
445
|
+
const elseBlock = stmt.else_ ? this.transformBlock(stmt.else_, scope) : undefined;
|
|
446
|
+
if (cond === stmt.cond && thenBlock === stmt.then && elseBlock === stmt.else_) {
|
|
447
|
+
return [stmt];
|
|
448
|
+
}
|
|
449
|
+
return [copySpan({ ...stmt, cond, then: thenBlock, else_: elseBlock }, stmt)];
|
|
450
|
+
}
|
|
451
|
+
case 'while': {
|
|
452
|
+
const cond = this.transformExpr(stmt.cond, scope);
|
|
453
|
+
if (isConstantBoolean(cond) === false) {
|
|
454
|
+
return [];
|
|
455
|
+
}
|
|
456
|
+
const body = this.transformBlock(stmt.body, scope);
|
|
457
|
+
return [copySpan({ ...stmt, cond, body }, stmt)];
|
|
458
|
+
}
|
|
459
|
+
case 'for': {
|
|
460
|
+
const forScope = [...scope, []];
|
|
461
|
+
const init = stmt.init ? this.transformStmt(stmt.init, forScope)[0] : undefined;
|
|
462
|
+
const cond = this.transformExpr(stmt.cond, forScope);
|
|
463
|
+
if (isConstantBoolean(cond) === false) {
|
|
464
|
+
return init ? [init] : [];
|
|
465
|
+
}
|
|
466
|
+
const step = this.transformExpr(stmt.step, forScope);
|
|
467
|
+
const body = this.transformBlock(stmt.body, forScope);
|
|
468
|
+
return [copySpan({ ...stmt, init, cond, step, body }, stmt)];
|
|
469
|
+
}
|
|
470
|
+
case 'foreach': {
|
|
471
|
+
const iterable = this.transformExpr(stmt.iterable, scope);
|
|
472
|
+
const foreachScope = [...scope, [{ id: `foreach:${stmt.binding}:${stmt.span?.line ?? 0}:${stmt.span?.col ?? 0}`, name: stmt.binding }]];
|
|
473
|
+
const body = this.transformBlock(stmt.body, foreachScope);
|
|
474
|
+
return [copySpan({ ...stmt, iterable, body }, stmt)];
|
|
475
|
+
}
|
|
476
|
+
case 'for_range': {
|
|
477
|
+
const start = this.transformExpr(stmt.start, scope);
|
|
478
|
+
const end = this.transformExpr(stmt.end, scope);
|
|
479
|
+
const rangeScope = [...scope, [{ id: `range:${stmt.varName}:${stmt.span?.line ?? 0}:${stmt.span?.col ?? 0}`, name: stmt.varName }]];
|
|
480
|
+
const body = this.transformBlock(stmt.body, rangeScope);
|
|
481
|
+
return [copySpan({ ...stmt, start, end, body }, stmt)];
|
|
482
|
+
}
|
|
483
|
+
case 'match': {
|
|
484
|
+
const expr = this.transformExpr(stmt.expr, scope);
|
|
485
|
+
const arms = stmt.arms.map(arm => ({
|
|
486
|
+
pattern: arm.pattern ? this.transformExpr(arm.pattern, scope) : null,
|
|
487
|
+
body: this.transformBlock(arm.body, scope),
|
|
488
|
+
}));
|
|
489
|
+
return [copySpan({ ...stmt, expr, arms }, stmt)];
|
|
490
|
+
}
|
|
491
|
+
case 'as_block':
|
|
492
|
+
return [copySpan({ ...stmt, body: this.transformBlock(stmt.body, scope) }, stmt)];
|
|
493
|
+
case 'at_block':
|
|
494
|
+
return [copySpan({ ...stmt, body: this.transformBlock(stmt.body, scope) }, stmt)];
|
|
495
|
+
case 'as_at':
|
|
496
|
+
return [copySpan({ ...stmt, body: this.transformBlock(stmt.body, scope) }, stmt)];
|
|
497
|
+
case 'execute':
|
|
498
|
+
return [copySpan({ ...stmt, body: this.transformBlock(stmt.body, scope) }, stmt)];
|
|
499
|
+
case 'raw':
|
|
500
|
+
return [stmt];
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
transformExpr(expr, scope) {
|
|
504
|
+
switch (expr.kind) {
|
|
505
|
+
case 'call':
|
|
506
|
+
return copySpan({ ...expr, args: expr.args.map(arg => this.transformExpr(arg, scope)) }, expr);
|
|
507
|
+
case 'static_call':
|
|
508
|
+
return copySpan({ ...expr, args: expr.args.map(arg => this.transformExpr(arg, scope)) }, expr);
|
|
509
|
+
case 'invoke':
|
|
510
|
+
return copySpan({
|
|
511
|
+
...expr,
|
|
512
|
+
callee: this.transformExpr(expr.callee, scope),
|
|
513
|
+
args: expr.args.map(arg => this.transformExpr(arg, scope)),
|
|
514
|
+
}, expr);
|
|
515
|
+
case 'binary':
|
|
516
|
+
return copySpan({
|
|
517
|
+
...expr,
|
|
518
|
+
left: this.transformExpr(expr.left, scope),
|
|
519
|
+
right: this.transformExpr(expr.right, scope),
|
|
520
|
+
}, expr);
|
|
521
|
+
case 'is_check':
|
|
522
|
+
return copySpan({ ...expr, expr: this.transformExpr(expr.expr, scope) }, expr);
|
|
523
|
+
case 'unary':
|
|
524
|
+
return copySpan({ ...expr, operand: this.transformExpr(expr.operand, scope) }, expr);
|
|
525
|
+
case 'assign':
|
|
526
|
+
return copySpan({ ...expr, value: this.transformExpr(expr.value, scope) }, expr);
|
|
527
|
+
case 'member':
|
|
528
|
+
return copySpan({ ...expr, obj: this.transformExpr(expr.obj, scope) }, expr);
|
|
529
|
+
case 'member_assign':
|
|
530
|
+
return copySpan({
|
|
531
|
+
...expr,
|
|
532
|
+
obj: this.transformExpr(expr.obj, scope),
|
|
533
|
+
value: this.transformExpr(expr.value, scope),
|
|
534
|
+
}, expr);
|
|
535
|
+
case 'index':
|
|
536
|
+
return copySpan({
|
|
537
|
+
...expr,
|
|
538
|
+
obj: this.transformExpr(expr.obj, scope),
|
|
539
|
+
index: this.transformExpr(expr.index, scope),
|
|
540
|
+
}, expr);
|
|
541
|
+
case 'array_lit':
|
|
542
|
+
return copySpan({ ...expr, elements: expr.elements.map(element => this.transformExpr(element, scope)) }, expr);
|
|
543
|
+
case 'struct_lit':
|
|
544
|
+
return copySpan({
|
|
545
|
+
...expr,
|
|
546
|
+
fields: expr.fields.map(field => ({ ...field, value: this.transformExpr(field.value, scope) })),
|
|
547
|
+
}, expr);
|
|
548
|
+
case 'str_interp':
|
|
549
|
+
return copySpan({
|
|
550
|
+
...expr,
|
|
551
|
+
parts: expr.parts.map(part => typeof part === 'string' ? part : this.transformExpr(part, scope)),
|
|
552
|
+
}, expr);
|
|
553
|
+
case 'f_string':
|
|
554
|
+
return copySpan({
|
|
555
|
+
...expr,
|
|
556
|
+
parts: expr.parts.map(part => part.kind === 'text' ? part : { kind: 'expr', expr: this.transformExpr(part.expr, scope) }),
|
|
557
|
+
}, expr);
|
|
558
|
+
case 'lambda': {
|
|
559
|
+
const lambdaScope = [
|
|
560
|
+
...scope.map(entries => [...entries]),
|
|
561
|
+
expr.params.map(param => ({ id: `lambda:${param.name}:${expr.span?.line ?? 0}:${expr.span?.col ?? 0}`, name: param.name })),
|
|
562
|
+
];
|
|
563
|
+
const body = Array.isArray(expr.body)
|
|
564
|
+
? this.transformBlock(expr.body, lambdaScope)
|
|
565
|
+
: this.transformExpr(expr.body, lambdaScope);
|
|
566
|
+
return copySpan({ ...expr, body }, expr);
|
|
567
|
+
}
|
|
568
|
+
case 'blockpos':
|
|
569
|
+
case 'bool_lit':
|
|
570
|
+
case 'byte_lit':
|
|
571
|
+
case 'double_lit':
|
|
572
|
+
case 'float_lit':
|
|
573
|
+
case 'ident':
|
|
574
|
+
case 'int_lit':
|
|
575
|
+
case 'long_lit':
|
|
576
|
+
case 'mc_name':
|
|
577
|
+
case 'range_lit':
|
|
578
|
+
case 'rel_coord':
|
|
579
|
+
case 'local_coord':
|
|
580
|
+
case 'selector':
|
|
581
|
+
case 'short_lit':
|
|
582
|
+
case 'str_lit':
|
|
583
|
+
return expr;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
exports.DeadCodeEliminator = DeadCodeEliminator;
|
|
588
|
+
function eliminateDeadCode(program) {
|
|
589
|
+
return new DeadCodeEliminator().eliminate(program);
|
|
590
|
+
}
|
|
591
|
+
//# sourceMappingURL=dce.js.map
|
package/dist/parser/index.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export declare class Parser {
|
|
|
60
60
|
private parseLambdaExpr;
|
|
61
61
|
private finishLambdaExpr;
|
|
62
62
|
private parseStringExpr;
|
|
63
|
+
private parseFStringExpr;
|
|
63
64
|
private parseEmbeddedExpr;
|
|
64
65
|
private parseStructLit;
|
|
65
66
|
private parseArrayLit;
|
|
@@ -69,6 +70,7 @@ export declare class Parser {
|
|
|
69
70
|
private coordComponentTokenLength;
|
|
70
71
|
private parseBlockPos;
|
|
71
72
|
private parseCoordComponent;
|
|
73
|
+
private parseCoordOffsetFromValue;
|
|
72
74
|
private parseSignedCoordOffset;
|
|
73
75
|
private parseSelector;
|
|
74
76
|
private parseSelectorOrVarSelector;
|