redscript-mc 1.2.26 → 1.2.28

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.
Files changed (243) hide show
  1. package/README.md +78 -9
  2. package/README.zh.md +72 -4
  3. package/dist/__tests__/cli.test.js +13 -13
  4. package/dist/__tests__/optimizer-advanced.test.js +4 -4
  5. package/dist/__tests__/stdlib-advanced.test.js +114 -0
  6. package/dist/__tests__/stdlib-bigint.test.d.ts +7 -0
  7. package/dist/__tests__/stdlib-bigint.test.js +428 -0
  8. package/dist/cli.js +13 -5
  9. package/dist/codegen/mcfunction/index.d.ts +4 -0
  10. package/dist/codegen/mcfunction/index.js +9 -4
  11. package/dist/compile.d.ts +4 -0
  12. package/dist/compile.js +9 -1
  13. package/dist/data/arena/function/__load.mcfunction +6 -0
  14. package/dist/data/arena/function/__tick.mcfunction +2 -0
  15. package/dist/data/arena/function/announce_leaders/else_1.mcfunction +3 -0
  16. package/dist/data/arena/function/announce_leaders/foreach_0/merge_2.mcfunction +1 -0
  17. package/dist/data/arena/function/announce_leaders/foreach_0/then_0.mcfunction +3 -0
  18. package/dist/data/arena/function/announce_leaders/foreach_0.mcfunction +7 -0
  19. package/dist/data/arena/function/announce_leaders/foreach_1/merge_2.mcfunction +1 -0
  20. package/dist/data/arena/function/announce_leaders/foreach_1/then_0.mcfunction +4 -0
  21. package/dist/data/arena/function/announce_leaders/foreach_1.mcfunction +6 -0
  22. package/dist/data/arena/function/announce_leaders/merge_2.mcfunction +1 -0
  23. package/dist/data/arena/function/announce_leaders/then_0.mcfunction +4 -0
  24. package/dist/data/arena/function/announce_leaders.mcfunction +6 -0
  25. package/dist/data/arena/function/arena_tick/merge_2.mcfunction +1 -0
  26. package/dist/data/arena/function/arena_tick/then_0.mcfunction +4 -0
  27. package/dist/data/arena/function/arena_tick.mcfunction +11 -0
  28. package/dist/data/counter/function/__load.mcfunction +5 -0
  29. package/dist/data/counter/function/__tick.mcfunction +2 -0
  30. package/dist/data/counter/function/counter_tick/merge_2.mcfunction +1 -0
  31. package/dist/data/counter/function/counter_tick/then_0.mcfunction +3 -0
  32. package/dist/data/counter/function/counter_tick.mcfunction +11 -0
  33. package/dist/data/gcd2/function/__load.mcfunction +3 -0
  34. package/dist/data/gcd2/function/abs/merge_2.mcfunction +3 -0
  35. package/dist/data/gcd2/function/abs/then_0.mcfunction +5 -0
  36. package/dist/data/gcd2/function/abs.mcfunction +7 -0
  37. package/dist/data/gcd2/function/gcd/loop_body_1.mcfunction +7 -0
  38. package/dist/data/gcd2/function/gcd/loop_check_0.mcfunction +5 -0
  39. package/dist/data/gcd2/function/gcd/loop_exit_2.mcfunction +3 -0
  40. package/dist/data/gcd2/function/gcd.mcfunction +14 -0
  41. package/dist/data/gcd3/function/__load.mcfunction +3 -0
  42. package/dist/data/gcd3/function/abs/merge_2.mcfunction +3 -0
  43. package/dist/data/gcd3/function/abs/then_0.mcfunction +5 -0
  44. package/dist/data/gcd3/function/abs.mcfunction +7 -0
  45. package/dist/data/gcd3/function/gcd/loop_body_1.mcfunction +7 -0
  46. package/dist/data/gcd3/function/gcd/loop_check_0.mcfunction +5 -0
  47. package/dist/data/gcd3/function/gcd/loop_exit_2.mcfunction +3 -0
  48. package/dist/data/gcd3/function/gcd.mcfunction +14 -0
  49. package/dist/data/gcd3/function/test.mcfunction +7 -0
  50. package/dist/data/gcd3nm/function/__load.mcfunction +3 -0
  51. package/dist/data/gcd3nm/function/abs/merge_2.mcfunction +3 -0
  52. package/dist/data/gcd3nm/function/abs/then_0.mcfunction +5 -0
  53. package/dist/data/gcd3nm/function/abs.mcfunction +7 -0
  54. package/dist/data/gcd3nm/function/gcd/loop_body_1.mcfunction +7 -0
  55. package/dist/data/gcd3nm/function/gcd/loop_check_0.mcfunction +5 -0
  56. package/dist/data/gcd3nm/function/gcd/loop_exit_2.mcfunction +3 -0
  57. package/dist/data/gcd3nm/function/gcd.mcfunction +14 -0
  58. package/dist/data/gcd3nm/function/test.mcfunction +7 -0
  59. package/dist/data/gcd_test/function/__load.mcfunction +3 -0
  60. package/dist/data/gcd_test/function/abs/merge_2.mcfunction +3 -0
  61. package/dist/data/gcd_test/function/abs/then_0.mcfunction +5 -0
  62. package/dist/data/gcd_test/function/abs.mcfunction +7 -0
  63. package/dist/data/gcd_test/function/gcd/loop_body_1.mcfunction +7 -0
  64. package/dist/data/gcd_test/function/gcd/loop_check_0.mcfunction +5 -0
  65. package/dist/data/gcd_test/function/gcd/loop_exit_2.mcfunction +3 -0
  66. package/dist/data/gcd_test/function/gcd.mcfunction +14 -0
  67. package/dist/data/isqrttest/function/__load.mcfunction +6 -0
  68. package/dist/data/isqrttest/function/isqrt/loop_body_4.mcfunction +12 -0
  69. package/dist/data/isqrttest/function/isqrt/loop_check_3.mcfunction +5 -0
  70. package/dist/data/isqrttest/function/isqrt/loop_exit_5.mcfunction +3 -0
  71. package/dist/data/isqrttest/function/isqrt/merge_2.mcfunction +4 -0
  72. package/dist/data/isqrttest/function/isqrt/merge_8.mcfunction +6 -0
  73. package/dist/data/isqrttest/function/isqrt/then_0.mcfunction +3 -0
  74. package/dist/data/isqrttest/function/isqrt/then_6.mcfunction +3 -0
  75. package/dist/data/isqrttest/function/isqrt.mcfunction +7 -0
  76. package/dist/data/isqrttest/function/test.mcfunction +6 -0
  77. package/dist/data/mathtest/function/__load.mcfunction +3 -0
  78. package/dist/data/mathtest/function/abs/merge_2.mcfunction +3 -0
  79. package/dist/data/mathtest/function/abs/then_0.mcfunction +5 -0
  80. package/dist/data/mathtest/function/abs.mcfunction +6 -0
  81. package/dist/data/mathtest/function/test.mcfunction +5 -0
  82. package/dist/data/minecraft/tags/function/load.json +5 -0
  83. package/dist/data/minecraft/tags/function/tick.json +5 -0
  84. package/dist/data/mypack/function/__load.mcfunction +13 -0
  85. package/dist/data/mypack/function/_atan_init.mcfunction +2 -0
  86. package/dist/data/mypack/function/abs/merge_2.mcfunction +3 -0
  87. package/dist/data/mypack/function/abs/then_0.mcfunction +5 -0
  88. package/dist/data/mypack/function/abs.mcfunction +6 -0
  89. package/dist/data/mypack/function/atan2_fixed/__sgi_1.mcfunction +2 -0
  90. package/dist/data/mypack/function/atan2_fixed/else_34.mcfunction +3 -0
  91. package/dist/data/mypack/function/atan2_fixed/loop_body_31.mcfunction +19 -0
  92. package/dist/data/mypack/function/atan2_fixed/loop_check_30.mcfunction +5 -0
  93. package/dist/data/mypack/function/atan2_fixed/loop_exit_32.mcfunction +6 -0
  94. package/dist/data/mypack/function/atan2_fixed/merge_11.mcfunction +6 -0
  95. package/dist/data/mypack/function/atan2_fixed/merge_14.mcfunction +3 -0
  96. package/dist/data/mypack/function/atan2_fixed/merge_17.mcfunction +6 -0
  97. package/dist/data/mypack/function/atan2_fixed/merge_2.mcfunction +5 -0
  98. package/dist/data/mypack/function/atan2_fixed/merge_20.mcfunction +5 -0
  99. package/dist/data/mypack/function/atan2_fixed/merge_23.mcfunction +5 -0
  100. package/dist/data/mypack/function/atan2_fixed/merge_26.mcfunction +6 -0
  101. package/dist/data/mypack/function/atan2_fixed/merge_29.mcfunction +4 -0
  102. package/dist/data/mypack/function/atan2_fixed/merge_38.mcfunction +5 -0
  103. package/dist/data/mypack/function/atan2_fixed/merge_41.mcfunction +5 -0
  104. package/dist/data/mypack/function/atan2_fixed/merge_44.mcfunction +5 -0
  105. package/dist/data/mypack/function/atan2_fixed/merge_47.mcfunction +5 -0
  106. package/dist/data/mypack/function/atan2_fixed/merge_5.mcfunction +5 -0
  107. package/dist/data/mypack/function/atan2_fixed/merge_8.mcfunction +3 -0
  108. package/dist/data/mypack/function/atan2_fixed/then_0.mcfunction +5 -0
  109. package/dist/data/mypack/function/atan2_fixed/then_12.mcfunction +3 -0
  110. package/dist/data/mypack/function/atan2_fixed/then_15.mcfunction +5 -0
  111. package/dist/data/mypack/function/atan2_fixed/then_18.mcfunction +5 -0
  112. package/dist/data/mypack/function/atan2_fixed/then_21.mcfunction +3 -0
  113. package/dist/data/mypack/function/atan2_fixed/then_24.mcfunction +3 -0
  114. package/dist/data/mypack/function/atan2_fixed/then_27.mcfunction +6 -0
  115. package/dist/data/mypack/function/atan2_fixed/then_3.mcfunction +3 -0
  116. package/dist/data/mypack/function/atan2_fixed/then_33.mcfunction +5 -0
  117. package/dist/data/mypack/function/atan2_fixed/then_36.mcfunction +5 -0
  118. package/dist/data/mypack/function/atan2_fixed/then_39.mcfunction +5 -0
  119. package/dist/data/mypack/function/atan2_fixed/then_42.mcfunction +3 -0
  120. package/dist/data/mypack/function/atan2_fixed/then_45.mcfunction +5 -0
  121. package/dist/data/mypack/function/atan2_fixed/then_6.mcfunction +3 -0
  122. package/dist/data/mypack/function/atan2_fixed/then_9.mcfunction +5 -0
  123. package/dist/data/mypack/function/atan2_fixed.mcfunction +7 -0
  124. package/dist/data/mypack/function/my_game.mcfunction +10 -0
  125. package/dist/data/quiz/function/__load.mcfunction +16 -0
  126. package/dist/data/quiz/function/__tick.mcfunction +6 -0
  127. package/dist/data/quiz/function/__trigger_quiz_a_dispatch.mcfunction +4 -0
  128. package/dist/data/quiz/function/__trigger_quiz_b_dispatch.mcfunction +4 -0
  129. package/dist/data/quiz/function/__trigger_quiz_c_dispatch.mcfunction +4 -0
  130. package/dist/data/quiz/function/__trigger_quiz_start_dispatch.mcfunction +4 -0
  131. package/dist/data/quiz/function/answer_a.mcfunction +4 -0
  132. package/dist/data/quiz/function/answer_b.mcfunction +4 -0
  133. package/dist/data/quiz/function/answer_c.mcfunction +4 -0
  134. package/dist/data/quiz/function/ask_question/else_1.mcfunction +5 -0
  135. package/dist/data/quiz/function/ask_question/else_4.mcfunction +5 -0
  136. package/dist/data/quiz/function/ask_question/else_7.mcfunction +4 -0
  137. package/dist/data/quiz/function/ask_question/merge_2.mcfunction +1 -0
  138. package/dist/data/quiz/function/ask_question/merge_5.mcfunction +2 -0
  139. package/dist/data/quiz/function/ask_question/merge_8.mcfunction +2 -0
  140. package/dist/data/quiz/function/ask_question/then_0.mcfunction +4 -0
  141. package/dist/data/quiz/function/ask_question/then_3.mcfunction +4 -0
  142. package/dist/data/quiz/function/ask_question/then_6.mcfunction +4 -0
  143. package/dist/data/quiz/function/ask_question.mcfunction +7 -0
  144. package/dist/data/quiz/function/finish_quiz.mcfunction +6 -0
  145. package/dist/data/quiz/function/handle_answer/else_1.mcfunction +5 -0
  146. package/dist/data/quiz/function/handle_answer/else_10.mcfunction +3 -0
  147. package/dist/data/quiz/function/handle_answer/else_16.mcfunction +3 -0
  148. package/dist/data/quiz/function/handle_answer/else_4.mcfunction +3 -0
  149. package/dist/data/quiz/function/handle_answer/else_7.mcfunction +5 -0
  150. package/dist/data/quiz/function/handle_answer/merge_11.mcfunction +2 -0
  151. package/dist/data/quiz/function/handle_answer/merge_14.mcfunction +2 -0
  152. package/dist/data/quiz/function/handle_answer/merge_17.mcfunction +2 -0
  153. package/dist/data/quiz/function/handle_answer/merge_2.mcfunction +8 -0
  154. package/dist/data/quiz/function/handle_answer/merge_5.mcfunction +2 -0
  155. package/dist/data/quiz/function/handle_answer/merge_8.mcfunction +2 -0
  156. package/dist/data/quiz/function/handle_answer/then_0.mcfunction +5 -0
  157. package/dist/data/quiz/function/handle_answer/then_12.mcfunction +5 -0
  158. package/dist/data/quiz/function/handle_answer/then_15.mcfunction +6 -0
  159. package/dist/data/quiz/function/handle_answer/then_3.mcfunction +6 -0
  160. package/dist/data/quiz/function/handle_answer/then_6.mcfunction +5 -0
  161. package/dist/data/quiz/function/handle_answer/then_9.mcfunction +6 -0
  162. package/dist/data/quiz/function/handle_answer.mcfunction +11 -0
  163. package/dist/data/quiz/function/start_quiz.mcfunction +5 -0
  164. package/dist/data/reqtest/function/__load.mcfunction +4 -0
  165. package/dist/data/reqtest/function/_table_init.mcfunction +2 -0
  166. package/dist/data/reqtest/function/no_trig.mcfunction +3 -0
  167. package/dist/data/reqtest/function/use_table.mcfunction +4 -0
  168. package/dist/data/reqtest2/function/__load.mcfunction +3 -0
  169. package/dist/data/reqtest2/function/no_trig.mcfunction +3 -0
  170. package/dist/data/runtime/function/__load.mcfunction +5 -0
  171. package/dist/data/runtime/function/__tick.mcfunction +2 -0
  172. package/dist/data/runtime/function/counter_tick/then_0.mcfunction +3 -0
  173. package/dist/data/runtime/function/counter_tick.mcfunction +13 -0
  174. package/dist/data/shop/function/__load.mcfunction +7 -0
  175. package/dist/data/shop/function/__tick.mcfunction +3 -0
  176. package/dist/data/shop/function/__trigger_shop_buy_dispatch.mcfunction +4 -0
  177. package/dist/data/shop/function/complete_purchase/else_1.mcfunction +5 -0
  178. package/dist/data/shop/function/complete_purchase/else_4.mcfunction +5 -0
  179. package/dist/data/shop/function/complete_purchase/else_7.mcfunction +3 -0
  180. package/dist/data/shop/function/complete_purchase/merge_2.mcfunction +2 -0
  181. package/dist/data/shop/function/complete_purchase/merge_5.mcfunction +2 -0
  182. package/dist/data/shop/function/complete_purchase/merge_8.mcfunction +2 -0
  183. package/dist/data/shop/function/complete_purchase/then_0.mcfunction +4 -0
  184. package/dist/data/shop/function/complete_purchase/then_3.mcfunction +4 -0
  185. package/dist/data/shop/function/complete_purchase/then_6.mcfunction +4 -0
  186. package/dist/data/shop/function/complete_purchase.mcfunction +7 -0
  187. package/dist/data/shop/function/handle_shop_trigger.mcfunction +3 -0
  188. package/dist/data/swap_test/function/__load.mcfunction +3 -0
  189. package/dist/data/swap_test/function/gcd_old/loop_body_1.mcfunction +7 -0
  190. package/dist/data/swap_test/function/gcd_old/loop_check_0.mcfunction +5 -0
  191. package/dist/data/swap_test/function/gcd_old/loop_exit_2.mcfunction +3 -0
  192. package/dist/data/swap_test/function/gcd_old.mcfunction +8 -0
  193. package/dist/data/turret/function/__load.mcfunction +5 -0
  194. package/dist/data/turret/function/__tick.mcfunction +4 -0
  195. package/dist/data/turret/function/__trigger_deploy_turret_dispatch.mcfunction +4 -0
  196. package/dist/data/turret/function/deploy_turret.mcfunction +8 -0
  197. package/dist/data/turret/function/turret_tick/at_1.mcfunction +2 -0
  198. package/dist/data/turret/function/turret_tick/foreach_0.mcfunction +2 -0
  199. package/dist/data/turret/function/turret_tick/foreach_2.mcfunction +2 -0
  200. package/dist/data/turret/function/turret_tick/tick_body.mcfunction +3 -0
  201. package/dist/data/turret/function/turret_tick/tick_skip.mcfunction +1 -0
  202. package/dist/data/turret/function/turret_tick.mcfunction +5 -0
  203. package/dist/gcd2.map.json +15 -0
  204. package/dist/gcd3.map.json +17 -0
  205. package/dist/gcd_test.map.json +15 -0
  206. package/dist/index.d.ts +4 -0
  207. package/dist/index.js +11 -6
  208. package/dist/isqrttest.map.json +15 -0
  209. package/dist/lowering/index.d.ts +3 -0
  210. package/dist/lowering/index.js +161 -64
  211. package/dist/mathtest.map.json +6 -0
  212. package/dist/mypack.map.json +27 -0
  213. package/dist/optimizer/commands.d.ts +1 -0
  214. package/dist/optimizer/commands.js +18 -11
  215. package/dist/optimizer/structure.d.ts +1 -0
  216. package/dist/optimizer/structure.js +6 -1
  217. package/dist/pack.mcmeta +6 -0
  218. package/dist/reqtest.map.json +4 -0
  219. package/dist/reqtest2.map.json +4 -0
  220. package/dist/runtime/index.js +26 -5
  221. package/dist/runtime.map.json +7 -0
  222. package/dist/swap_test.map.json +14 -0
  223. package/editors/vscode/package-lock.json +3 -3
  224. package/editors/vscode/package.json +1 -1
  225. package/examples/math-showcase.mcrs +146 -0
  226. package/examples/readme-demo.mcrs +92 -0
  227. package/examples/showcase.mcrs +505 -0
  228. package/package.json +1 -1
  229. package/src/__tests__/cli.test.ts +13 -13
  230. package/src/__tests__/optimizer-advanced.test.ts +4 -4
  231. package/src/__tests__/stdlib-advanced.test.ts +120 -0
  232. package/src/__tests__/stdlib-bigint.test.ts +427 -0
  233. package/src/cli.ts +14 -5
  234. package/src/codegen/mcfunction/index.ts +14 -5
  235. package/src/compile.ts +15 -2
  236. package/src/index.ts +17 -7
  237. package/src/lowering/index.ts +165 -63
  238. package/src/optimizer/commands.ts +18 -12
  239. package/src/optimizer/structure.ts +6 -2
  240. package/src/runtime/index.ts +27 -5
  241. package/src/stdlib/advanced.mcrs +81 -0
  242. package/src/stdlib/bigint.mcrs +205 -0
  243. package/src/stdlib/math.mcrs +19 -6
