@creationix/rex 0.1.4 → 0.3.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.
package/rex-compile.ts DELETED
@@ -1,224 +0,0 @@
1
- import { compile, parseToIR } from "./rex.ts";
2
- import { dirname, resolve } from "node:path";
3
- import { readFile, writeFile } from "node:fs/promises";
4
-
5
- type CliOptions = {
6
- expr?: string;
7
- file?: string;
8
- out?: string;
9
- ir: boolean;
10
- minifyNames: boolean;
11
- dedupeValues: boolean;
12
- dedupeMinBytes?: number;
13
- domainRefs: Record<string, number>;
14
- help: boolean;
15
- };
16
-
17
- type DomainSchema = {
18
- globals?: Record<string, { ref?: unknown }>;
19
- };
20
-
21
- const FIRST_NON_RESERVED_REF = 5;
22
-
23
- function parseArgs(argv: string[]): CliOptions {
24
- const options: CliOptions = {
25
- ir: false,
26
- minifyNames: false,
27
- dedupeValues: false,
28
- domainRefs: {},
29
- help: false,
30
- };
31
- for (let index = 0; index < argv.length; index += 1) {
32
- const arg = argv[index];
33
- if (!arg) continue;
34
- if (arg === "--help" || arg === "-h") {
35
- options.help = true;
36
- continue;
37
- }
38
- if (arg === "--ir") {
39
- options.ir = true;
40
- continue;
41
- }
42
- if (arg === "--minify-names" || arg === "-m") {
43
- options.minifyNames = true;
44
- continue;
45
- }
46
- if (arg === "--dedupe-values") {
47
- options.dedupeValues = true;
48
- continue;
49
- }
50
- if (arg === "--dedupe-min-bytes") {
51
- const value = argv[index + 1];
52
- if (!value) throw new Error("Missing value for --dedupe-min-bytes");
53
- const parsed = Number(value);
54
- if (!Number.isInteger(parsed) || parsed < 1) throw new Error("--dedupe-min-bytes must be a positive integer");
55
- options.dedupeMinBytes = parsed;
56
- index += 1;
57
- continue;
58
- }
59
- if (arg === "--domain-extension") {
60
- const value = argv[index + 1];
61
- if (!value) throw new Error("Missing value for --domain-extension");
62
- options.domainRefs[value] = 0;
63
- index += 1;
64
- continue;
65
- }
66
- if (arg === "--domain-ref") {
67
- const value = argv[index + 1];
68
- if (!value) throw new Error("Missing value for --domain-ref");
69
- const separator = value.indexOf("=");
70
- if (separator < 1 || separator === value.length - 1) {
71
- throw new Error("--domain-ref expects NAME=ID (for example: headers=0)");
72
- }
73
- const name = value.slice(0, separator);
74
- const idText = value.slice(separator + 1);
75
- const id = Number(idText);
76
- if (!Number.isInteger(id) || id < 0) throw new Error(`Invalid domain ref id in --domain-ref '${value}'`);
77
- options.domainRefs[name] = id;
78
- index += 1;
79
- continue;
80
- }
81
- if (arg === "--expr" || arg === "-e") {
82
- const value = argv[index + 1];
83
- if (!value) throw new Error("Missing value for --expr");
84
- options.expr = value;
85
- index += 1;
86
- continue;
87
- }
88
- if (arg === "--file" || arg === "-f") {
89
- const value = argv[index + 1];
90
- if (!value) throw new Error("Missing value for --file");
91
- options.file = value;
92
- index += 1;
93
- continue;
94
- }
95
- if (arg === "--out" || arg === "-o") {
96
- const value = argv[index + 1];
97
- if (!value) throw new Error("Missing value for --out");
98
- options.out = value;
99
- index += 1;
100
- continue;
101
- }
102
- throw new Error(`Unknown option: ${arg}`);
103
- }
104
- return options;
105
- }
106
-
107
- function usage() {
108
- return [
109
- "Compile high-level Rex to compact encoding (rexc).",
110
- "",
111
- "Usage:",
112
- " rex --expr \"when x do y end\"",
113
- " rex --file input.rex",
114
- " cat input.rex | rex",
115
- "",
116
- "(Repo script alternative: bun run rex:compile --expr \"when x do y end\")",
117
- "",
118
- "Options:",
119
- " -e, --expr <source> Compile an inline expression/program",
120
- " -f, --file <path> Compile source from a file",
121
- " -o, --out <path> Write output to file instead of stdout",
122
- " --ir Output lowered IR JSON instead of compact encoding",
123
- " -m, --minify-names Minify local variable names in compiled output",
124
- " --dedupe-values Deduplicate large repeated values using forward pointers",
125
- " --dedupe-min-bytes <n> Minimum encoded value bytes for pointer dedupe (default: 4)",
126
- " --domain-extension <name> Map domain symbol name to ref 0 (apostrophe)",
127
- " --domain-ref <name=id> Map domain symbol name to a specific ref id",
128
- " -h, --help Show this message",
129
- ].join("\n");
130
- }
131
-
132
- async function readStdin(): Promise<string> {
133
- const chunks: Buffer[] = [];
134
- for await (const chunk of process.stdin) {
135
- chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
136
- }
137
- return Buffer.concat(chunks).toString("utf8");
138
- }
139
-
140
- async function resolveSource(options: CliOptions): Promise<string> {
141
- if (options.expr && options.file) throw new Error("Use only one of --expr or --file");
142
- if (options.expr) return options.expr;
143
- if (options.file) return readFile(options.file, "utf8");
144
- if (!process.stdin.isTTY) {
145
- const piped = await readStdin();
146
- if (piped.trim().length > 0) return piped;
147
- }
148
- throw new Error("No input provided. Use --expr, --file, or pipe source via stdin.");
149
- }
150
-
151
- async function loadDomainRefsFromFolder(folderPath: string): Promise<Record<string, number>> {
152
- const schemaPath = resolve(folderPath, "rex-domain.json");
153
- let parsed: DomainSchema;
154
- try {
155
- parsed = JSON.parse(await readFile(schemaPath, "utf8")) as DomainSchema;
156
- } catch (error) {
157
- if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
158
- throw error;
159
- }
160
- if (!parsed || typeof parsed !== "object" || !parsed.globals || typeof parsed.globals !== "object") {
161
- throw new Error(`Invalid rex-domain.json at ${schemaPath}: expected { globals: { ... } }`);
162
- }
163
-
164
- const refs: Record<string, number> = {};
165
- const seenRefIds = new Map<number, string>();
166
- for (const [name, entry] of Object.entries(parsed.globals)) {
167
- if (!entry || typeof entry !== "object") {
168
- throw new Error(`Invalid rex-domain.json at ${schemaPath}: globals.${name} must be an object with a numeric ref`);
169
- }
170
- const ref = entry.ref;
171
- if (!Number.isInteger(ref)) {
172
- throw new Error(`Invalid rex-domain.json at ${schemaPath}: globals.${name}.ref must be an integer`);
173
- }
174
- if (ref < FIRST_NON_RESERVED_REF) {
175
- throw new Error(
176
- `Invalid rex-domain.json at ${schemaPath}: globals.${name}.ref must be >= ${FIRST_NON_RESERVED_REF} (0-4 are reserved built-ins)`,
177
- );
178
- }
179
- const existing = seenRefIds.get(ref);
180
- if (existing) {
181
- throw new Error(`Invalid rex-domain.json at ${schemaPath}: duplicate ref ${ref} for globals.${existing} and globals.${name}`);
182
- }
183
- seenRefIds.set(ref, name);
184
- refs[name] = ref;
185
- }
186
- return refs;
187
- }
188
-
189
- async function resolveDomainRefs(options: CliOptions): Promise<Record<string, number>> {
190
- const baseFolder = options.file ? dirname(resolve(options.file)) : process.cwd();
191
- const autoRefs = await loadDomainRefsFromFolder(baseFolder);
192
- return { ...autoRefs, ...options.domainRefs };
193
- }
194
-
195
- async function main() {
196
- const options = parseArgs(process.argv.slice(2));
197
- if (options.help) {
198
- console.log(usage());
199
- return;
200
- }
201
-
202
- const source = await resolveSource(options);
203
- const domainRefs = await resolveDomainRefs(options);
204
- const output = options.ir
205
- ? JSON.stringify(parseToIR(source), null, 2)
206
- : compile(source, {
207
- minifyNames: options.minifyNames,
208
- dedupeValues: options.dedupeValues,
209
- dedupeMinBytes: options.dedupeMinBytes,
210
- domainRefs,
211
- });
212
-
213
- if (options.out) {
214
- await writeFile(options.out, `${output}\n`, "utf8");
215
- return;
216
- }
217
- console.log(output);
218
- }
219
-
220
- await main().catch((error) => {
221
- const message = error instanceof Error ? error.message : String(error);
222
- console.error(`rex:compile error: ${message}`);
223
- process.exit(1);
224
- });