redscript-mc 1.2.12 → 1.2.14

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Tests for MC 1.20.2+ macro function support
3
+ *
4
+ * When a function uses runtime parameters in positions that require literal
5
+ * values in MC commands (coordinates, entity types, etc.), RedScript should
6
+ * automatically compile it as a macro function using $-prefixed syntax.
7
+ */
8
+ export {};
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+ /**
3
+ * Tests for MC 1.20.2+ macro function support
4
+ *
5
+ * When a function uses runtime parameters in positions that require literal
6
+ * values in MC commands (coordinates, entity types, etc.), RedScript should
7
+ * automatically compile it as a macro function using $-prefixed syntax.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const lexer_1 = require("../lexer");
11
+ const parser_1 = require("../parser");
12
+ const lowering_1 = require("../lowering");
13
+ const mcfunction_1 = require("../codegen/mcfunction");
14
+ function compile(source, namespace = 'test') {
15
+ const tokens = new lexer_1.Lexer(source).tokenize();
16
+ const ast = new parser_1.Parser(tokens).parse(namespace);
17
+ const lowering = new lowering_1.Lowering(namespace);
18
+ return lowering.lower(ast);
19
+ }
20
+ function getFunction(module, name) {
21
+ return module.functions.find(f => f.name === name);
22
+ }
23
+ function getRawCommands(fn) {
24
+ return fn.blocks
25
+ .flatMap(b => b.instrs)
26
+ .filter((i) => i.op === 'raw')
27
+ .map(i => i.cmd);
28
+ }
29
+ function getGeneratedContent(module, fnName) {
30
+ const files = (0, mcfunction_1.generateDatapack)(module);
31
+ const file = files.find(f => f.path.includes(`/${fnName}.mcfunction`));
32
+ return file?.content;
33
+ }
34
+ // ---------------------------------------------------------------------------
35
+ // Macro function detection
36
+ // ---------------------------------------------------------------------------
37
+ describe('MC macro function detection', () => {
38
+ it('marks function as macro when int param used in summon coordinates', () => {
39
+ const ir = compile(`
40
+ fn spawn_zombie(x: int, y: int, z: int) {
41
+ summon("minecraft:zombie", x, y, z);
42
+ }
43
+ `);
44
+ const fn = getFunction(ir, 'spawn_zombie');
45
+ expect(fn).toBeDefined();
46
+ expect(fn.isMacroFunction).toBe(true);
47
+ expect(fn.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']));
48
+ });
49
+ it('does NOT mark function as macro when all summon args are constants', () => {
50
+ const ir = compile(`
51
+ fn spawn_fixed() {
52
+ summon("minecraft:zombie", 100, 64, 200);
53
+ }
54
+ `);
55
+ const fn = getFunction(ir, 'spawn_fixed');
56
+ expect(fn).toBeDefined();
57
+ expect(fn.isMacroFunction).toBeFalsy();
58
+ });
59
+ it('marks function as macro when int param used in particle coordinates', () => {
60
+ const ir = compile(`
61
+ fn show_particle(x: int, y: int, z: int) {
62
+ particle("minecraft:flame", x, y, z);
63
+ }
64
+ `);
65
+ const fn = getFunction(ir, 'show_particle');
66
+ expect(fn).toBeDefined();
67
+ expect(fn.isMacroFunction).toBe(true);
68
+ });
69
+ it('marks function as macro when int param used in setblock coordinates', () => {
70
+ const ir = compile(`
71
+ fn place_block(x: int, y: int, z: int) {
72
+ setblock(x, y, z, "minecraft:stone");
73
+ }
74
+ `);
75
+ const fn = getFunction(ir, 'place_block');
76
+ expect(fn).toBeDefined();
77
+ expect(fn.isMacroFunction).toBe(true);
78
+ expect(fn.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']));
79
+ });
80
+ it('identifies only the params used in macro positions', () => {
81
+ const ir = compile(`
82
+ fn do_stuff(count: int, x: int, y: int, z: int) {
83
+ summon("minecraft:zombie", x, y, z);
84
+ // count is not used in a macro position
85
+ }
86
+ `);
87
+ const fn = getFunction(ir, 'do_stuff');
88
+ expect(fn).toBeDefined();
89
+ expect(fn.isMacroFunction).toBe(true);
90
+ // x, y, z should be macro params; count should NOT be
91
+ expect(fn.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']));
92
+ expect(fn.macroParamNames).not.toContain('count');
93
+ });
94
+ });
95
+ // ---------------------------------------------------------------------------
96
+ // Macro command generation in function body
97
+ // ---------------------------------------------------------------------------
98
+ describe('MC macro command generation', () => {
99
+ it('generates $-prefixed summon command with $(param) for macro params', () => {
100
+ const ir = compile(`
101
+ fn spawn_zombie(x: int, y: int, z: int) {
102
+ summon("minecraft:zombie", x, y, z);
103
+ }
104
+ `);
105
+ const fn = getFunction(ir, 'spawn_zombie');
106
+ const cmds = getRawCommands(fn);
107
+ // Should have a macro command for summon
108
+ const macroCmd = cmds.find(c => c.startsWith('$summon'));
109
+ expect(macroCmd).toBeDefined();
110
+ expect(macroCmd).toContain('$(x)');
111
+ expect(macroCmd).toContain('$(y)');
112
+ expect(macroCmd).toContain('$(z)');
113
+ expect(macroCmd).toBe('$summon minecraft:zombie $(x) $(y) $(z)');
114
+ });
115
+ it('generates non-prefixed command when args are literals', () => {
116
+ const ir = compile(`
117
+ fn spawn_fixed() {
118
+ summon("minecraft:zombie", 100, 64, 200);
119
+ }
120
+ `);
121
+ const fn = getFunction(ir, 'spawn_fixed');
122
+ const cmds = getRawCommands(fn);
123
+ const summonCmd = cmds.find(c => c.includes('summon'));
124
+ expect(summonCmd).toBeDefined();
125
+ expect(summonCmd.startsWith('$')).toBe(false);
126
+ expect(summonCmd).toContain('100');
127
+ expect(summonCmd).toContain('64');
128
+ expect(summonCmd).toContain('200');
129
+ });
130
+ it('generates $-prefixed particle command with $(param)', () => {
131
+ const ir = compile(`
132
+ fn show_particle(x: int, y: int, z: int) {
133
+ particle("minecraft:flame", x, y, z);
134
+ }
135
+ `);
136
+ const fn = getFunction(ir, 'show_particle');
137
+ const cmds = getRawCommands(fn);
138
+ const macroCmd = cmds.find(c => c.startsWith('$particle'));
139
+ expect(macroCmd).toBeDefined();
140
+ expect(macroCmd).toContain('$(x)');
141
+ });
142
+ it('generates $-prefixed setblock command with $(param)', () => {
143
+ const ir = compile(`
144
+ fn place_block(x: int, y: int, z: int) {
145
+ setblock(x, y, z, "minecraft:stone");
146
+ }
147
+ `);
148
+ const fn = getFunction(ir, 'place_block');
149
+ const cmds = getRawCommands(fn);
150
+ const macroCmd = cmds.find(c => c.startsWith('$setblock'));
151
+ expect(macroCmd).toBeDefined();
152
+ expect(macroCmd).toContain('$(x)');
153
+ expect(macroCmd).toContain('$(y)');
154
+ expect(macroCmd).toContain('$(z)');
155
+ expect(macroCmd).toContain('minecraft:stone');
156
+ });
157
+ });
158
+ // ---------------------------------------------------------------------------
159
+ // Call site code generation
160
+ // ---------------------------------------------------------------------------
161
+ describe('MC macro call site generation', () => {
162
+ it('emits NBT setup + with-storage call for variable args', () => {
163
+ const ir = compile(`
164
+ fn spawn_zombie(x: int, y: int, z: int) {
165
+ summon("minecraft:zombie", x, y, z);
166
+ }
167
+
168
+ fn caller(px: int, pz: int) {
169
+ spawn_zombie(px, 64, pz);
170
+ }
171
+ `);
172
+ const callerFn = getFunction(ir, 'caller');
173
+ const cmds = getRawCommands(callerFn);
174
+ // Should have NBT setup for variable params (px → x, pz → z)
175
+ const xSetup = cmds.find(c => c.includes('macro_args') && c.includes(' x '));
176
+ const zSetup = cmds.find(c => c.includes('macro_args') && c.includes(' z '));
177
+ expect(xSetup).toBeDefined();
178
+ expect(zSetup).toBeDefined();
179
+ // Should have 'function test:spawn_zombie with storage rs:macro_args'
180
+ const callCmd = cmds.find(c => c.includes('spawn_zombie') && c.includes('with storage'));
181
+ expect(callCmd).toBeDefined();
182
+ expect(callCmd).toContain('rs:macro_args');
183
+ });
184
+ it('emits NBT setup for constant args too', () => {
185
+ const ir = compile(`
186
+ fn spawn_zombie(x: int, y: int, z: int) {
187
+ summon("minecraft:zombie", x, y, z);
188
+ }
189
+
190
+ fn caller_const() {
191
+ spawn_zombie(100, 64, 200);
192
+ }
193
+ `);
194
+ const callerFn = getFunction(ir, 'caller_const');
195
+ const cmds = getRawCommands(callerFn);
196
+ // Should have NBT setup for all macro params
197
+ const nbtCmds = cmds.filter(c => c.includes('macro_args'));
198
+ expect(nbtCmds.length).toBeGreaterThan(0);
199
+ // Should call with storage
200
+ const callCmd = cmds.find(c => c.includes('spawn_zombie') && c.includes('with storage'));
201
+ expect(callCmd).toBeDefined();
202
+ });
203
+ it('correctly sets up int variable args into NBT storage', () => {
204
+ const ir = compile(`
205
+ fn spawn_zombie(x: int, y: int, z: int) {
206
+ summon("minecraft:zombie", x, y, z);
207
+ }
208
+
209
+ fn caller(my_x: int) {
210
+ spawn_zombie(my_x, 64, 0);
211
+ }
212
+ `);
213
+ const callerFn = getFunction(ir, 'caller');
214
+ const cmds = getRawCommands(callerFn);
215
+ // For variable arg my_x → x: should use execute store result
216
+ const varSetup = cmds.find(c => c.includes('execute store result storage rs:macro_args x') &&
217
+ c.includes('scoreboard players get'));
218
+ expect(varSetup).toBeDefined();
219
+ // For constant 64 → y: should use data modify ... set value
220
+ const constSetup = cmds.find(c => c.includes('data modify storage rs:macro_args y set value 64'));
221
+ expect(constSetup).toBeDefined();
222
+ });
223
+ });
224
+ // ---------------------------------------------------------------------------
225
+ // Codegen output (mcfunction file content)
226
+ // ---------------------------------------------------------------------------
227
+ describe('MC macro function codegen output', () => {
228
+ it('generates $-prefixed lines in the macro function mcfunction file', () => {
229
+ const ir = compile(`
230
+ fn spawn_zombie(x: int, y: int, z: int) {
231
+ summon("minecraft:zombie", x, y, z);
232
+ }
233
+ `);
234
+ const content = getGeneratedContent(ir, 'spawn_zombie');
235
+ expect(content).toBeDefined();
236
+ expect(content).toContain('$summon minecraft:zombie $(x) $(y) $(z)');
237
+ });
238
+ it('generates correct call site in caller mcfunction file', () => {
239
+ const ir = compile(`
240
+ fn spawn_zombie(x: int, y: int, z: int) {
241
+ summon("minecraft:zombie", x, y, z);
242
+ }
243
+
244
+ fn caller(px: int, pz: int) {
245
+ spawn_zombie(px, 64, pz);
246
+ }
247
+ `);
248
+ const content = getGeneratedContent(ir, 'caller');
249
+ expect(content).toBeDefined();
250
+ expect(content).toContain('with storage rs:macro_args');
251
+ expect(content).toContain('spawn_zombie');
252
+ });
253
+ });
254
+ // ---------------------------------------------------------------------------
255
+ // Edge cases
256
+ // ---------------------------------------------------------------------------
257
+ describe('MC macro edge cases', () => {
258
+ it('handles mixed literal and variable args correctly', () => {
259
+ const ir = compile(`
260
+ fn teleport_y(y: int) {
261
+ summon("minecraft:zombie", 100, y, 200);
262
+ }
263
+ `);
264
+ const fn = getFunction(ir, 'teleport_y');
265
+ expect(fn.isMacroFunction).toBe(true);
266
+ expect(fn.macroParamNames).toContain('y');
267
+ expect(fn.macroParamNames).not.toContain('x');
268
+ const cmds = getRawCommands(fn);
269
+ const macroCmd = cmds.find(c => c.startsWith('$summon'));
270
+ expect(macroCmd).toBeDefined();
271
+ // x and z are literals, y is macro
272
+ expect(macroCmd).toContain('100');
273
+ expect(macroCmd).toContain('$(y)');
274
+ expect(macroCmd).toContain('200');
275
+ });
276
+ it('non-macro functions still work normally', () => {
277
+ const ir = compile(`
278
+ fn greet() {
279
+ say("hello world");
280
+ }
281
+ `);
282
+ const fn = getFunction(ir, 'greet');
283
+ expect(fn.isMacroFunction).toBeFalsy();
284
+ const cmds = getRawCommands(fn);
285
+ const sayCmd = cmds.find(c => c.includes('say') || c.includes('tellraw'));
286
+ expect(sayCmd).toBeDefined();
287
+ expect(sayCmd.startsWith('$')).toBe(false);
288
+ });
289
+ it('macro function with params used in arithmetic still works', () => {
290
+ const ir = compile(`
291
+ fn spawn_offset(x: int, y: int, z: int) {
292
+ summon("minecraft:zombie", x, y, z);
293
+ // params are also used in the macro commands
294
+ }
295
+ `);
296
+ const fn = getFunction(ir, 'spawn_offset');
297
+ expect(fn.isMacroFunction).toBe(true);
298
+ // The macro commands should use $(param) syntax
299
+ const cmds = getRawCommands(fn);
300
+ const macroCmd = cmds.find(c => c.startsWith('$summon'));
301
+ expect(macroCmd).toBeDefined();
302
+ expect(macroCmd).toContain('$(x)');
303
+ });
304
+ });
305
+ //# sourceMappingURL=macro.test.js.map
@@ -201,8 +201,16 @@ function applyFunctionOptimization(files) {
201
201
  commands: entry.commands,
202
202
  })));
203
203
  const commandMap = new Map(optimized.functions.map(fn => [fn.name, fn.commands]));
204
+ // Filter out files for functions that were removed (inlined trivial functions)
205
+ const optimizedNames = new Set(optimized.functions.map(fn => fn.name));
204
206
  return {
205
- files: files.map(file => {
207
+ files: files
208
+ .filter(file => {
209
+ const functionName = toFunctionName(file);
210
+ // Keep non-function files and functions that weren't removed
211
+ return !functionName || optimizedNames.has(functionName);
212
+ })
213
+ .map(file => {
206
214
  const functionName = toFunctionName(file);
207
215
  if (!functionName)
208
216
  return file;
@@ -114,6 +114,8 @@ export interface IRFunction {
114
114
  eventType: string;
115
115
  tag: string;
116
116
  };
117
+ isMacroFunction?: boolean;
118
+ macroParamNames?: string[];
117
119
  }
118
120
  export interface GlobalVar {
119
121
  name: string;
@@ -47,7 +47,29 @@ export declare class Lowering {
47
47
  private floatVars;
48
48
  private worldObjCounter;
49
49
  private loopStack;
50
+ private currentFnParamNames;
51
+ private currentFnMacroParams;
52
+ private macroFunctionInfo;
50
53
  constructor(namespace: string, sourceRanges?: SourceRange[]);
54
+ private preScanMacroFunctions;
55
+ private preScanStmts;
56
+ private preScanStmt;
57
+ private preScanExpr;
58
+ /**
59
+ * If `expr` is a function parameter that needs macro treatment (runtime value
60
+ * used in a literal position), returns the param name; otherwise null.
61
+ */
62
+ private tryGetMacroParam;
63
+ /**
64
+ * Converts an expression to a string for use as a builtin arg.
65
+ * If the expression is a macro param, returns `$(name)` and sets macroParam.
66
+ */
67
+ private exprToBuiltinArg;
68
+ /**
69
+ * Emits a call to a macro function, setting up both scoreboard params
70
+ * (for arithmetic use) and NBT macro args (for coordinate/literal use).
71
+ */
72
+ private emitMacroFunctionCall;
51
73
  lower(program: Program): IRModule;
52
74
  private lowerFn;
53
75
  private getTickRate;
@@ -124,6 +146,7 @@ export declare class Lowering {
124
146
  private filePathForSpan;
125
147
  private lowerCoordinateBuiltin;
126
148
  private lowerTpCommand;
149
+ private lowerTpCommandMacroAware;
127
150
  private resolveBlockPosExpr;
128
151
  private getArrayStorageName;
129
152
  private inferLambdaReturnType;