redscript-mc 2.0.0 → 2.1.1

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 (643) hide show
  1. package/.claudeignore +21 -0
  2. package/README.md +48 -16
  3. package/README.zh.md +2 -2
  4. package/dist/cli.js +0 -0
  5. package/dist/src/__tests__/budget.test.js +261 -0
  6. package/dist/src/__tests__/diagnostics.test.js +2 -3
  7. package/dist/src/__tests__/e2e/coroutine.test.d.ts +7 -0
  8. package/dist/src/__tests__/e2e/coroutine.test.js +132 -0
  9. package/dist/{src2 → src}/__tests__/e2e/macros.test.js +1 -1
  10. package/dist/{src2 → src}/__tests__/e2e/migrate.test.js +1 -1
  11. package/dist/src/__tests__/e2e/stdlib-e2e.test.d.ts +10 -0
  12. package/dist/src/__tests__/e2e/stdlib-e2e.test.js +324 -0
  13. package/dist/src/__tests__/enum.test.d.ts +10 -0
  14. package/dist/src/__tests__/enum.test.js +389 -0
  15. package/dist/src/__tests__/generics.test.d.ts +14 -0
  16. package/dist/src/__tests__/generics.test.js +367 -0
  17. package/dist/{src2 → src}/__tests__/hir/desugar.test.js +2 -2
  18. package/dist/src/__tests__/incremental.test.d.ts +5 -0
  19. package/dist/src/__tests__/incremental.test.js +308 -0
  20. package/dist/src/__tests__/lsp.test.d.ts +7 -0
  21. package/dist/src/__tests__/lsp.test.js +321 -0
  22. package/dist/src/__tests__/mc-syntax.test.js +1 -6
  23. package/dist/src/__tests__/mc-version.test.d.ts +10 -0
  24. package/dist/src/__tests__/mc-version.test.js +154 -0
  25. package/dist/{src2 → src}/__tests__/mir/arithmetic.test.js +2 -2
  26. package/dist/{src2 → src}/__tests__/mir/control-flow.test.js +2 -2
  27. package/dist/src/__tests__/modules.test.d.ts +7 -0
  28. package/dist/src/__tests__/modules.test.js +333 -0
  29. package/dist/src/__tests__/optimizer/coroutine.test.d.ts +12 -0
  30. package/dist/src/__tests__/optimizer/coroutine.test.js +251 -0
  31. package/dist/src/__tests__/optimizer/interprocedural.test.js +145 -0
  32. package/dist/src/__tests__/optimizer/lir/const_imm.test.js +138 -0
  33. package/dist/src/__tests__/optimizer/lir/dead_slot.test.js +141 -0
  34. package/dist/src/__tests__/optimizer/lir/peephole.test.js +126 -0
  35. package/dist/src/__tests__/optimizer/lir/pipeline.test.js +84 -0
  36. package/dist/src/__tests__/optimizer/nbt-batch.test.js +110 -0
  37. package/dist/src/__tests__/optimizer/selector-cache.test.js +103 -0
  38. package/dist/src/__tests__/optimizer/unroll.test.js +206 -0
  39. package/dist/src/__tests__/option.test.d.ts +14 -0
  40. package/dist/src/__tests__/option.test.js +275 -0
  41. package/dist/src/__tests__/schedule.test.d.ts +7 -0
  42. package/dist/src/__tests__/schedule.test.js +98 -0
  43. package/dist/src/__tests__/sourcemap.test.d.ts +7 -0
  44. package/dist/src/__tests__/sourcemap.test.js +227 -0
  45. package/dist/src/__tests__/stdlib-include.test.js +86 -0
  46. package/dist/src/__tests__/tuple.test.d.ts +11 -0
  47. package/dist/src/__tests__/tuple.test.js +202 -0
  48. package/dist/src/__tests__/typechecker-strict.test.d.ts +10 -0
  49. package/dist/src/__tests__/typechecker-strict.test.js +197 -0
  50. package/dist/src/ast/types.d.ts +56 -2
  51. package/dist/src/cache/deps.d.ts +41 -0
  52. package/dist/src/cache/deps.js +158 -0
  53. package/dist/src/cache/incremental.d.ts +35 -0
  54. package/dist/src/cache/incremental.js +165 -0
  55. package/dist/src/cache/index.d.ts +37 -0
  56. package/dist/src/cache/index.js +152 -0
  57. package/dist/src/cli.js +83 -45
  58. package/dist/src/compile.d.ts +3 -2
  59. package/dist/src/compile.js +34 -11
  60. package/dist/src/diagnostics/index.d.ts +1 -1
  61. package/dist/src/diagnostics/index.js +8 -11
  62. package/dist/src/emit/compile.d.ts +31 -0
  63. package/dist/src/emit/compile.js +143 -0
  64. package/dist/{src2 → src}/emit/index.d.ts +9 -0
  65. package/dist/{src2 → src}/emit/index.js +59 -8
  66. package/dist/src/emit/modules.d.ts +29 -0
  67. package/dist/src/emit/modules.js +492 -0
  68. package/dist/src/emit/sourcemap.d.ts +53 -0
  69. package/dist/src/emit/sourcemap.js +73 -0
  70. package/dist/{src2 → src}/hir/lower.d.ts +1 -1
  71. package/dist/{src2 → src}/hir/lower.js +22 -1
  72. package/dist/src/hir/monomorphize.d.ts +22 -0
  73. package/dist/src/hir/monomorphize.js +379 -0
  74. package/dist/{src2 → src}/hir/types.d.ts +35 -2
  75. package/dist/src/index.d.ts +19 -2
  76. package/dist/src/index.js +36 -14
  77. package/dist/src/lexer/index.d.ts +1 -1
  78. package/dist/src/lexer/index.js +1 -0
  79. package/dist/src/lir/budget.d.ts +37 -0
  80. package/dist/src/lir/budget.js +280 -0
  81. package/dist/{src2 → src}/lir/lower.js +19 -0
  82. package/dist/{src2 → src}/lir/types.d.ts +7 -4
  83. package/dist/src/lsp/main.d.ts +8 -0
  84. package/dist/src/lsp/main.js +11 -0
  85. package/dist/src/lsp/server.d.ts +11 -0
  86. package/dist/src/lsp/server.js +403 -0
  87. package/dist/{src2 → src}/mir/lower.d.ts +1 -1
  88. package/dist/{src2 → src}/mir/lower.js +244 -10
  89. package/dist/{src2 → src}/mir/types.d.ts +10 -2
  90. package/dist/src/optimizer/coroutine.d.ts +34 -0
  91. package/dist/src/optimizer/coroutine.js +789 -0
  92. package/dist/src/optimizer/dce.d.ts +8 -34
  93. package/dist/src/optimizer/dce.js +146 -629
  94. package/dist/src/optimizer/interprocedural.d.ts +14 -0
  95. package/dist/src/optimizer/interprocedural.js +186 -0
  96. package/dist/src/optimizer/lir/const_imm.d.ts +12 -0
  97. package/dist/src/optimizer/lir/const_imm.js +139 -0
  98. package/dist/src/optimizer/lir/dead_slot.d.ts +14 -0
  99. package/dist/src/optimizer/lir/dead_slot.js +130 -0
  100. package/dist/src/optimizer/lir/peephole.d.ts +21 -0
  101. package/dist/src/optimizer/lir/peephole.js +52 -0
  102. package/dist/src/optimizer/lir/pipeline.d.ts +10 -0
  103. package/dist/src/optimizer/lir/pipeline.js +34 -0
  104. package/dist/src/optimizer/nbt-batch.d.ts +11 -0
  105. package/dist/src/optimizer/nbt-batch.js +51 -0
  106. package/dist/{src2 → src}/optimizer/pipeline.d.ts +4 -0
  107. package/dist/{src2 → src}/optimizer/pipeline.js +17 -1
  108. package/dist/src/optimizer/selector-cache.d.ts +22 -0
  109. package/dist/src/optimizer/selector-cache.js +100 -0
  110. package/dist/src/optimizer/unroll.d.ts +32 -0
  111. package/dist/src/optimizer/unroll.js +348 -0
  112. package/dist/src/parser/index.d.ts +8 -0
  113. package/dist/src/parser/index.js +204 -14
  114. package/dist/src/repl.d.ts +1 -1
  115. package/dist/src/typechecker/index.d.ts +4 -0
  116. package/dist/src/typechecker/index.js +198 -13
  117. package/dist/src/types/mc-version.d.ts +24 -0
  118. package/dist/src/types/mc-version.js +49 -0
  119. package/docs/ROADMAP.md +395 -0
  120. package/docs/compiler-pipeline-redesign.md +27 -10
  121. package/editors/vscode/out/extension.js +25176 -8000
  122. package/editors/vscode/package-lock.json +90 -6
  123. package/editors/vscode/package.json +3 -2
  124. package/editors/vscode/src/extension.ts +97 -67
  125. package/editors/vscode/syntaxes/redscript.tmLanguage.json +34 -0
  126. package/examples/coroutine-demo.mcrs +50 -0
  127. package/examples/enum-demo.mcrs +95 -0
  128. package/examples/scheduler-demo.mcrs +59 -0
  129. package/examples/showcase.mcrs +3 -3
  130. package/jest.config.js +1 -1
  131. package/package.json +9 -3
  132. package/src/__tests__/budget.test.ts +297 -0
  133. package/src/__tests__/diagnostics.test.ts +2 -3
  134. package/src/__tests__/e2e/coroutine.test.ts +142 -0
  135. package/{src2 → src}/__tests__/e2e/macros.test.ts +1 -1
  136. package/{src2 → src}/__tests__/e2e/migrate.test.ts +1 -1
  137. package/src/__tests__/e2e/stdlib-e2e.test.ts +348 -0
  138. package/src/__tests__/enum.test.ts +425 -0
  139. package/src/__tests__/generics.test.ts +390 -0
  140. package/{src2 → src}/__tests__/hir/desugar.test.ts +2 -2
  141. package/src/__tests__/incremental.test.ts +337 -0
  142. package/src/__tests__/lsp.test.ts +359 -0
  143. package/src/__tests__/mc-syntax.test.ts +1 -7
  144. package/src/__tests__/mc-version.test.ts +178 -0
  145. package/{src2 → src}/__tests__/mir/arithmetic.test.ts +2 -2
  146. package/{src2 → src}/__tests__/mir/control-flow.test.ts +2 -2
  147. package/src/__tests__/modules.test.ts +365 -0
  148. package/src/__tests__/optimizer/coroutine.test.ts +312 -0
  149. package/src/__tests__/optimizer/interprocedural.test.ts +174 -0
  150. package/src/__tests__/optimizer/lir/const_imm.test.ts +151 -0
  151. package/src/__tests__/optimizer/lir/dead_slot.test.ts +156 -0
  152. package/src/__tests__/optimizer/lir/peephole.test.ts +136 -0
  153. package/src/__tests__/optimizer/lir/pipeline.test.ts +113 -0
  154. package/src/__tests__/optimizer/nbt-batch.test.ts +119 -0
  155. package/src/__tests__/optimizer/selector-cache.test.ts +112 -0
  156. package/src/__tests__/optimizer/unroll.test.ts +231 -0
  157. package/src/__tests__/option.test.ts +299 -0
  158. package/src/__tests__/schedule.test.ts +105 -0
  159. package/src/__tests__/sourcemap.test.ts +254 -0
  160. package/src/__tests__/stdlib-include.test.ts +61 -0
  161. package/src/__tests__/tuple.test.ts +220 -0
  162. package/src/__tests__/typechecker-strict.test.ts +216 -0
  163. package/src/ast/types.ts +33 -2
  164. package/src/cache/deps.ts +132 -0
  165. package/src/cache/incremental.ts +173 -0
  166. package/src/cache/index.ts +135 -0
  167. package/src/cli.ts +96 -45
  168. package/src/compile.ts +46 -17
  169. package/src/diagnostics/index.ts +8 -11
  170. package/src/emit/compile.ts +179 -0
  171. package/{src2 → src}/emit/index.ts +72 -8
  172. package/src/emit/modules.ts +581 -0
  173. package/src/emit/sourcemap.ts +101 -0
  174. package/{src2 → src}/hir/lower.ts +29 -2
  175. package/src/hir/monomorphize.ts +416 -0
  176. package/{src2 → src}/hir/types.ts +15 -3
  177. package/src/index.ts +29 -10
  178. package/src/lexer/index.ts +2 -1
  179. package/src/lir/budget.ts +321 -0
  180. package/{src2 → src}/lir/lower.ts +32 -1
  181. package/{src2 → src}/lir/types.ts +7 -3
  182. package/src/lsp/main.ts +9 -0
  183. package/src/lsp/server.ts +469 -0
  184. package/{src2 → src}/mir/lower.ts +251 -8
  185. package/{src2 → src}/mir/types.ts +12 -1
  186. package/src/optimizer/coroutine.ts +996 -0
  187. package/{src2 → src}/optimizer/dce.ts +2 -1
  188. package/src/optimizer/interprocedural.ts +177 -0
  189. package/src/optimizer/lir/const_imm.ts +143 -0
  190. package/src/optimizer/lir/dead_slot.ts +123 -0
  191. package/src/optimizer/lir/peephole.ts +57 -0
  192. package/src/optimizer/lir/pipeline.ts +37 -0
  193. package/src/optimizer/nbt-batch.ts +50 -0
  194. package/{src2 → src}/optimizer/pipeline.ts +16 -1
  195. package/src/optimizer/selector-cache.ts +103 -0
  196. package/src/optimizer/unroll.ts +386 -0
  197. package/src/parser/index.ts +212 -15
  198. package/src/repl.ts +1 -1
  199. package/src/stdlib/math.mcrs +4 -4
  200. package/src/templates/quest.mcrs +4 -4
  201. package/src/typechecker/index.ts +215 -15
  202. package/src/types/mc-version.ts +46 -0
  203. package/tsconfig.json +1 -1
  204. package/.claude/commands/build-test.md +0 -10
  205. package/.claude/commands/deploy-demo.md +0 -12
  206. package/.claude/commands/stage-status.md +0 -13
  207. package/.claude/settings.json +0 -12
  208. package/CLAUDE.md +0 -231
  209. package/dist/__tests__/cli.test.js +0 -278
  210. package/dist/__tests__/codegen.test.js +0 -152
  211. package/dist/__tests__/compile-all.test.d.ts +0 -10
  212. package/dist/__tests__/compile-all.test.js +0 -108
  213. package/dist/__tests__/dce.test.js +0 -138
  214. package/dist/__tests__/diagnostics.test.d.ts +0 -4
  215. package/dist/__tests__/diagnostics.test.js +0 -149
  216. package/dist/__tests__/e2e.test.d.ts +0 -6
  217. package/dist/__tests__/e2e.test.js +0 -1847
  218. package/dist/__tests__/entity-types.test.js +0 -203
  219. package/dist/__tests__/formatter.test.js +0 -40
  220. package/dist/__tests__/lexer.test.js +0 -343
  221. package/dist/__tests__/lowering.test.js +0 -1015
  222. package/dist/__tests__/macro.test.d.ts +0 -8
  223. package/dist/__tests__/macro.test.js +0 -305
  224. package/dist/__tests__/mc-integration.test.d.ts +0 -12
  225. package/dist/__tests__/mc-integration.test.js +0 -819
  226. package/dist/__tests__/mc-syntax.test.js +0 -124
  227. package/dist/__tests__/nbt.test.js +0 -82
  228. package/dist/__tests__/optimizer-advanced.test.js +0 -124
  229. package/dist/__tests__/optimizer.test.js +0 -149
  230. package/dist/__tests__/parser.test.d.ts +0 -1
  231. package/dist/__tests__/parser.test.js +0 -807
  232. package/dist/__tests__/repl.test.d.ts +0 -1
  233. package/dist/__tests__/repl.test.js +0 -27
  234. package/dist/__tests__/runtime.test.d.ts +0 -1
  235. package/dist/__tests__/runtime.test.js +0 -289
  236. package/dist/__tests__/stdlib-advanced.test.d.ts +0 -4
  237. package/dist/__tests__/stdlib-advanced.test.js +0 -378
  238. package/dist/__tests__/stdlib-bigint.test.d.ts +0 -7
  239. package/dist/__tests__/stdlib-bigint.test.js +0 -428
  240. package/dist/__tests__/stdlib-math.test.d.ts +0 -7
  241. package/dist/__tests__/stdlib-math.test.js +0 -352
  242. package/dist/__tests__/stdlib-vec.test.d.ts +0 -4
  243. package/dist/__tests__/stdlib-vec.test.js +0 -264
  244. package/dist/__tests__/structure-optimizer.test.d.ts +0 -1
  245. package/dist/__tests__/structure-optimizer.test.js +0 -33
  246. package/dist/__tests__/typechecker.test.d.ts +0 -1
  247. package/dist/__tests__/typechecker.test.js +0 -552
  248. package/dist/__tests__/var-allocator.test.d.ts +0 -1
  249. package/dist/__tests__/var-allocator.test.js +0 -69
  250. package/dist/ast/types.d.ts +0 -514
  251. package/dist/ast/types.js +0 -9
  252. package/dist/builtins/metadata.d.ts +0 -36
  253. package/dist/builtins/metadata.js +0 -1014
  254. package/dist/cli.d.ts +0 -11
  255. package/dist/codegen/cmdblock/index.d.ts +0 -26
  256. package/dist/codegen/cmdblock/index.js +0 -45
  257. package/dist/codegen/mcfunction/index.d.ts +0 -40
  258. package/dist/codegen/mcfunction/index.js +0 -606
  259. package/dist/codegen/structure/index.d.ts +0 -24
  260. package/dist/codegen/structure/index.js +0 -279
  261. package/dist/codegen/var-allocator.d.ts +0 -45
  262. package/dist/codegen/var-allocator.js +0 -104
  263. package/dist/compile.d.ts +0 -68
  264. package/dist/data/arena/function/__load.mcfunction +0 -6
  265. package/dist/data/arena/function/__tick.mcfunction +0 -2
  266. package/dist/data/arena/function/announce_leaders/else_1.mcfunction +0 -3
  267. package/dist/data/arena/function/announce_leaders/foreach_0/merge_2.mcfunction +0 -1
  268. package/dist/data/arena/function/announce_leaders/foreach_0/then_0.mcfunction +0 -3
  269. package/dist/data/arena/function/announce_leaders/foreach_0.mcfunction +0 -7
  270. package/dist/data/arena/function/announce_leaders/foreach_1/merge_2.mcfunction +0 -1
  271. package/dist/data/arena/function/announce_leaders/foreach_1/then_0.mcfunction +0 -4
  272. package/dist/data/arena/function/announce_leaders/foreach_1.mcfunction +0 -6
  273. package/dist/data/arena/function/announce_leaders/merge_2.mcfunction +0 -1
  274. package/dist/data/arena/function/announce_leaders/then_0.mcfunction +0 -4
  275. package/dist/data/arena/function/announce_leaders.mcfunction +0 -6
  276. package/dist/data/arena/function/arena_tick/merge_2.mcfunction +0 -1
  277. package/dist/data/arena/function/arena_tick/then_0.mcfunction +0 -4
  278. package/dist/data/arena/function/arena_tick.mcfunction +0 -11
  279. package/dist/data/counter/function/__load.mcfunction +0 -5
  280. package/dist/data/counter/function/__tick.mcfunction +0 -2
  281. package/dist/data/counter/function/counter_tick/merge_2.mcfunction +0 -1
  282. package/dist/data/counter/function/counter_tick/then_0.mcfunction +0 -3
  283. package/dist/data/counter/function/counter_tick.mcfunction +0 -11
  284. package/dist/data/gcd2/function/__load.mcfunction +0 -3
  285. package/dist/data/gcd2/function/abs/merge_2.mcfunction +0 -3
  286. package/dist/data/gcd2/function/abs/then_0.mcfunction +0 -5
  287. package/dist/data/gcd2/function/abs.mcfunction +0 -7
  288. package/dist/data/gcd2/function/gcd/loop_body_1.mcfunction +0 -7
  289. package/dist/data/gcd2/function/gcd/loop_check_0.mcfunction +0 -5
  290. package/dist/data/gcd2/function/gcd/loop_exit_2.mcfunction +0 -3
  291. package/dist/data/gcd2/function/gcd.mcfunction +0 -14
  292. package/dist/data/gcd3/function/__load.mcfunction +0 -3
  293. package/dist/data/gcd3/function/abs/merge_2.mcfunction +0 -3
  294. package/dist/data/gcd3/function/abs/then_0.mcfunction +0 -5
  295. package/dist/data/gcd3/function/abs.mcfunction +0 -7
  296. package/dist/data/gcd3/function/gcd/loop_body_1.mcfunction +0 -7
  297. package/dist/data/gcd3/function/gcd/loop_check_0.mcfunction +0 -5
  298. package/dist/data/gcd3/function/gcd/loop_exit_2.mcfunction +0 -3
  299. package/dist/data/gcd3/function/gcd.mcfunction +0 -14
  300. package/dist/data/gcd3/function/test.mcfunction +0 -7
  301. package/dist/data/gcd3nm/function/__load.mcfunction +0 -3
  302. package/dist/data/gcd3nm/function/abs/merge_2.mcfunction +0 -3
  303. package/dist/data/gcd3nm/function/abs/then_0.mcfunction +0 -5
  304. package/dist/data/gcd3nm/function/abs.mcfunction +0 -7
  305. package/dist/data/gcd3nm/function/gcd/loop_body_1.mcfunction +0 -7
  306. package/dist/data/gcd3nm/function/gcd/loop_check_0.mcfunction +0 -5
  307. package/dist/data/gcd3nm/function/gcd/loop_exit_2.mcfunction +0 -3
  308. package/dist/data/gcd3nm/function/gcd.mcfunction +0 -14
  309. package/dist/data/gcd3nm/function/test.mcfunction +0 -7
  310. package/dist/data/gcd_test/function/__load.mcfunction +0 -3
  311. package/dist/data/gcd_test/function/abs/merge_2.mcfunction +0 -3
  312. package/dist/data/gcd_test/function/abs/then_0.mcfunction +0 -5
  313. package/dist/data/gcd_test/function/abs.mcfunction +0 -7
  314. package/dist/data/gcd_test/function/gcd/loop_body_1.mcfunction +0 -7
  315. package/dist/data/gcd_test/function/gcd/loop_check_0.mcfunction +0 -5
  316. package/dist/data/gcd_test/function/gcd/loop_exit_2.mcfunction +0 -3
  317. package/dist/data/gcd_test/function/gcd.mcfunction +0 -14
  318. package/dist/data/isqrttest/function/__load.mcfunction +0 -6
  319. package/dist/data/isqrttest/function/isqrt/loop_body_4.mcfunction +0 -12
  320. package/dist/data/isqrttest/function/isqrt/loop_check_3.mcfunction +0 -5
  321. package/dist/data/isqrttest/function/isqrt/loop_exit_5.mcfunction +0 -3
  322. package/dist/data/isqrttest/function/isqrt/merge_2.mcfunction +0 -4
  323. package/dist/data/isqrttest/function/isqrt/merge_8.mcfunction +0 -6
  324. package/dist/data/isqrttest/function/isqrt/then_0.mcfunction +0 -3
  325. package/dist/data/isqrttest/function/isqrt/then_6.mcfunction +0 -3
  326. package/dist/data/isqrttest/function/isqrt.mcfunction +0 -7
  327. package/dist/data/isqrttest/function/test.mcfunction +0 -6
  328. package/dist/data/mathtest/function/__load.mcfunction +0 -3
  329. package/dist/data/mathtest/function/abs/merge_2.mcfunction +0 -3
  330. package/dist/data/mathtest/function/abs/then_0.mcfunction +0 -5
  331. package/dist/data/mathtest/function/abs.mcfunction +0 -6
  332. package/dist/data/mathtest/function/test.mcfunction +0 -5
  333. package/dist/data/minecraft/tags/function/load.json +0 -5
  334. package/dist/data/minecraft/tags/function/tick.json +0 -5
  335. package/dist/data/mypack/function/__load.mcfunction +0 -13
  336. package/dist/data/mypack/function/_atan_init.mcfunction +0 -2
  337. package/dist/data/mypack/function/abs/merge_2.mcfunction +0 -3
  338. package/dist/data/mypack/function/abs/then_0.mcfunction +0 -5
  339. package/dist/data/mypack/function/abs.mcfunction +0 -6
  340. package/dist/data/mypack/function/atan2_fixed/__sgi_1.mcfunction +0 -2
  341. package/dist/data/mypack/function/atan2_fixed/else_34.mcfunction +0 -3
  342. package/dist/data/mypack/function/atan2_fixed/loop_body_31.mcfunction +0 -19
  343. package/dist/data/mypack/function/atan2_fixed/loop_check_30.mcfunction +0 -5
  344. package/dist/data/mypack/function/atan2_fixed/loop_exit_32.mcfunction +0 -6
  345. package/dist/data/mypack/function/atan2_fixed/merge_11.mcfunction +0 -6
  346. package/dist/data/mypack/function/atan2_fixed/merge_14.mcfunction +0 -3
  347. package/dist/data/mypack/function/atan2_fixed/merge_17.mcfunction +0 -6
  348. package/dist/data/mypack/function/atan2_fixed/merge_2.mcfunction +0 -5
  349. package/dist/data/mypack/function/atan2_fixed/merge_20.mcfunction +0 -5
  350. package/dist/data/mypack/function/atan2_fixed/merge_23.mcfunction +0 -5
  351. package/dist/data/mypack/function/atan2_fixed/merge_26.mcfunction +0 -6
  352. package/dist/data/mypack/function/atan2_fixed/merge_29.mcfunction +0 -4
  353. package/dist/data/mypack/function/atan2_fixed/merge_38.mcfunction +0 -5
  354. package/dist/data/mypack/function/atan2_fixed/merge_41.mcfunction +0 -5
  355. package/dist/data/mypack/function/atan2_fixed/merge_44.mcfunction +0 -5
  356. package/dist/data/mypack/function/atan2_fixed/merge_47.mcfunction +0 -5
  357. package/dist/data/mypack/function/atan2_fixed/merge_5.mcfunction +0 -5
  358. package/dist/data/mypack/function/atan2_fixed/merge_8.mcfunction +0 -3
  359. package/dist/data/mypack/function/atan2_fixed/then_0.mcfunction +0 -5
  360. package/dist/data/mypack/function/atan2_fixed/then_12.mcfunction +0 -3
  361. package/dist/data/mypack/function/atan2_fixed/then_15.mcfunction +0 -5
  362. package/dist/data/mypack/function/atan2_fixed/then_18.mcfunction +0 -5
  363. package/dist/data/mypack/function/atan2_fixed/then_21.mcfunction +0 -3
  364. package/dist/data/mypack/function/atan2_fixed/then_24.mcfunction +0 -3
  365. package/dist/data/mypack/function/atan2_fixed/then_27.mcfunction +0 -6
  366. package/dist/data/mypack/function/atan2_fixed/then_3.mcfunction +0 -3
  367. package/dist/data/mypack/function/atan2_fixed/then_33.mcfunction +0 -5
  368. package/dist/data/mypack/function/atan2_fixed/then_36.mcfunction +0 -5
  369. package/dist/data/mypack/function/atan2_fixed/then_39.mcfunction +0 -5
  370. package/dist/data/mypack/function/atan2_fixed/then_42.mcfunction +0 -3
  371. package/dist/data/mypack/function/atan2_fixed/then_45.mcfunction +0 -5
  372. package/dist/data/mypack/function/atan2_fixed/then_6.mcfunction +0 -3
  373. package/dist/data/mypack/function/atan2_fixed/then_9.mcfunction +0 -5
  374. package/dist/data/mypack/function/atan2_fixed.mcfunction +0 -7
  375. package/dist/data/mypack/function/my_game.mcfunction +0 -10
  376. package/dist/data/quiz/function/__load.mcfunction +0 -16
  377. package/dist/data/quiz/function/__tick.mcfunction +0 -6
  378. package/dist/data/quiz/function/__trigger_quiz_a_dispatch.mcfunction +0 -4
  379. package/dist/data/quiz/function/__trigger_quiz_b_dispatch.mcfunction +0 -4
  380. package/dist/data/quiz/function/__trigger_quiz_c_dispatch.mcfunction +0 -4
  381. package/dist/data/quiz/function/__trigger_quiz_start_dispatch.mcfunction +0 -4
  382. package/dist/data/quiz/function/answer_a.mcfunction +0 -4
  383. package/dist/data/quiz/function/answer_b.mcfunction +0 -4
  384. package/dist/data/quiz/function/answer_c.mcfunction +0 -4
  385. package/dist/data/quiz/function/ask_question/else_1.mcfunction +0 -5
  386. package/dist/data/quiz/function/ask_question/else_4.mcfunction +0 -5
  387. package/dist/data/quiz/function/ask_question/else_7.mcfunction +0 -4
  388. package/dist/data/quiz/function/ask_question/merge_2.mcfunction +0 -1
  389. package/dist/data/quiz/function/ask_question/merge_5.mcfunction +0 -2
  390. package/dist/data/quiz/function/ask_question/merge_8.mcfunction +0 -2
  391. package/dist/data/quiz/function/ask_question/then_0.mcfunction +0 -4
  392. package/dist/data/quiz/function/ask_question/then_3.mcfunction +0 -4
  393. package/dist/data/quiz/function/ask_question/then_6.mcfunction +0 -4
  394. package/dist/data/quiz/function/ask_question.mcfunction +0 -7
  395. package/dist/data/quiz/function/finish_quiz.mcfunction +0 -6
  396. package/dist/data/quiz/function/handle_answer/else_1.mcfunction +0 -5
  397. package/dist/data/quiz/function/handle_answer/else_10.mcfunction +0 -3
  398. package/dist/data/quiz/function/handle_answer/else_16.mcfunction +0 -3
  399. package/dist/data/quiz/function/handle_answer/else_4.mcfunction +0 -3
  400. package/dist/data/quiz/function/handle_answer/else_7.mcfunction +0 -5
  401. package/dist/data/quiz/function/handle_answer/merge_11.mcfunction +0 -2
  402. package/dist/data/quiz/function/handle_answer/merge_14.mcfunction +0 -2
  403. package/dist/data/quiz/function/handle_answer/merge_17.mcfunction +0 -2
  404. package/dist/data/quiz/function/handle_answer/merge_2.mcfunction +0 -8
  405. package/dist/data/quiz/function/handle_answer/merge_5.mcfunction +0 -2
  406. package/dist/data/quiz/function/handle_answer/merge_8.mcfunction +0 -2
  407. package/dist/data/quiz/function/handle_answer/then_0.mcfunction +0 -5
  408. package/dist/data/quiz/function/handle_answer/then_12.mcfunction +0 -5
  409. package/dist/data/quiz/function/handle_answer/then_15.mcfunction +0 -6
  410. package/dist/data/quiz/function/handle_answer/then_3.mcfunction +0 -6
  411. package/dist/data/quiz/function/handle_answer/then_6.mcfunction +0 -5
  412. package/dist/data/quiz/function/handle_answer/then_9.mcfunction +0 -6
  413. package/dist/data/quiz/function/handle_answer.mcfunction +0 -11
  414. package/dist/data/quiz/function/start_quiz.mcfunction +0 -5
  415. package/dist/data/reqtest/function/__load.mcfunction +0 -4
  416. package/dist/data/reqtest/function/_table_init.mcfunction +0 -2
  417. package/dist/data/reqtest/function/no_trig.mcfunction +0 -3
  418. package/dist/data/reqtest/function/use_table.mcfunction +0 -4
  419. package/dist/data/reqtest2/function/__load.mcfunction +0 -3
  420. package/dist/data/reqtest2/function/no_trig.mcfunction +0 -3
  421. package/dist/data/runtime/function/__load.mcfunction +0 -5
  422. package/dist/data/runtime/function/__tick.mcfunction +0 -2
  423. package/dist/data/runtime/function/counter_tick/then_0.mcfunction +0 -3
  424. package/dist/data/runtime/function/counter_tick.mcfunction +0 -13
  425. package/dist/data/shop/function/__load.mcfunction +0 -7
  426. package/dist/data/shop/function/__tick.mcfunction +0 -3
  427. package/dist/data/shop/function/__trigger_shop_buy_dispatch.mcfunction +0 -4
  428. package/dist/data/shop/function/complete_purchase/else_1.mcfunction +0 -5
  429. package/dist/data/shop/function/complete_purchase/else_4.mcfunction +0 -5
  430. package/dist/data/shop/function/complete_purchase/else_7.mcfunction +0 -3
  431. package/dist/data/shop/function/complete_purchase/merge_2.mcfunction +0 -2
  432. package/dist/data/shop/function/complete_purchase/merge_5.mcfunction +0 -2
  433. package/dist/data/shop/function/complete_purchase/merge_8.mcfunction +0 -2
  434. package/dist/data/shop/function/complete_purchase/then_0.mcfunction +0 -4
  435. package/dist/data/shop/function/complete_purchase/then_3.mcfunction +0 -4
  436. package/dist/data/shop/function/complete_purchase/then_6.mcfunction +0 -4
  437. package/dist/data/shop/function/complete_purchase.mcfunction +0 -7
  438. package/dist/data/shop/function/handle_shop_trigger.mcfunction +0 -3
  439. package/dist/data/swap_test/function/__load.mcfunction +0 -3
  440. package/dist/data/swap_test/function/gcd_old/loop_body_1.mcfunction +0 -7
  441. package/dist/data/swap_test/function/gcd_old/loop_check_0.mcfunction +0 -5
  442. package/dist/data/swap_test/function/gcd_old/loop_exit_2.mcfunction +0 -3
  443. package/dist/data/swap_test/function/gcd_old.mcfunction +0 -8
  444. package/dist/data/turret/function/__load.mcfunction +0 -5
  445. package/dist/data/turret/function/__tick.mcfunction +0 -4
  446. package/dist/data/turret/function/__trigger_deploy_turret_dispatch.mcfunction +0 -4
  447. package/dist/data/turret/function/deploy_turret.mcfunction +0 -8
  448. package/dist/data/turret/function/turret_tick/at_1.mcfunction +0 -2
  449. package/dist/data/turret/function/turret_tick/foreach_0.mcfunction +0 -2
  450. package/dist/data/turret/function/turret_tick/foreach_2.mcfunction +0 -2
  451. package/dist/data/turret/function/turret_tick/tick_body.mcfunction +0 -3
  452. package/dist/data/turret/function/turret_tick/tick_skip.mcfunction +0 -1
  453. package/dist/data/turret/function/turret_tick.mcfunction +0 -5
  454. package/dist/diagnostics/index.d.ts +0 -44
  455. package/dist/diagnostics/index.js +0 -140
  456. package/dist/events/types.d.ts +0 -35
  457. package/dist/events/types.js +0 -59
  458. package/dist/formatter/index.d.ts +0 -1
  459. package/dist/formatter/index.js +0 -26
  460. package/dist/gcd2.map.json +0 -15
  461. package/dist/gcd3.map.json +0 -17
  462. package/dist/gcd_test.map.json +0 -15
  463. package/dist/index.d.ts +0 -62
  464. package/dist/ir/builder.d.ts +0 -33
  465. package/dist/ir/builder.js +0 -99
  466. package/dist/ir/types.d.ts +0 -132
  467. package/dist/ir/types.js +0 -15
  468. package/dist/isqrttest.map.json +0 -15
  469. package/dist/lexer/index.d.ts +0 -37
  470. package/dist/lexer/index.js +0 -569
  471. package/dist/lowering/index.d.ts +0 -188
  472. package/dist/lowering/index.js +0 -3405
  473. package/dist/mathtest.map.json +0 -6
  474. package/dist/mc-test/client.d.ts +0 -128
  475. package/dist/mc-test/client.js +0 -174
  476. package/dist/mc-test/runner.d.ts +0 -28
  477. package/dist/mc-test/runner.js +0 -150
  478. package/dist/mc-test/setup.d.ts +0 -11
  479. package/dist/mc-test/setup.js +0 -98
  480. package/dist/mc-validator/index.d.ts +0 -17
  481. package/dist/mc-validator/index.js +0 -322
  482. package/dist/mypack.map.json +0 -27
  483. package/dist/nbt/index.d.ts +0 -86
  484. package/dist/nbt/index.js +0 -250
  485. package/dist/optimizer/commands.d.ts +0 -38
  486. package/dist/optimizer/commands.js +0 -451
  487. package/dist/optimizer/dce.d.ts +0 -34
  488. package/dist/optimizer/dce.js +0 -639
  489. package/dist/optimizer/passes.d.ts +0 -34
  490. package/dist/optimizer/passes.js +0 -243
  491. package/dist/optimizer/structure.d.ts +0 -9
  492. package/dist/optimizer/structure.js +0 -356
  493. package/dist/pack.mcmeta +0 -6
  494. package/dist/parser/index.d.ts +0 -93
  495. package/dist/parser/index.js +0 -1687
  496. package/dist/repl.d.ts +0 -16
  497. package/dist/repl.js +0 -165
  498. package/dist/reqtest.map.json +0 -4
  499. package/dist/reqtest2.map.json +0 -4
  500. package/dist/runtime/index.d.ts +0 -107
  501. package/dist/runtime/index.js +0 -1409
  502. package/dist/runtime.map.json +0 -7
  503. package/dist/src/__tests__/codegen.test.d.ts +0 -1
  504. package/dist/src/__tests__/codegen.test.js +0 -152
  505. package/dist/src/__tests__/e2e.test.d.ts +0 -6
  506. package/dist/src/__tests__/e2e.test.js +0 -1789
  507. package/dist/src/__tests__/entity-types.test.d.ts +0 -1
  508. package/dist/src/__tests__/entity-types.test.js +0 -203
  509. package/dist/src/__tests__/lowering.test.d.ts +0 -1
  510. package/dist/src/__tests__/lowering.test.js +0 -1015
  511. package/dist/src/__tests__/macro.test.d.ts +0 -8
  512. package/dist/src/__tests__/macro.test.js +0 -306
  513. package/dist/src/__tests__/nbt.test.d.ts +0 -1
  514. package/dist/src/__tests__/nbt.test.js +0 -82
  515. package/dist/src/__tests__/optimizer-advanced.test.d.ts +0 -1
  516. package/dist/src/__tests__/optimizer-advanced.test.js +0 -124
  517. package/dist/src/__tests__/optimizer.test.d.ts +0 -1
  518. package/dist/src/__tests__/optimizer.test.js +0 -149
  519. package/dist/src/__tests__/runtime.test.d.ts +0 -1
  520. package/dist/src/__tests__/runtime.test.js +0 -289
  521. package/dist/src/__tests__/stdlib-advanced.test.d.ts +0 -4
  522. package/dist/src/__tests__/stdlib-advanced.test.js +0 -374
  523. package/dist/src/__tests__/stdlib-bigint.test.d.ts +0 -7
  524. package/dist/src/__tests__/stdlib-bigint.test.js +0 -426
  525. package/dist/src/__tests__/stdlib-math.test.d.ts +0 -7
  526. package/dist/src/__tests__/stdlib-math.test.js +0 -351
  527. package/dist/src/__tests__/stdlib-vec.test.d.ts +0 -4
  528. package/dist/src/__tests__/stdlib-vec.test.js +0 -263
  529. package/dist/src/__tests__/structure-optimizer.test.d.ts +0 -1
  530. package/dist/src/__tests__/structure-optimizer.test.js +0 -33
  531. package/dist/src/__tests__/var-allocator.test.d.ts +0 -1
  532. package/dist/src/__tests__/var-allocator.test.js +0 -69
  533. package/dist/src/codegen/cmdblock/index.d.ts +0 -26
  534. package/dist/src/codegen/cmdblock/index.js +0 -45
  535. package/dist/src/codegen/mcfunction/index.d.ts +0 -40
  536. package/dist/src/codegen/mcfunction/index.js +0 -606
  537. package/dist/src/codegen/structure/index.d.ts +0 -24
  538. package/dist/src/codegen/structure/index.js +0 -279
  539. package/dist/src/codegen/var-allocator.d.ts +0 -45
  540. package/dist/src/codegen/var-allocator.js +0 -104
  541. package/dist/src/ir/builder.d.ts +0 -33
  542. package/dist/src/ir/builder.js +0 -99
  543. package/dist/src/ir/types.d.ts +0 -132
  544. package/dist/src/ir/types.js +0 -15
  545. package/dist/src/lowering/index.d.ts +0 -188
  546. package/dist/src/lowering/index.js +0 -3405
  547. package/dist/src/optimizer/commands.d.ts +0 -38
  548. package/dist/src/optimizer/commands.js +0 -451
  549. package/dist/src/optimizer/passes.d.ts +0 -34
  550. package/dist/src/optimizer/passes.js +0 -243
  551. package/dist/src/optimizer/structure.d.ts +0 -9
  552. package/dist/src/optimizer/structure.js +0 -356
  553. package/dist/src2/__tests__/optimizer/dce.test.d.ts +0 -1
  554. package/dist/src2/emit/compile.d.ts +0 -19
  555. package/dist/src2/emit/compile.js +0 -80
  556. package/dist/src2/optimizer/dce.d.ts +0 -8
  557. package/dist/src2/optimizer/dce.js +0 -155
  558. package/dist/swap_test.map.json +0 -14
  559. package/dist/tsconfig.tsbuildinfo +0 -1
  560. package/dist/typechecker/index.d.ts +0 -61
  561. package/dist/typechecker/index.js +0 -1034
  562. package/dist/types/entity-hierarchy.d.ts +0 -29
  563. package/dist/types/entity-hierarchy.js +0 -107
  564. package/examples/spiral.mcrs +0 -43
  565. package/src/examples/arena.mcrs +0 -44
  566. package/src/examples/counter.mcrs +0 -12
  567. package/src/examples/new_features_demo.mcrs +0 -193
  568. package/src/examples/rpg.mcrs +0 -13
  569. package/src/examples/stdlib_demo.mcrs +0 -181
  570. package/src2/emit/compile.ts +0 -99
  571. /package/dist/{__tests__/cli.test.d.ts → src/__tests__/budget.test.d.ts} +0 -0
  572. /package/dist/{src2 → src}/__tests__/e2e/basic.test.d.ts +0 -0
  573. /package/dist/{src2 → src}/__tests__/e2e/basic.test.js +0 -0
  574. /package/dist/{src2 → src}/__tests__/e2e/macros.test.d.ts +0 -0
  575. /package/dist/{src2 → src}/__tests__/e2e/migrate.test.d.ts +0 -0
  576. /package/dist/{src2 → src}/__tests__/hir/desugar.test.d.ts +0 -0
  577. /package/dist/{src2 → src}/__tests__/lir/lower.test.d.ts +0 -0
  578. /package/dist/{src2 → src}/__tests__/lir/lower.test.js +0 -0
  579. /package/dist/{src2 → src}/__tests__/lir/types.test.d.ts +0 -0
  580. /package/dist/{src2 → src}/__tests__/lir/types.test.js +0 -0
  581. /package/dist/{src2 → src}/__tests__/lir/verify.test.d.ts +0 -0
  582. /package/dist/{src2 → src}/__tests__/lir/verify.test.js +0 -0
  583. /package/dist/{src2 → src}/__tests__/mir/arithmetic.test.d.ts +0 -0
  584. /package/dist/{src2 → src}/__tests__/mir/control-flow.test.d.ts +0 -0
  585. /package/dist/{src2 → src}/__tests__/mir/verify.test.d.ts +0 -0
  586. /package/dist/{src2 → src}/__tests__/mir/verify.test.js +0 -0
  587. /package/dist/{src2 → src}/__tests__/optimizer/block_merge.test.d.ts +0 -0
  588. /package/dist/{src2 → src}/__tests__/optimizer/block_merge.test.js +0 -0
  589. /package/dist/{src2 → src}/__tests__/optimizer/branch_simplify.test.d.ts +0 -0
  590. /package/dist/{src2 → src}/__tests__/optimizer/branch_simplify.test.js +0 -0
  591. /package/dist/{src2 → src}/__tests__/optimizer/constant_fold.test.d.ts +0 -0
  592. /package/dist/{src2 → src}/__tests__/optimizer/constant_fold.test.js +0 -0
  593. /package/dist/{src2 → src}/__tests__/optimizer/copy_prop.test.d.ts +0 -0
  594. /package/dist/{src2 → src}/__tests__/optimizer/copy_prop.test.js +0 -0
  595. /package/dist/{__tests__ → src/__tests__/optimizer}/dce.test.d.ts +0 -0
  596. /package/dist/{src2 → src}/__tests__/optimizer/dce.test.js +0 -0
  597. /package/dist/{__tests__/codegen.test.d.ts → src/__tests__/optimizer/interprocedural.test.d.ts} +0 -0
  598. /package/dist/{__tests__/entity-types.test.d.ts → src/__tests__/optimizer/lir/const_imm.test.d.ts} +0 -0
  599. /package/dist/{__tests__/formatter.test.d.ts → src/__tests__/optimizer/lir/dead_slot.test.d.ts} +0 -0
  600. /package/dist/{__tests__/lexer.test.d.ts → src/__tests__/optimizer/lir/peephole.test.d.ts} +0 -0
  601. /package/dist/{src2/__tests__/optimizer → src/__tests__/optimizer/lir}/pipeline.test.d.ts +0 -0
  602. /package/dist/{__tests__/lowering.test.d.ts → src/__tests__/optimizer/nbt-batch.test.d.ts} +0 -0
  603. /package/dist/{__tests__/mc-syntax.test.d.ts → src/__tests__/optimizer/pipeline.test.d.ts} +0 -0
  604. /package/dist/{src2 → src}/__tests__/optimizer/pipeline.test.js +0 -0
  605. /package/dist/{__tests__/nbt.test.d.ts → src/__tests__/optimizer/selector-cache.test.d.ts} +0 -0
  606. /package/dist/{__tests__/optimizer-advanced.test.d.ts → src/__tests__/optimizer/unroll.test.d.ts} +0 -0
  607. /package/dist/{__tests__/optimizer.test.d.ts → src/__tests__/stdlib-include.test.d.ts} +0 -0
  608. /package/dist/{src2 → src}/hir/types.js +0 -0
  609. /package/dist/{src2 → src}/lir/lower.d.ts +0 -0
  610. /package/dist/{src2 → src}/lir/types.js +0 -0
  611. /package/dist/{src2 → src}/lir/verify.d.ts +0 -0
  612. /package/dist/{src2 → src}/lir/verify.js +0 -0
  613. /package/dist/{src2 → src}/mir/macro.d.ts +0 -0
  614. /package/dist/{src2 → src}/mir/macro.js +0 -0
  615. /package/dist/{src2 → src}/mir/types.js +0 -0
  616. /package/dist/{src2 → src}/mir/verify.d.ts +0 -0
  617. /package/dist/{src2 → src}/mir/verify.js +0 -0
  618. /package/dist/{src2 → src}/optimizer/block_merge.d.ts +0 -0
  619. /package/dist/{src2 → src}/optimizer/block_merge.js +0 -0
  620. /package/dist/{src2 → src}/optimizer/branch_simplify.d.ts +0 -0
  621. /package/dist/{src2 → src}/optimizer/branch_simplify.js +0 -0
  622. /package/dist/{src2 → src}/optimizer/constant_fold.d.ts +0 -0
  623. /package/dist/{src2 → src}/optimizer/constant_fold.js +0 -0
  624. /package/dist/{src2 → src}/optimizer/copy_prop.d.ts +0 -0
  625. /package/dist/{src2 → src}/optimizer/copy_prop.js +0 -0
  626. /package/{src2 → src}/__tests__/e2e/basic.test.ts +0 -0
  627. /package/{src2 → src}/__tests__/lir/lower.test.ts +0 -0
  628. /package/{src2 → src}/__tests__/lir/types.test.ts +0 -0
  629. /package/{src2 → src}/__tests__/lir/verify.test.ts +0 -0
  630. /package/{src2 → src}/__tests__/mir/verify.test.ts +0 -0
  631. /package/{src2 → src}/__tests__/optimizer/block_merge.test.ts +0 -0
  632. /package/{src2 → src}/__tests__/optimizer/branch_simplify.test.ts +0 -0
  633. /package/{src2 → src}/__tests__/optimizer/constant_fold.test.ts +0 -0
  634. /package/{src2 → src}/__tests__/optimizer/copy_prop.test.ts +0 -0
  635. /package/{src2 → src}/__tests__/optimizer/dce.test.ts +0 -0
  636. /package/{src2 → src}/__tests__/optimizer/pipeline.test.ts +0 -0
  637. /package/{src2 → src}/lir/verify.ts +0 -0
  638. /package/{src2 → src}/mir/macro.ts +0 -0
  639. /package/{src2 → src}/mir/verify.ts +0 -0
  640. /package/{src2 → src}/optimizer/block_merge.ts +0 -0
  641. /package/{src2 → src}/optimizer/branch_simplify.ts +0 -0
  642. /package/{src2 → src}/optimizer/constant_fold.ts +0 -0
  643. /package/{src2 → src}/optimizer/copy_prop.ts +0 -0
