redscript-mc 1.2.25 → 1.2.27

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 (265) hide show
  1. package/README.md +67 -9
  2. package/README.zh.md +61 -4
  3. package/dist/__tests__/cli.test.js +1 -1
  4. package/dist/__tests__/codegen.test.js +12 -6
  5. package/dist/__tests__/e2e.test.js +6 -6
  6. package/dist/__tests__/lowering.test.js +8 -8
  7. package/dist/__tests__/optimizer.test.js +31 -0
  8. package/dist/__tests__/stdlib-advanced.test.d.ts +4 -0
  9. package/dist/__tests__/stdlib-advanced.test.js +378 -0
  10. package/dist/__tests__/stdlib-bigint.test.d.ts +7 -0
  11. package/dist/__tests__/stdlib-bigint.test.js +428 -0
  12. package/dist/__tests__/stdlib-math.test.d.ts +7 -0
  13. package/dist/__tests__/stdlib-math.test.js +352 -0
  14. package/dist/__tests__/stdlib-vec.test.d.ts +4 -0
  15. package/dist/__tests__/stdlib-vec.test.js +264 -0
  16. package/dist/ast/types.d.ts +17 -1
  17. package/dist/codegen/mcfunction/index.js +154 -18
  18. package/dist/codegen/var-allocator.d.ts +17 -0
  19. package/dist/codegen/var-allocator.js +26 -0
  20. package/dist/compile.d.ts +14 -0
  21. package/dist/compile.js +62 -5
  22. package/dist/data/arena/function/__load.mcfunction +6 -0
  23. package/dist/data/arena/function/__tick.mcfunction +2 -0
  24. package/dist/data/arena/function/announce_leaders/else_1.mcfunction +3 -0
  25. package/dist/data/arena/function/announce_leaders/foreach_0/merge_2.mcfunction +1 -0
  26. package/dist/data/arena/function/announce_leaders/foreach_0/then_0.mcfunction +3 -0
  27. package/dist/data/arena/function/announce_leaders/foreach_0.mcfunction +7 -0
  28. package/dist/data/arena/function/announce_leaders/foreach_1/merge_2.mcfunction +1 -0
  29. package/dist/data/arena/function/announce_leaders/foreach_1/then_0.mcfunction +4 -0
  30. package/dist/data/arena/function/announce_leaders/foreach_1.mcfunction +6 -0
  31. package/dist/data/arena/function/announce_leaders/merge_2.mcfunction +1 -0
  32. package/dist/data/arena/function/announce_leaders/then_0.mcfunction +4 -0
  33. package/dist/data/arena/function/announce_leaders.mcfunction +6 -0
  34. package/dist/data/arena/function/arena_tick/merge_2.mcfunction +1 -0
  35. package/dist/data/arena/function/arena_tick/then_0.mcfunction +4 -0
  36. package/dist/data/arena/function/arena_tick.mcfunction +11 -0
  37. package/dist/data/counter/function/__load.mcfunction +5 -0
  38. package/dist/data/counter/function/__tick.mcfunction +2 -0
  39. package/dist/data/counter/function/counter_tick/merge_2.mcfunction +1 -0
  40. package/dist/data/counter/function/counter_tick/then_0.mcfunction +3 -0
  41. package/dist/data/counter/function/counter_tick.mcfunction +11 -0
  42. package/dist/data/gcd2/function/__load.mcfunction +3 -0
  43. package/dist/data/gcd2/function/abs/merge_2.mcfunction +3 -0
  44. package/dist/data/gcd2/function/abs/then_0.mcfunction +5 -0
  45. package/dist/data/gcd2/function/abs.mcfunction +7 -0
  46. package/dist/data/gcd2/function/gcd/loop_body_1.mcfunction +7 -0
  47. package/dist/data/gcd2/function/gcd/loop_check_0.mcfunction +5 -0
  48. package/dist/data/gcd2/function/gcd/loop_exit_2.mcfunction +3 -0
  49. package/dist/data/gcd2/function/gcd.mcfunction +14 -0
  50. package/dist/data/gcd3/function/__load.mcfunction +3 -0
  51. package/dist/data/gcd3/function/abs/merge_2.mcfunction +3 -0
  52. package/dist/data/gcd3/function/abs/then_0.mcfunction +5 -0
  53. package/dist/data/gcd3/function/abs.mcfunction +7 -0
  54. package/dist/data/gcd3/function/gcd/loop_body_1.mcfunction +7 -0
  55. package/dist/data/gcd3/function/gcd/loop_check_0.mcfunction +5 -0
  56. package/dist/data/gcd3/function/gcd/loop_exit_2.mcfunction +3 -0
  57. package/dist/data/gcd3/function/gcd.mcfunction +14 -0
  58. package/dist/data/gcd3/function/test.mcfunction +7 -0
  59. package/dist/data/gcd3nm/function/__load.mcfunction +3 -0
  60. package/dist/data/gcd3nm/function/abs/merge_2.mcfunction +3 -0
  61. package/dist/data/gcd3nm/function/abs/then_0.mcfunction +5 -0
  62. package/dist/data/gcd3nm/function/abs.mcfunction +7 -0
  63. package/dist/data/gcd3nm/function/gcd/loop_body_1.mcfunction +7 -0
  64. package/dist/data/gcd3nm/function/gcd/loop_check_0.mcfunction +5 -0
  65. package/dist/data/gcd3nm/function/gcd/loop_exit_2.mcfunction +3 -0
  66. package/dist/data/gcd3nm/function/gcd.mcfunction +14 -0
  67. package/dist/data/gcd3nm/function/test.mcfunction +7 -0
  68. package/dist/data/gcd_test/function/__load.mcfunction +3 -0
  69. package/dist/data/gcd_test/function/abs/merge_2.mcfunction +3 -0
  70. package/dist/data/gcd_test/function/abs/then_0.mcfunction +5 -0
  71. package/dist/data/gcd_test/function/abs.mcfunction +7 -0
  72. package/dist/data/gcd_test/function/gcd/loop_body_1.mcfunction +7 -0
  73. package/dist/data/gcd_test/function/gcd/loop_check_0.mcfunction +5 -0
  74. package/dist/data/gcd_test/function/gcd/loop_exit_2.mcfunction +3 -0
  75. package/dist/data/gcd_test/function/gcd.mcfunction +14 -0
  76. package/dist/data/isqrttest/function/__load.mcfunction +6 -0
  77. package/dist/data/isqrttest/function/isqrt/loop_body_4.mcfunction +12 -0
  78. package/dist/data/isqrttest/function/isqrt/loop_check_3.mcfunction +5 -0
  79. package/dist/data/isqrttest/function/isqrt/loop_exit_5.mcfunction +3 -0
  80. package/dist/data/isqrttest/function/isqrt/merge_2.mcfunction +4 -0
  81. package/dist/data/isqrttest/function/isqrt/merge_8.mcfunction +6 -0
  82. package/dist/data/isqrttest/function/isqrt/then_0.mcfunction +3 -0
  83. package/dist/data/isqrttest/function/isqrt/then_6.mcfunction +3 -0
  84. package/dist/data/isqrttest/function/isqrt.mcfunction +7 -0
  85. package/dist/data/isqrttest/function/test.mcfunction +6 -0
  86. package/dist/data/mathtest/function/__load.mcfunction +3 -0
  87. package/dist/data/mathtest/function/abs/merge_2.mcfunction +3 -0
  88. package/dist/data/mathtest/function/abs/then_0.mcfunction +5 -0
  89. package/dist/data/mathtest/function/abs.mcfunction +6 -0
  90. package/dist/data/mathtest/function/test.mcfunction +5 -0
  91. package/dist/data/minecraft/tags/function/load.json +5 -0
  92. package/dist/data/minecraft/tags/function/tick.json +5 -0
  93. package/dist/data/mypack/function/__load.mcfunction +13 -0
  94. package/dist/data/mypack/function/_atan_init.mcfunction +2 -0
  95. package/dist/data/mypack/function/abs/merge_2.mcfunction +3 -0
  96. package/dist/data/mypack/function/abs/then_0.mcfunction +5 -0
  97. package/dist/data/mypack/function/abs.mcfunction +6 -0
  98. package/dist/data/mypack/function/atan2_fixed/__sgi_1.mcfunction +2 -0
  99. package/dist/data/mypack/function/atan2_fixed/else_34.mcfunction +3 -0
  100. package/dist/data/mypack/function/atan2_fixed/loop_body_31.mcfunction +19 -0
  101. package/dist/data/mypack/function/atan2_fixed/loop_check_30.mcfunction +5 -0
  102. package/dist/data/mypack/function/atan2_fixed/loop_exit_32.mcfunction +6 -0
  103. package/dist/data/mypack/function/atan2_fixed/merge_11.mcfunction +6 -0
  104. package/dist/data/mypack/function/atan2_fixed/merge_14.mcfunction +3 -0
  105. package/dist/data/mypack/function/atan2_fixed/merge_17.mcfunction +6 -0
  106. package/dist/data/mypack/function/atan2_fixed/merge_2.mcfunction +5 -0
  107. package/dist/data/mypack/function/atan2_fixed/merge_20.mcfunction +5 -0
  108. package/dist/data/mypack/function/atan2_fixed/merge_23.mcfunction +5 -0
  109. package/dist/data/mypack/function/atan2_fixed/merge_26.mcfunction +6 -0
  110. package/dist/data/mypack/function/atan2_fixed/merge_29.mcfunction +4 -0
  111. package/dist/data/mypack/function/atan2_fixed/merge_38.mcfunction +5 -0
  112. package/dist/data/mypack/function/atan2_fixed/merge_41.mcfunction +5 -0
  113. package/dist/data/mypack/function/atan2_fixed/merge_44.mcfunction +5 -0
  114. package/dist/data/mypack/function/atan2_fixed/merge_47.mcfunction +5 -0
  115. package/dist/data/mypack/function/atan2_fixed/merge_5.mcfunction +5 -0
  116. package/dist/data/mypack/function/atan2_fixed/merge_8.mcfunction +3 -0
  117. package/dist/data/mypack/function/atan2_fixed/then_0.mcfunction +5 -0
  118. package/dist/data/mypack/function/atan2_fixed/then_12.mcfunction +3 -0
  119. package/dist/data/mypack/function/atan2_fixed/then_15.mcfunction +5 -0
  120. package/dist/data/mypack/function/atan2_fixed/then_18.mcfunction +5 -0
  121. package/dist/data/mypack/function/atan2_fixed/then_21.mcfunction +3 -0
  122. package/dist/data/mypack/function/atan2_fixed/then_24.mcfunction +3 -0
  123. package/dist/data/mypack/function/atan2_fixed/then_27.mcfunction +6 -0
  124. package/dist/data/mypack/function/atan2_fixed/then_3.mcfunction +3 -0
  125. package/dist/data/mypack/function/atan2_fixed/then_33.mcfunction +5 -0
  126. package/dist/data/mypack/function/atan2_fixed/then_36.mcfunction +5 -0
  127. package/dist/data/mypack/function/atan2_fixed/then_39.mcfunction +5 -0
  128. package/dist/data/mypack/function/atan2_fixed/then_42.mcfunction +3 -0
  129. package/dist/data/mypack/function/atan2_fixed/then_45.mcfunction +5 -0
  130. package/dist/data/mypack/function/atan2_fixed/then_6.mcfunction +3 -0
  131. package/dist/data/mypack/function/atan2_fixed/then_9.mcfunction +5 -0
  132. package/dist/data/mypack/function/atan2_fixed.mcfunction +7 -0
  133. package/dist/data/mypack/function/my_game.mcfunction +10 -0
  134. package/dist/data/quiz/function/__load.mcfunction +16 -0
  135. package/dist/data/quiz/function/__tick.mcfunction +6 -0
  136. package/dist/data/quiz/function/__trigger_quiz_a_dispatch.mcfunction +4 -0
  137. package/dist/data/quiz/function/__trigger_quiz_b_dispatch.mcfunction +4 -0
  138. package/dist/data/quiz/function/__trigger_quiz_c_dispatch.mcfunction +4 -0
  139. package/dist/data/quiz/function/__trigger_quiz_start_dispatch.mcfunction +4 -0
  140. package/dist/data/quiz/function/answer_a.mcfunction +4 -0
  141. package/dist/data/quiz/function/answer_b.mcfunction +4 -0
  142. package/dist/data/quiz/function/answer_c.mcfunction +4 -0
  143. package/dist/data/quiz/function/ask_question/else_1.mcfunction +5 -0
  144. package/dist/data/quiz/function/ask_question/else_4.mcfunction +5 -0
  145. package/dist/data/quiz/function/ask_question/else_7.mcfunction +4 -0
  146. package/dist/data/quiz/function/ask_question/merge_2.mcfunction +1 -0
  147. package/dist/data/quiz/function/ask_question/merge_5.mcfunction +2 -0
  148. package/dist/data/quiz/function/ask_question/merge_8.mcfunction +2 -0
  149. package/dist/data/quiz/function/ask_question/then_0.mcfunction +4 -0
  150. package/dist/data/quiz/function/ask_question/then_3.mcfunction +4 -0
  151. package/dist/data/quiz/function/ask_question/then_6.mcfunction +4 -0
  152. package/dist/data/quiz/function/ask_question.mcfunction +7 -0
  153. package/dist/data/quiz/function/finish_quiz.mcfunction +6 -0
  154. package/dist/data/quiz/function/handle_answer/else_1.mcfunction +5 -0
  155. package/dist/data/quiz/function/handle_answer/else_10.mcfunction +3 -0
  156. package/dist/data/quiz/function/handle_answer/else_16.mcfunction +3 -0
  157. package/dist/data/quiz/function/handle_answer/else_4.mcfunction +3 -0
  158. package/dist/data/quiz/function/handle_answer/else_7.mcfunction +5 -0
  159. package/dist/data/quiz/function/handle_answer/merge_11.mcfunction +2 -0
  160. package/dist/data/quiz/function/handle_answer/merge_14.mcfunction +2 -0
  161. package/dist/data/quiz/function/handle_answer/merge_17.mcfunction +2 -0
  162. package/dist/data/quiz/function/handle_answer/merge_2.mcfunction +8 -0
  163. package/dist/data/quiz/function/handle_answer/merge_5.mcfunction +2 -0
  164. package/dist/data/quiz/function/handle_answer/merge_8.mcfunction +2 -0
  165. package/dist/data/quiz/function/handle_answer/then_0.mcfunction +5 -0
  166. package/dist/data/quiz/function/handle_answer/then_12.mcfunction +5 -0
  167. package/dist/data/quiz/function/handle_answer/then_15.mcfunction +6 -0
  168. package/dist/data/quiz/function/handle_answer/then_3.mcfunction +6 -0
  169. package/dist/data/quiz/function/handle_answer/then_6.mcfunction +5 -0
  170. package/dist/data/quiz/function/handle_answer/then_9.mcfunction +6 -0
  171. package/dist/data/quiz/function/handle_answer.mcfunction +11 -0
  172. package/dist/data/quiz/function/start_quiz.mcfunction +5 -0
  173. package/dist/data/reqtest/function/__load.mcfunction +4 -0
  174. package/dist/data/reqtest/function/_table_init.mcfunction +2 -0
  175. package/dist/data/reqtest/function/no_trig.mcfunction +3 -0
  176. package/dist/data/reqtest/function/use_table.mcfunction +4 -0
  177. package/dist/data/reqtest2/function/__load.mcfunction +3 -0
  178. package/dist/data/reqtest2/function/no_trig.mcfunction +3 -0
  179. package/dist/data/runtime/function/__load.mcfunction +5 -0
  180. package/dist/data/runtime/function/__tick.mcfunction +2 -0
  181. package/dist/data/runtime/function/counter_tick/then_0.mcfunction +3 -0
  182. package/dist/data/runtime/function/counter_tick.mcfunction +13 -0
  183. package/dist/data/shop/function/__load.mcfunction +7 -0
  184. package/dist/data/shop/function/__tick.mcfunction +3 -0
  185. package/dist/data/shop/function/__trigger_shop_buy_dispatch.mcfunction +4 -0
  186. package/dist/data/shop/function/complete_purchase/else_1.mcfunction +5 -0
  187. package/dist/data/shop/function/complete_purchase/else_4.mcfunction +5 -0
  188. package/dist/data/shop/function/complete_purchase/else_7.mcfunction +3 -0
  189. package/dist/data/shop/function/complete_purchase/merge_2.mcfunction +2 -0
  190. package/dist/data/shop/function/complete_purchase/merge_5.mcfunction +2 -0
  191. package/dist/data/shop/function/complete_purchase/merge_8.mcfunction +2 -0
  192. package/dist/data/shop/function/complete_purchase/then_0.mcfunction +4 -0
  193. package/dist/data/shop/function/complete_purchase/then_3.mcfunction +4 -0
  194. package/dist/data/shop/function/complete_purchase/then_6.mcfunction +4 -0
  195. package/dist/data/shop/function/complete_purchase.mcfunction +7 -0
  196. package/dist/data/shop/function/handle_shop_trigger.mcfunction +3 -0
  197. package/dist/data/swap_test/function/__load.mcfunction +3 -0
  198. package/dist/data/swap_test/function/gcd_old/loop_body_1.mcfunction +7 -0
  199. package/dist/data/swap_test/function/gcd_old/loop_check_0.mcfunction +5 -0
  200. package/dist/data/swap_test/function/gcd_old/loop_exit_2.mcfunction +3 -0
  201. package/dist/data/swap_test/function/gcd_old.mcfunction +8 -0
  202. package/dist/data/turret/function/__load.mcfunction +5 -0
  203. package/dist/data/turret/function/__tick.mcfunction +4 -0
  204. package/dist/data/turret/function/__trigger_deploy_turret_dispatch.mcfunction +4 -0
  205. package/dist/data/turret/function/deploy_turret.mcfunction +8 -0
  206. package/dist/data/turret/function/turret_tick/at_1.mcfunction +2 -0
  207. package/dist/data/turret/function/turret_tick/foreach_0.mcfunction +2 -0
  208. package/dist/data/turret/function/turret_tick/foreach_2.mcfunction +2 -0
  209. package/dist/data/turret/function/turret_tick/tick_body.mcfunction +3 -0
  210. package/dist/data/turret/function/turret_tick/tick_skip.mcfunction +1 -0
  211. package/dist/data/turret/function/turret_tick.mcfunction +5 -0
  212. package/dist/gcd2.map.json +15 -0
  213. package/dist/gcd3.map.json +17 -0
  214. package/dist/gcd_test.map.json +15 -0
  215. package/dist/index.js +20 -1
  216. package/dist/ir/types.d.ts +4 -0
  217. package/dist/isqrttest.map.json +15 -0
  218. package/dist/lexer/index.d.ts +1 -1
  219. package/dist/lexer/index.js +1 -0
  220. package/dist/lowering/index.d.ts +5 -0
  221. package/dist/lowering/index.js +154 -14
  222. package/dist/mathtest.map.json +6 -0
  223. package/dist/mypack.map.json +27 -0
  224. package/dist/optimizer/dce.js +21 -5
  225. package/dist/optimizer/passes.js +18 -6
  226. package/dist/optimizer/structure.js +7 -0
  227. package/dist/pack.mcmeta +6 -0
  228. package/dist/parser/index.d.ts +5 -0
  229. package/dist/parser/index.js +43 -2
  230. package/dist/reqtest.map.json +4 -0
  231. package/dist/reqtest2.map.json +4 -0
  232. package/dist/runtime/index.d.ts +6 -0
  233. package/dist/runtime/index.js +130 -9
  234. package/dist/runtime.map.json +7 -0
  235. package/dist/swap_test.map.json +14 -0
  236. package/editors/vscode/package-lock.json +3 -3
  237. package/editors/vscode/package.json +1 -1
  238. package/examples/showcase.mcrs +505 -0
  239. package/package.json +1 -1
  240. package/src/__tests__/cli.test.ts +1 -1
  241. package/src/__tests__/codegen.test.ts +12 -6
  242. package/src/__tests__/e2e.test.ts +6 -6
  243. package/src/__tests__/lowering.test.ts +8 -8
  244. package/src/__tests__/optimizer.test.ts +33 -0
  245. package/src/__tests__/stdlib-advanced.test.ts +379 -0
  246. package/src/__tests__/stdlib-bigint.test.ts +427 -0
  247. package/src/__tests__/stdlib-math.test.ts +374 -0
  248. package/src/__tests__/stdlib-vec.test.ts +259 -0
  249. package/src/ast/types.ts +11 -1
  250. package/src/codegen/mcfunction/index.ts +143 -19
  251. package/src/codegen/var-allocator.ts +29 -0
  252. package/src/compile.ts +72 -5
  253. package/src/index.ts +21 -1
  254. package/src/ir/types.ts +2 -0
  255. package/src/lexer/index.ts +2 -1
  256. package/src/lowering/index.ts +171 -14
  257. package/src/optimizer/dce.ts +22 -5
  258. package/src/optimizer/passes.ts +18 -5
  259. package/src/optimizer/structure.ts +6 -1
  260. package/src/parser/index.ts +47 -2
  261. package/src/runtime/index.ts +130 -10
  262. package/src/stdlib/advanced.mcrs +330 -0
  263. package/src/stdlib/bigint.mcrs +205 -0
  264. package/src/stdlib/math.mcrs +274 -21
  265. package/src/stdlib/vec.mcrs +246 -0
