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