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.
@@ -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
- const SCOREBOARD_READ_RE = /^execute store result score (\$[A-Za-z0-9_]+) rs run scoreboard players get (\S+) (\S+)$/;
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(SCOREBOARD_READ_RE);
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} rs run scoreboard players get `);
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(SCOREBOARD_READ_RE);
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} rs run scoreboard players get ${item.player} ${item.objective}`,
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(/^scoreboard players operation (\$[A-Za-z0-9_]+) rs = (\$[A-Za-z0-9_]+|\$const_-?\d+) rs$/) ??
156
- commands[index]?.cmd.match(/^scoreboard players set (\$[A-Za-z0-9_]+) rs (-?\d+)$/);
157
- const op = commands[index + 1]?.cmd.match(/^scoreboard players operation (\$[A-Za-z0-9_]+) rs ([+\-*/%]=) (\$[A-Za-z0-9_]+|\$const_-?\d+) rs$/);
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(SCOREBOARD_READ_RE);
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} rs = ${cached} rs` });
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} rs = ${cached} rs` });
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
- const OBJ = 'rs';
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
  '+': '+=',