@@ -106,6 +106,39 @@ describe('deadCodeElimination', () => {
106
106
  })
107
107
  })
108
108
 
109
+ describe('copyPropagation – stale alias invalidation', () => {
110
+ it('does not propagate $tmp = $y after $y is overwritten (swap pattern)', () => {
111
+ // Simulates: let tmp = y; y = x % y; x = tmp
112
+ // The copy $tmp = $y must be invalidated when $y is reassigned.
113
+ // Before fix: x = tmp was propagated to x = y (new y, wrong value).
114
+ const fn = makeFn([
115
+ { op: 'assign', dst: '$tmp', src: { kind: 'var', name: '$y' } }, // tmp = y
116
+ { op: 'binop', dst: '$r', lhs: { kind: 'var', name: '$x' }, bop: '%', rhs: { kind: 'var', name: '$y' } }, // r = x%y
117
+ { op: 'assign', dst: '$y', src: { kind: 'var', name: '$r' } }, // y = r ← stale: tmp still points to OLD y
118
+ { op: 'assign', dst: '$x', src: { kind: 'var', name: '$tmp' } }, // x = tmp (should NOT be x = y)
119
+ ])
120
+ const opt = copyPropagation(fn)
121
+ const instrs = opt.blocks[0].instrs
122
+ const xAssign = instrs.find((i: any) => i.dst === '$x') as any
123
+ // x = tmp must NOT be optimised to x = $y (stale) or x = $r (new y).
124
+ // It should stay as x = $tmp (the original copy).
125
+ expect(xAssign.src).toEqual({ kind: 'var', name: '$tmp' })
126
+ })
127
+
128
+ it('still propagates simple non-conflicting copies', () => {
129
+ // a = 5; b = a; c = b → after propagation b and c should both be const 5
130
+ const fn = makeFn([
131
+ { op: 'assign', dst: '$a', src: { kind: 'const', value: 5 } },
132
+ { op: 'assign', dst: '$b', src: { kind: 'var', name: '$a' } },
133
+ { op: 'assign', dst: '$c', src: { kind: 'var', name: '$b' } },
134
+ ])
135
+ const opt = copyPropagation(fn)
136
+ const instrs = opt.blocks[0].instrs
137
+ const cAssign = instrs.find((i: any) => i.dst === '$c') as any
138
+ expect(cAssign.src).toEqual({ kind: 'const', value: 5 })
139
+ })
140
+ })
141
+
109
142
  describe('optimize pipeline', () => {
110
143
  it('combines all passes', () => {
111
144
  // t0 = 2 + 3 (→ constant fold → t0 = 5)
@@ -0,0 +1,379 @@
1
+ /**
2
+ * stdlib/advanced.mcrs — runtime behavioural tests
3
+ */
4
+
5
+ import * as fs from 'fs'
6
+ import * as path from 'path'
7
+ import { compile } from '../compile'
8
+ import { MCRuntime } from '../runtime'
9
+
10
+ const MATH_SRC = fs.readFileSync(path.join(__dirname, '../../src/stdlib/math.mcrs'), 'utf-8')
11
+ const ADV_SRC = fs.readFileSync(path.join(__dirname, '../../src/stdlib/advanced.mcrs'), 'utf-8')
12
+
13
+ function run(driver: string): MCRuntime {
14
+ const result = compile(driver, {
15
+ namespace: 'advtest',
16
+ librarySources: [MATH_SRC, ADV_SRC],
17
+ })
18
+ if (!result.success) throw new Error(result.error?.message ?? 'compile failed')
19
+ const rt = new MCRuntime('advtest')
20
+ for (const file of result.files ?? []) {
21
+ if (!file.path.endsWith('.mcfunction')) continue
22
+ const match = file.path.match(/data\/([^/]+)\/function\/(.+)\.mcfunction$/)
23
+ if (!match) continue
24
+ rt.loadFunction(`${match[1]}:${match[2]}`, file.content.split('\n'))
25
+ }
26
+ rt.load()
27
+ return rt
28
+ }
29
+
30
+ function scoreOf(rt: MCRuntime, key: string): number {
31
+ return rt.getScore('out', `advtest.${key}`)
32
+ }
33
+
34
+ // ─── fib ─────────────────────────────────────────────────────────────────────
35
+
36
+ describe('fib', () => {
37
+ it.each([
38
+ [0, 0],
39
+ [1, 1],
40
+ [2, 1],
41
+ [5, 5],
42
+ [10, 55],
43
+ [20, 6765],
44
+ ])('fib(%d) == %d', (n, expected) => {
45
+ const rt = run(`fn test() { scoreboard_set("out", "r", fib(${n})); }`)
46
+ rt.execFunction('test')
47
+ expect(scoreOf(rt, 'r')).toBe(expected)
48
+ })
49
+ })
50
+
51
+ // ─── is_prime ─────────────────────────────────────────────────────────────────
52
+
53
+ describe('is_prime', () => {
54
+ it.each([
55
+ [0, 0], [1, 0], [2, 1], [3, 1], [4, 0],
56
+ [7, 1], [9, 0], [11, 1], [97, 1], [100, 0],
57
+ ])('is_prime(%d) == %d', (n, expected) => {
58
+ const rt = run(`fn test() { scoreboard_set("out", "r", is_prime(${n})); }`)
59
+ rt.execFunction('test')
60
+ expect(scoreOf(rt, 'r')).toBe(expected)
61
+ })
62
+ })
63
+
64
+ // ─── collatz_steps ───────────────────────────────────────────────────────────
65
+
66
+ describe('collatz_steps', () => {
67
+ it.each([
68
+ [1, 0],
69
+ [2, 1],
70
+ [4, 2],
71
+ [6, 8],
72
+ [27, 111],
73
+ ])('collatz_steps(%d) == %d', (n, expected) => {
74
+ const rt = run(`fn test() { scoreboard_set("out", "r", collatz_steps(${n})); }`)
75
+ rt.execFunction('test')
76
+ expect(scoreOf(rt, 'r')).toBe(expected)
77
+ })
78
+ })
79
+
80
+ // ─── digit helpers ───────────────────────────────────────────────────────────
81
+
82
+ describe('digit_sum', () => {
83
+ it.each([
84
+ [0, 0], [1, 1], [9, 9], [123, 6], [999, 27], [-42, 6],
85
+ ])('digit_sum(%d) == %d', (n, expected) => {
86
+ const rt = run(`fn test() { scoreboard_set("out", "r", digit_sum(${n})); }`)
87
+ rt.execFunction('test')
88
+ expect(scoreOf(rt, 'r')).toBe(expected)
89
+ })
90
+ })
91
+
92
+ describe('count_digits', () => {
93
+ it.each([
94
+ [0, 1], [9, 1], [10, 2], [100, 3], [9999, 4],
95
+ ])('count_digits(%d) == %d', (n, expected) => {
96
+ const rt = run(`fn test() { scoreboard_set("out", "r", count_digits(${n})); }`)
97
+ rt.execFunction('test')
98
+ expect(scoreOf(rt, 'r')).toBe(expected)
99
+ })
100
+ })
101
+
102
+ describe('reverse_int', () => {
103
+ it.each([
104
+ [12345, 54321],
105
+ [100, 1],
106
+ [7, 7],
107
+ ])('reverse_int(%d) == %d', (n, expected) => {
108
+ const rt = run(`fn test() { scoreboard_set("out", "r", reverse_int(${n})); }`)
109
+ rt.execFunction('test')
110
+ expect(scoreOf(rt, 'r')).toBe(expected)
111
+ })
112
+ })
113
+
114
+ // ─── mod_pow ─────────────────────────────────────────────────────────────────
115
+
116
+ describe('mod_pow', () => {
117
+ it.each([
118
+ [2, 10, 1000, 24], // 2^10 = 1024, 1024 mod 1000 = 24
119
+ [3, 4, 100, 81], // 3^4 = 81
120
+ [2, 0, 10, 1], // any^0 = 1
121
+ [5, 1, 100, 5],
122
+ [7, 3, 13, 343 % 13], // 343 mod 13 = 5
123
+ ])('mod_pow(%d,%d,%d) == %d', (b, e, m, expected) => {
124
+ const rt = run(`fn test() { scoreboard_set("out", "r", mod_pow(${b},${e},${m})); }`)
125
+ rt.execFunction('test')
126
+ expect(scoreOf(rt, 'r')).toBe(expected)
127
+ })
128
+ })
129
+
130
+ // ─── hash_int ────────────────────────────────────────────────────────────────
131
+
132
+ describe('hash_int', () => {
133
+ it('is deterministic', () => {
134
+ const rt1 = run(`fn test() { scoreboard_set("out", "r", hash_int(42)); }`)
135
+ rt1.execFunction('test')
136
+ const rt2 = run(`fn test() { scoreboard_set("out", "r", hash_int(42)); }`)
137
+ rt2.execFunction('test')
138
+ expect(scoreOf(rt1, 'r')).toBe(scoreOf(rt2, 'r'))
139
+ })
140
+
141
+ it('different inputs → different outputs', () => {
142
+ const rt = run(`fn test() {
143
+ scoreboard_set("out", "a", hash_int(0));
144
+ scoreboard_set("out", "b", hash_int(1));
145
+ scoreboard_set("out", "c", hash_int(1000));
146
+ }`)
147
+ rt.execFunction('test')
148
+ const a = scoreOf(rt, 'a')
149
+ const b = scoreOf(rt, 'b')
150
+ const c = scoreOf(rt, 'c')
151
+ expect(a).not.toBe(b)
152
+ expect(b).not.toBe(c)
153
+ })
154
+
155
+ it('output is non-negative', () => {
156
+ for (const n of [-1000, -1, 0, 1, 999, 46340]) {
157
+ const rt = run(`fn test() { scoreboard_set("out", "r", hash_int(${n})); }`)
158
+ rt.execFunction('test')
159
+ expect(scoreOf(rt, 'r')).toBeGreaterThanOrEqual(0)
160
+ }
161
+ })
162
+ })
163
+
164
+ // ─── noise1d ─────────────────────────────────────────────────────────────────
165
+
166
+ describe('noise1d', () => {
167
+ it('output in [0, 999]', () => {
168
+ for (const x of [0, 100, 500, 999, 1000, 2000]) {
169
+ const rt = run(`fn test() { scoreboard_set("out", "r", noise1d(${x})); }`)
170
+ rt.execFunction('test')
171
+ const v = scoreOf(rt, 'r')
172
+ expect(v).toBeGreaterThanOrEqual(0)
173
+ expect(v).toBeLessThan(1000)
174
+ }
175
+ })
176
+
177
+ it('is deterministic', () => {
178
+ const rt1 = run(`fn test() { scoreboard_set("out", "r", noise1d(1234)); }`)
179
+ rt1.execFunction('test')
180
+ const rt2 = run(`fn test() { scoreboard_set("out", "r", noise1d(1234)); }`)
181
+ rt2.execFunction('test')
182
+ expect(scoreOf(rt1, 'r')).toBe(scoreOf(rt2, 'r'))
183
+ })
184
+ })
185
+
186
+ // ─── bezier ──────────────────────────────────────────────────────────────────
187
+
188
+ describe('bezier_quad', () => {
189
+ it.each([
190
+ [0, 500, 1000, 0, 0], // t=0: start
191
+ [0, 500, 1000, 1000, 1000], // t=1000: end
192
+ [0, 1000, 0, 500, 500], // arch midpoint
193
+ ])('bezier_quad(%d,%d,%d,t=%d) == %d', (p0, p1, p2, t, expected) => {
194
+ const rt = run(`fn test() { scoreboard_set("out", "r", bezier_quad(${p0},${p1},${p2},${t})); }`)
195
+ rt.execFunction('test')
196
+ expect(scoreOf(rt, 'r')).toBe(expected)
197
+ })
198
+ })
199
+
200
+ describe('bezier_cubic', () => {
201
+ it('t=0: start', () => {
202
+ const rt = run(`fn test() { scoreboard_set("out", "r", bezier_cubic(0,333,667,1000,0)); }`)
203
+ rt.execFunction('test')
204
+ expect(scoreOf(rt, 'r')).toBe(0)
205
+ })
206
+ it('t=1000: end', () => {
207
+ const rt = run(`fn test() { scoreboard_set("out", "r", bezier_cubic(0,333,667,1000,1000)); }`)
208
+ rt.execFunction('test')
209
+ expect(scoreOf(rt, 'r')).toBe(1000)
210
+ })
211
+ })
212
+
213
+ // ─── mandelbrot_iter ─────────────────────────────────────────────────────────
214
+
215
+ describe('mandelbrot_iter', () => {
216
+ // c = 0 + 0i → always in set (z always 0)
217
+ it('origin (0,0) stays bounded for max_iter=20', () => {
218
+ const rt = run(`fn test() { scoreboard_set("out", "r", mandelbrot_iter(0,0,20)); }`)
219
+ rt.execFunction('test')
220
+ expect(scoreOf(rt, 'r')).toBe(20)
221
+ })
222
+
223
+ // c = 2 + 0i → escapes immediately (|z1| = 2, |z2| = 6 > 2)
224
+ it('c=(2000,0) escapes quickly', () => {
225
+ const rt = run(`fn test() { scoreboard_set("out", "r", mandelbrot_iter(2000,0,50)); }`)
226
+ rt.execFunction('test')
227
+ expect(scoreOf(rt, 'r')).toBeLessThan(5)
228
+ })
229
+
230
+ // c = -1 + 0i → stays bounded (period-2 cycle: 0 → -1 → 0 → ...)
231
+ it('c=(-1000,0) is in the set', () => {
232
+ const rt = run(`fn test() { scoreboard_set("out", "r", mandelbrot_iter(-1000,0,50)); }`)
233
+ rt.execFunction('test')
234
+ expect(scoreOf(rt, 'r')).toBe(50)
235
+ })
236
+
237
+ // c = 0.5 + 0.5i → escapes
238
+ it('c=(500,500) escapes before max_iter=100', () => {
239
+ const rt = run(`fn test() { scoreboard_set("out", "r", mandelbrot_iter(500,500,100)); }`)
240
+ rt.execFunction('test')
241
+ expect(scoreOf(rt, 'r')).toBeLessThan(100)
242
+ })
243
+ })
244
+
245
+ // ─── julia_iter ──────────────────────────────────────────────────────────────
246
+
247
+ describe('julia_iter', () => {
248
+ it('z0=(0,0) with c=(0,0) stays bounded', () => {
249
+ const rt = run(`fn test() { scoreboard_set("out", "r", julia_iter(0,0,0,0,20)); }`)
250
+ rt.execFunction('test')
251
+ expect(scoreOf(rt, 'r')).toBe(20)
252
+ })
253
+
254
+ it('z0=(3000,0) escapes immediately', () => {
255
+ const rt = run(`fn test() { scoreboard_set("out", "r", julia_iter(3000,0,0,0,20)); }`)
256
+ rt.execFunction('test')
257
+ expect(scoreOf(rt, 'r')).toBe(0)
258
+ })
259
+ })
260
+
261
+ // ─── Experiment: cross-stdlib (vec + math + advanced) ────────────────────────
262
+
263
+ const VEC_SRC = fs.readFileSync(path.join(__dirname, '../../src/stdlib/vec.mcrs'), 'utf-8')
264
+
265
+ function runAll(driver: string): MCRuntime {
266
+ const result = compile(driver, {
267
+ namespace: 'exptest',
268
+ librarySources: [MATH_SRC, VEC_SRC, ADV_SRC],
269
+ })
270
+ if (!result.success) throw new Error(result.error?.message ?? 'compile failed: ' + result.error?.message)
271
+ const rt = new MCRuntime('exptest')
272
+ for (const file of result.files ?? []) {
273
+ if (!file.path.endsWith('.mcfunction')) continue
274
+ const match = file.path.match(/data\/([^/]+)\/function\/(.+)\.mcfunction$/)
275
+ if (!match) continue
276
+ rt.loadFunction(`${match[1]}:${match[2]}`, file.content.split('\n'))
277
+ }
278
+ rt.load()
279
+ return rt
280
+ }
281
+
282
+ function exp(rt: MCRuntime, key: string): number {
283
+ return rt.getScore('out', `exptest.${key}`) ?? 0
284
+ }
285
+
286
+ describe('newton_sqrt', () => {
287
+ it('newton_sqrt(25) == 5', () => {
288
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", newton_sqrt(25)); }`)
289
+ rt.execFunction('test')
290
+ expect(exp(rt, 'r')).toBe(5)
291
+ })
292
+ it('newton_sqrt(100) == 10', () => {
293
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", newton_sqrt(100)); }`)
294
+ rt.execFunction('test')
295
+ expect(exp(rt, 'r')).toBe(10)
296
+ })
297
+ it('newton_sqrt(2) == 1', () => {
298
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", newton_sqrt(2)); }`)
299
+ rt.execFunction('test')
300
+ expect(exp(rt, 'r')).toBe(1)
301
+ })
302
+ it('newton_sqrt(0) == 0', () => {
303
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", newton_sqrt(0)); }`)
304
+ rt.execFunction('test')
305
+ expect(exp(rt, 'r')).toBe(0)
306
+ })
307
+ })
308
+
309
+ describe('digital_root', () => {
310
+ it('digital_root(493) == 7', () => {
311
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", digital_root(493)); }`)
312
+ rt.execFunction('test')
313
+ expect(exp(rt, 'r')).toBe(7)
314
+ })
315
+ it('digital_root(9) == 9', () => {
316
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", digital_root(9)); }`)
317
+ rt.execFunction('test')
318
+ expect(exp(rt, 'r')).toBe(9)
319
+ })
320
+ it('digital_root(0) == 0', () => {
321
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", digital_root(0)); }`)
322
+ rt.execFunction('test')
323
+ expect(exp(rt, 'r')).toBe(0)
324
+ })
325
+ })
326
+
327
+ describe('spiral_ring', () => {
328
+ it('spiral_ring(1) == 0', () => {
329
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", spiral_ring(1)); }`)
330
+ rt.execFunction('test')
331
+ expect(exp(rt, 'r')).toBe(0)
332
+ })
333
+ it('spiral_ring(9) == 1', () => {
334
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", spiral_ring(9)); }`)
335
+ rt.execFunction('test')
336
+ expect(exp(rt, 'r')).toBe(1)
337
+ })
338
+ it('spiral_ring(25) == 2', () => {
339
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", spiral_ring(25)); }`)
340
+ rt.execFunction('test')
341
+ expect(exp(rt, 'r')).toBe(2)
342
+ })
343
+ })
344
+
345
+ describe('clamp_circle', () => {
346
+ it('point inside: clamp_circle_x(3,4,10) == 3', () => {
347
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", clamp_circle_x(3, 4, 10)); }`)
348
+ rt.execFunction('test')
349
+ expect(exp(rt, 'r')).toBe(3)
350
+ })
351
+ it('point outside: clamp_circle_x(600,0,500) == 500', () => {
352
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", clamp_circle_x(600, 0, 500)); }`)
353
+ rt.execFunction('test')
354
+ expect(exp(rt, 'r')).toBe(500)
355
+ })
356
+ it('clamp_circle_y(0,600,500) == 500', () => {
357
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", clamp_circle_y(0, 600, 500)); }`)
358
+ rt.execFunction('test')
359
+ expect(exp(rt, 'r')).toBe(500)
360
+ })
361
+ })
362
+
363
+ describe('angle_between', () => {
364
+ it('perpendicular vectors: angle_between(1000,0,0,1000) == 90', () => {
365
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", angle_between(1000, 0, 0, 1000)); }`)
366
+ rt.execFunction('test')
367
+ expect(exp(rt, 'r')).toBe(90)
368
+ })
369
+ it('parallel vectors: angle_between(1000,0,1000,0) == 0', () => {
370
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", angle_between(1000, 0, 1000, 0)); }`)
371
+ rt.execFunction('test')
372
+ expect(exp(rt, 'r')).toBe(0)
373
+ })
374
+ it('opposite vectors: angle_between(1000,0,-1000,0) == 180', () => {
375
+ const rt = runAll(`fn test() { scoreboard_set("out", "r", angle_between(1000, 0, -1000, 0)); }`)
376
+ rt.execFunction('test')
377
+ expect(exp(rt, 'r')).toBe(180)
378
+ })
379
+ })