redscript-mc 1.2.29 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. package/.claude/commands/build-test.md +10 -0
  2. package/.claude/commands/deploy-demo.md +12 -0
  3. package/.claude/commands/stage-status.md +13 -0
  4. package/.claude/settings.json +12 -0
  5. package/.github/workflows/ci.yml +1 -0
  6. package/CLAUDE.md +231 -0
  7. package/README.md +29 -28
  8. package/README.zh.md +28 -28
  9. package/demo.gif +0 -0
  10. package/dist/cli.js +2 -554
  11. package/dist/compile.js +2 -266
  12. package/dist/index.js +2 -159
  13. package/dist/lexer/index.js +9 -1
  14. package/dist/lowering/index.js +22 -5
  15. package/dist/src/__tests__/cli.test.d.ts +1 -0
  16. package/dist/src/__tests__/cli.test.js +104 -0
  17. package/dist/src/__tests__/codegen.test.d.ts +1 -0
  18. package/dist/src/__tests__/codegen.test.js +152 -0
  19. package/dist/src/__tests__/compile-all.test.d.ts +10 -0
  20. package/dist/src/__tests__/compile-all.test.js +108 -0
  21. package/dist/src/__tests__/dce.test.d.ts +1 -0
  22. package/dist/src/__tests__/dce.test.js +102 -0
  23. package/dist/src/__tests__/diagnostics.test.d.ts +4 -0
  24. package/dist/src/__tests__/diagnostics.test.js +177 -0
  25. package/dist/src/__tests__/e2e.test.d.ts +6 -0
  26. package/dist/src/__tests__/e2e.test.js +1789 -0
  27. package/dist/src/__tests__/entity-types.test.d.ts +1 -0
  28. package/dist/src/__tests__/entity-types.test.js +203 -0
  29. package/dist/src/__tests__/formatter.test.d.ts +1 -0
  30. package/dist/src/__tests__/formatter.test.js +40 -0
  31. package/dist/src/__tests__/lexer.test.d.ts +1 -0
  32. package/dist/src/__tests__/lexer.test.js +343 -0
  33. package/dist/src/__tests__/lowering.test.d.ts +1 -0
  34. package/dist/src/__tests__/lowering.test.js +1015 -0
  35. package/dist/src/__tests__/macro.test.d.ts +8 -0
  36. package/dist/src/__tests__/macro.test.js +306 -0
  37. package/dist/src/__tests__/mc-integration.test.d.ts +12 -0
  38. package/dist/src/__tests__/mc-integration.test.js +817 -0
  39. package/dist/src/__tests__/mc-syntax.test.d.ts +1 -0
  40. package/dist/src/__tests__/mc-syntax.test.js +124 -0
  41. package/dist/src/__tests__/nbt.test.d.ts +1 -0
  42. package/dist/src/__tests__/nbt.test.js +82 -0
  43. package/dist/src/__tests__/optimizer-advanced.test.d.ts +1 -0
  44. package/dist/src/__tests__/optimizer-advanced.test.js +124 -0
  45. package/dist/src/__tests__/optimizer.test.d.ts +1 -0
  46. package/dist/src/__tests__/optimizer.test.js +149 -0
  47. package/dist/src/__tests__/parser.test.d.ts +1 -0
  48. package/dist/src/__tests__/parser.test.js +807 -0
  49. package/dist/src/__tests__/repl.test.d.ts +1 -0
  50. package/dist/src/__tests__/repl.test.js +27 -0
  51. package/dist/src/__tests__/runtime.test.d.ts +1 -0
  52. package/dist/src/__tests__/runtime.test.js +289 -0
  53. package/dist/src/__tests__/stdlib-advanced.test.d.ts +4 -0
  54. package/dist/src/__tests__/stdlib-advanced.test.js +374 -0
  55. package/dist/src/__tests__/stdlib-bigint.test.d.ts +7 -0
  56. package/dist/src/__tests__/stdlib-bigint.test.js +426 -0
  57. package/dist/src/__tests__/stdlib-math.test.d.ts +7 -0
  58. package/dist/src/__tests__/stdlib-math.test.js +351 -0
  59. package/dist/src/__tests__/stdlib-vec.test.d.ts +4 -0
  60. package/dist/src/__tests__/stdlib-vec.test.js +263 -0
  61. package/dist/src/__tests__/structure-optimizer.test.d.ts +1 -0
  62. package/dist/src/__tests__/structure-optimizer.test.js +33 -0
  63. package/dist/src/__tests__/typechecker.test.d.ts +1 -0
  64. package/dist/src/__tests__/typechecker.test.js +552 -0
  65. package/dist/src/__tests__/var-allocator.test.d.ts +1 -0
  66. package/dist/src/__tests__/var-allocator.test.js +69 -0
  67. package/dist/src/ast/types.d.ts +515 -0
  68. package/dist/src/ast/types.js +9 -0
  69. package/dist/src/builtins/metadata.d.ts +36 -0
  70. package/dist/src/builtins/metadata.js +1014 -0
  71. package/dist/src/cli.d.ts +11 -0
  72. package/dist/src/cli.js +443 -0
  73. package/dist/src/codegen/cmdblock/index.d.ts +26 -0
  74. package/dist/src/codegen/cmdblock/index.js +45 -0
  75. package/dist/src/codegen/mcfunction/index.d.ts +40 -0
  76. package/dist/src/codegen/mcfunction/index.js +606 -0
  77. package/dist/src/codegen/structure/index.d.ts +24 -0
  78. package/dist/src/codegen/structure/index.js +279 -0
  79. package/dist/src/codegen/var-allocator.d.ts +45 -0
  80. package/dist/src/codegen/var-allocator.js +104 -0
  81. package/dist/src/compile.d.ts +37 -0
  82. package/dist/src/compile.js +165 -0
  83. package/dist/src/diagnostics/index.d.ts +44 -0
  84. package/dist/src/diagnostics/index.js +140 -0
  85. package/dist/src/events/types.d.ts +35 -0
  86. package/dist/src/events/types.js +59 -0
  87. package/dist/src/formatter/index.d.ts +1 -0
  88. package/dist/src/formatter/index.js +26 -0
  89. package/dist/src/index.d.ts +22 -0
  90. package/dist/src/index.js +45 -0
  91. package/dist/src/ir/builder.d.ts +33 -0
  92. package/dist/src/ir/builder.js +99 -0
  93. package/dist/src/ir/types.d.ts +132 -0
  94. package/dist/src/ir/types.js +15 -0
  95. package/dist/src/lexer/index.d.ts +37 -0
  96. package/dist/src/lexer/index.js +569 -0
  97. package/dist/src/lowering/index.d.ts +188 -0
  98. package/dist/src/lowering/index.js +3405 -0
  99. package/dist/src/mc-test/client.d.ts +128 -0
  100. package/dist/src/mc-test/client.js +174 -0
  101. package/dist/src/mc-test/runner.d.ts +28 -0
  102. package/dist/src/mc-test/runner.js +151 -0
  103. package/dist/src/mc-test/setup.d.ts +11 -0
  104. package/dist/src/mc-test/setup.js +98 -0
  105. package/dist/src/mc-validator/index.d.ts +17 -0
  106. package/dist/src/mc-validator/index.js +322 -0
  107. package/dist/src/nbt/index.d.ts +86 -0
  108. package/dist/src/nbt/index.js +250 -0
  109. package/dist/src/optimizer/commands.d.ts +38 -0
  110. package/dist/src/optimizer/commands.js +451 -0
  111. package/dist/src/optimizer/dce.d.ts +34 -0
  112. package/dist/src/optimizer/dce.js +639 -0
  113. package/dist/src/optimizer/passes.d.ts +34 -0
  114. package/dist/src/optimizer/passes.js +243 -0
  115. package/dist/src/optimizer/structure.d.ts +9 -0
  116. package/dist/src/optimizer/structure.js +356 -0
  117. package/dist/src/parser/index.d.ts +93 -0
  118. package/dist/src/parser/index.js +1687 -0
  119. package/dist/src/repl.d.ts +16 -0
  120. package/dist/src/repl.js +165 -0
  121. package/dist/src/runtime/index.d.ts +107 -0
  122. package/dist/src/runtime/index.js +1409 -0
  123. package/dist/src/typechecker/index.d.ts +61 -0
  124. package/dist/src/typechecker/index.js +1034 -0
  125. package/dist/src/types/entity-hierarchy.d.ts +29 -0
  126. package/dist/src/types/entity-hierarchy.js +107 -0
  127. package/dist/src2/__tests__/e2e/basic.test.d.ts +8 -0
  128. package/dist/src2/__tests__/e2e/basic.test.js +140 -0
  129. package/dist/src2/__tests__/e2e/macros.test.d.ts +9 -0
  130. package/dist/src2/__tests__/e2e/macros.test.js +182 -0
  131. package/dist/src2/__tests__/e2e/migrate.test.d.ts +13 -0
  132. package/dist/src2/__tests__/e2e/migrate.test.js +2739 -0
  133. package/dist/src2/__tests__/hir/desugar.test.d.ts +1 -0
  134. package/dist/src2/__tests__/hir/desugar.test.js +234 -0
  135. package/dist/src2/__tests__/lir/lower.test.d.ts +1 -0
  136. package/dist/src2/__tests__/lir/lower.test.js +559 -0
  137. package/dist/src2/__tests__/lir/types.test.d.ts +1 -0
  138. package/dist/src2/__tests__/lir/types.test.js +185 -0
  139. package/dist/src2/__tests__/lir/verify.test.d.ts +1 -0
  140. package/dist/src2/__tests__/lir/verify.test.js +221 -0
  141. package/dist/src2/__tests__/mir/arithmetic.test.d.ts +1 -0
  142. package/dist/src2/__tests__/mir/arithmetic.test.js +130 -0
  143. package/dist/src2/__tests__/mir/control-flow.test.d.ts +1 -0
  144. package/dist/src2/__tests__/mir/control-flow.test.js +205 -0
  145. package/dist/src2/__tests__/mir/verify.test.d.ts +1 -0
  146. package/dist/src2/__tests__/mir/verify.test.js +223 -0
  147. package/dist/src2/__tests__/optimizer/block_merge.test.d.ts +1 -0
  148. package/dist/src2/__tests__/optimizer/block_merge.test.js +78 -0
  149. package/dist/src2/__tests__/optimizer/branch_simplify.test.d.ts +1 -0
  150. package/dist/src2/__tests__/optimizer/branch_simplify.test.js +58 -0
  151. package/dist/src2/__tests__/optimizer/constant_fold.test.d.ts +1 -0
  152. package/dist/src2/__tests__/optimizer/constant_fold.test.js +131 -0
  153. package/dist/src2/__tests__/optimizer/copy_prop.test.d.ts +1 -0
  154. package/dist/src2/__tests__/optimizer/copy_prop.test.js +91 -0
  155. package/dist/src2/__tests__/optimizer/dce.test.d.ts +1 -0
  156. package/dist/src2/__tests__/optimizer/dce.test.js +76 -0
  157. package/dist/src2/__tests__/optimizer/pipeline.test.d.ts +1 -0
  158. package/dist/src2/__tests__/optimizer/pipeline.test.js +102 -0
  159. package/dist/src2/emit/compile.d.ts +19 -0
  160. package/dist/src2/emit/compile.js +80 -0
  161. package/dist/src2/emit/index.d.ts +17 -0
  162. package/dist/src2/emit/index.js +172 -0
  163. package/dist/src2/hir/lower.d.ts +15 -0
  164. package/dist/src2/hir/lower.js +378 -0
  165. package/dist/src2/hir/types.d.ts +373 -0
  166. package/dist/src2/hir/types.js +16 -0
  167. package/dist/src2/lir/lower.d.ts +15 -0
  168. package/dist/src2/lir/lower.js +453 -0
  169. package/dist/src2/lir/types.d.ts +136 -0
  170. package/dist/src2/lir/types.js +11 -0
  171. package/dist/src2/lir/verify.d.ts +14 -0
  172. package/dist/src2/lir/verify.js +113 -0
  173. package/dist/src2/mir/lower.d.ts +9 -0
  174. package/dist/src2/mir/lower.js +1030 -0
  175. package/dist/src2/mir/macro.d.ts +22 -0
  176. package/dist/src2/mir/macro.js +168 -0
  177. package/dist/src2/mir/types.d.ts +183 -0
  178. package/dist/src2/mir/types.js +11 -0
  179. package/dist/src2/mir/verify.d.ts +16 -0
  180. package/dist/src2/mir/verify.js +216 -0
  181. package/dist/src2/optimizer/block_merge.d.ts +12 -0
  182. package/dist/src2/optimizer/block_merge.js +84 -0
  183. package/dist/src2/optimizer/branch_simplify.d.ts +9 -0
  184. package/dist/src2/optimizer/branch_simplify.js +28 -0
  185. package/dist/src2/optimizer/constant_fold.d.ts +10 -0
  186. package/dist/src2/optimizer/constant_fold.js +85 -0
  187. package/dist/src2/optimizer/copy_prop.d.ts +9 -0
  188. package/dist/src2/optimizer/copy_prop.js +113 -0
  189. package/dist/src2/optimizer/dce.d.ts +8 -0
  190. package/dist/src2/optimizer/dce.js +155 -0
  191. package/dist/src2/optimizer/pipeline.d.ts +10 -0
  192. package/dist/src2/optimizer/pipeline.js +42 -0
  193. package/dist/tsconfig.tsbuildinfo +1 -0
  194. package/docs/compiler-pipeline-redesign.md +2243 -0
  195. package/docs/optimization-ideas.md +1076 -0
  196. package/editors/vscode/package-lock.json +3 -3
  197. package/editors/vscode/package.json +1 -1
  198. package/examples/readme-demo.mcrs +44 -66
  199. package/jest.config.js +1 -1
  200. package/package.json +6 -5
  201. package/scripts/postbuild.js +15 -0
  202. package/src/__tests__/cli.test.ts +8 -220
  203. package/src/__tests__/dce.test.ts +11 -56
  204. package/src/__tests__/diagnostics.test.ts +59 -38
  205. package/src/__tests__/mc-integration.test.ts +1 -2
  206. package/src/ast/types.ts +6 -1
  207. package/src/cli.ts +29 -156
  208. package/src/compile.ts +6 -162
  209. package/src/index.ts +14 -178
  210. package/src/lexer/index.ts +9 -1
  211. package/src/mc-test/runner.ts +4 -3
  212. package/src/parser/index.ts +1 -1
  213. package/src/repl.ts +1 -1
  214. package/src/runtime/index.ts +1 -1
  215. package/src2/__tests__/e2e/basic.test.ts +154 -0
  216. package/src2/__tests__/e2e/macros.test.ts +199 -0
  217. package/src2/__tests__/e2e/migrate.test.ts +3008 -0
  218. package/src2/__tests__/hir/desugar.test.ts +263 -0
  219. package/src2/__tests__/lir/lower.test.ts +619 -0
  220. package/src2/__tests__/lir/types.test.ts +207 -0
  221. package/src2/__tests__/lir/verify.test.ts +249 -0
  222. package/src2/__tests__/mir/arithmetic.test.ts +156 -0
  223. package/src2/__tests__/mir/control-flow.test.ts +242 -0
  224. package/src2/__tests__/mir/verify.test.ts +254 -0
  225. package/src2/__tests__/optimizer/block_merge.test.ts +84 -0
  226. package/src2/__tests__/optimizer/branch_simplify.test.ts +64 -0
  227. package/src2/__tests__/optimizer/constant_fold.test.ts +145 -0
  228. package/src2/__tests__/optimizer/copy_prop.test.ts +99 -0
  229. package/src2/__tests__/optimizer/dce.test.ts +83 -0
  230. package/src2/__tests__/optimizer/pipeline.test.ts +116 -0
  231. package/src2/emit/compile.ts +99 -0
  232. package/src2/emit/index.ts +222 -0
  233. package/src2/hir/lower.ts +428 -0
  234. package/src2/hir/types.ts +216 -0
  235. package/src2/lir/lower.ts +556 -0
  236. package/src2/lir/types.ts +109 -0
  237. package/src2/lir/verify.ts +129 -0
  238. package/src2/mir/lower.ts +1160 -0
  239. package/src2/mir/macro.ts +167 -0
  240. package/src2/mir/types.ts +106 -0
  241. package/src2/mir/verify.ts +218 -0
  242. package/src2/optimizer/block_merge.ts +93 -0
  243. package/src2/optimizer/branch_simplify.ts +27 -0
  244. package/src2/optimizer/constant_fold.ts +88 -0
  245. package/src2/optimizer/copy_prop.ts +106 -0
  246. package/src2/optimizer/dce.ts +133 -0
  247. package/src2/optimizer/pipeline.ts +44 -0
  248. package/tsconfig.json +2 -2
  249. package/src/__tests__/codegen.test.ts +0 -161
  250. package/src/__tests__/e2e.test.ts +0 -2039
  251. package/src/__tests__/entity-types.test.ts +0 -236
  252. package/src/__tests__/lowering.test.ts +0 -1185
  253. package/src/__tests__/macro.test.ts +0 -343
  254. package/src/__tests__/nbt.test.ts +0 -58
  255. package/src/__tests__/optimizer-advanced.test.ts +0 -144
  256. package/src/__tests__/optimizer.test.ts +0 -162
  257. package/src/__tests__/runtime.test.ts +0 -305
  258. package/src/__tests__/stdlib-advanced.test.ts +0 -379
  259. package/src/__tests__/stdlib-bigint.test.ts +0 -427
  260. package/src/__tests__/stdlib-math.test.ts +0 -374
  261. package/src/__tests__/stdlib-vec.test.ts +0 -259
  262. package/src/__tests__/structure-optimizer.test.ts +0 -38
  263. package/src/__tests__/var-allocator.test.ts +0 -75
  264. package/src/codegen/cmdblock/index.ts +0 -63
  265. package/src/codegen/mcfunction/index.ts +0 -662
  266. package/src/codegen/structure/index.ts +0 -346
  267. package/src/codegen/var-allocator.ts +0 -104
  268. package/src/ir/builder.ts +0 -116
  269. package/src/ir/types.ts +0 -134
  270. package/src/lowering/index.ts +0 -3860
  271. package/src/optimizer/commands.ts +0 -534
  272. package/src/optimizer/dce.ts +0 -679
  273. package/src/optimizer/passes.ts +0 -250
  274. package/src/optimizer/structure.ts +0 -450
