Files
ww/cmd/wwdump/main.c
Hojun-Cho 2c33228b7e ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
2026-05-11 13:49:27 +09:00

120 lines
2.8 KiB
C

/*
* wwdump — deterministic dump tool for ww source.
*
* Reads a .ww file and writes either a token stream or an AST
* s-expression to stdout, byte-for-byte stable across runs. It is the
* diff anchor for self-host: the C-side libwcc and the future ww-side
* frontend must produce the same dump for the same input.
*
* wwdump -t file.ww tokens, one per line: "<file>:<l>:<c> <kind> [val]"
* wwdump -a file.ww AST as s-expr, one node per line
*
* No newlines or trailing whitespace varies by phase of the moon. If
* the bytes differ, somebody changed the front end.
*/
#include "ww.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
slurp(const char *path, char **outbuf, u64 *outlen)
{
FILE *f = fopen(path, "rb");
if (f == NULL) return -1;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n < 0) { fclose(f); return -1; }
char *b = malloc((size_t)n + 1);
if (b == NULL) { fclose(f); return -1; }
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
b[n] = '\0';
fclose(f);
*outbuf = b;
*outlen = (u64)n;
return 0;
}
static int
dump_tokens(const char *src, char *buf, u64 len, FILE *out)
{
Arena *a = newarena();
Lex l;
lexinit(&l, a, src, buf, len);
for (;;) {
Tok t = lexnext(&l);
tokprint(out, t);
if (t.kind == TK_EOF || t.kind == TK_ERR) break;
}
int errs = l.errs;
freearena(a);
return errs ? 1 : 0;
}
static int
dump_ast(const char *src, char *buf, u64 len, FILE *out)
{
Arena *a = newarena();
Lex l;
Parser p;
lexinit(&l, a, src, buf, len);
parserinit(&p, a, &l);
Node *file = parsefile(&p);
int errs = l.errs || p.errs;
if (file) astprint(out, file);
freearena(a);
return errs ? 1 : 0;
}
int
main(int argc, char **argv)
{
int mode = 't'; /* tokens by default */
const char *src = NULL;
const char *out = NULL;
for (int i = 1; i < argc; i++) {
const char *a = argv[i];
if (strcmp(a, "-t") == 0) mode = 't';
else if (strcmp(a, "-a") == 0) mode = 'a';
else if (strcmp(a, "-o") == 0 && i + 1 < argc) out = argv[++i];
else if (a[0] == '-') {
fprintf(stderr, "wwdump: unknown flag %s\n", a);
return 2;
}
else if (src == NULL) src = a;
else {
fputs("wwdump: only one input supported\n", stderr);
return 2;
}
}
if (src == NULL) {
fputs("usage: wwdump [-t|-a] [-o out] file.ww\n", stderr);
return 2;
}
char *buf;
u64 len;
if (slurp(src, &buf, &len) < 0) {
fprintf(stderr, "wwdump: %s: cannot read\n", src);
return 1;
}
FILE *of = stdout;
if (out) {
of = fopen(out, "wb");
if (of == NULL) {
fprintf(stderr, "wwdump: cannot open %s\n", out);
return 1;
}
}
int rc;
if (mode == 'a') rc = dump_ast(src, buf, len, of);
else rc = dump_tokens(src, buf, len, of);
if (of != stdout) fclose(of);
free(buf);
return rc;
}