w6c+selfhost+lib: Hare-style variadic call sites
Param-decl `name: T...` (Tparam.variadic=1, type []T), call-site
gather of N args into a fresh `[N]T`, forward via `xs...`, full
selfhost mirror, and lib/fmt graduated to the Hare shape.
Frontend:
- parse: `T...` after a param's type stamps Node.op=TK_ELLIPSIS
and breaks out (variadic must be last).
- check: resolve_type N_TFN / build_fn_type wrap the param type
as []T and set tp->variadic. N_CALL accepts either a tail of
args assignable to T (gather) or a single `xs...` spread of
[]T (forward); both bypass the "too many args" check on the
variadic slot.
- type: type_eq compares Tparam.variadic.
Cgen (cstage):
- call site: when the callee has a variadic last param,
materialise the tail args into a frame-resident `[N]T` via
localoff, write a 24B slice descriptor (ptr,len,cap), and
splice a synthesised N_IDENT into args[] so the downstream
widen/eval/pop loops see one slice slot. Tagged-element types
route each store through cg_widen_tagged_store. Forwarding
skips gather: the N_SPREAD wrapper is replaced with its inner
slice expression. Empty form writes {nil,0,0}. args[] / widen[]
bump from 16 to 64 to accommodate Hare's mixed-arg printers.
Selfhost mirror:
- lib/ww/parse: `T...` mark on N_PARAM.op.
- cgen: varargseq counter on Cg; scanlocals reserves
@vararg_d_N + @vararg_sl_N per variadic call (seq recorded on
N_CALL.uval so cgcall picks the same names). cgcall does the
same gather/forward and N_IDENT splice. cgfnparams treats
variadic params as 24B slice slots via a synthesised TSLICE
tnode. pushargsrev skips the tagged-widen detection for
variadic params (effective type is []T, not tagged).
- rhstargetname now recognises N_TRUE/N_FALSE/N_RUNELIT and
typed N_INTLIT so the variant-tag lookup finds bool/rune/iN
variants instead of falling through to "first non-str" (which
misassigned tag 0 to bool in tagged unions like formattable).
lib/fmt graduated: print/println/fprint/fprintln/errorln/fatal
take `args: formattable...`. Bare `error` (no -ln) is skipped —
the leaf name collides with strconv's `type error = !(invalid |
overflow)` under the driver's flat namespace.
Tests: 5 new e2e rows (plain gather, zero-arg, tagged element,
forwarding, fmt.println end-to-end). lib/CLAUDE.md workaround
paragraph replaced with the Hare-shape description.
This commit is contained in:
120
lib/fmt/fmt.ww
120
lib/fmt/fmt.ww
@@ -1,37 +1,30 @@
|
||||
// fmt — formatting writers. Goes through os.write to a file
|
||||
// descriptor. Hare's variadic call-site sugar (`fmt::println(42)`
|
||||
// gathering args into a `[]formattable`) isn't wired yet; until then,
|
||||
// callers either:
|
||||
//
|
||||
// 1. Hand-build the slice:
|
||||
// let args: [2]formattable;
|
||||
// args[0] = 42i64: formattable;
|
||||
// args[1] = " hi": formattable;
|
||||
// fmt.print(args[0:2]);
|
||||
//
|
||||
// 2. Compose a single str via strconv.i64tos / strings.concat:
|
||||
// fmt.println(strconv.i64tos(42, strconv.base.DEC));
|
||||
//
|
||||
// `print(s: str)` keeps the single-string form for the common case.
|
||||
// fmt — formatting writers. Mirrors Hare's lib/fmt subset that fmt-
|
||||
// prints values via [[io::handle]]-style fd writers. Call sites take
|
||||
// Hare's variadic shape: `fmt.println(42, "hi", true)` gathers the
|
||||
// args into a `[]formattable` slice; wrappers forward via `args...`.
|
||||
|
||||
use os;
|
||||
use strconv;
|
||||
use strings;
|
||||
|
||||
// formattable — tagged union of types fmt can render. Mirrors
|
||||
// Hare's `fmt::formattable = (...types::numeric | uintptr | str |
|
||||
// rune | bool | nullable *opaque | void)`, narrowed to the set ww
|
||||
// actually has codegen for. Slot size is 24B (8 tag + 16 str
|
||||
// payload).
|
||||
// formattable — tagged union of types fmt can render. Mirrors Hare's
|
||||
// `fmt::formattable = (...types::numeric | uintptr | str | rune |
|
||||
// bool | nullable *opaque | void)`, narrowed to the set ww actually
|
||||
// has codegen for. Slot size is 24B (8 tag + 16 str payload).
|
||||
export type formattable = (i64 | str | bool | rune);
|
||||
|
||||
// vprint — write the formatted form of each element of `args` to
|
||||
// `fd`. Returns total bytes written or the first negative os.write
|
||||
// result.
|
||||
fn vprint(fd: i32, args: []formattable) i64 = {
|
||||
// fprint — write the formatted form of each `args` element to `fd`,
|
||||
// separated by spaces. Returns total bytes written or the first
|
||||
// negative os.write result. Hare's separator-by-space matches.
|
||||
export fn fprint(fd: i32, args: formattable...) i64 = {
|
||||
let total: i64 = 0;
|
||||
let i: i32 = 0;
|
||||
for (i < args.len) {
|
||||
if (i > 0) {
|
||||
let r: i64 = os.write(fd, " ".ptr, 1u64);
|
||||
if (r < 0) { return r; };
|
||||
total += r;
|
||||
};
|
||||
match (args[i]) {
|
||||
case let n: i64 => {
|
||||
let s: str = strconv.i64tos(n, strconv.base.DEC);
|
||||
@@ -64,55 +57,40 @@ fn vprint(fd: i32, args: []formattable) i64 = {
|
||||
return total;
|
||||
};
|
||||
|
||||
// print(s: str) — single-string form for the common case. The
|
||||
// variadic-style `print(args: []formattable)` lives as `printv`
|
||||
// until call-site sugar lands.
|
||||
export fn print(s: str) i64 = {
|
||||
return os.write(1, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
// printv — Hare-shaped `print(args: formattable...)` modulo the
|
||||
// call-site sugar. Callers pass an explicit `[]formattable` slice.
|
||||
export fn printv(args: []formattable) i64 = {
|
||||
return vprint(1, args);
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
// printlnv — like printv but adds a trailing newline.
|
||||
export fn printlnv(args: []formattable) i64 = {
|
||||
let n: i64 = vprint(1, args);
|
||||
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);
|
||||
// fprintln — fprint plus a trailing newline.
|
||||
export fn fprintln(fd: i32, args: formattable...) i64 = {
|
||||
let n: i64 = fprint(fd, args...);
|
||||
if (n < 0) { return n; };
|
||||
let m: i64 = os.write(fd, "\n".ptr, 1u64);
|
||||
if (m < 0) { return m; };
|
||||
return n + m;
|
||||
};
|
||||
|
||||
// print / println — fprint / fprintln on stdout. Direct counterparts
|
||||
// of Hare's fmt::print / fmt::println.
|
||||
export fn print(args: formattable...) i64 = {
|
||||
return fprint(1, args...);
|
||||
};
|
||||
|
||||
export fn println(args: formattable...) i64 = {
|
||||
return fprintln(1, args...);
|
||||
};
|
||||
|
||||
// errorln — fprintln on stderr. Hare's `fmt::error` (without -ln) is
|
||||
// skipped here: the bare `error` name collides with strconv's
|
||||
// `type error = !(invalid | overflow)` under the driver's flat
|
||||
// concatenation namespace. Callers wanting the no-newline form use
|
||||
// `fprint(2, args...)` directly.
|
||||
export fn errorln(args: formattable...) i64 = {
|
||||
return fprintln(2, args...);
|
||||
};
|
||||
|
||||
// fatal — errorln then exit(255). `never` return marks the bottom
|
||||
// type so flow-control checks treat callers as terminated. The
|
||||
// fprintln result is dropped as an expression statement (Hare's
|
||||
// `_ = fprintln(...)` wouldn't add safety here — process exit
|
||||
// follows immediately).
|
||||
export fn fatal(args: formattable...) never = {
|
||||
fprintln(2, args...);
|
||||
os.exit(255);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user