@@ -1,1034 +0,0 @@
1
- "use strict";
2
- /**
3
- * RedScript Type Checker
4
- *
5
- * Performs basic type checking between Parser and Lowering phases.
6
- * Collects errors but doesn't block compilation (warn mode).
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.TypeChecker = void 0;
10
- const diagnostics_1 = require("../diagnostics");
11
- const types_1 = require("../events/types");
12
- // Entity type hierarchy for subtype checking
13
- const ENTITY_HIERARCHY = {
14
- 'entity': null,
15
- 'Player': 'entity',
16
- 'Mob': 'entity',
17
- 'HostileMob': 'Mob',
18
- 'PassiveMob': 'Mob',
19
- 'Zombie': 'HostileMob',
20
- 'Skeleton': 'HostileMob',
21
- 'Creeper': 'HostileMob',
22
- 'Spider': 'HostileMob',
23
- 'Enderman': 'HostileMob',
24
- 'Blaze': 'HostileMob',
25
- 'Witch': 'HostileMob',
26
- 'Slime': 'HostileMob',
27
- 'ZombieVillager': 'HostileMob',
28
- 'Husk': 'HostileMob',
29
- 'Drowned': 'HostileMob',
30
- 'Stray': 'HostileMob',
31
- 'WitherSkeleton': 'HostileMob',
32
- 'CaveSpider': 'HostileMob',
33
- 'Pig': 'PassiveMob',
34
- 'Cow': 'PassiveMob',
35
- 'Sheep': 'PassiveMob',
36
- 'Chicken': 'PassiveMob',
37
- 'Villager': 'PassiveMob',
38
- 'WanderingTrader': 'PassiveMob',
39
- 'ArmorStand': 'entity',
40
- 'Item': 'entity',
41
- 'Arrow': 'entity',
42
- };
43
- // Map Minecraft type names to entity types
44
- const MC_TYPE_TO_ENTITY = {
45
- 'zombie': 'Zombie',
46
- 'minecraft:zombie': 'Zombie',
47
- 'skeleton': 'Skeleton',
48
- 'minecraft:skeleton': 'Skeleton',
49
- 'creeper': 'Creeper',
50
- 'minecraft:creeper': 'Creeper',
51
- 'spider': 'Spider',
52
- 'minecraft:spider': 'Spider',
53
- 'enderman': 'Enderman',
54
- 'minecraft:enderman': 'Enderman',
55
- 'blaze': 'Blaze',
56
- 'minecraft:blaze': 'Blaze',
57
- 'witch': 'Witch',
58
- 'minecraft:witch': 'Witch',
59
- 'slime': 'Slime',
60
- 'minecraft:slime': 'Slime',
61
- 'zombie_villager': 'ZombieVillager',
62
- 'minecraft:zombie_villager': 'ZombieVillager',
63
- 'husk': 'Husk',
64
- 'minecraft:husk': 'Husk',
65
- 'drowned': 'Drowned',
66
- 'minecraft:drowned': 'Drowned',
67
- 'stray': 'Stray',
68
- 'minecraft:stray': 'Stray',
69
- 'wither_skeleton': 'WitherSkeleton',
70
- 'minecraft:wither_skeleton': 'WitherSkeleton',
71
- 'cave_spider': 'CaveSpider',
72
- 'minecraft:cave_spider': 'CaveSpider',
73
- 'pig': 'Pig',
74
- 'minecraft:pig': 'Pig',
75
- 'cow': 'Cow',
76
- 'minecraft:cow': 'Cow',
77
- 'sheep': 'Sheep',
78
- 'minecraft:sheep': 'Sheep',
79
- 'chicken': 'Chicken',
80
- 'minecraft:chicken': 'Chicken',
81
- 'villager': 'Villager',
82
- 'minecraft:villager': 'Villager',
83
- 'wandering_trader': 'WanderingTrader',
84
- 'minecraft:wandering_trader': 'WanderingTrader',
85
- 'armor_stand': 'ArmorStand',
86
- 'minecraft:armor_stand': 'ArmorStand',
87
- 'item': 'Item',
88
- 'minecraft:item': 'Item',
89
- 'arrow': 'Arrow',
90
- 'minecraft:arrow': 'Arrow',
91
- };
92
- const VOID_TYPE = { kind: 'named', name: 'void' };
93
- const INT_TYPE = { kind: 'named', name: 'int' };
94
- const STRING_TYPE = { kind: 'named', name: 'string' };
95
- const FORMAT_STRING_TYPE = { kind: 'named', name: 'format_string' };
96
- const BUILTIN_SIGNATURES = {
97
- setTimeout: {
98
- params: [INT_TYPE, { kind: 'function_type', params: [], return: VOID_TYPE }],
99
- return: VOID_TYPE,
100
- },
101
- setInterval: {
102
- params: [INT_TYPE, { kind: 'function_type', params: [], return: VOID_TYPE }],
103
- return: INT_TYPE,
104
- },
105
- clearInterval: {
106
- params: [INT_TYPE],
107
- return: VOID_TYPE,
108
- },
109
- };
110
- // ---------------------------------------------------------------------------
111
- // Type Checker
112
- // ---------------------------------------------------------------------------
113
- class TypeChecker {
114
- constructor(source, filePath) {
115
- this.functions = new Map();
116
- this.implMethods = new Map();
117
- this.structs = new Map();
118
- this.enums = new Map();
119
- this.consts = new Map();
120
- this.currentFn = null;
121
- this.currentReturnType = null;
122
- this.scope = new Map();
123
- // Stack for tracking @s type in different contexts
124
- this.selfTypeStack = ['entity'];
125
- this.richTextBuiltins = new Map([
126
- ['say', { messageIndex: 0 }],
127
- ['announce', { messageIndex: 0 }],
128
- ['tell', { messageIndex: 1 }],
129
- ['tellraw', { messageIndex: 1 }],
130
- ['title', { messageIndex: 1 }],
131
- ['actionbar', { messageIndex: 1 }],
132
- ['subtitle', { messageIndex: 1 }],
133
- ]);
134
- this.collector = new diagnostics_1.DiagnosticCollector(source, filePath);
135
- }
136
- getNodeLocation(node) {
137
- const span = node?.span;
138
- return {
139
- line: span?.line ?? 1,
140
- col: span?.col ?? 1,
141
- };
142
- }
143
- report(message, node) {
144
- const { line, col } = this.getNodeLocation(node);
145
- this.collector.error('TypeError', message, line, col);
146
- }
147
- /**
148
- * Type check a program. Returns collected errors.
149
- */
150
- check(program) {
151
- // First pass: collect function and struct declarations
152
- for (const fn of program.declarations) {
153
- this.functions.set(fn.name, fn);
154
- }
155
- for (const implBlock of program.implBlocks ?? []) {
156
- let methods = this.implMethods.get(implBlock.typeName);
157
- if (!methods) {
158
- methods = new Map();
159
- this.implMethods.set(implBlock.typeName, methods);
160
- }
161
- for (const method of implBlock.methods) {
162
- const selfIndex = method.params.findIndex(param => param.name === 'self');
163
- if (selfIndex > 0) {
164
- this.report(`Method '${method.name}' must declare 'self' as the first parameter`, method.params[selfIndex]);
165
- }
166
- if (selfIndex === 0) {
167
- const selfType = this.normalizeType(method.params[0].type);
168
- if (selfType.kind !== 'struct' || selfType.name !== implBlock.typeName) {
169
- this.report(`Method '${method.name}' has invalid 'self' type`, method.params[0]);
170
- }
171
- }
172
- methods.set(method.name, method);
173
- }
174
- }
175
- for (const struct of program.structs ?? []) {
176
- const fields = new Map();
177
- for (const field of struct.fields) {
178
- fields.set(field.name, field.type);
179
- }
180
- this.structs.set(struct.name, fields);
181
- }
182
- for (const enumDecl of program.enums ?? []) {
183
- const variants = new Map();
184
- for (const variant of enumDecl.variants) {
185
- variants.set(variant.name, variant.value ?? 0);
186
- }
187
- this.enums.set(enumDecl.name, variants);
188
- }
189
- for (const constDecl of program.consts ?? []) {
190
- const constType = this.normalizeType(constDecl.type);
191
- const actualType = this.inferType(constDecl.value);
192
- if (!this.typesMatch(constType, actualType)) {
193
- this.report(`Type mismatch: expected ${this.typeToString(constType)}, got ${this.typeToString(actualType)}`, constDecl.value);
194
- }
195
- this.consts.set(constDecl.name, constType);
196
- }
197
- // Second pass: type check function bodies
198
- for (const fn of program.declarations) {
199
- this.checkFunction(fn);
200
- }
201
- for (const implBlock of program.implBlocks ?? []) {
202
- for (const method of implBlock.methods) {
203
- this.checkFunction(method);
204
- }
205
- }
206
- return this.collector.getErrors();
207
- }
208
- checkFunction(fn) {
209
- this.currentFn = fn;
210
- this.currentReturnType = this.normalizeType(fn.returnType);
211
- this.scope = new Map();
212
- let seenDefault = false;
213
- this.checkFunctionDecorators(fn);
214
- for (const [name, type] of this.consts.entries()) {
215
- this.scope.set(name, { type, mutable: false });
216
- }
217
- // Add parameters to scope
218
- for (const param of fn.params) {
219
- this.scope.set(param.name, { type: this.normalizeType(param.type), mutable: true });
220
- if (param.default) {
221
- seenDefault = true;
222
- this.checkExpr(param.default);
223
- const defaultType = this.inferType(param.default);
224
- const paramType = this.normalizeType(param.type);
225
- if (!this.typesMatch(paramType, defaultType)) {
226
- this.report(`Default value for '${param.name}' must be ${this.typeToString(paramType)}, got ${this.typeToString(defaultType)}`, param.default);
227
- }
228
- }
229
- else if (seenDefault) {
230
- this.report(`Parameter '${param.name}' cannot follow a default parameter`, param);
231
- }
232
- }
233
- // Check body
234
- this.checkBlock(fn.body);
235
- this.currentFn = null;
236
- this.currentReturnType = null;
237
- }
238
- checkFunctionDecorators(fn) {
239
- const eventDecorators = fn.decorators.filter(decorator => decorator.name === 'on');
240
- if (eventDecorators.length === 0) {
241
- return;
242
- }
243
- if (eventDecorators.length > 1) {
244
- this.report(`Function '${fn.name}' cannot have multiple @on decorators`, fn);
245
- return;
246
- }
247
- const eventType = eventDecorators[0].args?.eventType;
248
- if (!eventType) {
249
- this.report(`Function '${fn.name}' is missing an event type in @on(...)`, fn);
250
- return;
251
- }
252
- if (!(0, types_1.isEventTypeName)(eventType)) {
253
- this.report(`Unknown event type '${eventType}'`, fn);
254
- return;
255
- }
256
- const expectedParams = (0, types_1.getEventParamSpecs)(eventType);
257
- if (fn.params.length !== expectedParams.length) {
258
- this.report(`Event handler '${fn.name}' for ${eventType} must declare ${expectedParams.length} parameter(s), got ${fn.params.length}`, fn);
259
- return;
260
- }
261
- for (let i = 0; i < expectedParams.length; i++) {
262
- const actual = this.normalizeType(fn.params[i].type);
263
- const expected = this.normalizeType(expectedParams[i].type);
264
- if (!this.typesMatch(expected, actual)) {
265
- this.report(`Event handler '${fn.name}' parameter ${i + 1} must be ${this.typeToString(expected)}, got ${this.typeToString(actual)}`, fn.params[i]);
266
- }
267
- }
268
- }
269
- checkBlock(stmts) {
270
- for (const stmt of stmts) {
271
- this.checkStmt(stmt);
272
- }
273
- }
274
- checkStmt(stmt) {
275
- switch (stmt.kind) {
276
- case 'let':
277
- this.checkLetStmt(stmt);
278
- break;
279
- case 'return':
280
- this.checkReturnStmt(stmt);
281
- break;
282
- case 'if':
283
- this.checkExpr(stmt.cond);
284
- this.checkIfBranches(stmt);
285
- break;
286
- case 'while':
287
- this.checkExpr(stmt.cond);
288
- this.checkBlock(stmt.body);
289
- break;
290
- case 'for':
291
- if (stmt.init)
292
- this.checkStmt(stmt.init);
293
- this.checkExpr(stmt.cond);
294
- this.checkExpr(stmt.step);
295
- this.checkBlock(stmt.body);
296
- break;
297
- case 'foreach':
298
- this.checkExpr(stmt.iterable);
299
- if (stmt.iterable.kind === 'selector') {
300
- // Infer entity type from selector (access .sel for the EntitySelector)
301
- const entityType = this.inferEntityTypeFromSelector(stmt.iterable.sel);
302
- this.scope.set(stmt.binding, {
303
- type: { kind: 'entity', entityType },
304
- mutable: false // Entity bindings are not reassignable
305
- });
306
- // Push self type context for @s inside the loop
307
- this.pushSelfType(entityType);
308
- this.checkBlock(stmt.body);
309
- this.popSelfType();
310
- }
311
- else {
312
- const iterableType = this.inferType(stmt.iterable);
313
- if (iterableType.kind === 'array') {
314
- this.scope.set(stmt.binding, { type: iterableType.elem, mutable: true });
315
- }
316
- else {
317
- this.scope.set(stmt.binding, { type: { kind: 'named', name: 'void' }, mutable: true });
318
- }
319
- this.checkBlock(stmt.body);
320
- }
321
- break;
322
- case 'match':
323
- this.checkExpr(stmt.expr);
324
- for (const arm of stmt.arms) {
325
- if (arm.pattern) {
326
- this.checkExpr(arm.pattern);
327
- if (!this.typesMatch(this.inferType(stmt.expr), this.inferType(arm.pattern))) {
328
- this.report('Match arm pattern type must match subject type', arm.pattern);
329
- }
330
- }
331
- this.checkBlock(arm.body);
332
- }
333
- break;
334
- case 'as_block': {
335
- // as block changes @s to the selector's entity type
336
- const entityType = this.inferEntityTypeFromSelector(stmt.selector);
337
- this.pushSelfType(entityType);
338
- this.checkBlock(stmt.body);
339
- this.popSelfType();
340
- break;
341
- }
342
- case 'at_block':
343
- // at block doesn't change @s type, only position
344
- this.checkBlock(stmt.body);
345
- break;
346
- case 'as_at': {
347
- // as @x at @y - @s becomes the as selector's type
348
- const entityType = this.inferEntityTypeFromSelector(stmt.as_sel);
349
- this.pushSelfType(entityType);
350
- this.checkBlock(stmt.body);
351
- this.popSelfType();
352
- break;
353
- }
354
- case 'execute':
355
- // execute with subcommands - check for 'as' subcommands
356
- for (const sub of stmt.subcommands) {
357
- if (sub.kind === 'as' && sub.selector) {
358
- const entityType = this.inferEntityTypeFromSelector(sub.selector);
359
- this.pushSelfType(entityType);
360
- }
361
- }
362
- this.checkBlock(stmt.body);
363
- // Pop for each 'as' subcommand
364
- for (const sub of stmt.subcommands) {
365
- if (sub.kind === 'as') {
366
- this.popSelfType();
367
- }
368
- }
369
- break;
370
- case 'expr':
371
- this.checkExpr(stmt.expr);
372
- break;
373
- case 'raw':
374
- // Raw commands are not type checked
375
- break;
376
- }
377
- }
378
- checkLetStmt(stmt) {
379
- // Check initializer
380
- const expectedType = stmt.type ? this.normalizeType(stmt.type) : undefined;
381
- this.checkExpr(stmt.init, expectedType);
382
- // Add variable to scope
383
- const type = expectedType ?? this.inferType(stmt.init);
384
- this.scope.set(stmt.name, { type, mutable: true });
385
- const actualType = this.inferType(stmt.init, expectedType);
386
- if (expectedType &&
387
- stmt.init.kind !== 'struct_lit' &&
388
- stmt.init.kind !== 'array_lit' &&
389
- !(actualType.kind === 'named' && actualType.name === 'void') &&
390
- !this.typesMatch(expectedType, actualType)) {
391
- this.report(`Type mismatch: expected ${this.typeToString(expectedType)}, got ${this.typeToString(actualType)}`, stmt);
392
- }
393
- }
394
- checkReturnStmt(stmt) {
395
- if (!this.currentReturnType)
396
- return;
397
- const expectedType = this.currentReturnType;
398
- if (stmt.value) {
399
- const actualType = this.inferType(stmt.value, expectedType);
400
- this.checkExpr(stmt.value, expectedType);
401
- if (!this.typesMatch(expectedType, actualType)) {
402
- this.report(`Return type mismatch: expected ${this.typeToString(expectedType)}, got ${this.typeToString(actualType)}`, stmt);
403
- }
404
- }
405
- else {
406
- // No return value
407
- if (expectedType.kind !== 'named' || expectedType.name !== 'void') {
408
- this.report(`Missing return value: expected ${this.typeToString(expectedType)}`, stmt);
409
- }
410
- }
411
- }
412
- checkExpr(expr, expectedType) {
413
- switch (expr.kind) {
414
- case 'ident':
415
- if (!this.scope.has(expr.name)) {
416
- this.report(`Variable '${expr.name}' used before declaration`, expr);
417
- }
418
- break;
419
- case 'call':
420
- this.checkCallExpr(expr);
421
- break;
422
- case 'invoke':
423
- this.checkInvokeExpr(expr);
424
- break;
425
- case 'member':
426
- this.checkMemberExpr(expr);
427
- break;
428
- case 'static_call':
429
- this.checkStaticCallExpr(expr);
430
- break;
431
- case 'binary':
432
- this.checkExpr(expr.left);
433
- this.checkExpr(expr.right);
434
- break;
435
- case 'is_check': {
436
- this.checkExpr(expr.expr);
437
- const checkedType = this.inferType(expr.expr);
438
- if (checkedType.kind !== 'entity') {
439
- this.report(`'is' checks require an entity expression, got ${this.typeToString(checkedType)}`, expr.expr);
440
- }
441
- break;
442
- }
443
- case 'unary':
444
- this.checkExpr(expr.operand);
445
- break;
446
- case 'assign':
447
- if (!this.scope.has(expr.target)) {
448
- this.report(`Variable '${expr.target}' used before declaration`, expr);
449
- }
450
- else if (!this.scope.get(expr.target)?.mutable) {
451
- this.report(`Cannot assign to const '${expr.target}'`, expr);
452
- }
453
- this.checkExpr(expr.value, this.scope.get(expr.target)?.type);
454
- break;
455
- case 'member_assign':
456
- this.checkExpr(expr.obj);
457
- this.checkExpr(expr.value);
458
- break;
459
- case 'index':
460
- this.checkExpr(expr.obj);
461
- this.checkExpr(expr.index);
462
- const indexType = this.inferType(expr.index);
463
- if (indexType.kind !== 'named' || indexType.name !== 'int') {
464
- this.report('Array index must be int', expr.index);
465
- }
466
- break;
467
- case 'struct_lit':
468
- for (const field of expr.fields) {
469
- this.checkExpr(field.value);
470
- }
471
- break;
472
- case 'str_interp':
473
- for (const part of expr.parts) {
474
- if (typeof part !== 'string') {
475
- this.checkExpr(part);
476
- }
477
- }
478
- break;
479
- case 'f_string':
480
- for (const part of expr.parts) {
481
- if (part.kind !== 'expr') {
482
- continue;
483
- }
484
- this.checkExpr(part.expr);
485
- const partType = this.inferType(part.expr);
486
- if (!(partType.kind === 'named' && (partType.name === 'int' || partType.name === 'string' || partType.name === 'format_string'))) {
487
- this.report(`f-string placeholder must be int or string, got ${this.typeToString(partType)}`, part.expr);
488
- }
489
- }
490
- break;
491
- case 'array_lit':
492
- for (const elem of expr.elements) {
493
- this.checkExpr(elem);
494
- }
495
- break;
496
- case 'lambda':
497
- this.checkLambdaExpr(expr, expectedType);
498
- break;
499
- case 'blockpos':
500
- break;
501
- // Literals don't need checking
502
- case 'int_lit':
503
- case 'float_lit':
504
- case 'bool_lit':
505
- case 'str_lit':
506
- case 'mc_name':
507
- case 'range_lit':
508
- case 'selector':
509
- case 'byte_lit':
510
- case 'short_lit':
511
- case 'long_lit':
512
- case 'double_lit':
513
- break;
514
- }
515
- }
516
- checkCallExpr(expr) {
517
- if (expr.fn === 'tp' || expr.fn === 'tp_to') {
518
- this.checkTpCall(expr);
519
- }
520
- const richTextBuiltin = this.richTextBuiltins.get(expr.fn);
521
- if (richTextBuiltin) {
522
- this.checkRichTextBuiltinCall(expr, richTextBuiltin.messageIndex);
523
- return;
524
- }
525
- const builtin = BUILTIN_SIGNATURES[expr.fn];
526
- if (builtin) {
527
- this.checkFunctionCallArgs(expr.args, builtin.params, expr.fn, expr);
528
- return;
529
- }
530
- // Check if function exists and arg count matches
531
- const fn = this.functions.get(expr.fn);
532
- if (fn) {
533
- const requiredParams = fn.params.filter(param => !param.default).length;
534
- if (expr.args.length < requiredParams || expr.args.length > fn.params.length) {
535
- const expectedRange = requiredParams === fn.params.length
536
- ? `${fn.params.length}`
537
- : `${requiredParams}-${fn.params.length}`;
538
- this.report(`Function '${expr.fn}' expects ${expectedRange} arguments, got ${expr.args.length}`, expr);
539
- }
540
- for (let i = 0; i < expr.args.length; i++) {
541
- const paramType = fn.params[i] ? this.normalizeType(fn.params[i].type) : undefined;
542
- if (paramType) {
543
- this.checkExpr(expr.args[i], paramType);
544
- }
545
- const argType = this.inferType(expr.args[i], paramType);
546
- if (paramType && !this.typesMatch(paramType, argType)) {
547
- this.report(`Argument ${i + 1} of '${expr.fn}' expects ${this.typeToString(paramType)}, got ${this.typeToString(argType)}`, expr.args[i]);
548
- }
549
- }
550
- return;
551
- }
552
- const varType = this.scope.get(expr.fn)?.type;
553
- if (varType?.kind === 'function_type') {
554
- this.checkFunctionCallArgs(expr.args, varType.params, expr.fn, expr);
555
- return;
556
- }
557
- const implMethod = this.resolveInstanceMethod(expr);
558
- if (implMethod) {
559
- this.checkFunctionCallArgs(expr.args, implMethod.params.map(param => this.normalizeType(param.type)), implMethod.name, expr);
560
- return;
561
- }
562
- for (const arg of expr.args) {
563
- this.checkExpr(arg);
564
- }
565
- // Built-in functions are not checked for arg count
566
- }
567
- checkRichTextBuiltinCall(expr, messageIndex) {
568
- for (let i = 0; i < expr.args.length; i++) {
569
- this.checkExpr(expr.args[i], i === messageIndex ? undefined : STRING_TYPE);
570
- }
571
- const message = expr.args[messageIndex];
572
- if (!message) {
573
- return;
574
- }
575
- const messageType = this.inferType(message);
576
- if (messageType.kind !== 'named' ||
577
- (messageType.name !== 'string' && messageType.name !== 'format_string')) {
578
- this.report(`Argument ${messageIndex + 1} of '${expr.fn}' expects string or format_string, got ${this.typeToString(messageType)}`, message);
579
- }
580
- }
581
- checkInvokeExpr(expr) {
582
- this.checkExpr(expr.callee);
583
- const calleeType = this.inferType(expr.callee);
584
- if (calleeType.kind !== 'function_type') {
585
- this.report('Attempted to call a non-function value', expr.callee);
586
- for (const arg of expr.args) {
587
- this.checkExpr(arg);
588
- }
589
- return;
590
- }
591
- this.checkFunctionCallArgs(expr.args, calleeType.params, 'lambda', expr);
592
- }
593
- checkFunctionCallArgs(args, params, calleeName, node) {
594
- if (args.length !== params.length) {
595
- this.report(`Function '${calleeName}' expects ${params.length} arguments, got ${args.length}`, node);
596
- }
597
- for (let i = 0; i < args.length; i++) {
598
- const paramType = params[i];
599
- if (!paramType) {
600
- this.checkExpr(args[i]);
601
- continue;
602
- }
603
- this.checkExpr(args[i], paramType);
604
- const argType = this.inferType(args[i], paramType);
605
- if (!this.typesMatch(paramType, argType)) {
606
- this.report(`Argument ${i + 1} of '${calleeName}' expects ${this.typeToString(paramType)}, got ${this.typeToString(argType)}`, args[i]);
607
- }
608
- }
609
- }
610
- checkTpCall(expr) {
611
- const dest = expr.args[1];
612
- if (!dest) {
613
- return;
614
- }
615
- const destType = this.inferType(dest);
616
- if (destType.kind === 'named' && destType.name === 'BlockPos') {
617
- return;
618
- }
619
- if (dest.kind === 'selector' && !dest.isSingle) {
620
- this.report('tp destination must be a single-entity selector (@s, @p, @r, or limit=1)', dest);
621
- }
622
- }
623
- checkMemberExpr(expr) {
624
- if (!(expr.obj.kind === 'ident' && this.enums.has(expr.obj.name))) {
625
- this.checkExpr(expr.obj);
626
- }
627
- // Check if accessing member on appropriate type
628
- if (expr.obj.kind === 'ident') {
629
- if (this.enums.has(expr.obj.name)) {
630
- const enumVariants = this.enums.get(expr.obj.name);
631
- if (!enumVariants.has(expr.field)) {
632
- this.report(`Enum '${expr.obj.name}' has no variant '${expr.field}'`, expr);
633
- }
634
- return;
635
- }
636
- const varSymbol = this.scope.get(expr.obj.name);
637
- const varType = varSymbol?.type;
638
- if (varType) {
639
- // Allow member access on struct types
640
- if (varType.kind === 'struct') {
641
- const structFields = this.structs.get(varType.name);
642
- if (structFields && !structFields.has(expr.field)) {
643
- this.report(`Struct '${varType.name}' has no field '${expr.field}'`, expr);
644
- }
645
- }
646
- else if (varType.kind === 'array') {
647
- if (expr.field !== 'len' && expr.field !== 'push' && expr.field !== 'pop') {
648
- this.report(`Array has no field '${expr.field}'`, expr);
649
- }
650
- }
651
- else if (varType.kind === 'named') {
652
- // Entity marker (void) - allow all members
653
- if (varType.name !== 'void') {
654
- // Only warn for primitive types
655
- if (['int', 'bool', 'float', 'string', 'byte', 'short', 'long', 'double'].includes(varType.name)) {
656
- this.report(`Cannot access member '${expr.field}' on ${this.typeToString(varType)}`, expr);
657
- }
658
- }
659
- }
660
- }
661
- }
662
- }
663
- checkStaticCallExpr(expr) {
664
- const method = this.implMethods.get(expr.type)?.get(expr.method);
665
- if (!method) {
666
- this.report(`Type '${expr.type}' has no static method '${expr.method}'`, expr);
667
- for (const arg of expr.args) {
668
- this.checkExpr(arg);
669
- }
670
- return;
671
- }
672
- if (method.params[0]?.name === 'self') {
673
- this.report(`Method '${expr.type}::${expr.method}' is an instance method`, expr);
674
- return;
675
- }
676
- this.checkFunctionCallArgs(expr.args, method.params.map(param => this.normalizeType(param.type)), `${expr.type}::${expr.method}`, expr);
677
- }
678
- checkLambdaExpr(expr, expectedType) {
679
- const normalizedExpected = expectedType ? this.normalizeType(expectedType) : undefined;
680
- const expectedFnType = normalizedExpected?.kind === 'function_type' ? normalizedExpected : undefined;
681
- const lambdaType = this.inferLambdaType(expr, expectedFnType);
682
- if (expectedFnType && !this.typesMatch(expectedFnType, lambdaType)) {
683
- this.report(`Type mismatch: expected ${this.typeToString(expectedFnType)}, got ${this.typeToString(lambdaType)}`, expr);
684
- return;
685
- }
686
- const outerScope = this.scope;
687
- const outerReturnType = this.currentReturnType;
688
- const lambdaScope = new Map(this.scope);
689
- const paramTypes = expectedFnType?.params ?? lambdaType.params;
690
- for (let i = 0; i < expr.params.length; i++) {
691
- lambdaScope.set(expr.params[i].name, {
692
- type: paramTypes[i] ?? { kind: 'named', name: 'void' },
693
- mutable: true,
694
- });
695
- }
696
- this.scope = lambdaScope;
697
- this.currentReturnType = expr.returnType
698
- ? this.normalizeType(expr.returnType)
699
- : (expectedFnType?.return ?? lambdaType.return);
700
- if (Array.isArray(expr.body)) {
701
- this.checkBlock(expr.body);
702
- }
703
- else {
704
- this.checkExpr(expr.body, this.currentReturnType);
705
- const actualType = this.inferType(expr.body, this.currentReturnType);
706
- if (!this.typesMatch(this.currentReturnType, actualType)) {
707
- this.report(`Return type mismatch: expected ${this.typeToString(this.currentReturnType)}, got ${this.typeToString(actualType)}`, expr.body);
708
- }
709
- }
710
- this.scope = outerScope;
711
- this.currentReturnType = outerReturnType;
712
- }
713
- checkIfBranches(stmt) {
714
- const narrowed = this.getThenBranchNarrowing(stmt.cond);
715
- if (narrowed) {
716
- const thenScope = new Map(this.scope);
717
- thenScope.set(narrowed.name, { type: narrowed.type, mutable: narrowed.mutable });
718
- const outerScope = this.scope;
719
- this.scope = thenScope;
720
- this.checkBlock(stmt.then);
721
- this.scope = outerScope;
722
- }
723
- else {
724
- this.checkBlock(stmt.then);
725
- }
726
- if (stmt.else_) {
727
- this.checkBlock(stmt.else_);
728
- }
729
- }
730
- getThenBranchNarrowing(cond) {
731
- if (cond.kind !== 'is_check' || cond.expr.kind !== 'ident') {
732
- return null;
733
- }
734
- const symbol = this.scope.get(cond.expr.name);
735
- if (!symbol || symbol.type.kind !== 'entity') {
736
- return null;
737
- }
738
- return {
739
- name: cond.expr.name,
740
- type: { kind: 'entity', entityType: cond.entityType },
741
- mutable: symbol.mutable,
742
- };
743
- }
744
- inferType(expr, expectedType) {
745
- switch (expr.kind) {
746
- case 'int_lit':
747
- return { kind: 'named', name: 'int' };
748
- case 'float_lit':
749
- return { kind: 'named', name: 'float' };
750
- case 'byte_lit':
751
- return { kind: 'named', name: 'byte' };
752
- case 'short_lit':
753
- return { kind: 'named', name: 'short' };
754
- case 'long_lit':
755
- return { kind: 'named', name: 'long' };
756
- case 'double_lit':
757
- return { kind: 'named', name: 'double' };
758
- case 'bool_lit':
759
- return { kind: 'named', name: 'bool' };
760
- case 'str_lit':
761
- case 'mc_name':
762
- return { kind: 'named', name: 'string' };
763
- case 'str_interp':
764
- for (const part of expr.parts) {
765
- if (typeof part !== 'string') {
766
- this.checkExpr(part);
767
- }
768
- }
769
- return { kind: 'named', name: 'string' };
770
- case 'f_string':
771
- for (const part of expr.parts) {
772
- if (part.kind === 'expr') {
773
- this.checkExpr(part.expr);
774
- }
775
- }
776
- return FORMAT_STRING_TYPE;
777
- case 'blockpos':
778
- return { kind: 'named', name: 'BlockPos' };
779
- case 'ident':
780
- return this.scope.get(expr.name)?.type ?? { kind: 'named', name: 'void' };
781
- case 'call': {
782
- const builtin = BUILTIN_SIGNATURES[expr.fn];
783
- if (builtin) {
784
- return builtin.return;
785
- }
786
- if (expr.fn === '__array_push') {
787
- return VOID_TYPE;
788
- }
789
- if (expr.fn === '__array_pop') {
790
- const target = expr.args[0];
791
- if (target && target.kind === 'ident') {
792
- const targetType = this.scope.get(target.name)?.type;
793
- if (targetType?.kind === 'array')
794
- return targetType.elem;
795
- }
796
- return INT_TYPE;
797
- }
798
- if (expr.fn === 'bossbar_get_value') {
799
- return INT_TYPE;
800
- }
801
- if (expr.fn === 'random_sequence') {
802
- return VOID_TYPE;
803
- }
804
- const varType = this.scope.get(expr.fn)?.type;
805
- if (varType?.kind === 'function_type') {
806
- return varType.return;
807
- }
808
- const implMethod = this.resolveInstanceMethod(expr);
809
- if (implMethod) {
810
- return this.normalizeType(implMethod.returnType);
811
- }
812
- const fn = this.functions.get(expr.fn);
813
- return fn?.returnType ?? INT_TYPE;
814
- }
815
- case 'static_call': {
816
- const method = this.implMethods.get(expr.type)?.get(expr.method);
817
- return method ? this.normalizeType(method.returnType) : { kind: 'named', name: 'void' };
818
- }
819
- case 'invoke': {
820
- const calleeType = this.inferType(expr.callee);
821
- if (calleeType.kind === 'function_type') {
822
- return calleeType.return;
823
- }
824
- return { kind: 'named', name: 'void' };
825
- }
826
- case 'member':
827
- if (expr.obj.kind === 'ident' && this.enums.has(expr.obj.name)) {
828
- return { kind: 'enum', name: expr.obj.name };
829
- }
830
- if (expr.obj.kind === 'ident') {
831
- const objTypeNode = this.scope.get(expr.obj.name)?.type;
832
- if (objTypeNode?.kind === 'array' && expr.field === 'len') {
833
- return { kind: 'named', name: 'int' };
834
- }
835
- }
836
- return { kind: 'named', name: 'void' };
837
- case 'index': {
838
- const objType = this.inferType(expr.obj);
839
- if (objType.kind === 'array')
840
- return objType.elem;
841
- return { kind: 'named', name: 'void' };
842
- }
843
- case 'binary':
844
- if (['==', '!=', '<', '<=', '>', '>=', '&&', '||'].includes(expr.op)) {
845
- return { kind: 'named', name: 'bool' };
846
- }
847
- return this.inferType(expr.left);
848
- case 'is_check':
849
- return { kind: 'named', name: 'bool' };
850
- case 'unary':
851
- if (expr.op === '!')
852
- return { kind: 'named', name: 'bool' };
853
- return this.inferType(expr.operand);
854
- case 'array_lit':
855
- if (expr.elements.length > 0) {
856
- return { kind: 'array', elem: this.inferType(expr.elements[0]) };
857
- }
858
- return { kind: 'array', elem: { kind: 'named', name: 'int' } };
859
- case 'struct_lit':
860
- if (expectedType) {
861
- const normalized = this.normalizeType(expectedType);
862
- if (normalized.kind === 'struct') {
863
- return normalized;
864
- }
865
- }
866
- return { kind: 'named', name: 'void' };
867
- case 'lambda':
868
- return this.inferLambdaType(expr, expectedType && this.normalizeType(expectedType).kind === 'function_type'
869
- ? this.normalizeType(expectedType)
870
- : undefined);
871
- default:
872
- return { kind: 'named', name: 'void' };
873
- }
874
- }
875
- inferLambdaType(expr, expectedType) {
876
- const params = expr.params.map((param, index) => {
877
- if (param.type) {
878
- return this.normalizeType(param.type);
879
- }
880
- const inferred = expectedType?.params[index];
881
- if (inferred) {
882
- return inferred;
883
- }
884
- this.report(`Lambda parameter '${param.name}' requires a type annotation`, expr);
885
- return { kind: 'named', name: 'void' };
886
- });
887
- let returnType = expr.returnType
888
- ? this.normalizeType(expr.returnType)
889
- : expectedType?.return;
890
- if (!returnType) {
891
- returnType = Array.isArray(expr.body) ? { kind: 'named', name: 'void' } : this.inferType(expr.body);
892
- }
893
- return { kind: 'function_type', params, return: returnType };
894
- }
895
- // ---------------------------------------------------------------------------
896
- // Entity Type Helpers
897
- // ---------------------------------------------------------------------------
898
- /** Infer entity type from a selector */
899
- inferEntityTypeFromSelector(selector) {
900
- // @a, @p, @r always return Player
901
- if (selector.kind === '@a' || selector.kind === '@p' || selector.kind === '@r') {
902
- return 'Player';
903
- }
904
- // @e or @s with type= filter
905
- if (selector.filters?.type) {
906
- const mcType = selector.filters.type.toLowerCase();
907
- return MC_TYPE_TO_ENTITY[mcType] ?? 'entity';
908
- }
909
- // @s uses current context
910
- if (selector.kind === '@s') {
911
- return this.selfTypeStack[this.selfTypeStack.length - 1];
912
- }
913
- // Default to entity
914
- return 'entity';
915
- }
916
- resolveInstanceMethod(expr) {
917
- const receiver = expr.args[0];
918
- if (!receiver) {
919
- return null;
920
- }
921
- const receiverType = this.inferType(receiver);
922
- if (receiverType.kind !== 'struct') {
923
- return null;
924
- }
925
- const method = this.implMethods.get(receiverType.name)?.get(expr.fn);
926
- if (!method || method.params[0]?.name !== 'self') {
927
- return null;
928
- }
929
- return method;
930
- }
931
- /** Check if childType is a subtype of parentType */
932
- isEntitySubtype(childType, parentType) {
933
- if (childType === parentType)
934
- return true;
935
- let current = childType;
936
- while (current !== null) {
937
- if (current === parentType)
938
- return true;
939
- current = ENTITY_HIERARCHY[current];
940
- }
941
- return false;
942
- }
943
- /** Push a new self type context */
944
- pushSelfType(entityType) {
945
- this.selfTypeStack.push(entityType);
946
- }
947
- /** Pop self type context */
948
- popSelfType() {
949
- if (this.selfTypeStack.length > 1) {
950
- this.selfTypeStack.pop();
951
- }
952
- }
953
- /** Get current @s type */
954
- getCurrentSelfType() {
955
- return this.selfTypeStack[this.selfTypeStack.length - 1];
956
- }
957
- typesMatch(expected, actual) {
958
- if (expected.kind !== actual.kind)
959
- return false;
960
- if (expected.kind === 'named' && actual.kind === 'named') {
961
- // void matches anything (for inferred types)
962
- if (actual.name === 'void')
963
- return true;
964
- return expected.name === actual.name;
965
- }
966
- if (expected.kind === 'array' && actual.kind === 'array') {
967
- return this.typesMatch(expected.elem, actual.elem);
968
- }
969
- if (expected.kind === 'struct' && actual.kind === 'struct') {
970
- return expected.name === actual.name;
971
- }
972
- if (expected.kind === 'enum' && actual.kind === 'enum') {
973
- return expected.name === actual.name;
974
- }
975
- if (expected.kind === 'function_type' && actual.kind === 'function_type') {
976
- return expected.params.length === actual.params.length &&
977
- expected.params.every((param, index) => this.typesMatch(param, actual.params[index])) &&
978
- this.typesMatch(expected.return, actual.return);
979
- }
980
- // Entity type matching with subtype support
981
- if (expected.kind === 'entity' && actual.kind === 'entity') {
982
- return this.isEntitySubtype(actual.entityType, expected.entityType);
983
- }
984
- // Selector matches any entity type
985
- if (expected.kind === 'selector' && actual.kind === 'entity') {
986
- return true;
987
- }
988
- return false;
989
- }
990
- typeToString(type) {
991
- switch (type.kind) {
992
- case 'named':
993
- return type.name;
994
- case 'array':
995
- return `${this.typeToString(type.elem)}[]`;
996
- case 'struct':
997
- return type.name;
998
- case 'enum':
999
- return type.name;
1000
- case 'function_type':
1001
- return `(${type.params.map(param => this.typeToString(param)).join(', ')}) -> ${this.typeToString(type.return)}`;
1002
- case 'entity':
1003
- return type.entityType;
1004
- case 'selector':
1005
- return 'selector';
1006
- default:
1007
- return 'unknown';
1008
- }
1009
- }
1010
- normalizeType(type) {
1011
- if (type.kind === 'array') {
1012
- return { kind: 'array', elem: this.normalizeType(type.elem) };
1013
- }
1014
- if (type.kind === 'function_type') {
1015
- return {
1016
- kind: 'function_type',
1017
- params: type.params.map(param => this.normalizeType(param)),
1018
- return: this.normalizeType(type.return),
1019
- };
1020
- }
1021
- if ((type.kind === 'struct' || type.kind === 'enum') && this.enums.has(type.name)) {
1022
- return { kind: 'enum', name: type.name };
1023
- }
1024
- if (type.kind === 'struct' && type.name in ENTITY_HIERARCHY) {
1025
- return { kind: 'entity', entityType: type.name };
1026
- }
1027
- if (type.kind === 'named' && type.name in ENTITY_HIERARCHY) {
1028
- return { kind: 'entity', entityType: type.name };
1029
- }
1030
- return type;
1031
- }
1032
- }
1033
- exports.TypeChecker = TypeChecker;
1034
- //# sourceMappingURL=index.js.map