package/dist/cli.js CHANGED
@@ -1,554 +1,2 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- /**
4
- * RedScript CLI
5
- *
6
- * Usage:
7
- * redscript compile <file> [-o <out>] [--output-nbt <file>] [--namespace <ns>]
8
- * redscript check <file>
9
- * redscript repl
10
- * redscript version
11
- */
12
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
- if (k2 === undefined) k2 = k;
14
- var desc = Object.getOwnPropertyDescriptor(m, k);
15
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
16
- desc = { enumerable: true, get: function() { return m[k]; } };
17
- }
18
- Object.defineProperty(o, k2, desc);
19
- }) : (function(o, m, k, k2) {
20
- if (k2 === undefined) k2 = k;
21
- o[k2] = m[k];
22
- }));
23
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
24
- Object.defineProperty(o, "default", { enumerable: true, value: v });
25
- }) : function(o, v) {
26
- o["default"] = v;
27
- });
28
- var __importStar = (this && this.__importStar) || (function () {
29
- var ownKeys = function(o) {
30
- ownKeys = Object.getOwnPropertyNames || function (o) {
31
- var ar = [];
32
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
33
- return ar;
34
- };
35
- return ownKeys(o);
36
- };
37
- return function (mod) {
38
- if (mod && mod.__esModule) return mod;
39
- var result = {};
40
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
41
- __setModuleDefault(result, mod);
42
- return result;
43
- };
44
- })();
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- const index_1 = require("./index");
47
- const cmdblock_1 = require("./codegen/cmdblock");
48
- const structure_1 = require("./codegen/structure");
49
- const diagnostics_1 = require("./diagnostics");
50
- const repl_1 = require("./repl");
51
- const metadata_1 = require("./builtins/metadata");
52
- const fs = __importStar(require("fs"));
53
- const path = __importStar(require("path"));
54
- const https = __importStar(require("https"));
55
- const child_process_1 = require("child_process");
56
- // Parse command line arguments
57
- const args = process.argv.slice(2);
58
- function printUsage() {
59
- console.log(`
60
- RedScript Compiler
61
-
62
- Usage:
63
- redscript compile <file> [-o <out>] [--output-nbt <file>] [--namespace <ns>] [--scoreboard <obj>] [--target <target>] [--no-dce]
64
- redscript watch <dir> [-o <outdir>] [--namespace <ns>] [--hot-reload <url>]
65
- redscript check <file>
66
- redscript fmt <file.mcrs> [file2.mcrs ...]
67
- redscript generate-dts [-o <file>]
68
- redscript repl
69
- redscript version
70
-
71
- Commands:
72
- compile Compile a RedScript file to a Minecraft datapack
73
- watch Watch a directory for .mcrs file changes, recompile, and hot reload
74
- check Check a RedScript file for errors without generating output
75
- fmt Auto-format RedScript source files
76
- generate-dts Generate builtin function declaration file (builtins.d.mcrs)
77
- repl Start an interactive RedScript REPL
78
- version Print the RedScript version
79
- upgrade Upgrade to the latest version (npm install -g redscript-mc@latest)
80
-
81
- Options:
82
- -o, --output <path> Output directory or file path, depending on target
83
- --output-nbt <file> Output .nbt file path for structure target
84
- --namespace <ns> Datapack namespace (default: derived from filename)
85
- --target <target> Output target: datapack (default), cmdblock, or structure
86
- --no-dce Disable AST dead code elimination
87
- --no-mangle Disable variable name mangling (use readable names)
88
- --scoreboard <obj> Scoreboard objective for variables (default: namespace).
89
- Each datapack automatically uses its namespace as the objective
90
- so multiple datapacks can coexist without collisions.
91
- --stats Print optimizer statistics
92
- --hot-reload <url> After each successful compile, POST to <url>/reload
93
- (use with redscript-testharness; e.g. http://localhost:25561)
94
- -h, --help Show this help message
95
-
96
- Targets:
97
- datapack Generate a full Minecraft datapack (default)
98
- cmdblock Generate JSON structure for command block placement
99
- structure Generate a Minecraft structure .nbt file with command blocks
100
- `);
101
- }
102
- function getLocalVersion() {
103
- const packagePath = path.join(__dirname, '..', 'package.json');
104
- try {
105
- const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));
106
- return pkg.version ?? '0.0.0';
107
- }
108
- catch {
109
- return '0.0.0';
110
- }
111
- }
112
- function printVersion() {
113
- console.log(`RedScript v${getLocalVersion()}`);
114
- }
115
- /** Fetch latest version from npm registry (non-blocking, best-effort). */
116
- function fetchLatestVersion() {
117
- return new Promise(resolve => {
118
- const req = https.get('https://registry.npmjs.org/redscript-mc/latest', { timeout: 3000 }, res => {
119
- let data = '';
120
- res.on('data', chunk => { data += chunk; });
121
- res.on('end', () => {
122
- try {
123
- const json = JSON.parse(data);
124
- resolve(json.version ?? null);
125
- }
126
- catch {
127
- resolve(null);
128
- }
129
- });
130
- });
131
- req.on('error', () => resolve(null));
132
- req.on('timeout', () => { req.destroy(); resolve(null); });
133
- });
134
- }
135
- /** Compare semver strings. Returns true if b > a. */
136
- function isNewer(current, latest) {
137
- const parse = (v) => v.replace(/^v/, '').split('.').map(Number);
138
- const [ca, cb, cc] = parse(current);
139
- const [la, lb, lc] = parse(latest);
140
- if (la !== ca)
141
- return la > ca;
142
- if (lb !== cb)
143
- return lb > cb;
144
- return lc > cc;
145
- }
146
- /**
147
- * Check for a newer version and print a notice if one exists.
148
- * Runs in background — does NOT block normal CLI operation.
149
- */
150
- async function checkForUpdates(silent = false) {
151
- const current = getLocalVersion();
152
- const latest = await fetchLatestVersion();
153
- if (latest && isNewer(current, latest)) {
154
- console.log(`\n💡 New version available: v${current} → v${latest}`);
155
- console.log(` Run: redscript upgrade\n`);
156
- }
157
- else if (!silent && latest) {
158
- // Only print when explicitly running 'version' or 'upgrade'
159
- // No output for normal commands — keep startup noise-free
160
- }
161
- }
162
- /** Run npm install -g to upgrade to latest. */
163
- function upgradeCommand() {
164
- const current = getLocalVersion();
165
- console.log(`Current version: v${current}`);
166
- console.log('Checking latest version...');
167
- fetchLatestVersion().then(latest => {
168
- if (!latest) {
169
- console.error('Could not fetch latest version from npm.');
170
- process.exit(1);
171
- }
172
- if (!isNewer(current, latest)) {
173
- console.log(`✅ Already up to date (v${current})`);
174
- return;
175
- }
176
- console.log(`Upgrading v${current} → v${latest}...`);
177
- try {
178
- (0, child_process_1.execSync)('npm install -g redscript-mc@latest', { stdio: 'inherit' });
179
- console.log(`✅ Upgraded to v${latest}`);
180
- }
181
- catch {
182
- console.error('Upgrade failed. Try manually: npm install -g redscript-mc@latest');
183
- process.exit(1);
184
- }
185
- });
186
- }
187
- function parseArgs(args) {
188
- const result = { dce: true, mangle: true };
189
- let i = 0;
190
- while (i < args.length) {
191
- const arg = args[i];
192
- if (arg === '-h' || arg === '--help') {
193
- result.help = true;
194
- i++;
195
- }
196
- else if (arg === '-o' || arg === '--output') {
197
- result.output = args[++i];
198
- i++;
199
- }
200
- else if (arg === '--output-nbt') {
201
- result.outputNbt = args[++i];
202
- i++;
203
- }
204
- else if (arg === '--namespace') {
205
- result.namespace = args[++i];
206
- i++;
207
- }
208
- else if (arg === '--target') {
209
- result.target = args[++i];
210
- i++;
211
- }
212
- else if (arg === '--stats') {
213
- result.stats = true;
214
- i++;
215
- }
216
- else if (arg === '--no-dce') {
217
- result.dce = false;
218
- i++;
219
- }
220
- else if (arg === '--no-mangle') {
221
- result.mangle = false;
222
- i++;
223
- }
224
- else if (arg === '--scoreboard') {
225
- result.scoreboardObjective = args[++i];
226
- i++;
227
- }
228
- else if (arg === '--hot-reload') {
229
- result.hotReload = args[++i];
230
- i++;
231
- }
232
- else if (!result.command) {
233
- result.command = arg;
234
- i++;
235
- }
236
- else if (!result.file) {
237
- result.file = arg;
238
- i++;
239
- }
240
- else {
241
- i++;
242
- }
243
- }
244
- return result;
245
- }
246
- function deriveNamespace(filePath) {
247
- const basename = path.basename(filePath, path.extname(filePath));
248
- // Convert to valid identifier: lowercase, replace non-alphanumeric with underscore
249
- return basename.toLowerCase().replace(/[^a-z0-9]/g, '_');
250
- }
251
- function printWarnings(warnings) {
252
- if (!warnings || warnings.length === 0) {
253
- return;
254
- }
255
- for (const warning of warnings) {
256
- const loc = warning.filePath
257
- ? `${warning.filePath}:${warning.line ?? '?'}`
258
- : warning.line != null
259
- ? `line ${warning.line}`
260
- : null;
261
- const locStr = loc ? ` (${loc})` : '';
262
- console.error(`Warning [${warning.code}]: ${warning.message}${locStr}`);
263
- }
264
- }
265
- function formatReduction(before, after) {
266
- if (before === 0)
267
- return '0%';
268
- return `${Math.round(((before - after) / before) * 100)}%`;
269
- }
270
- function printOptimizationStats(stats) {
271
- if (!stats)
272
- return;
273
- console.log('Optimizations applied:');
274
- console.log(` LICM: ${stats.licmHoists} reads hoisted from ${stats.licmLoopBodies} loop bodies`);
275
- console.log(` CSE: ${stats.cseRedundantReads + stats.cseArithmetic} expressions eliminated`);
276
- console.log(` setblock batching: ${stats.setblockMergedCommands} setblocks -> ${stats.setblockFillCommands} fills (saved ${stats.setblockSavedCommands} commands)`);
277
- console.log(` dead code: ${stats.deadCodeRemoved} commands removed`);
278
- console.log(` constant folding: ${stats.constantFolds} constants folded`);
279
- console.log(` Total mcfunction commands: ${stats.totalCommandsBefore} -> ${stats.totalCommandsAfter} (${formatReduction(stats.totalCommandsBefore, stats.totalCommandsAfter)} reduction)`);
280
- }
281
- function compileCommand(file, output, namespace, target = 'datapack', showStats = false, dce = true, mangle = true, scoreboardObjective = undefined) {
282
- // Read source file
283
- if (!fs.existsSync(file)) {
284
- console.error(`Error: File not found: ${file}`);
285
- process.exit(1);
286
- }
287
- const source = fs.readFileSync(file, 'utf-8');
288
- try {
289
- if (target === 'cmdblock') {
290
- const result = (0, index_1.compile)(source, { namespace, filePath: file, dce, mangle, scoreboardObjective });
291
- printWarnings(result.warnings);
292
- // Generate command block JSON
293
- const hasTick = result.files.some(f => f.path.includes('__tick.mcfunction'));
294
- const hasLoad = result.files.some(f => f.path.includes('__load.mcfunction'));
295
- const cmdBlocks = (0, cmdblock_1.generateCommandBlocks)(namespace, hasTick, hasLoad);
296
- // Write command block JSON
297
- fs.mkdirSync(output, { recursive: true });
298
- const outputFile = path.join(output, `${namespace}_cmdblocks.json`);
299
- fs.writeFileSync(outputFile, JSON.stringify(cmdBlocks, null, 2));
300
- console.log(`✓ Generated command blocks for ${file}`);
301
- console.log(` Output: ${outputFile}`);
302
- console.log(` Blocks: ${cmdBlocks.blocks.length}`);
303
- if (showStats) {
304
- printOptimizationStats(result.stats);
305
- }
306
- }
307
- else if (target === 'structure') {
308
- const structure = (0, structure_1.compileToStructure)(source, namespace, file, { dce, mangle });
309
- fs.mkdirSync(path.dirname(output), { recursive: true });
310
- fs.writeFileSync(output, structure.buffer);
311
- console.log(`✓ Generated structure for ${file}`);
312
- console.log(` Output: ${output}`);
313
- console.log(` Blocks: ${structure.blockCount}`);
314
- if (showStats) {
315
- printOptimizationStats(structure.stats);
316
- }
317
- }
318
- else {
319
- const result = (0, index_1.compile)(source, { namespace, filePath: file, dce, mangle, scoreboardObjective });
320
- printWarnings(result.warnings);
321
- // Default: generate datapack
322
- // Create output directory
323
- fs.mkdirSync(output, { recursive: true });
324
- // Write all files
325
- for (const dataFile of result.files) {
326
- const filePath = path.join(output, dataFile.path);
327
- const dir = path.dirname(filePath);
328
- fs.mkdirSync(dir, { recursive: true });
329
- fs.writeFileSync(filePath, dataFile.content);
330
- }
331
- // Write sourcemap alongside datapack when mangle mode is active
332
- if (mangle && result.sourceMap && Object.keys(result.sourceMap).length > 0) {
333
- const mapPath = path.join(output, `${namespace}.map.json`);
334
- fs.writeFileSync(mapPath, JSON.stringify(result.sourceMap, null, 2));
335
- console.log(` Sourcemap: ${mapPath}`);
336
- }
337
- console.log(`✓ Compiled ${file} to ${output}/`);
338
- console.log(` Namespace: ${namespace}`);
339
- console.log(` Functions: ${result.ir.functions.length}`);
340
- console.log(` Files: ${result.files.length}`);
341
- if (showStats) {
342
- printOptimizationStats(result.stats);
343
- }
344
- }
345
- }
346
- catch (err) {
347
- console.error((0, diagnostics_1.formatError)(err, source));
348
- process.exit(1);
349
- }
350
- }
351
- function checkCommand(file) {
352
- // Read source file
353
- if (!fs.existsSync(file)) {
354
- console.error(`Error: File not found: ${file}`);
355
- process.exit(1);
356
- }
357
- const source = fs.readFileSync(file, 'utf-8');
358
- const error = (0, index_1.check)(source, 'redscript', file);
359
- if (error) {
360
- console.error((0, diagnostics_1.formatError)(error, source));
361
- process.exit(1);
362
- }
363
- console.log(`✓ ${file} is valid`);
364
- }
365
- async function hotReload(url) {
366
- try {
367
- const res = await fetch(`${url}/reload`, { method: 'POST' });
368
- if (res.ok) {
369
- console.log(`🔄 Hot reload sent → ${url}`);
370
- }
371
- else {
372
- console.warn(`⚠ Hot reload failed: HTTP ${res.status}`);
373
- }
374
- }
375
- catch (e) {
376
- console.warn(`⚠ Hot reload failed (is the server running?): ${e.message}`);
377
- }
378
- }
379
- function watchCommand(dir, output, namespace, hotReloadUrl, dce = true) {
380
- // Check if directory exists
381
- if (!fs.existsSync(dir)) {
382
- console.error(`Error: Directory not found: ${dir}`);
383
- process.exit(1);
384
- }
385
- const stat = fs.statSync(dir);
386
- if (!stat.isDirectory()) {
387
- console.error(`Error: ${dir} is not a directory`);
388
- process.exit(1);
389
- }
390
- console.log(`👁 Watching ${dir} for .mcrs file changes...`);
391
- console.log(` Output: ${output}`);
392
- if (hotReloadUrl)
393
- console.log(` Hot reload: ${hotReloadUrl}`);
394
- console.log(` Press Ctrl+C to stop\n`);
395
- // Debounce timer
396
- let debounceTimer = null;
397
- // Compile all .mcrs files in directory
398
- async function compileAll() {
399
- const files = findRsFiles(dir);
400
- if (files.length === 0) {
401
- console.log(`⚠ No .mcrs files found in ${dir}`);
402
- return;
403
- }
404
- let hasErrors = false;
405
- for (const file of files) {
406
- let source = '';
407
- try {
408
- source = fs.readFileSync(file, 'utf-8');
409
- const ns = namespace ?? deriveNamespace(file);
410
- const result = (0, index_1.compile)(source, { namespace: ns, filePath: file, dce });
411
- printWarnings(result.warnings);
412
- // Create output directory
413
- fs.mkdirSync(output, { recursive: true });
414
- // Write all files
415
- for (const dataFile of result.files) {
416
- const filePath = path.join(output, dataFile.path);
417
- const fileDir = path.dirname(filePath);
418
- fs.mkdirSync(fileDir, { recursive: true });
419
- fs.writeFileSync(filePath, dataFile.content);
420
- }
421
- const timestamp = new Date().toLocaleTimeString();
422
- console.log(`✓ [${timestamp}] Compiled ${file} (${result.files.length} files)`);
423
- }
424
- catch (err) {
425
- hasErrors = true;
426
- const timestamp = new Date().toLocaleTimeString();
427
- console.error(`✗ [${timestamp}] ${(0, diagnostics_1.formatError)(err, source)}`);
428
- }
429
- }
430
- if (!hasErrors) {
431
- if (hotReloadUrl)
432
- await hotReload(hotReloadUrl);
433
- console.log('');
434
- }
435
- }
436
- // Find all .mcrs files recursively
437
- function findRsFiles(directory) {
438
- const results = [];
439
- const entries = fs.readdirSync(directory, { withFileTypes: true });
440
- for (const entry of entries) {
441
- const fullPath = path.join(directory, entry.name);
442
- if (entry.isDirectory()) {
443
- results.push(...findRsFiles(fullPath));
444
- }
445
- else if (entry.isFile() && entry.name.endsWith('.mcrs')) {
446
- results.push(fullPath);
447
- }
448
- }
449
- return results;
450
- }
451
- // Initial compile
452
- void compileAll();
453
- // Watch for changes
454
- fs.watch(dir, { recursive: true }, (eventType, filename) => {
455
- if (filename && filename.endsWith('.mcrs')) {
456
- // Debounce rapid changes
457
- if (debounceTimer) {
458
- clearTimeout(debounceTimer);
459
- }
460
- debounceTimer = setTimeout(() => {
461
- console.log(`📝 Change detected: ${filename}`);
462
- void compileAll();
463
- }, 100);
464
- }
465
- });
466
- }
467
- // Main
468
- const parsed = parseArgs(args);
469
- async function main() {
470
- if (parsed.help || !parsed.command) {
471
- printUsage();
472
- process.exit(parsed.help ? 0 : 1);
473
- }
474
- // Background update check — non-blocking, only shows notice if newer version exists
475
- // Skip for repl/upgrade/version to avoid double-printing
476
- const noCheckCmds = new Set(['upgrade', 'update', 'version', 'repl']);
477
- if (!noCheckCmds.has(parsed.command ?? '')) {
478
- checkForUpdates().catch(() => { });
479
- }
480
- switch (parsed.command) {
481
- case 'compile':
482
- if (!parsed.file) {
483
- console.error('Error: No input file specified');
484
- printUsage();
485
- process.exit(1);
486
- }
487
- {
488
- const namespace = parsed.namespace ?? deriveNamespace(parsed.file);
489
- const target = parsed.target ?? 'datapack';
490
- const output = target === 'structure'
491
- ? (parsed.outputNbt ?? parsed.output ?? `./${namespace}.nbt`)
492
- : (parsed.output ?? './dist');
493
- compileCommand(parsed.file, output, namespace, target, parsed.stats, parsed.dce, parsed.mangle, parsed.scoreboardObjective // undefined = derive from namespace in compile()
494
- );
495
- }
496
- break;
497
- case 'watch':
498
- if (!parsed.file) {
499
- console.error('Error: No directory specified');
500
- printUsage();
501
- process.exit(1);
502
- }
503
- watchCommand(parsed.file, parsed.output ?? './dist', parsed.namespace, parsed.hotReload, parsed.dce);
504
- break;
505
- case 'check':
506
- if (!parsed.file) {
507
- console.error('Error: No input file specified');
508
- printUsage();
509
- process.exit(1);
510
- }
511
- checkCommand(parsed.file);
512
- break;
513
- case 'fmt':
514
- case 'format': {
515
- const files = args.filter(a => a.endsWith('.mcrs'));
516
- if (files.length === 0) {
517
- console.error('Usage: redscript fmt <file.mcrs> [file2.mcrs ...]');
518
- process.exit(1);
519
- }
520
- const { format } = require('./formatter');
521
- for (const file of files) {
522
- const content = fs.readFileSync(file, 'utf8');
523
- const formatted = format(content);
524
- fs.writeFileSync(file, formatted);
525
- console.log(`Formatted: ${file}`);
526
- }
527
- break;
528
- }
529
- case 'generate-dts': {
530
- const output = parsed.output ?? 'builtins.d.mcrs';
531
- const dtsContent = (0, metadata_1.generateDts)();
532
- fs.writeFileSync(output, dtsContent, 'utf-8');
533
- console.log(`Generated ${output}`);
534
- break;
535
- }
536
- case 'repl':
537
- await (0, repl_1.startRepl)(parsed.namespace ?? 'repl');
538
- break;
539
- case 'version':
540
- printVersion();
541
- await checkForUpdates();
542
- break;
543
- case 'upgrade':
544
- case 'update':
545
- upgradeCommand();
546
- break;
547
- default:
548
- console.error(`Error: Unknown command '${parsed.command}'`);
549
- printUsage();
550
- process.exit(1);
551
- }
552
- }
553
- void main();
554
- //# sourceMappingURL=cli.js.map
1
+ // shim — delegates to dist/src/
2
+ module.exports = require('./src/cli');