@@ -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
  '+': '+=',
@@ -0,0 +1,6 @@
1
+ {
2
+ "pack": {
3
+ "pack_format": 26,
4
+ "description": "mypack datapack — compiled by redscript"
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$a": "const:42",
3
+ "$b": "internal:ret"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$a": "const:42",
3
+ "$b": "internal:ret"
4
+ }
@@ -647,17 +647,27 @@ class MCRuntime {
647
647
  condition = condition && entities.length === 0;
648
648
  continue;
649
649
  }
650
- // Handle 'store result storage <ns:path> <field> <type> <scale>'
650
+ // Handle 'store result storage <ns:path> <field>[<idx>] <type> <scale>' (array element)
651
651
  if (rest.startsWith('store result storage ')) {
652
- rest = rest.slice(21);
653
- // format: <ns:path> <field> <type> <scale> <run-cmd>
654
- const storageParts = rest.match(/^(\S+)\s+(\S+)\s+(\S+)\s+([\d.]+)\s+(.*)$/);
652
+ const sliced = rest.slice(21);
653
+ // Try array-index form first: <ns:path> <field>[<idx>] <type> <scale> <run-cmd>
654
+ // Use [^\[\s]+ for field (no brackets or spaces) so that \[ matches correctly.
655
+ const arrParts = sliced.match(/^(\S+)\s+([^\[\s]+)\[(\d+)\]\s+(\S+)\s+([\d.]+)\s+(.*)$/);
656
+ if (arrParts) {
657
+ const [, storagePath, field, indexStr, , , remaining] = arrParts;
658
+ storeTarget = { storagePath, field: `${field}[${indexStr}]`, type: 'result' };
659
+ rest = remaining.trim();
660
+ continue;
661
+ }
662
+ // Plain form: <ns:path> <field> <type> <scale> <run-cmd>
663
+ const storageParts = sliced.match(/^(\S+)\s+(\S+)\s+(\S+)\s+([\d.]+)\s+(.*)$/);
655
664
  if (storageParts) {
656
665
  const [, storagePath, field, , , remaining] = storageParts;
657
666
  storeTarget = { storagePath, field, type: 'result' };
658
667
  rest = remaining.trim();
659
668
  continue;
660
669
  }
670
+ rest = sliced;
661
671
  }
662
672
  // Handle 'store result score <player> <obj>'
663
673
  if (rest.startsWith('store result score ')) {
@@ -771,8 +781,19 @@ class MCRuntime {
771
781
  }
772
782
  return true;
773
783
  }
784
+ // data modify storage <ns:path> <field>[<index>] set value <val> (array element write)
785
+ const setArrMatch = cmd.match(/^data modify storage (\S+) ([^\[\s]+)\[(\d+)\] set value (.+)$/);
786
+ if (setArrMatch) {
787
+ const [, storagePath, field, indexStr, valueStr] = setArrMatch;
788
+ const arr = this.getStorageField(storagePath, field);
789
+ const idx = parseInt(indexStr, 10);
790
+ if (Array.isArray(arr) && idx >= 0 && idx < arr.length) {
791
+ arr[idx] = this.parseDataValue(valueStr);
792
+ }
793
+ return true;
794
+ }
774
795
  // data get storage <ns:path> <field>[<index>] [scale] (array element access)
775
- const getArrMatch = cmd.match(/^data get storage (\S+) (\S+)\[(\d+)\](?:\s+[\d.]+)?$/);
796
+ const getArrMatch = cmd.match(/^data get storage (\S+) ([^\[\s]+)\[(\d+)\](?:\s+[\d.]+)?$/);
776
797
  if (getArrMatch) {
777
798
  const [, storagePath, field, indexStr] = getArrMatch;
778
799
  const arr = this.getStorageField(storagePath, field);
@@ -0,0 +1,7 @@
1
+ {
2
+ "$d": "ticks",
3
+ "$a": "const:0",
4
+ "$b": "const:1",
5
+ "$c": "const:100",
6
+ "$i": "internal:ret"
7
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$b": "a",
3
+ "$d": "b",
4
+ "$f": "p0",
5
+ "$g": "p1",
6
+ "$h": "x",
7
+ "$i": "y",
8
+ "$k": "tmp",
9
+ "$n": "m",
10
+ "$a": "const:0",
11
+ "$c": "internal:p0",
12
+ "$e": "internal:p1",
13
+ "$m": "internal:ret"
14
+ }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "redscript-vscode",
3
- "version": "1.0.28",
3
+ "version": "1.0.35",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "redscript-vscode",
9
- "version": "1.0.28",
9
+ "version": "1.0.35",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "redscript": "file:../../"
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "../..": {
25
25
  "name": "redscript-mc",
26
- "version": "1.2.26",
26
+ "version": "1.2.27",
27
27
  "license": "MIT",
28
28
  "bin": {
29
29
  "redscript": "dist/cli.js",
@@ -2,7 +2,7 @@
2
2
  "name": "redscript-vscode",
3
3
  "displayName": "RedScript for Minecraft",
4
4
  "description": "Syntax highlighting, error diagnostics, and language support for RedScript — a compiler targeting Minecraft Java Edition",
5
- "version": "1.0.28",
5
+ "version": "1.0.35",
6
6
  "publisher": "bkmashiro",
7
7
  "icon": "icon.png",
8
8
  "license": "MIT",
@@ -0,0 +1,146 @@
1
+ // math-showcase.mcrs — Fancy math examples for README / demo
2
+ // Run each fn individually: /function mathshow:<name>
3
+ //
4
+ // Compile: node dist/cli.js compile examples/math-showcase.mcrs -o <dp> --namespace mathshow
5
+
6
+ import "../src/stdlib/math.mcrs"
7
+ import "../src/stdlib/vec.mcrs"
8
+ import "../src/stdlib/advanced.mcrs"
9
+ import "../src/stdlib/bigint.mcrs"
10
+
11
+ // ─── 1. Trig table — print sin & cos for 0°, 15°, 30° … 90° ────────────────
12
+ // NOTE: sin_fixed/cos_fixed return values in ×1000 space (sin(30°) = 500, not 0.5).
13
+ // s*s + c*c ≈ 1000000 because (sin×1000)²+(cos×1000)² = 10⁶×(sin²+cos²) = 10⁶.
14
+ // Requires __load to have run (initializes the sin table).
15
+ fn trig_table() {
16
+ say("§e=== Trig Table (×1000 fixed-point, needs __load) ===");
17
+ let deg: int = 0;
18
+ while (deg <= 90) {
19
+ let s: int = sin_fixed(deg);
20
+ let c: int = cos_fixed(deg);
21
+ say(f" {deg}° sin={s} cos={c} s²+c²={s*s + c*c} (expect ~1000000)");
22
+ deg = deg + 15;
23
+ }
24
+ }
25
+
26
+ // ─── 2. Newton–Raphson sqrt — compare isqrt vs sqrt_fixed ──────────────────
27
+ // isqrt(n) → floor(sqrt(n)), raw integer
28
+ // sqrt_fixed(x) → sqrt of x/1000 expressed in ×1000 format
29
+ // e.g. sqrt_fixed(2000) = sqrt(2.0)×1000 = 1414
30
+ // Input must be in ×1000 space. Max input ≈ 2147000 (before overflow).
31
+ // NOTE: Each isqrt call runs a convergence loop (bits+guess+Newton).
32
+ // Large values = more loop iterations = more MC commands.
33
+ // Keep inputs moderate to stay under Paper's maxCommandChainLength (65536).
34
+ fn sqrt_showcase() {
35
+ say("§e=== Integer Square Root (isqrt) ===");
36
+ say(f" isqrt(2) = {isqrt(2)} (exact: 1)");
37
+ say(f" isqrt(144) = {isqrt(144)} (exact: 12)");
38
+ say(f" isqrt(10000) = {isqrt(10000)} (exact: 100)");
39
+ say(f" isqrt(1000000) = {isqrt(1000000)} (exact: 1000)");
40
+
41
+ say("§e=== Fixed-Point sqrt (input&output in ×1000) ===");
42
+ say(f" sqrt_fixed(1000) = {sqrt_fixed(1000)} (sqrt(1.0) = 1.000)");
43
+ say(f" sqrt_fixed(2000) = {sqrt_fixed(2000)} (sqrt(2.0) ≈ 1.414)");
44
+ say(f" sqrt_fixed(4000) = {sqrt_fixed(4000)} (sqrt(4.0) = 2.000)");
45
+ say(f" sqrt_fixed(9000) = {sqrt_fixed(9000)} (sqrt(9.0) = 3.000)");
46
+ }
47
+
48
+ // ─── 3. 2D vector geometry ──────────────────────────────────────────────────
49
+ fn vector_showcase() {
50
+ say("§e=== 2D Vector Geometry ===");
51
+ // 3-4-5 right triangle
52
+ let d345: int = distance2d_fixed(0, 0, 300, 400);
53
+ say(f" dist((0,0)→(300,400)) = {d345} (expect 500)");
54
+ // unit circle
55
+ let d_unit: int = length2d_fixed(1000, 0);
56
+ say(f" length(1000, 0) = {d_unit} (expect 1000)");
57
+ // atan2
58
+ say(f" atan2(1000, 0) = {atan2_fixed(1000, 0)}° (expect 90)");
59
+ say(f" atan2(1000, 1000) = {atan2_fixed(1000, 1000)}° (expect 45)");
60
+ say(f" atan2(-1, 0) = {atan2_fixed(-1, 0)}° (expect 270)");
61
+ // dot product: orthogonal vectors → 0
62
+ say(f" dot2d(1000,0, 0,1000) = {dot2d(1000, 0, 0, 1000)} (expect 0, orthogonal)");
63
+ say(f" dot2d(1000,0, 1000,0) = {dot2d(1000, 0, 1000, 0)} (expect 1000000, parallel)");
64
+ }
65
+
66
+ // ─── 4. Number theory ───────────────────────────────────────────────────────
67
+ fn number_theory() {
68
+ say("§e=== Number Theory ===");
69
+ say(f" gcd(360, 252) = {gcd(360, 252)} (expect 36)");
70
+ say(f" lcm(12, 18) = {lcm(12, 18)} (expect 36)");
71
+ say(f" is_prime(97) = {is_prime(97)} (expect 1)");
72
+ say(f" is_prime(100) = {is_prime(100)} (expect 0)");
73
+ say(f" fib(20) = {fib(20)} (expect 6765)");
74
+ say(f" collatz_steps(27) = {collatz_steps(27)} (expect 111)");
75
+ say(f" digit_sum(123456789) = {digit_sum(123456789)} (expect 45)");
76
+ say(f" mod_pow(2, 10, 1000) = {mod_pow(2, 10, 1000)} (expect 24)");
77
+ say(f" mod_pow(7, 5, 13) = {mod_pow(7, 5, 13)} (expect 11)");
78
+ }
79
+
80
+ // ─── 5. Fixed-point interpolation & easing ──────────────────────────────────
81
+ fn easing_showcase() {
82
+ say("§e=== Easing & Interpolation (t = 0..1000) ===");
83
+ let t: int = 0;
84
+ while (t <= 1000) {
85
+ let lin: int = lerp(0, 1000, t);
86
+ let sm: int = smoothstep(0, 1000, t);
87
+ let smr: int = smootherstep(0, 1000, t);
88
+ say(f" t={t} lerp={lin} smooth={sm} smoother={smr}");
89
+ t = t + 250;
90
+ }
91
+ }
92
+
93
+ // ─── 6. Hash & noise ────────────────────────────────────────────────────────
94
+ fn hash_showcase() {
95
+ say("§e=== Hash & Procedural Noise ===");
96
+ say(f" hash_int(0) = {hash_int(0)}");
97
+ say(f" hash_int(1) = {hash_int(1)}");
98
+ say(f" hash_int(42) = {hash_int(42)}");
99
+ say(f" noise1d(0) = {noise1d(0)} (0..1000)");
100
+ say(f" noise1d(100) = {noise1d(100)}");
101
+ say(f" noise1d(200) = {noise1d(200)}");
102
+ say(f" noise1d(300) = {noise1d(300)}");
103
+ }
104
+
105
+ // ─── 7. BigInt Fibonacci — numbers beyond INT32 ─────────────────────────────
106
+ fn bigint_showcase() {
107
+ say("§e=== BigInt — Arbitrary Precision (base 10000, 8 limbs) ===");
108
+ bigint_fib(50);
109
+ let l0: int = bigint_get_a(0);
110
+ let l1: int = bigint_get_a(1);
111
+ let l2: int = bigint_get_a(2);
112
+ say(f" fib(50) limbs: [{l2}][{l1}][{l0}]");
113
+ say(f" = {l2}×10⁸ + {l1}×10⁴ + {l0}");
114
+ say(f" = 12,586,269,025 (verify: l0=9025 l1=8626 l2=125)");
115
+
116
+ bigint_fib(70);
117
+ let m0: int = bigint_get_a(0);
118
+ let m1: int = bigint_get_a(1);
119
+ let m2: int = bigint_get_a(2);
120
+ let m3: int = bigint_get_a(3);
121
+ say(f" fib(70) limbs: [{m3}][{m2}][{m1}][{m0}]");
122
+ say(f" = 190,392,490,709,135");
123
+ }
124
+
125
+ // ─── 8. Mandelbrot / Julia — fractals in integer arithmetic ─────────────────
126
+ fn fractal_showcase() {
127
+ say("§e=== Mandelbrot & Julia (fixed-point, scale=1000) ===");
128
+ // Mandelbrot: does (cr, ci) escape in <max_iter> steps?
129
+ say(f" mandelbrot_iter(0, 0, 64) = {mandelbrot_iter(0, 0, 64)} (inside, expect 64)");
130
+ say(f" mandelbrot_iter(2000, 0, 64) = {mandelbrot_iter(2000, 0, 64)} (outside, < 64)");
131
+ say(f" mandelbrot_iter(-500, 500, 64) = {mandelbrot_iter(-500, 500, 64)}");
132
+ say(f" julia_iter(0, 0, -500, 250, 64) = {julia_iter(0, 0, -500, 250, 64)}");
133
+ say(f" julia_iter(500, 0, -500, 250, 64)= {julia_iter(500, 0, -500, 250, 64)}");
134
+ }
135
+
136
+ // ─── 9. Run everything ──────────────────────────────────────────────────────
137
+ fn run_all() {
138
+ trig_table();
139
+ sqrt_showcase();
140
+ vector_showcase();
141
+ number_theory();
142
+ easing_showcase();
143
+ hash_showcase();
144
+ bigint_showcase();
145
+ fractal_showcase();
146
+ }
@@ -0,0 +1,92 @@
1
+ // readme-demo.mcrs — RedScript v1.2.27 README visual showcase
2
+ // What 55 lines of RedScript can do on a vanilla Minecraft server.
3
+ //
4
+ // Compile: node dist/cli.js compile examples/readme-demo.mcrs -o <datapack> --namespace rsdemo
5
+ // In-game: /function rsdemo:start (then record your gif)
6
+ // /function rsdemo:stop
7
+
8
+ import "../src/stdlib/math.mcrs"
9
+
10
+ let t: int = 0; // 0..359 degrees, 4° per tick = full cycle every 1.5 s
11
+ let frame: int = 0;
12
+ let on: bool = false;
13
+
14
+ @load fn _init() {
15
+ t = 0;
16
+ frame = 0;
17
+ }
18
+
19
+ @tick fn _wave_tick() {
20
+ if (!on) { return; }
21
+
22
+ t = (t + 4) % 360;
23
+ frame = frame + 1;
24
+
25
+ let s: int = sin_fixed(t); // -1000 .. 1000
26
+ let c: int = cos_fixed(t); // -1000 .. 1000
27
+ let mag: int = abs(s); // 0 .. 1000
28
+
29
+ // ── Particle rings: spread values scale with sin(t) ─────────────────
30
+ // Inner ring — always visible, small tight shimmer
31
+ foreach (p in @a) at @s {
32
+ particle("minecraft:end_rod", ~0, ~1, ~0, 0.25, 0.08, 0.25, 0.02, 6);
33
+ }
34
+
35
+ // Mid ring — appears when sin > 0 (upper half of cycle)
36
+ if (s > 0) {
37
+ foreach (p in @a) at @s {
38
+ particle("minecraft:end_rod", ~0, ~1, ~0, 0.9, 0.25, 0.9, 0.02, 12);
39
+ }
40
+ }
41
+
42
+ // Outer ring + soul fire — sin > 600
43
+ if (s > 600) {
44
+ foreach (p in @a) at @s {
45
+ particle("minecraft:end_rod", ~0, ~1, ~0, 1.8, 0.4, 1.8, 0.02, 18);
46
+ particle("minecraft:soul_fire_flame", ~0, ~2, ~0, 0.9, 0.3, 0.9, 0.03, 8);
47
+ }
48
+ }
49
+
50
+ // Peak flash — fires ~4 ticks per cycle
51
+ if (s > 940) {
52
+ foreach (p in @a) at @s {
53
+ particle("minecraft:flash", ~0, ~3, ~0, 0.4, 0.3, 0.4, 0.1, 3);
54
+ }
55
+ }
56
+
57
+ // Negative trough — ground portal swirl
58
+ if (s < -700) {
59
+ foreach (p in @a) at @s {
60
+ particle("minecraft:portal", ~0, ~0.5, ~0, 1.2, 0.4, 1.2, 0.05, 12);
61
+ }
62
+ }
63
+
64
+ // ── Actionbar: live math rotating every 3 s ──────────────────────────
65
+ let seg: int = (frame / 45) % 4;
66
+ if (seg == 0) {
67
+ actionbar(@a, f"§3⚡ §bsin({t}°) = §e{s}‰ cos({t}°) = §e{c}‰");
68
+ }
69
+ if (seg == 1) {
70
+ actionbar(@a, f"§3⚡ §bsmoothstep(|sin|) = §e{smoothstep(0, 1000, mag)}‰ isqrt({frame}) = §e{isqrt(frame)}");
71
+ }
72
+ if (seg == 2) {
73
+ actionbar(@a, f"§3⚡ §bgcd(360, {t + 1}) = §e{gcd(360, t + 1)} log2({mag + 1}) = §e{log2_int(mag + 1)}");
74
+ }
75
+ if (seg == 3) {
76
+ actionbar(@a, f"§3⚡ §bpow(2, {(frame / 30) % 10 + 1}) = §e{pow_int(2, (frame / 30) % 10 + 1)} clamp(sin) = §e{clamp(s, -500, 500)}");
77
+ }
78
+ }
79
+
80
+ fn start() {
81
+ on = true;
82
+ t = 0;
83
+ frame = 0;
84
+ title(@a, f"§e✦ §bRedScript§e ✦", f"§7trig-driven particle rings");
85
+ say("§a▶ readme-demo started — /function rsdemo:stop to end");
86
+ }
87
+
88
+ fn stop() {
89
+ on = false;
90
+ actionbar(@a, f"§7[ demo stopped ]");
91
+ say("§c■ readme-demo stopped");
92
+ }