Files
ww/lib/fmt/fmt.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

44 lines
1.3 KiB
Plaintext

// fmt — minimal formatting writers. All output goes through os.write
// to a file descriptor. No printf-family yet — we don't have varargs
// in the language proper — so callers compose with strconv.i64tos /
// strings.concat to build the message and then call print / println.
// Hare's `fmt::println(42)` becomes `fmt.println(strconv.i64tos(42,
// strconv.DEC))`.
use os;
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;
};
// 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;
};