redscript-mc 1.0.0
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/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +31 -0
- package/.github/ISSUE_TEMPLATE/wrong_output.md +33 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +34 -0
- package/.github/workflows/ci.yml +29 -0
- package/.github/workflows/publish-extension.yml +35 -0
- package/LICENSE +21 -0
- package/README.md +261 -0
- package/README.zh.md +261 -0
- package/dist/__tests__/cli.test.d.ts +1 -0
- package/dist/__tests__/cli.test.js +140 -0
- package/dist/__tests__/codegen.test.d.ts +1 -0
- package/dist/__tests__/codegen.test.js +121 -0
- package/dist/__tests__/diagnostics.test.d.ts +4 -0
- package/dist/__tests__/diagnostics.test.js +149 -0
- package/dist/__tests__/e2e.test.d.ts +6 -0
- package/dist/__tests__/e2e.test.js +1528 -0
- package/dist/__tests__/lexer.test.d.ts +1 -0
- package/dist/__tests__/lexer.test.js +316 -0
- package/dist/__tests__/lowering.test.d.ts +1 -0
- package/dist/__tests__/lowering.test.js +819 -0
- package/dist/__tests__/mc-integration.test.d.ts +12 -0
- package/dist/__tests__/mc-integration.test.js +395 -0
- package/dist/__tests__/mc-syntax.test.d.ts +1 -0
- package/dist/__tests__/mc-syntax.test.js +112 -0
- package/dist/__tests__/nbt.test.d.ts +1 -0
- package/dist/__tests__/nbt.test.js +82 -0
- package/dist/__tests__/optimizer-advanced.test.d.ts +1 -0
- package/dist/__tests__/optimizer-advanced.test.js +124 -0
- package/dist/__tests__/optimizer.test.d.ts +1 -0
- package/dist/__tests__/optimizer.test.js +118 -0
- package/dist/__tests__/parser.test.d.ts +1 -0
- package/dist/__tests__/parser.test.js +717 -0
- package/dist/__tests__/repl.test.d.ts +1 -0
- package/dist/__tests__/repl.test.js +27 -0
- package/dist/__tests__/runtime.test.d.ts +1 -0
- package/dist/__tests__/runtime.test.js +276 -0
- package/dist/__tests__/structure-optimizer.test.d.ts +1 -0
- package/dist/__tests__/structure-optimizer.test.js +33 -0
- package/dist/__tests__/typechecker.test.d.ts +1 -0
- package/dist/__tests__/typechecker.test.js +364 -0
- package/dist/ast/types.d.ts +357 -0
- package/dist/ast/types.js +9 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +407 -0
- package/dist/codegen/cmdblock/index.d.ts +26 -0
- package/dist/codegen/cmdblock/index.js +45 -0
- package/dist/codegen/mcfunction/index.d.ts +34 -0
- package/dist/codegen/mcfunction/index.js +413 -0
- package/dist/codegen/structure/index.d.ts +18 -0
- package/dist/codegen/structure/index.js +249 -0
- package/dist/compile.d.ts +30 -0
- package/dist/compile.js +152 -0
- package/dist/data/arena/function/__load.mcfunction +6 -0
- package/dist/data/arena/function/__tick.mcfunction +2 -0
- package/dist/data/arena/function/announce_leaders/else_1.mcfunction +3 -0
- package/dist/data/arena/function/announce_leaders/foreach_0/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/foreach_0/then_0.mcfunction +3 -0
- package/dist/data/arena/function/announce_leaders/foreach_0.mcfunction +7 -0
- package/dist/data/arena/function/announce_leaders/foreach_1/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/foreach_1/then_0.mcfunction +4 -0
- package/dist/data/arena/function/announce_leaders/foreach_1.mcfunction +6 -0
- package/dist/data/arena/function/announce_leaders/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/announce_leaders/then_0.mcfunction +4 -0
- package/dist/data/arena/function/announce_leaders.mcfunction +6 -0
- package/dist/data/arena/function/arena_tick/merge_2.mcfunction +1 -0
- package/dist/data/arena/function/arena_tick/then_0.mcfunction +4 -0
- package/dist/data/arena/function/arena_tick.mcfunction +11 -0
- package/dist/data/counter/function/__load.mcfunction +5 -0
- package/dist/data/counter/function/__tick.mcfunction +2 -0
- package/dist/data/counter/function/counter_tick/merge_2.mcfunction +1 -0
- package/dist/data/counter/function/counter_tick/then_0.mcfunction +3 -0
- package/dist/data/counter/function/counter_tick.mcfunction +11 -0
- package/dist/data/minecraft/tags/function/load.json +5 -0
- package/dist/data/minecraft/tags/function/tick.json +5 -0
- package/dist/data/quiz/function/__load.mcfunction +16 -0
- package/dist/data/quiz/function/__tick.mcfunction +6 -0
- package/dist/data/quiz/function/__trigger_quiz_a_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_b_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_c_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/__trigger_quiz_start_dispatch.mcfunction +4 -0
- package/dist/data/quiz/function/answer_a.mcfunction +4 -0
- package/dist/data/quiz/function/answer_b.mcfunction +4 -0
- package/dist/data/quiz/function/answer_c.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/else_1.mcfunction +5 -0
- package/dist/data/quiz/function/ask_question/else_4.mcfunction +5 -0
- package/dist/data/quiz/function/ask_question/else_7.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/merge_2.mcfunction +1 -0
- package/dist/data/quiz/function/ask_question/merge_5.mcfunction +2 -0
- package/dist/data/quiz/function/ask_question/merge_8.mcfunction +2 -0
- package/dist/data/quiz/function/ask_question/then_0.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/then_3.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question/then_6.mcfunction +4 -0
- package/dist/data/quiz/function/ask_question.mcfunction +7 -0
- package/dist/data/quiz/function/finish_quiz.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/else_1.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/else_10.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_16.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_4.mcfunction +3 -0
- package/dist/data/quiz/function/handle_answer/else_7.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/merge_11.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_14.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_17.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_2.mcfunction +8 -0
- package/dist/data/quiz/function/handle_answer/merge_5.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/merge_8.mcfunction +2 -0
- package/dist/data/quiz/function/handle_answer/then_0.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_12.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_15.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/then_3.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer/then_6.mcfunction +5 -0
- package/dist/data/quiz/function/handle_answer/then_9.mcfunction +6 -0
- package/dist/data/quiz/function/handle_answer.mcfunction +11 -0
- package/dist/data/quiz/function/start_quiz.mcfunction +5 -0
- package/dist/data/shop/function/__load.mcfunction +7 -0
- package/dist/data/shop/function/__tick.mcfunction +3 -0
- package/dist/data/shop/function/__trigger_shop_buy_dispatch.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/else_1.mcfunction +5 -0
- package/dist/data/shop/function/complete_purchase/else_4.mcfunction +5 -0
- package/dist/data/shop/function/complete_purchase/else_7.mcfunction +3 -0
- package/dist/data/shop/function/complete_purchase/merge_2.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/merge_5.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/merge_8.mcfunction +2 -0
- package/dist/data/shop/function/complete_purchase/then_0.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/then_3.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase/then_6.mcfunction +4 -0
- package/dist/data/shop/function/complete_purchase.mcfunction +7 -0
- package/dist/data/shop/function/handle_shop_trigger.mcfunction +3 -0
- package/dist/data/turret/function/__load.mcfunction +5 -0
- package/dist/data/turret/function/__tick.mcfunction +4 -0
- package/dist/data/turret/function/__trigger_deploy_turret_dispatch.mcfunction +4 -0
- package/dist/data/turret/function/deploy_turret.mcfunction +8 -0
- package/dist/data/turret/function/turret_tick/at_1.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/foreach_0.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/foreach_2.mcfunction +2 -0
- package/dist/data/turret/function/turret_tick/tick_body.mcfunction +3 -0
- package/dist/data/turret/function/turret_tick/tick_skip.mcfunction +1 -0
- package/dist/data/turret/function/turret_tick.mcfunction +5 -0
- package/dist/diagnostics/index.d.ts +44 -0
- package/dist/diagnostics/index.js +140 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +126 -0
- package/dist/ir/builder.d.ts +32 -0
- package/dist/ir/builder.js +99 -0
- package/dist/ir/types.d.ts +117 -0
- package/dist/ir/types.js +15 -0
- package/dist/lexer/index.d.ts +36 -0
- package/dist/lexer/index.js +458 -0
- package/dist/lowering/index.d.ts +106 -0
- package/dist/lowering/index.js +2041 -0
- package/dist/mc-test/client.d.ts +128 -0
- package/dist/mc-test/client.js +174 -0
- package/dist/mc-test/runner.d.ts +28 -0
- package/dist/mc-test/runner.js +150 -0
- package/dist/mc-test/setup.d.ts +11 -0
- package/dist/mc-test/setup.js +98 -0
- package/dist/mc-validator/index.d.ts +17 -0
- package/dist/mc-validator/index.js +322 -0
- package/dist/nbt/index.d.ts +86 -0
- package/dist/nbt/index.js +250 -0
- package/dist/optimizer/commands.d.ts +36 -0
- package/dist/optimizer/commands.js +349 -0
- package/dist/optimizer/passes.d.ts +34 -0
- package/dist/optimizer/passes.js +227 -0
- package/dist/optimizer/structure.d.ts +8 -0
- package/dist/optimizer/structure.js +344 -0
- package/dist/pack.mcmeta +6 -0
- package/dist/parser/index.d.ts +76 -0
- package/dist/parser/index.js +1193 -0
- package/dist/repl.d.ts +16 -0
- package/dist/repl.js +165 -0
- package/dist/runtime/index.d.ts +101 -0
- package/dist/runtime/index.js +1288 -0
- package/dist/typechecker/index.d.ts +42 -0
- package/dist/typechecker/index.js +629 -0
- package/docs/COMPILATION_STATS.md +142 -0
- package/docs/IMPLEMENTATION_GUIDE.md +512 -0
- package/docs/LANGUAGE_REFERENCE.md +415 -0
- package/docs/MC_MAPPING.md +280 -0
- package/docs/STRUCTURE_TARGET.md +80 -0
- package/docs/mc-reference/commands.md +259 -0
- package/editors/vscode/.vscodeignore +10 -0
- package/editors/vscode/LICENSE +21 -0
- package/editors/vscode/README.md +78 -0
- package/editors/vscode/build.mjs +28 -0
- package/editors/vscode/icon.png +0 -0
- package/editors/vscode/mcfunction-language-configuration.json +28 -0
- package/editors/vscode/out/extension.js +7236 -0
- package/editors/vscode/package-lock.json +566 -0
- package/editors/vscode/package.json +137 -0
- package/editors/vscode/redscript-language-configuration.json +28 -0
- package/editors/vscode/snippets/redscript.json +114 -0
- package/editors/vscode/src/codeactions.ts +89 -0
- package/editors/vscode/src/completion.ts +130 -0
- package/editors/vscode/src/extension.ts +239 -0
- package/editors/vscode/src/hover.ts +1120 -0
- package/editors/vscode/src/symbols.ts +207 -0
- package/editors/vscode/syntaxes/mcfunction.tmLanguage.json +740 -0
- package/editors/vscode/syntaxes/redscript.tmLanguage.json +357 -0
- package/editors/vscode/tsconfig.json +13 -0
- package/jest.config.js +5 -0
- package/package.json +38 -0
- package/src/__tests__/cli.test.ts +130 -0
- package/src/__tests__/codegen.test.ts +128 -0
- package/src/__tests__/diagnostics.test.ts +195 -0
- package/src/__tests__/e2e.test.ts +1721 -0
- package/src/__tests__/fixtures/mc-commands-1.21.4.json +18734 -0
- package/src/__tests__/formatter.test.ts +46 -0
- package/src/__tests__/lexer.test.ts +356 -0
- package/src/__tests__/lowering.test.ts +962 -0
- package/src/__tests__/mc-integration.test.ts +409 -0
- package/src/__tests__/mc-syntax.test.ts +96 -0
- package/src/__tests__/nbt.test.ts +58 -0
- package/src/__tests__/optimizer-advanced.test.ts +144 -0
- package/src/__tests__/optimizer.test.ts +129 -0
- package/src/__tests__/parser.test.ts +800 -0
- package/src/__tests__/repl.test.ts +33 -0
- package/src/__tests__/runtime.test.ts +289 -0
- package/src/__tests__/structure-optimizer.test.ts +38 -0
- package/src/__tests__/typechecker.test.ts +395 -0
- package/src/ast/types.ts +248 -0
- package/src/cli.ts +445 -0
- package/src/codegen/cmdblock/index.ts +63 -0
- package/src/codegen/mcfunction/index.ts +471 -0
- package/src/codegen/structure/index.ts +305 -0
- package/src/compile.ts +188 -0
- package/src/diagnostics/index.ts +186 -0
- package/src/examples/README.md +77 -0
- package/src/examples/SHOWCASE_GAME.md +43 -0
- package/src/examples/arena.rs +44 -0
- package/src/examples/counter.rs +12 -0
- package/src/examples/pvp_arena.rs +131 -0
- package/src/examples/quiz.rs +90 -0
- package/src/examples/rpg.rs +13 -0
- package/src/examples/shop.rs +30 -0
- package/src/examples/showcase_game.rs +552 -0
- package/src/examples/stdlib_demo.rs +181 -0
- package/src/examples/turret.rs +27 -0
- package/src/examples/world_manager.rs +23 -0
- package/src/formatter/index.ts +22 -0
- package/src/index.ts +161 -0
- package/src/ir/builder.ts +114 -0
- package/src/ir/types.ts +119 -0
- package/src/lexer/index.ts +555 -0
- package/src/lowering/index.ts +2406 -0
- package/src/mc-test/client.ts +259 -0
- package/src/mc-test/runner.ts +140 -0
- package/src/mc-test/setup.ts +70 -0
- package/src/mc-validator/index.ts +367 -0
- package/src/nbt/index.ts +321 -0
- package/src/optimizer/commands.ts +416 -0
- package/src/optimizer/passes.ts +233 -0
- package/src/optimizer/structure.ts +441 -0
- package/src/parser/index.ts +1437 -0
- package/src/repl.ts +165 -0
- package/src/runtime/index.ts +1403 -0
- package/src/stdlib/README.md +156 -0
- package/src/stdlib/combat.rs +20 -0
- package/src/stdlib/cooldown.rs +45 -0
- package/src/stdlib/math.rs +49 -0
- package/src/stdlib/mobs.rs +99 -0
- package/src/stdlib/player.rs +29 -0
- package/src/stdlib/strings.rs +7 -0
- package/src/stdlib/timer.rs +51 -0
- package/src/templates/README.md +126 -0
- package/src/templates/combat.rs +96 -0
- package/src/templates/economy.rs +40 -0
- package/src/templates/mini-game-framework.rs +117 -0
- package/src/templates/quest.rs +78 -0
- package/src/test_programs/zombie_game.rs +25 -0
- package/src/typechecker/index.ts +737 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { compile } from '../index'
|
|
2
|
+
import { compileToStructure } from '../codegen/structure'
|
|
3
|
+
|
|
4
|
+
function getFileContent(files: ReturnType<typeof compile>['files'], suffix: string): string {
|
|
5
|
+
const file = files.find(candidate => candidate.path.endsWith(suffix))
|
|
6
|
+
if (!file) {
|
|
7
|
+
throw new Error(`Missing file: ${suffix}`)
|
|
8
|
+
}
|
|
9
|
+
return file.content
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('LICM', () => {
|
|
13
|
+
test('hoists loop-invariant scoreboard read out of foreach', () => {
|
|
14
|
+
const source = `
|
|
15
|
+
fn turret_tick() {
|
|
16
|
+
foreach (turret in @e[tag=turret]) {
|
|
17
|
+
let range: int = scoreboard_get("config", "turret_range");
|
|
18
|
+
if (range > 0) {
|
|
19
|
+
if (range > -1) {
|
|
20
|
+
say("ready");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
`
|
|
26
|
+
|
|
27
|
+
const result = compile(source, { namespace: 'test' })
|
|
28
|
+
const parent = getFileContent(result.files, 'data/test/function/turret_tick.mcfunction')
|
|
29
|
+
const loopBody = getFileContent(result.files, 'data/test/function/turret_tick/foreach_0.mcfunction')
|
|
30
|
+
|
|
31
|
+
const hoistedRead = 'execute store result score $t0 rs run scoreboard players get config turret_range'
|
|
32
|
+
const executeCall = 'execute as @e[tag=turret] run function test:turret_tick/foreach_0'
|
|
33
|
+
|
|
34
|
+
expect(parent).toContain(hoistedRead)
|
|
35
|
+
expect(parent.indexOf(hoistedRead)).toBeLessThan(parent.indexOf(executeCall))
|
|
36
|
+
expect(loopBody).not.toContain('scoreboard players get config turret_range')
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('CSE', () => {
|
|
41
|
+
test('eliminates duplicate scoreboard reads', () => {
|
|
42
|
+
const source = `
|
|
43
|
+
fn read_twice() {
|
|
44
|
+
let a: int = scoreboard_get(@s, "coins");
|
|
45
|
+
let b: int = scoreboard_get(@s, "coins");
|
|
46
|
+
if (a == b) {
|
|
47
|
+
say("same");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
`
|
|
51
|
+
|
|
52
|
+
const result = compile(source, { namespace: 'test' })
|
|
53
|
+
const fn = getFileContent(result.files, 'data/test/function/read_twice.mcfunction')
|
|
54
|
+
const readMatches = fn.match(/scoreboard players get @s coins/g) ?? []
|
|
55
|
+
|
|
56
|
+
expect(readMatches).toHaveLength(1)
|
|
57
|
+
expect(fn).toContain('scoreboard players operation $t1 rs = $t0 rs')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('reuses duplicate arithmetic sequences', () => {
|
|
61
|
+
const source = `
|
|
62
|
+
fn math() {
|
|
63
|
+
let base: int = 4;
|
|
64
|
+
let a: int = base + 2;
|
|
65
|
+
let b: int = base + 2;
|
|
66
|
+
if (a == b) {
|
|
67
|
+
say("same");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
`
|
|
71
|
+
|
|
72
|
+
const result = compile(source, { namespace: 'test' })
|
|
73
|
+
const fn = getFileContent(result.files, 'data/test/function/math.mcfunction')
|
|
74
|
+
const addMatches = fn.match(/\+= \$const_2 rs/g) ?? []
|
|
75
|
+
|
|
76
|
+
expect(addMatches).toHaveLength(1)
|
|
77
|
+
expect(fn).toContain('scoreboard players operation $t1 rs = $t0 rs')
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('setblock batching', () => {
|
|
82
|
+
test('merges 4 consecutive setblocks into fill', () => {
|
|
83
|
+
const source = `
|
|
84
|
+
fn build() {
|
|
85
|
+
setblock((0, 64, 0), "minecraft:stone");
|
|
86
|
+
setblock((1, 64, 0), "minecraft:stone");
|
|
87
|
+
setblock((2, 64, 0), "minecraft:stone");
|
|
88
|
+
setblock((3, 64, 0), "minecraft:stone");
|
|
89
|
+
}
|
|
90
|
+
`
|
|
91
|
+
|
|
92
|
+
const result = compile(source, { namespace: 'test' })
|
|
93
|
+
const fn = getFileContent(result.files, 'data/test/function/build.mcfunction')
|
|
94
|
+
|
|
95
|
+
expect(fn).toContain('fill 0 64 0 3 64 0 minecraft:stone')
|
|
96
|
+
expect(fn).not.toContain('setblock 1 64 0 minecraft:stone')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('does not merge setblocks with different blocks', () => {
|
|
100
|
+
const source = `
|
|
101
|
+
fn build() {
|
|
102
|
+
setblock((0, 64, 0), "minecraft:stone");
|
|
103
|
+
setblock((1, 64, 0), "minecraft:dirt");
|
|
104
|
+
}
|
|
105
|
+
`
|
|
106
|
+
|
|
107
|
+
const result = compile(source, { namespace: 'test' })
|
|
108
|
+
const fn = getFileContent(result.files, 'data/test/function/build.mcfunction')
|
|
109
|
+
|
|
110
|
+
expect(fn).toContain('setblock 0 64 0 minecraft:stone')
|
|
111
|
+
expect(fn).toContain('setblock 1 64 0 minecraft:dirt')
|
|
112
|
+
expect(fn).not.toContain('fill 0 64 0 1 64 0')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('does not merge non-adjacent setblocks', () => {
|
|
116
|
+
const source = `
|
|
117
|
+
fn build() {
|
|
118
|
+
setblock((0, 64, 0), "minecraft:stone");
|
|
119
|
+
setblock((2, 64, 0), "minecraft:stone");
|
|
120
|
+
}
|
|
121
|
+
`
|
|
122
|
+
|
|
123
|
+
const result = compile(source, { namespace: 'test' })
|
|
124
|
+
const fn = getFileContent(result.files, 'data/test/function/build.mcfunction')
|
|
125
|
+
|
|
126
|
+
expect(fn).toContain('setblock 0 64 0 minecraft:stone')
|
|
127
|
+
expect(fn).toContain('setblock 2 64 0 minecraft:stone')
|
|
128
|
+
expect(fn).not.toContain('fill 0 64 0 2 64 0')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('applies batching to structure target output too', () => {
|
|
132
|
+
const source = `
|
|
133
|
+
fn build() {
|
|
134
|
+
setblock((0, 64, 0), "minecraft:stone");
|
|
135
|
+
setblock((1, 64, 0), "minecraft:stone");
|
|
136
|
+
setblock((2, 64, 0), "minecraft:stone");
|
|
137
|
+
}
|
|
138
|
+
`
|
|
139
|
+
|
|
140
|
+
const result = compileToStructure(source, 'test')
|
|
141
|
+
|
|
142
|
+
expect(result.blocks.some(block => block.command === 'fill 0 64 0 2 64 0 minecraft:stone')).toBe(true)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { constantFolding, copyPropagation, deadCodeElimination, optimize } from '../optimizer/passes'
|
|
2
|
+
import type { IRFunction } from '../ir/types'
|
|
3
|
+
|
|
4
|
+
function makeFn(instrs: any[], term: any = { op: 'return' }): IRFunction {
|
|
5
|
+
return {
|
|
6
|
+
name: 'test',
|
|
7
|
+
params: [],
|
|
8
|
+
locals: [],
|
|
9
|
+
blocks: [{ label: 'entry', instrs, term }],
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('constantFolding', () => {
|
|
14
|
+
it('folds 2 + 3 → 5', () => {
|
|
15
|
+
const fn = makeFn([
|
|
16
|
+
{ op: 'binop', dst: '$x', lhs: { kind: 'const', value: 2 }, bop: '+', rhs: { kind: 'const', value: 3 } },
|
|
17
|
+
])
|
|
18
|
+
const opt = constantFolding(fn)
|
|
19
|
+
expect(opt.blocks[0].instrs[0]).toEqual({
|
|
20
|
+
op: 'assign', dst: '$x', src: { kind: 'const', value: 5 },
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('folds 10 / 3 → 3 (truncated int division)', () => {
|
|
25
|
+
const fn = makeFn([
|
|
26
|
+
{ op: 'binop', dst: '$x', lhs: { kind: 'const', value: 10 }, bop: '/', rhs: { kind: 'const', value: 3 } },
|
|
27
|
+
])
|
|
28
|
+
const opt = constantFolding(fn)
|
|
29
|
+
expect((opt.blocks[0].instrs[0] as any).src.value).toBe(3)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('folds cmp 5 == 5 → 1', () => {
|
|
33
|
+
const fn = makeFn([
|
|
34
|
+
{ op: 'cmp', dst: '$r', lhs: { kind: 'const', value: 5 }, cop: '==', rhs: { kind: 'const', value: 5 } },
|
|
35
|
+
])
|
|
36
|
+
const opt = constantFolding(fn)
|
|
37
|
+
expect((opt.blocks[0].instrs[0] as any).src.value).toBe(1)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('folds cmp 5 > 10 → 0', () => {
|
|
41
|
+
const fn = makeFn([
|
|
42
|
+
{ op: 'cmp', dst: '$r', lhs: { kind: 'const', value: 5 }, cop: '>', rhs: { kind: 'const', value: 10 } },
|
|
43
|
+
])
|
|
44
|
+
const opt = constantFolding(fn)
|
|
45
|
+
expect((opt.blocks[0].instrs[0] as any).src.value).toBe(0)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('does not fold division by zero', () => {
|
|
49
|
+
const fn = makeFn([
|
|
50
|
+
{ op: 'binop', dst: '$x', lhs: { kind: 'const', value: 5 }, bop: '/', rhs: { kind: 'const', value: 0 } },
|
|
51
|
+
])
|
|
52
|
+
const opt = constantFolding(fn)
|
|
53
|
+
expect(opt.blocks[0].instrs[0].op).toBe('binop')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('copyPropagation', () => {
|
|
58
|
+
it('propagates simple copy', () => {
|
|
59
|
+
const fn = makeFn([
|
|
60
|
+
{ op: 'assign', dst: '$t0', src: { kind: 'var', name: '$x' } },
|
|
61
|
+
{ op: 'binop', dst: '$y', lhs: { kind: 'var', name: '$t0' }, bop: '+', rhs: { kind: 'const', value: 1 } },
|
|
62
|
+
])
|
|
63
|
+
const opt = copyPropagation(fn)
|
|
64
|
+
const binop = opt.blocks[0].instrs[1] as any
|
|
65
|
+
expect(binop.lhs).toEqual({ kind: 'var', name: '$x' })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('propagates constant copies', () => {
|
|
69
|
+
const fn = makeFn([
|
|
70
|
+
{ op: 'assign', dst: '$t0', src: { kind: 'const', value: 42 } },
|
|
71
|
+
{ op: 'assign', dst: '$y', src: { kind: 'var', name: '$t0' } },
|
|
72
|
+
])
|
|
73
|
+
const opt = copyPropagation(fn)
|
|
74
|
+
const second = opt.blocks[0].instrs[1] as any
|
|
75
|
+
expect(second.src).toEqual({ kind: 'const', value: 42 })
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
describe('deadCodeElimination', () => {
|
|
80
|
+
it('removes unused assignment', () => {
|
|
81
|
+
const fn = makeFn([
|
|
82
|
+
{ op: 'assign', dst: '$unused', src: { kind: 'const', value: 99 } },
|
|
83
|
+
{ op: 'assign', dst: '$used', src: { kind: 'const', value: 1 } },
|
|
84
|
+
], { op: 'return', value: { kind: 'var', name: '$used' } })
|
|
85
|
+
const opt = deadCodeElimination(fn)
|
|
86
|
+
expect(opt.blocks[0].instrs).toHaveLength(1)
|
|
87
|
+
expect((opt.blocks[0].instrs[0] as any).dst).toBe('$used')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('keeps call even if return value unused (side effects)', () => {
|
|
91
|
+
const fn = makeFn([
|
|
92
|
+
{ op: 'call', fn: 'foo', args: [], dst: '$unused' },
|
|
93
|
+
])
|
|
94
|
+
const opt = deadCodeElimination(fn)
|
|
95
|
+
expect(opt.blocks[0].instrs).toHaveLength(1)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('keeps assignments referenced by raw commands', () => {
|
|
99
|
+
const fn = makeFn([
|
|
100
|
+
{ op: 'assign', dst: '$used_by_raw', src: { kind: 'const', value: 7 } },
|
|
101
|
+
{ op: 'raw', cmd: 'execute store result score player obj run scoreboard players get $used_by_raw rs' },
|
|
102
|
+
])
|
|
103
|
+
const opt = deadCodeElimination(fn)
|
|
104
|
+
expect(opt.blocks[0].instrs).toHaveLength(2)
|
|
105
|
+
expect((opt.blocks[0].instrs[0] as any).dst).toBe('$used_by_raw')
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe('optimize pipeline', () => {
|
|
110
|
+
it('combines all passes', () => {
|
|
111
|
+
// t0 = 2 + 3 (→ constant fold → t0 = 5)
|
|
112
|
+
// x = t0 (→ copy prop → x = 5)
|
|
113
|
+
// unused = 0 (→ DCE → removed)
|
|
114
|
+
// return x
|
|
115
|
+
const fn = makeFn([
|
|
116
|
+
{ op: 'binop', dst: '$t0', lhs: { kind: 'const', value: 2 }, bop: '+', rhs: { kind: 'const', value: 3 } },
|
|
117
|
+
{ op: 'assign', dst: '$x', src: { kind: 'var', name: '$t0' } },
|
|
118
|
+
{ op: 'assign', dst: '$unused', src: { kind: 'const', value: 0 } },
|
|
119
|
+
], { op: 'return', value: { kind: 'var', name: '$x' } })
|
|
120
|
+
|
|
121
|
+
const opt = optimize(fn)
|
|
122
|
+
const instrs = opt.blocks[0].instrs
|
|
123
|
+
// $unused should be gone
|
|
124
|
+
expect(instrs.some((i: any) => i.dst === '$unused')).toBe(false)
|
|
125
|
+
// $x should be const 5 (after folding + propagation)
|
|
126
|
+
const xInstr = instrs.find((i: any) => i.dst === '$x') as any
|
|
127
|
+
expect(xInstr?.src).toEqual({ kind: 'const', value: 5 })
|
|
128
|
+
})
|
|
129
|
+
})
|