redscript-mc 1.2.13 → 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,343 @@
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
+
9
+ import { Lexer } from '../lexer'
10
+ import { Parser } from '../parser'
11
+ import { Lowering } from '../lowering'
12
+ import { generateDatapack } from '../codegen/mcfunction'
13
+ import type { IRModule, IRFunction, IRInstr } from '../ir/types'
14
+
15
+ function compile(source: string, namespace = 'test'): IRModule {
16
+ const tokens = new Lexer(source).tokenize()
17
+ const ast = new Parser(tokens).parse(namespace)
18
+ const lowering = new Lowering(namespace)
19
+ return lowering.lower(ast)
20
+ }
21
+
22
+ function getFunction(module: IRModule, name: string): IRFunction | undefined {
23
+ return module.functions.find(f => f.name === name)
24
+ }
25
+
26
+ function getRawCommands(fn: IRFunction): string[] {
27
+ return fn.blocks
28
+ .flatMap(b => b.instrs)
29
+ .filter((i): i is IRInstr & { op: 'raw' } => i.op === 'raw')
30
+ .map(i => i.cmd)
31
+ }
32
+
33
+ function getGeneratedContent(module: IRModule, fnName: string): string | undefined {
34
+ const files = generateDatapack(module)
35
+ const file = files.find(f => f.path.includes(`/${fnName}.mcfunction`))
36
+ return file?.content
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Macro function detection
41
+ // ---------------------------------------------------------------------------
42
+
43
+ describe('MC macro function detection', () => {
44
+ it('marks function as macro when int param used in summon coordinates', () => {
45
+ const ir = compile(`
46
+ fn spawn_zombie(x: int, y: int, z: int) {
47
+ summon("minecraft:zombie", x, y, z);
48
+ }
49
+ `)
50
+ const fn = getFunction(ir, 'spawn_zombie')
51
+ expect(fn).toBeDefined()
52
+ expect(fn!.isMacroFunction).toBe(true)
53
+ expect(fn!.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']))
54
+ })
55
+
56
+ it('does NOT mark function as macro when all summon args are constants', () => {
57
+ const ir = compile(`
58
+ fn spawn_fixed() {
59
+ summon("minecraft:zombie", 100, 64, 200);
60
+ }
61
+ `)
62
+ const fn = getFunction(ir, 'spawn_fixed')
63
+ expect(fn).toBeDefined()
64
+ expect(fn!.isMacroFunction).toBeFalsy()
65
+ })
66
+
67
+ it('marks function as macro when int param used in particle coordinates', () => {
68
+ const ir = compile(`
69
+ fn show_particle(x: int, y: int, z: int) {
70
+ particle("minecraft:flame", x, y, z);
71
+ }
72
+ `)
73
+ const fn = getFunction(ir, 'show_particle')
74
+ expect(fn).toBeDefined()
75
+ expect(fn!.isMacroFunction).toBe(true)
76
+ })
77
+
78
+ it('marks function as macro when int param used in setblock coordinates', () => {
79
+ const ir = compile(`
80
+ fn place_block(x: int, y: int, z: int) {
81
+ setblock(x, y, z, "minecraft:stone");
82
+ }
83
+ `)
84
+ const fn = getFunction(ir, 'place_block')
85
+ expect(fn).toBeDefined()
86
+ expect(fn!.isMacroFunction).toBe(true)
87
+ expect(fn!.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']))
88
+ })
89
+
90
+ it('identifies only the params used in macro positions', () => {
91
+ const ir = compile(`
92
+ fn do_stuff(count: int, x: int, y: int, z: int) {
93
+ summon("minecraft:zombie", x, y, z);
94
+ // count is not used in a macro position
95
+ }
96
+ `)
97
+ const fn = getFunction(ir, 'do_stuff')
98
+ expect(fn).toBeDefined()
99
+ expect(fn!.isMacroFunction).toBe(true)
100
+ // x, y, z should be macro params; count should NOT be
101
+ expect(fn!.macroParamNames).toEqual(expect.arrayContaining(['x', 'y', 'z']))
102
+ expect(fn!.macroParamNames).not.toContain('count')
103
+ })
104
+ })
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // Macro command generation in function body
108
+ // ---------------------------------------------------------------------------
109
+
110
+ describe('MC macro command generation', () => {
111
+ it('generates $-prefixed summon command with $(param) for macro params', () => {
112
+ const ir = compile(`
113
+ fn spawn_zombie(x: int, y: int, z: int) {
114
+ summon("minecraft:zombie", x, y, z);
115
+ }
116
+ `)
117
+ const fn = getFunction(ir, 'spawn_zombie')!
118
+ const cmds = getRawCommands(fn)
119
+
120
+ // Should have a macro command for summon
121
+ const macroCmd = cmds.find(c => c.startsWith('$summon'))
122
+ expect(macroCmd).toBeDefined()
123
+ expect(macroCmd).toContain('$(x)')
124
+ expect(macroCmd).toContain('$(y)')
125
+ expect(macroCmd).toContain('$(z)')
126
+ expect(macroCmd).toBe('$summon minecraft:zombie $(x) $(y) $(z)')
127
+ })
128
+
129
+ it('generates non-prefixed command when args are literals', () => {
130
+ const ir = compile(`
131
+ fn spawn_fixed() {
132
+ summon("minecraft:zombie", 100, 64, 200);
133
+ }
134
+ `)
135
+ const fn = getFunction(ir, 'spawn_fixed')!
136
+ const cmds = getRawCommands(fn)
137
+ const summonCmd = cmds.find(c => c.includes('summon'))
138
+ expect(summonCmd).toBeDefined()
139
+ expect(summonCmd!.startsWith('$')).toBe(false)
140
+ expect(summonCmd).toContain('100')
141
+ expect(summonCmd).toContain('64')
142
+ expect(summonCmd).toContain('200')
143
+ })
144
+
145
+ it('generates $-prefixed particle command with $(param)', () => {
146
+ const ir = compile(`
147
+ fn show_particle(x: int, y: int, z: int) {
148
+ particle("minecraft:flame", x, y, z);
149
+ }
150
+ `)
151
+ const fn = getFunction(ir, 'show_particle')!
152
+ const cmds = getRawCommands(fn)
153
+ const macroCmd = cmds.find(c => c.startsWith('$particle'))
154
+ expect(macroCmd).toBeDefined()
155
+ expect(macroCmd).toContain('$(x)')
156
+ })
157
+
158
+ it('generates $-prefixed setblock command with $(param)', () => {
159
+ const ir = compile(`
160
+ fn place_block(x: int, y: int, z: int) {
161
+ setblock(x, y, z, "minecraft:stone");
162
+ }
163
+ `)
164
+ const fn = getFunction(ir, 'place_block')!
165
+ const cmds = getRawCommands(fn)
166
+ const macroCmd = cmds.find(c => c.startsWith('$setblock'))
167
+ expect(macroCmd).toBeDefined()
168
+ expect(macroCmd).toContain('$(x)')
169
+ expect(macroCmd).toContain('$(y)')
170
+ expect(macroCmd).toContain('$(z)')
171
+ expect(macroCmd).toContain('minecraft:stone')
172
+ })
173
+ })
174
+
175
+ // ---------------------------------------------------------------------------
176
+ // Call site code generation
177
+ // ---------------------------------------------------------------------------
178
+
179
+ describe('MC macro call site generation', () => {
180
+ it('emits NBT setup + with-storage call for variable args', () => {
181
+ const ir = compile(`
182
+ fn spawn_zombie(x: int, y: int, z: int) {
183
+ summon("minecraft:zombie", x, y, z);
184
+ }
185
+
186
+ fn caller(px: int, pz: int) {
187
+ spawn_zombie(px, 64, pz);
188
+ }
189
+ `)
190
+ const callerFn = getFunction(ir, 'caller')!
191
+ const cmds = getRawCommands(callerFn)
192
+
193
+ // Should have NBT setup for variable params (px → x, pz → z)
194
+ const xSetup = cmds.find(c => c.includes('macro_args') && c.includes(' x '))
195
+ const zSetup = cmds.find(c => c.includes('macro_args') && c.includes(' z '))
196
+ expect(xSetup).toBeDefined()
197
+ expect(zSetup).toBeDefined()
198
+
199
+ // Should have 'function test:spawn_zombie with storage rs:macro_args'
200
+ const callCmd = cmds.find(c => c.includes('spawn_zombie') && c.includes('with storage'))
201
+ expect(callCmd).toBeDefined()
202
+ expect(callCmd).toContain('rs:macro_args')
203
+ })
204
+
205
+ it('emits NBT setup for constant args too', () => {
206
+ const ir = compile(`
207
+ fn spawn_zombie(x: int, y: int, z: int) {
208
+ summon("minecraft:zombie", x, y, z);
209
+ }
210
+
211
+ fn caller_const() {
212
+ spawn_zombie(100, 64, 200);
213
+ }
214
+ `)
215
+ const callerFn = getFunction(ir, 'caller_const')!
216
+ const cmds = getRawCommands(callerFn)
217
+
218
+ // Should have NBT setup for all macro params
219
+ const nbtCmds = cmds.filter(c => c.includes('macro_args'))
220
+ expect(nbtCmds.length).toBeGreaterThan(0)
221
+
222
+ // Should call with storage
223
+ const callCmd = cmds.find(c => c.includes('spawn_zombie') && c.includes('with storage'))
224
+ expect(callCmd).toBeDefined()
225
+ })
226
+
227
+ it('correctly sets up int variable args into NBT storage', () => {
228
+ const ir = compile(`
229
+ fn spawn_zombie(x: int, y: int, z: int) {
230
+ summon("minecraft:zombie", x, y, z);
231
+ }
232
+
233
+ fn caller(my_x: int) {
234
+ spawn_zombie(my_x, 64, 0);
235
+ }
236
+ `)
237
+ const callerFn = getFunction(ir, 'caller')!
238
+ const cmds = getRawCommands(callerFn)
239
+
240
+ // For variable arg my_x → x: should use execute store result
241
+ const varSetup = cmds.find(c =>
242
+ c.includes('execute store result storage rs:macro_args x') &&
243
+ c.includes('scoreboard players get')
244
+ )
245
+ expect(varSetup).toBeDefined()
246
+
247
+ // For constant 64 → y: should use data modify ... set value
248
+ const constSetup = cmds.find(c =>
249
+ c.includes('data modify storage rs:macro_args y set value 64')
250
+ )
251
+ expect(constSetup).toBeDefined()
252
+ })
253
+ })
254
+
255
+ // ---------------------------------------------------------------------------
256
+ // Codegen output (mcfunction file content)
257
+ // ---------------------------------------------------------------------------
258
+
259
+ describe('MC macro function codegen output', () => {
260
+ it('generates $-prefixed lines in the macro function mcfunction file', () => {
261
+ const ir = compile(`
262
+ fn spawn_zombie(x: int, y: int, z: int) {
263
+ summon("minecraft:zombie", x, y, z);
264
+ }
265
+ `)
266
+ const content = getGeneratedContent(ir, 'spawn_zombie')
267
+ expect(content).toBeDefined()
268
+ expect(content).toContain('$summon minecraft:zombie $(x) $(y) $(z)')
269
+ })
270
+
271
+ it('generates correct call site in caller mcfunction file', () => {
272
+ const ir = compile(`
273
+ fn spawn_zombie(x: int, y: int, z: int) {
274
+ summon("minecraft:zombie", x, y, z);
275
+ }
276
+
277
+ fn caller(px: int, pz: int) {
278
+ spawn_zombie(px, 64, pz);
279
+ }
280
+ `)
281
+ const content = getGeneratedContent(ir, 'caller')
282
+ expect(content).toBeDefined()
283
+ expect(content).toContain('with storage rs:macro_args')
284
+ expect(content).toContain('spawn_zombie')
285
+ })
286
+ })
287
+
288
+ // ---------------------------------------------------------------------------
289
+ // Edge cases
290
+ // ---------------------------------------------------------------------------
291
+
292
+ describe('MC macro edge cases', () => {
293
+ it('handles mixed literal and variable args correctly', () => {
294
+ const ir = compile(`
295
+ fn teleport_y(y: int) {
296
+ summon("minecraft:zombie", 100, y, 200);
297
+ }
298
+ `)
299
+ const fn = getFunction(ir, 'teleport_y')!
300
+ expect(fn.isMacroFunction).toBe(true)
301
+ expect(fn.macroParamNames).toContain('y')
302
+ expect(fn.macroParamNames).not.toContain('x')
303
+
304
+ const cmds = getRawCommands(fn)
305
+ const macroCmd = cmds.find(c => c.startsWith('$summon'))
306
+ expect(macroCmd).toBeDefined()
307
+ // x and z are literals, y is macro
308
+ expect(macroCmd).toContain('100')
309
+ expect(macroCmd).toContain('$(y)')
310
+ expect(macroCmd).toContain('200')
311
+ })
312
+
313
+ it('non-macro functions still work normally', () => {
314
+ const ir = compile(`
315
+ fn greet() {
316
+ say("hello world");
317
+ }
318
+ `)
319
+ const fn = getFunction(ir, 'greet')!
320
+ expect(fn.isMacroFunction).toBeFalsy()
321
+ const cmds = getRawCommands(fn)
322
+ const sayCmd = cmds.find(c => c.includes('say') || c.includes('tellraw'))
323
+ expect(sayCmd).toBeDefined()
324
+ expect(sayCmd!.startsWith('$')).toBe(false)
325
+ })
326
+
327
+ it('macro function with params used in arithmetic still works', () => {
328
+ const ir = compile(`
329
+ fn spawn_offset(x: int, y: int, z: int) {
330
+ summon("minecraft:zombie", x, y, z);
331
+ // params are also used in the macro commands
332
+ }
333
+ `)
334
+ const fn = getFunction(ir, 'spawn_offset')!
335
+ expect(fn.isMacroFunction).toBe(true)
336
+
337
+ // The macro commands should use $(param) syntax
338
+ const cmds = getRawCommands(fn)
339
+ const macroCmd = cmds.find(c => c.startsWith('$summon'))
340
+ expect(macroCmd).toBeDefined()
341
+ expect(macroCmd).toContain('$(x)')
342
+ })
343
+ })
package/src/ir/types.ts CHANGED
@@ -111,6 +111,9 @@ export interface IRFunction {
111
111
  eventType: string
112
112
  tag: string
113
113
  }
114
+ // MC 1.20.2+ macro function support
115
+ isMacroFunction?: boolean // true → function uses MC macro syntax ($-prefixed commands)
116
+ macroParamNames?: string[] // parameter names that are passed via NBT macro (not just scoreboard)
114
117
  }
115
118
 
116
119
  // ---------------------------------------------------------------------------