lib: rename stdlib surface to Hare names; add endian/math

Sweeping rename so the lib/ surface mirrors Hare's stdlib spellings.
- ascii: rune-taking predicates; ishex -> isxdigit
- bufio: rinit -> init; take1/takeline -> readbyte/readline
- bytes: indexsub -> index
- encoding/utf8: runelen -> runesz
- errors: eEOF/eShortRead/... -> eof/underread/...
- fmt: errln -> errorln; println/fprintln return i64
- os: readfull/writefull -> readall/writeall; unlink -> remove
- path: isabs -> abs; drop lastindex (now strings.rbyteindex)
- strconv: u64toa/i64toa -> u64tos/i64tos; parse64/parseu64 -> stoi64/stou64
- strings: drop len/isempty; equal -> compare; indexbyte -> byteindex; +rbyteindex
- types: drop numeric helpers (moved to math)
- new lib/endian (htonu16/ntohu16), lib/math (absi32/absi64)
- net: drop htons (use endian.htonu16)

Callers in selfhost/, lib/ww/, cmd/w6c/cgen.c, and test/wcc/700_e2e.c
updated to match.
This commit is contained in:
2026-05-12 00:45:18 +09:00
parent 35421f2561
commit 1ac1d985f6
37 changed files with 597 additions and 568 deletions

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -321,14 +324,16 @@ export fn freearena(a: *arena) void = {
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
// tagged-union work; kept for callers that already use it.
// - Hare style (parse64/parseu64): `(value | str)`. The error
// - Hare style (stoi64/stou64): `(value | str)`. The error
// variant carries a short, allocation-free message describing
// why the parse failed. Prefer this for new code.
// u64toa — write `v` in decimal into `buf` and return the byte count.
// Unsigned-only so callers don't have to think about wraparound when
// printing a u64 that happens to have the high bit set.
export fn u64toa(buf: []u8, v: u64) i32 = {
// u64tos — write `v` in decimal into `buf` and return the byte count.
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
// have to think about wraparound when printing a u64 with the high
// bit set.
export fn u64tos(buf: []u8, v: u64) i32 = {
let tmp: [32]u8;
let i: i32 = 0;
let n: u64 = v;
@@ -350,7 +355,7 @@ export fn u64toa(buf: []u8, v: u64) i32 = {
return out;
};
export fn i64toa(buf: []u8, v: i64) i32 = {
export fn i64tos(buf: []u8, v: i64) i32 = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) {
@@ -400,11 +405,12 @@ export fn atoi64(s: str) (i64, bool) = {
return v, true;
};
// parse64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str describing the
// reason. No locale, no whitespace, no underscores: a leading '-' is
// the only non-digit accepted, and only at position 0.
export fn parse64(s: str) (i64 | str) = {
// stoi64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str (subset of Hare's
// (invalid | overflow) tagged-union). No locale, no whitespace, no
// underscores: a leading '-' is the only non-digit accepted, and only
// at position 0.
export fn stoi64(s: str) (i64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let i: i32 = 0;
let neg: bool = false;
@@ -422,8 +428,9 @@ export fn parse64(s: str) (i64 | str) = {
return v;
};
// parseu64 — fallible unsigned decimal parser. No leading sign.
export fn parseu64(s: str) (u64 | str) = {
// stou64 — fallible unsigned decimal parser. No leading sign. Mirrors
// Hare's strconv::stou64.
export fn stou64(s: str) (u64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let v: u64 = 0u64;
let i: i32 = 0;
@@ -812,10 +819,10 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
fputcbyte(fd, 58u8); // ':'
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
let n: i32 = strconv.i64tos(buf[0:32], t.line: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 58u8);
n = strconv.i64toa(buf[0:32], t.col: i64);
n = strconv.i64tos(buf[0:32], t.col: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
@@ -831,11 +838,11 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_INT) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
} else { if (t.kind == TK_RUNE) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
};};};};};
// TK_FLOAT is intentionally not handled here — %g formatting
@@ -846,96 +853,96 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
// MODULE: ascii
// ascii — byte-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family. Bytes outside 0..127 always
// answer `false`. The lexer hot path uses these inline; they are
// expected to inline to a couple of compares.
// ascii — rune-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
// outside 0..127 always answer `false`. The lexer hot path uses these
// inline; they are expected to inline to a couple of compares.
export fn isdigit(c: u8) bool = {
if (c < 48u8) { return false; };
if (c > 57u8) { return false; };
export fn isdigit(c: rune) bool = {
if (c < 48) { return false; };
if (c > 57) { return false; };
return true;
};
export fn isupper(c: u8) bool = {
if (c < 65u8) { return false; };
if (c > 90u8) { return false; };
export fn isupper(c: rune) bool = {
if (c < 65) { return false; };
if (c > 90) { return false; };
return true;
};
export fn islower(c: u8) bool = {
if (c < 97u8) { return false; };
if (c > 122u8) { return false; };
export fn islower(c: rune) bool = {
if (c < 97) { return false; };
if (c > 122) { return false; };
return true;
};
export fn isalpha(c: u8) bool = {
export fn isalpha(c: rune) bool = {
if (isupper(c)) { return true; };
return islower(c);
};
export fn isalnum(c: u8) bool = {
export fn isalnum(c: rune) bool = {
if (isalpha(c)) { return true; };
return isdigit(c);
};
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
export fn isspace(c: u8) bool = {
if (c == 32u8) { return true; }; // ' '
if (c == 9u8) { return true; }; // '\t'
if (c == 10u8) { return true; }; // '\n'
if (c == 11u8) { return true; }; // '\v'
if (c == 12u8) { return true; }; // '\f'
if (c == 13u8) { return true; }; // '\r'
export fn isspace(c: rune) bool = {
if (c == 32) { return true; }; // ' '
if (c == 9) { return true; }; // '\t'
if (c == 10) { return true; }; // '\n'
if (c == 11) { return true; }; // '\v'
if (c == 12) { return true; }; // '\f'
if (c == 13) { return true; }; // '\r'
return false;
};
export fn ishex(c: u8) bool = {
export fn isxdigit(c: rune) bool = {
if (isdigit(c)) { return true; };
if (c >= 65u8) {
if (c <= 70u8) { return true; }; // 'A'..'F'
if (c >= 65) {
if (c <= 70) { return true; }; // 'A'..'F'
};
if (c >= 97u8) {
if (c <= 102u8) { return true; }; // 'a'..'f'
if (c >= 97) {
if (c <= 102) { return true; }; // 'a'..'f'
};
return false;
};
// digitval — value of `c` as a hex/decimal digit, or -1 if not one.
// Useful when scanning numeric literals.
export fn digitval(c: u8) i32 = {
if (isdigit(c)) { return (c - 48u8): i32; };
if (c >= 65u8) {
if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; };
export fn digitval(c: rune) i32 = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97u8) {
if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; };
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return -1;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: u8) bool = {
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95u8) { return true; }; // '_'
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: u8) bool = {
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95u8) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: u8) u8 = {
if (isupper(c)) { return c + 32u8; };
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
return c;
};
export fn toupper(c: u8) u8 = {
if (islower(c)) { return c - 32u8; };
export fn toupper(c: rune) rune = {
if (islower(c)) { return c - 32; };
return c;
};
@@ -1172,18 +1179,18 @@ fn escape(l: *lex, out: *i32) bool = {
let lo: i32 = lget(l);
if (hi < 0) { return false; };
if (lo < 0) { return false; };
if (!ascii.ishex(hi: u8)) {
if (!ascii.isxdigit(hi: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
if (!ascii.ishex(lo: u8)) {
if (!ascii.isxdigit(lo: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
let h: i32 = ascii.digitval(hi: u8);
let lv: i32 = ascii.digitval(lo: u8);
let h: i32 = ascii.digitval(hi: rune);
let lv: i32 = ascii.digitval(lo: rune);
*out = (h << 4) | lv;
return true;
};
@@ -1197,7 +1204,7 @@ fn scandecimalrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) {
if (!ascii.isdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1208,7 +1215,7 @@ fn scanhexrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.ishex(c: u8)) {
if (!ascii.isxdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1247,7 +1254,7 @@ fn scanexp(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) { break; };
if (!ascii.isdigit(c: rune)) { break; };
lget(l);
};
};
@@ -1332,12 +1339,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: u8)) {
if (ascii.isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: u8)) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -1381,7 +1388,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: u8)) { break; };
if (!ascii.isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -1530,8 +1537,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};
if (c == 34) { lget(l); lexstr(l, &start, out); return; };
@@ -1895,12 +1902,12 @@ fn pr(fd: i32, n: *node, d: i32) void = {
if (n.kind == N_INTLIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (n.kind == N_RUNELIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (
n.kind == N_STRLIT ||
@@ -7408,13 +7415,13 @@ fn emitline(s: str) void = { os.write(1, s.ptr, s.len: u64); };
fn emitint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
fn emituint(v: u64) void = {
let buf: [32]u8;
let n: i32 = strconv.u64toa(buf[0:32], v);
let n: i32 = strconv.u64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
@@ -7452,7 +7459,7 @@ fn mklabel(c: *cgen, base: str) str = {
i += 1; j += 1;
};
buf[i] = 95u8; i += 1; // '_'
let n: i32 = strconv.i64toa(buf[i:128], c.labelseq: i64);
let n: i32 = strconv.i64tos(buf[i:128], c.labelseq: i64);
c.labelseq += 1;
let total: i32 = i + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7491,7 +7498,7 @@ fn internstrlit(c: *cgen, bytes: str) str = {
// New label "_S_<seq>".
let buf: [32]u8;
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
let n: i32 = strconv.i64tos(buf[3:32], c.strlitseq: i64);
c.strlitseq += 1;
let total: i32 = 3 + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7979,7 +7986,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readfull(fd, buf, sz: u64);
let r: i64 = os.readall(fd, buf, sz: u64);
os.close(fd);
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
@@ -8018,11 +8025,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], ck.nresolved: i64);
let n: i32 = strconv.i64tos(buf[0:32], ck.nresolved: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
n = strconv.i64toa(buf[0:32], total: i64);
n = strconv.i64tos(buf[0:32], total: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };

View File

@@ -95,7 +95,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readfull(fd, buf, sz: u64);
let r: i64 = os.readall(fd, buf, sz: u64);
os.close(fd);
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
@@ -134,11 +134,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], ck.nresolved: i64);
let n: i32 = strconv.i64tos(buf[0:32], ck.nresolved: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
n = strconv.i64toa(buf[0:32], total: i64);
n = strconv.i64tos(buf[0:32], total: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };