lib/ww/lex: fputq + tokprint if-ladders -> switch (Wave-2 structural)

fputq: 6-deep escape if/else -> switch (c); named escapes \\ " \n \t \r
as explicit cases, empty-label default folds the control-char arms into
(c<' '||c==127)->fputhex2 else fputcbyte, mirroring cmd/wcc/tok.c:172.
tokprint: 4-deep kind if-ladder -> switch (t.kind) with comma-cases
(IDENT,STR,ERR -> fputq; INT,RUNE -> u64tos; default -> nothing),
mirroring tok.c:194.

toktest gains @test tokprint_cases: drives tokprint over a temp fd and
packs every fputq escape (\\ " \n \t \r, c<0x20, c==0x7f, printable)
into a TK_STR text — branches 990_selfhost's corpus never reaches.

w6c + wwdump combined.ww regenerated.
This commit is contained in:
2026-06-03 00:59:13 +09:00
parent 2303341cb0
commit 96cf957b07
4 changed files with 119 additions and 119 deletions

View File

@@ -6710,33 +6710,17 @@ fn fputq(fd: i32, p: *u8, n: i32) void = {
let i: i32 = 0;
for (i < n) {
let c: u8 = p[i];
if (c == '\\') {
fputsstr(fd, "\\\\");
} else {
if (c == '"') {
fputsstr(fd, "\\\"");
switch (c) {
case '\\': fputsstr(fd, "\\\\");
case '"': fputsstr(fd, "\\\"");
case '\n': fputsstr(fd, "\\n");
case '\t': fputsstr(fd, "\\t");
case '\r': fputsstr(fd, "\\r");
case:
if (c < ' ' || c == 127u8) {
fputhex2(fd, c);
} else {
if (c == '\n') {
fputsstr(fd, "\\n");
} else {
if (c == '\t') {
fputsstr(fd, "\\t");
} else {
if (c == '\r') {
fputsstr(fd, "\\r");
} else {
if (c < ' ') {
fputhex2(fd, c);
} else {
if (c == 127u8) {
fputhex2(fd, c);
} else {
fputcbyte(fd, c);
};
};
};
};
};
fputcbyte(fd, c);
};
};
i += 1;
@@ -6770,24 +6754,15 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputcbyte(fd, ' ');
fputsstr(fd, tokname(t.kind));
if (t.kind == tkind.TK_IDENT) {
switch (t.kind) {
case tkind.TK_IDENT, tkind.TK_STR, tkind.TK_ERR:
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_STR) {
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_ERR) {
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_INT) {
case tkind.TK_INT, tkind.TK_RUNE:
fputcbyte(fd, ' ');
let us: str = strconv.u64tos(t.uval, strconv.base.DEC);
os.write(fd, us.ptr, us.len: u64);
} else { if (t.kind == tkind.TK_RUNE) {
fputcbyte(fd, ' ');
let us: str = strconv.u64tos(t.uval, strconv.base.DEC);
os.write(fd, us.ptr, us.len: u64);
};};};};};
};
// tkind.TK_FLOAT is intentionally not handled here — %g formatting
// won't byte-match across implementations. Diff fixtures must
// be float-free until we implement a stable float formatter.

View File

@@ -6710,33 +6710,17 @@ fn fputq(fd: i32, p: *u8, n: i32) void = {
let i: i32 = 0;
for (i < n) {
let c: u8 = p[i];
if (c == '\\') {
fputsstr(fd, "\\\\");
} else {
if (c == '"') {
fputsstr(fd, "\\\"");
switch (c) {
case '\\': fputsstr(fd, "\\\\");
case '"': fputsstr(fd, "\\\"");
case '\n': fputsstr(fd, "\\n");
case '\t': fputsstr(fd, "\\t");
case '\r': fputsstr(fd, "\\r");
case:
if (c < ' ' || c == 127u8) {
fputhex2(fd, c);
} else {
if (c == '\n') {
fputsstr(fd, "\\n");
} else {
if (c == '\t') {
fputsstr(fd, "\\t");
} else {
if (c == '\r') {
fputsstr(fd, "\\r");
} else {
if (c < ' ') {
fputhex2(fd, c);
} else {
if (c == 127u8) {
fputhex2(fd, c);
} else {
fputcbyte(fd, c);
};
};
};
};
};
fputcbyte(fd, c);
};
};
i += 1;
@@ -6770,24 +6754,15 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputcbyte(fd, ' ');
fputsstr(fd, tokname(t.kind));
if (t.kind == tkind.TK_IDENT) {
switch (t.kind) {
case tkind.TK_IDENT, tkind.TK_STR, tkind.TK_ERR:
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_STR) {
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_ERR) {
fputcbyte(fd, ' ');
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_INT) {
case tkind.TK_INT, tkind.TK_RUNE:
fputcbyte(fd, ' ');
let us: str = strconv.u64tos(t.uval, strconv.base.DEC);
os.write(fd, us.ptr, us.len: u64);
} else { if (t.kind == tkind.TK_RUNE) {
fputcbyte(fd, ' ');
let us: str = strconv.u64tos(t.uval, strconv.base.DEC);
os.write(fd, us.ptr, us.len: u64);
};};};};};
};
// tkind.TK_FLOAT is intentionally not handled here — %g formatting
// won't byte-match across implementations. Diff fixtures must
// be float-free until we implement a stable float formatter.