Files
ww/lib/fmt/fmt.ww
Hojun-Cho 1ac1d985f6 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.
2026-05-12 00:45:18 +09:00

77 lines
2.1 KiB
Plaintext

// fmt — minimal formatting writers. All output goes through os.write
// to fd 1 (stdout). No printf-family yet — we don't have varargs in
// the language proper — but the typed entry points cover the common
// cases.
use os;
use strconv;
export fn print(s: str) i64 = {
return os.write(1, s.ptr, s.len: u64);
};
export fn println(s: str) i64 = {
let n: i64 = os.write(1, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(1, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
export fn printint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
export fn printlnint(v: i64) void = {
printint(v);
os.write(1, "\n".ptr, 1u64);
};
// errorln — write a message to stderr with a trailing newline.
export fn errorln(s: str) i64 = {
let n: i64 = os.write(2, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(2, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
// fprint / fprintln — same as print/println but on an arbitrary fd.
// Used by the compiler to write to its -o output file.
export fn fprint(fd: i32, s: str) i64 = {
return os.write(fd, s.ptr, s.len: u64);
};
export fn fprintln(fd: i32, s: str) i64 = {
let n: i64 = os.write(fd, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(fd, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
export fn fprintint(fd: i32, v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(fd, buf.ptr, n: u64);
};
// errpos — write "<file>:<line>:<col>: <msg>\n" to fd 2. The shape
// every compiler diagnostic uses; centralised so the format stays
// consistent across phases.
export fn errpos(file: str, line: i32, col: i32, msg: str) void = {
os.write(2, file.ptr, file.len: u64);
os.write(2, ":".ptr, 1u64);
let buf: [32]u8;
let n: i32 = strconv.i64tos(buf[0:32], line: i64);
os.write(2, buf.ptr, n: u64);
os.write(2, ":".ptr, 1u64);
n = strconv.i64tos(buf[0:32], col: i64);
os.write(2, buf.ptr, n: u64);
os.write(2, ": ".ptr, 2u64);
os.write(2, msg.ptr, msg.len: u64);
os.write(2, "\n".ptr, 1u64);
};