redscript-mc 1.2.27 → 1.2.29
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/README.md +24 -13
- package/README.zh.md +16 -5
- package/dist/__tests__/cli.test.js +13 -13
- package/dist/__tests__/optimizer-advanced.test.js +4 -4
- package/dist/cli.js +13 -5
- package/dist/codegen/mcfunction/index.d.ts +4 -0
- package/dist/codegen/mcfunction/index.js +9 -4
- package/dist/compile.d.ts +5 -0
- package/dist/compile.js +9 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.js +11 -6
- package/dist/lowering/index.d.ts +3 -0
- package/dist/lowering/index.js +95 -65
- package/dist/optimizer/commands.d.ts +1 -0
- package/dist/optimizer/commands.js +18 -11
- package/dist/optimizer/structure.d.ts +1 -0
- package/dist/optimizer/structure.js +6 -1
- package/editors/vscode/out/extension.js +1797 -1157
- package/editors/vscode/package-lock.json +3 -3
- package/editors/vscode/package.json +1 -1
- package/editors/vscode/src/hover.ts +18 -0
- package/editors/vscode/syntaxes/redscript.tmLanguage.json +12 -3
- package/examples/math-showcase.mcrs +146 -0
- package/examples/readme-demo.mcrs +92 -0
- package/package.json +1 -1
- package/src/__tests__/cli.test.ts +13 -13
- package/src/__tests__/optimizer-advanced.test.ts +4 -4
- package/src/cli.ts +14 -5
- package/src/codegen/mcfunction/index.ts +14 -5
- package/src/compile.ts +16 -2
- package/src/index.ts +18 -7
- package/src/lowering/index.ts +95 -64
- package/src/optimizer/commands.ts +18 -12
- package/src/optimizer/structure.ts +6 -2
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setOptimizerObjective = setOptimizerObjective;
|
|
3
4
|
exports.createEmptyOptimizationStats = createEmptyOptimizationStats;
|
|
4
5
|
exports.mergeOptimizationStats = mergeOptimizationStats;
|
|
5
6
|
exports.applyLICM = applyLICM;
|
|
6
7
|
exports.applyCSE = applyCSE;
|
|
7
8
|
exports.batchSetblocks = batchSetblocks;
|
|
8
9
|
exports.optimizeCommandFunctions = optimizeCommandFunctions;
|
|
9
|
-
|
|
10
|
+
// Matches scoreboard reads for LICM/CSE — objective is captured in group 2
|
|
11
|
+
// so the optimizer can reconstruct the command with the same objective.
|
|
12
|
+
let _OBJ_PATTERN = 'rs';
|
|
13
|
+
function setOptimizerObjective(obj) { _OBJ_PATTERN = obj; }
|
|
14
|
+
function scoreboardReadRe() {
|
|
15
|
+
return new RegExp(`^execute store result score (\\$[A-Za-z0-9_]+) ${_OBJ_PATTERN} run scoreboard players get (\\S+) (\\S+)$`);
|
|
16
|
+
}
|
|
10
17
|
const SCOREBOARD_WRITE_RE = /^(?:scoreboard players (?:set|add|remove|reset)\s+(\S+)\s+(\S+)|scoreboard players operation\s+(\S+)\s+(\S+)\s+[+\-*/%]?= )/;
|
|
11
18
|
const EXECUTE_STORE_SCORE_RE = /^execute store result score (\S+) (\S+) run /;
|
|
12
19
|
const FUNCTION_CALL_RE = /^execute as (.+) run function ([^:]+):(.+)$/;
|
|
@@ -95,7 +102,7 @@ function applyLICMInternal(functions) {
|
|
|
95
102
|
const readInfo = new Map();
|
|
96
103
|
const scoreboardWrites = new Set();
|
|
97
104
|
for (const inner of loopFn.commands) {
|
|
98
|
-
const readMatch = inner.cmd.match(
|
|
105
|
+
const readMatch = inner.cmd.match(scoreboardReadRe());
|
|
99
106
|
if (readMatch) {
|
|
100
107
|
const [, temp, player, objective] = readMatch;
|
|
101
108
|
const key = `${player} ${objective}`;
|
|
@@ -110,7 +117,7 @@ function applyLICMInternal(functions) {
|
|
|
110
117
|
for (const info of readInfo.values()) {
|
|
111
118
|
const matches = inner.cmd.match(TEMP_RE) ?? [];
|
|
112
119
|
const usageCount = matches.filter(name => name === info.temp).length;
|
|
113
|
-
const isDef = inner.cmd.startsWith(`execute store result score ${info.temp}
|
|
120
|
+
const isDef = inner.cmd.startsWith(`execute store result score ${info.temp} ${_OBJ_PATTERN} run scoreboard players get `);
|
|
114
121
|
if (!isDef) {
|
|
115
122
|
info.uses += usageCount;
|
|
116
123
|
}
|
|
@@ -134,7 +141,7 @@ function applyLICMInternal(functions) {
|
|
|
134
141
|
const hoistedTemps = new Set(hoistable.map(item => item.temp));
|
|
135
142
|
const rewrittenLoopCommands = [];
|
|
136
143
|
for (const inner of loopFn.commands) {
|
|
137
|
-
const readMatch = inner.cmd.match(
|
|
144
|
+
const readMatch = inner.cmd.match(scoreboardReadRe());
|
|
138
145
|
if (readMatch && hoistedTemps.has(readMatch[1])) {
|
|
139
146
|
continue;
|
|
140
147
|
}
|
|
@@ -142,7 +149,7 @@ function applyLICMInternal(functions) {
|
|
|
142
149
|
}
|
|
143
150
|
loopFn.commands = rewrittenLoopCommands;
|
|
144
151
|
nextCommands.push(...hoistable.map(item => ({
|
|
145
|
-
cmd: `execute store result score ${item.temp}
|
|
152
|
+
cmd: `execute store result score ${item.temp} ${_OBJ_PATTERN} run scoreboard players get ${item.player} ${item.objective}`,
|
|
146
153
|
})), command);
|
|
147
154
|
stats.licmHoists = (stats.licmHoists ?? 0) + hoistable.length;
|
|
148
155
|
stats.licmLoopBodies = (stats.licmLoopBodies ?? 0) + 1;
|
|
@@ -152,9 +159,9 @@ function applyLICMInternal(functions) {
|
|
|
152
159
|
return stats;
|
|
153
160
|
}
|
|
154
161
|
function extractArithmeticExpression(commands, index) {
|
|
155
|
-
const assign = commands[index]?.cmd.match(
|
|
156
|
-
commands[index]?.cmd.match(
|
|
157
|
-
const op = commands[index + 1]?.cmd.match(
|
|
162
|
+
const assign = commands[index]?.cmd.match(new RegExp(`^scoreboard players operation (\\$[A-Za-z0-9_]+) ${_OBJ_PATTERN} = (\\$[A-Za-z0-9_]+|\\$const_-?\\d+) ${_OBJ_PATTERN}$`)) ??
|
|
163
|
+
commands[index]?.cmd.match(new RegExp(`^scoreboard players set (\\$[A-Za-z0-9_]+) ${_OBJ_PATTERN} (-?\\d+)$`));
|
|
164
|
+
const op = commands[index + 1]?.cmd.match(new RegExp(`^scoreboard players operation (\\$[A-Za-z0-9_]+) ${_OBJ_PATTERN} ([+\\-*/%]=) (\\$[A-Za-z0-9_]+|\\$const_-?\\d+) ${_OBJ_PATTERN}$`));
|
|
158
165
|
if (!assign || !op || assign[1] !== op[1]) {
|
|
159
166
|
return null;
|
|
160
167
|
}
|
|
@@ -184,14 +191,14 @@ function applyCSEInternal(functions) {
|
|
|
184
191
|
}
|
|
185
192
|
for (let i = 0; i < commands.length; i++) {
|
|
186
193
|
const command = commands[i];
|
|
187
|
-
const readMatch = command.cmd.match(
|
|
194
|
+
const readMatch = command.cmd.match(scoreboardReadRe());
|
|
188
195
|
if (readMatch) {
|
|
189
196
|
const [, dst, player, objective] = readMatch;
|
|
190
197
|
const key = `${player} ${objective}`;
|
|
191
198
|
const cached = readCache.get(key);
|
|
192
199
|
if (cached) {
|
|
193
200
|
stats.cseRedundantReads = (stats.cseRedundantReads ?? 0) + 1;
|
|
194
|
-
rewritten.push({ ...command, cmd: `scoreboard players operation ${dst}
|
|
201
|
+
rewritten.push({ ...command, cmd: `scoreboard players operation ${dst} ${_OBJ_PATTERN} = ${cached} ${_OBJ_PATTERN}` });
|
|
195
202
|
}
|
|
196
203
|
else {
|
|
197
204
|
readCache.set(key, dst);
|
|
@@ -205,7 +212,7 @@ function applyCSEInternal(functions) {
|
|
|
205
212
|
if (expr) {
|
|
206
213
|
const cached = exprCache.get(expr.key);
|
|
207
214
|
if (cached) {
|
|
208
|
-
rewritten.push({ ...commands[i], cmd: `scoreboard players operation ${expr.dst}
|
|
215
|
+
rewritten.push({ ...commands[i], cmd: `scoreboard players operation ${expr.dst} ${_OBJ_PATTERN} = ${cached} ${_OBJ_PATTERN}` });
|
|
209
216
|
stats.cseArithmetic = (stats.cseArithmetic ?? 0) + 1;
|
|
210
217
|
i += 1;
|
|
211
218
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { IRCommand, IRFunction } from '../ir/types';
|
|
2
2
|
import { type OptimizationStats } from './commands';
|
|
3
|
+
export declare function setStructureObjective(obj: string): void;
|
|
3
4
|
export declare function optimizeFunctionForStructure(fn: IRFunction, functions: Map<string, IRFunction>, namespace: string): IRCommand[];
|
|
4
5
|
export declare function optimizeForStructure(functions: IRFunction[], namespace?: string): IRFunction[];
|
|
5
6
|
export declare function optimizeForStructureWithStats(functions: IRFunction[], namespace?: string): {
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setStructureObjective = setStructureObjective;
|
|
3
4
|
exports.optimizeFunctionForStructure = optimizeFunctionForStructure;
|
|
4
5
|
exports.optimizeForStructure = optimizeForStructure;
|
|
5
6
|
exports.optimizeForStructureWithStats = optimizeForStructureWithStats;
|
|
6
7
|
const commands_1 = require("./commands");
|
|
7
|
-
|
|
8
|
+
let OBJ = 'rs';
|
|
9
|
+
function setStructureObjective(obj) {
|
|
10
|
+
OBJ = obj;
|
|
11
|
+
(0, commands_1.setOptimizerObjective)(obj);
|
|
12
|
+
}
|
|
8
13
|
const INLINE_THRESHOLD = 8;
|
|
9
14
|
const BOP_OP = {
|
|
10
15
|
'+': '+=',
|