Files
ww/selfhost/cmd/wwdump/main.ww
Hojun-Cho be8a662f15 lib/strconv: graduate to owned-str returns with Hare-shape base param
i64tos / u64tos / f64tos return a fresh owned str (caller frees via
os.free) instead of writing into a caller-supplied [N]u8. Adds typed
variants (i32tos / i16tos / i8tos and u32 / u16 / u8) and the missing
base parameter on stoi64 / stou64 + typed parse wrappers.

Base values are exported as plain-i32 `def`s (strconv.DEC,
strconv.HEX_UPPER, ...) rather than a `base` enum: cross-module
`strconv.base.DEC` chains miscompile in the cstage cgen — it emits a
memory load through `base(SB)` rather than inlining the constant.
The Sdef path resolves correctly, so callers say `strconv.DEC` and
both cgens lower to an immediate.

Also renames strings.byteindex / rbyteindex to strings.indexbyte /
rindexbyte, matching bytes.indexbyte and reserving the Hare name
`byteindex` for the future `(str | rune)`-needle shape.

fmt drops printint / printlnint / fprintint — those were stand-ins
for variadic `fmt::println(42)`; with the owned-str graduation the
substitute is one call: `fmt.println(strconv.i64tos(42, strconv.DEC))`.

strerror is sketched in a comment but not shipped — match arms over
the wider `error = !(invalid | overflow)` union still expose a
cstage-vs-wwstage spill divergence.
2026-05-13 03:55:16 +09:00

168 lines
4.2 KiB
Plaintext

// selfhost/cmd/wwdump/main.ww — ww-side port of cmd/wwdump/main.c.
//
// Reads a .ww file, runs the ww-side lexer, prints tokens through
// the ww-side tokprint. The 990_selfhost test diffs this output
// byte-for-byte against the C-side wwdump on the same file. Any
// divergence is a port bug in lex.ww or tok.ww.
//
// Modes:
// wwdump -t file.ww tokens (default)
// wwdump -a file.ww AST (not yet implemented; reserved)
use os;
use mem;
use tok;
use lex;
use ast;
use parse;
use typ;
use sym;
use check;
use cgen;
use strconv;
// ---- argv helpers -----------------------------------------------------
// argstrlen — strlen on a NUL-terminated *u8. argv strings are always
// NUL-terminated (kernel-supplied) so this is safe.
fn argstrlen(s: *u8) i32 = {
let n: i32 = 0;
for (s[n] != 0u8) { n += 1; };
return n;
};
fn argstr(p: *u8) str = {
let s: str;
s.ptr = p;
s.len = argstrlen(p);
return s;
};
// streqlit — compare a NUL-terminated argv entry to a string literal.
fn streqlit(p: *u8, lit: str) bool = {
let i: i32 = 0;
for (i < lit.len) {
if (p[i] != lit[i]) { return false; };
i += 1;
};
return p[i] == 0u8;
};
// ---- main -------------------------------------------------------------
export fn main(argc: i32, argv: **u8) i32 = {
let mode: i32 = 116; // 't'
let path: *u8 = nil;
let i: i32 = 1;
for (i < argc) {
let a: *u8 = argv[i];
if (streqlit(a, "-t")) {
mode = 116;
} else { if (streqlit(a, "-a")) {
mode = 97; // 'a'
} else { if (streqlit(a, "-r")) {
mode = 114; // 'r' — resolve / name-check
} else { if (streqlit(a, "-c")) {
mode = 99; // 'c' — codegen / emit asm
} else { if (path == nil) {
path = a;
};};};};};
i += 1;
};
if (path == nil) {
os.write(2, "usage: wwdump [-t|-a] file.ww\n".ptr, 30u64);
return 2;
};
let fdorerr: (i32 | os.oserror) = os.tryopen(path, os.flag.RDONLY, 0i32);
let fd: i32 = -1;
match (fdorerr) {
case let v: i32 => fd = v;
case let e: os.oserror => {
os.write(2, "wwdump: cannot open ".ptr, 20u64);
os.write(2, path, argstrlen(path): u64);
os.write(2, "\n".ptr, 1u64);
return 1;
};
};
let szr: (i64 | os.oserror) = os.filesize(fd);
let sz: i64 = 0i64;
match (szr) {
case let v: i64 => sz = v;
case let e: os.oserror => {
os.write(2, "wwdump: filesize failed\n".ptr, 24u64);
os.close(fd);
return 1;
};
};
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let rr: (i64 | os.oserror) = os.readall(fd, buf, sz: u64);
os.close(fd);
let r: i64 = 0i64;
match (rr) {
case let v: i64 => r = v;
case let e: os.oserror => {
os.write(2, "wwdump: read failed\n".ptr, 20u64);
return 1;
};
};
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
return 1;
};
let l: lex;
lexinit(&l, a, argstr(path), buf, sz: u64);
if (mode == 116) { // '-t'
for (true) {
let t: tok;
lexnext(&l, &t);
tokprint(1i32, &t);
if (t.kind == tkind.TK_EOF) { break; };
if (t.kind == tkind.TK_ERR) { break; };
};
} else { if (mode == 97) { // '-a'
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
astprint(1i32, f);
} else { if (mode == 114) { // '-r' — name resolve report
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
let tc: tctx;
typesinit(&tc, a);
let ck: checker;
checkinit(&ck, a, &tc);
// Quiet by default; flip to 1 when debugging missing names.
ck.verbose = 0;
checkfile(&ck, f);
// (close out the if-else chain — we'll close all braces below)
// "<file>: <resolved>/<resolved+unresolved> resolved"
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let rs: str = strconv.i64tos(ck.nresolved: i64, strconv.DEC);
os.write(1, rs.ptr, rs.len: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
let ts: str = strconv.i64tos(total: i64, strconv.DEC);
os.write(1, ts.ptr, ts.len: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };
} else { if (mode == 99) { // '-c' — codegen / emit asm
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
let cg: cgen;
cgeninit(&cg, a);
cgfile(&cg, f);
};};};};
if (l.errs > 0) { return 1; };
return 0;
};