w6c+selfhost+lib: cgen quality batch + lib Hare-shape graduation

Six fixes across the toolchain, surfaced by lib/lisp porting work.

  1. f64 compound assigns (`acc += d`, `-=`, `*=`, `/=`). Both stages
     load slot → X1, OP X0 into X1, store back (ADDSD/SUBSD/MULSD/
     DIVSD are reg-reg only). Previous MOVSD-overwrite dropped the
     OP. Locals and top-level lets.

  2. Top-level `[N]u8` arrays + `&arr[i]`. let_emit_size grows a
     TY_ARRAY branch so zero-init DATAW lands; cgindex / N_INDEX
     store / `&base[i]` all detect a global array base and use
     LEAQ name(SB) instead of LEAQ (BP). TK_AMP no longer pre-
     evaluates the operand as a value-load — `&base[i]` computes
     base + i*esz directly. Unblocks Hare's static-buffer pattern:
     strconv.{u64,i64,f64}tos graduate to module-level `*_buf`
     arrays and return owned views.

  3. Cross-module `pkg.Enum.MEMBER`. Nested N_DOT chains that
     don't fold to a known shape now emit `MOVQ <leaf>(SB), AX`
     (mirrors the bare-IDENT unresolved fallback), so isolation
     probes — and the test 990 cgen-match floor — stay consistent
     across stages. strconv exposes `base` as a real `enum i32`;
     callers updated. The `main` exemption (linker entry-point
     keeps bare name even when not exported) mirrors C-side
     collectmods into selfhost cgendecl.

  4. Sum-typed parameter ABI. lib/bytes.{index,rindex} take
     `(u8 | []u8)` needle; lib/strings.byteindex / rbyteindex take
     `(str | rune)` needle (Hare-shaped; the byte-wise misnomer
     `index` is dropped). tagged_arg_size cap bumps to 48 (6 int
     regs), with a new partial-fit branch on the callee: when an
     N-word tagged arg overflows remaining regs, fill what fits and
     stitch the rest from positive BP offsets. scanlocals MCASE
     handles slice binds (24B) and walks each arm with a saved /
     restored seenmark set so two arms naming the same local each
     get their own slot — matches cstage's per-arm scope reset.

  5. 4-reg tagged-return ABI (AX=tag, DX=word0, CX=word1, R8=word2),
     up from 3 regs. Slice-payload variants (`([]T | E)`, slot 32B)
     round-trip ptr/len/cap end-to-end. Every receive site updates:
     let-init via cgwidentaggedstore, match scrutinee spill, cgindex
     tagged-element load (both N_IDENT and fallback bases),
     pushargsrev tagged-ident arg (reads word count from slot size),
     cgreturn slice variant in the shuffle path.

  6. `expr: TaggedAlias` is a widening, not a re-interpret. C cgen +
     selfhost cgwidentaggedstore peel an N_CAST whose destination IS
     the union — so cgexpr's natural shape (str: AX=ptr, BX=len;
     slice: AX=ptr, BX=len, CX=cap) is consumed by the matching
     concrete-variant branch instead of being misread as a tagged
     AX/DX/CX triple. Inner casts to a concrete variant (`7: i32`)
     keep their type for proper tag lookup. `[N]Alias` arrays
     resolve element size via slotsize + aliaslookup, and aliaslookup
     strips a `pkg.` prefix so cross-module references work.

lib/fmt grows `formattable = (i64 | str | bool | rune)` plus
`printv` / `printlnv` taking an explicit `[]formattable` slice (the
receive side of Hare's `args: formattable...`). Call-site variadic
gather isn't wired — callers either hand-build the slice or compose
strconv.i64tos + strings.concat.

700_e2e: 114 → 123 rows (f64 compound, top-level u8 arrays + `&buf[i]`,
pkg.Enum.MEMBER, sum-typed (str|rune) and (u8|[]u8) params, 4-reg
slice-return ABI, formattable array). 26/26 tests, bootstrap stable
through ww4.
This commit is contained in:
2026-05-13 08:05:01 +09:00
parent 6fd0160c0f
commit 46edb8db4a
23 changed files with 2665 additions and 915 deletions

View File

@@ -13,29 +13,38 @@ Signatures mirror Hare too, modulo:
- Tagged-union returns are spelled with the ww `!` error tag where
Hare uses `!void` / `!T`, and indices use the underlying length
type (`i32` today, since `str.len: i32`). Example:
`strconv.stoi64(s: str, b: i32) (i64 | invalid | overflow)`
same shape as Hare's; the base parameter is plain `i32` rather
than a `base` enum because cross-module `mod.enumtype.VALUE`
chains miscompile in the cstage cgen. `strconv` exports
`def DEC: i32 = 10;` etc. so callers say `strconv.DEC` and the
Sdef path lowers to an immediate.
`strconv.stoi64(s: str, b: strconv.base) (i64 | invalid | overflow)`
same shape as Hare's. The base parameter is the named enum
`strconv.base` (Hare uses `enum uint`; we pick `enum i32` to
match the index type).
- Owned-`str` returns. Where Hare returns `const str` into a
thread-local static buffer (`strconv.i64tos`, `strings.dup`,
`strings.concat`), ww allocates per call and the caller frees
via `os.free(r.ptr, r.len: u64)`. Mutating a module-level `*u8`
doesn't yet round-trip through the wwstage cgen, so the static-
buffer shape isn't expressible today.
- Static-buffer `str` returns where Hare uses them. `strconv.*tos`
returns a `const str` view into a module-level buffer that is
overwritten on the next call to the same function. Callers that
need the bytes to outlive the next call duplicate via
[[strings.dup]]. Functions that genuinely allocate a fresh
buffer (`strings.dup`, `strings.concat`) still return an owned
`str` that callers free via `os.free(r.ptr, r.len: u64)`.
- Variadic ABI doesn't land yet, so callers that Hare writes as
`fmt::println(42)` are spelled `fmt.println(strconv.i64tos(42,
strconv.DEC))` for now. `lib/fmt` is intentionally print-string-
only — no `printf`-family.
- Call-site variadic sugar (`fmt::println(42)`) doesn't land yet.
The receive side does — `fmt.formattable` is a tagged union of
the printable scalar types, and `fmt.printv` / `fmt.printlnv`
take an explicit `[]formattable` slice. Until the call-site
gather is implemented, callers either hand-build the slice:
let args: [2]fmt.formattable;
args[0] = "count: ": fmt.formattable;
args[1] = 42i64: fmt.formattable;
fmt.printlnv(args[0:2]);
or compose to a single str via strconv.i64tos + strings.concat:
fmt.println(strconv.i64tos(42, strconv.base.DEC));
`lib/fmt` is intentionally print-string-only — no `printf`-family.
- `(str | rune)`-style sum-typed parameters are split into typed
pairs (`strings.indexbyte` for the byte case, `strings.index` for
the slice case, etc.). The Hare public name `byteindex` will come
back once the sum-typed parameter ABI lands.
- `(T | U)` sum-typed parameters dispatch via `match` inside the
callee. `strings.byteindex(haystack: str, needle: (str | rune))`,
`bytes.index(s: []u8, needle: (u8 | []u8))`, and `rbyteindex`/
`rindex` follow Hare's shape directly. The rune-indexed
`strings.index` (rune-wise position) isn't shipped yet — we don't
have UTF-8 rune iteration in the language stack.
Don't ship a richer surface than Hare has. A documented subset is
fine; an extension, rename, or convenience-wrapper is not — callers

View File

@@ -10,64 +10,68 @@ export fn equal(a: []u8, b: []u8) bool = {
return i == b.len;
};
// indexbyte — first index of byte `c` in `s`. Hare-shaped optional:
// (i32 | void). void variant indicates "not found".
export fn indexbyte(s: []u8, c: u8) (i32 | void) = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
return;
};
// rindexbyte — last index of byte `c` in `s`. Mirrors Hare's
// bytes::rindex for the byte case.
export fn rindexbyte(s: []u8, c: u8) (i32 | void) = {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
return;
};
// index — first index of `sub` in `s`. Mirrors Hare's bytes::index
// (the []u8 needle variant; the u8 needle stays as indexbyte until we
// have union-arg dispatch). Empty `sub` matches at 0.
export fn index(s: []u8, sub: []u8) (i32 | void) = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// index — first index of `needle` in `s`. Mirrors Hare's bytes::index:
// `u8` needle scans for the byte, `[]u8` needle scans for the
// substring. Returns void if absent.
export fn index(s: []u8, needle: (u8 | []u8)) (i32 | void) = {
match (needle) {
case let c: u8 => {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
if (ok) { return i; };
i += 1;
return;
};
case let sub: []u8 => {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i += 1;
};
return;
};
};
return;
};
// rindex — last index of `sub` in `s`. Mirrors Hare's bytes::rindex
// for the slice case.
export fn rindex(s: []u8, sub: []u8) (i32 | void) = {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// rindex — last index of `needle` in `s`. Mirrors Hare's bytes::rindex.
// Empty []u8 needle matches at s.len.
export fn rindex(s: []u8, needle: (u8 | []u8)) (i32 | void) = {
match (needle) {
case let c: u8 => {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
if (ok) { return i; };
i -= 1;
return;
};
case let sub: []u8 => {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i -= 1;
};
return;
};
};
return;
};

View File

@@ -1,16 +1,82 @@
// 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))`.
// 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.
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).
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 = {
let total: i64 = 0;
let i: i32 = 0;
for (i < args.len) {
match (args[i]) {
case let n: i64 => {
let s: str = strconv.i64tos(n, strconv.base.DEC);
let r: i64 = os.write(fd, s.ptr, s.len: u64);
if (r < 0) { return r; };
total += r;
};
case let s: str => {
let r: i64 = os.write(fd, s.ptr, s.len: u64);
if (r < 0) { return r; };
total += r;
};
case let b: bool => {
let s: str = "false";
if (b) { s = "true"; };
let r: i64 = os.write(fd, s.ptr, s.len: u64);
if (r < 0) { return r; };
total += r;
};
case let r: rune => {
let buf: [4]u8;
buf[0] = r: u8;
let n: i64 = os.write(fd, &buf[0], 1u64);
if (n < 0) { return n; };
total += n;
};
};
i += 1;
};
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; };
@@ -19,6 +85,15 @@ export fn println(s: str) i64 = {
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);

View File

@@ -1,11 +1,10 @@
// strconv — number↔string conversions.
//
// Mirrors Hare's strconv:: surface. The *tos functions return a fresh
// owned `str`; release via os.free(r.ptr, r.len: u64) when done.
// Hare returns `const str` into a static buffer; ww allocates per
// call because the wwstage cgen doesn't currently support mutating a
// module-level `*u8` (so a lazy-init shared buffer isn't expressible
// today). Graduate to the static-buffer shape once that lands.
// Mirrors Hare's strconv:: surface. The *tos functions return a
// `const str` view into a module-level buffer that is overwritten on
// the next call to the same function; callers must copy the bytes if
// they need to outlive the next invocation. See [[strings.dup]] to
// duplicate. Matches Hare's strconv::*tos semantics.
use os;
use strings;
@@ -22,43 +21,44 @@ export type overflow = !void;
// error — any error from a strconv call. Mirrors Hare's strconv::error.
export type error = !(invalid | overflow);
// base — numeric base for parsing/formatting. Plain i32 (not a named
// enum) because cross-module `strconv.base.DEC` chains miscompile in
// the cstage cgen — it emits a memory load through `base(SB)` rather
// than inlining the enum value. Hare names them as `strconv::base`
// enum values; we expose them as module-level `def`s so callers say
// `strconv.DEC` and the cgen inlines the immediate.
// base — numeric base for parsing/formatting. Mirrors Hare's
// `strconv::base` (Hare uses `enum uint`; we pick `enum i32` since
// the underlying parse/format loops index with i32).
//
// HEX is HEX_UPPER; HEX_LOWER is a separate pseudo-base that produces
// lowercase a-f digits.
export def DEFAULT: i32 = 0;
export def BIN: i32 = 2;
export def OCT: i32 = 8;
export def DEC: i32 = 10;
export def HEX_UPPER: i32 = 16;
export def HEX: i32 = 16;
export def HEX_LOWER: i32 = 17;
// HEX is an alias for HEX_UPPER; HEX_LOWER is a pseudo-base that
// produces lowercase a-f digits.
export type base = enum i32 {
DEFAULT = 0,
BIN = 2,
OCT = 8,
DEC = 10,
HEX_UPPER = 16,
HEX = 16,
HEX_LOWER = 17,
};
fn basenum(b: i32) i64 = {
if (b == BIN) { return 2; };
if (b == OCT) { return 8; };
if (b == HEX) { return 16; };
if (b == HEX_UPPER) { return 16; };
if (b == HEX_LOWER) { return 16; };
fn basenum(b: base) i64 = {
if (b == base.BIN) { return 2; };
if (b == base.OCT) { return 8; };
if (b == base.HEX) { return 16; };
if (b == base.HEX_UPPER) { return 16; };
if (b == base.HEX_LOWER) { return 16; };
return 10; // DEC and DEFAULT
};
fn basedigit(d: i64, b: i32) u8 = {
fn basedigit(d: i64, b: base) u8 = {
if (d < 10) { return (d + 48): u8; };
let off: i64 = d - 10;
if (b == HEX_LOWER) { return (off + 97): u8; };
if (b == base.HEX_LOWER) { return (off + 97): u8; };
return (off + 65): u8;
};
// u64tos — convert v to a base-b numeric string. Returns owned str;
// release via os.free(r.ptr, r.len: u64). Mirrors Hare's
// strconv::u64tos (Hare returns const str into a static buffer).
export fn u64tos(v: u64, b: i32) str = {
// u64tos — convert v to a base-b numeric string. Returns a view into
// `u64tos_buf` which is overwritten on the next call. Matches Hare's
// strconv::u64tos.
let u64tos_buf: [65]u8;
export fn u64tos(v: u64, b: base) str = {
let nb: u64 = basenum(b): u64;
let tmp: [65]u8;
let i: i32 = 0;
@@ -70,22 +70,25 @@ export fn u64tos(v: u64, b: i32) str = {
n = n / nb;
i += 1;
};
let buf: *u8 = os.alloc(i: u64): *u8;
let out: i32 = 0;
for (i > 0) {
i -= 1;
buf[out] = tmp[i];
u64tos_buf[out] = tmp[i];
out += 1;
};
let r: str;
r.ptr = buf;
r.ptr = &u64tos_buf[0];
r.len = out;
return r;
};
// i64tos — convert v to a base-b numeric string. Returns owned str;
// release via os.free. Mirrors Hare's strconv::i64tos.
export fn i64tos(v: i64, b: i32) str = {
// i64tos — convert v to a base-b numeric string. Returns a view into
// `i64tos_buf` which is overwritten on the next call. Independent
// buffer from u64tos so i64tos's own call to u64tos doesn't clobber
// the in-flight result. Matches Hare's strconv::i64tos.
let i64tos_buf: [66]u8;
export fn i64tos(v: i64, b: base) str = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) { neg = true; n = -n; };
@@ -99,37 +102,33 @@ export fn i64tos(v: i64, b: i32) str = {
n = n / nb;
i += 1;
};
let extra: i32 = 0;
if (neg) { extra = 1; };
let total: i32 = i + extra;
let buf: *u8 = os.alloc(total: u64): *u8;
let out: i32 = 0;
if (neg) { buf[out] = 45u8; out += 1; }; // '-'
if (neg) { i64tos_buf[out] = 45u8; out += 1; }; // '-'
for (i > 0) {
i -= 1;
buf[out] = tmp[i];
i64tos_buf[out] = tmp[i];
out += 1;
};
let r: str;
r.ptr = buf;
r.ptr = &i64tos_buf[0];
r.len = out;
return r;
};
export fn i32tos(v: i32, b: i32) str = { return i64tos(v: i64, b); };
export fn i16tos(v: i16, b: i32) str = { return i64tos(v: i64, b); };
export fn i8tos(v: i8, b: i32) str = { return i64tos(v: i64, b); };
export fn i32tos(v: i32, b: base) str = { return i64tos(v: i64, b); };
export fn i16tos(v: i16, b: base) str = { return i64tos(v: i64, b); };
export fn i8tos(v: i8, b: base) str = { return i64tos(v: i64, b); };
export fn u32tos(v: u32, b: i32) str = { return u64tos(v: u64, b); };
export fn u16tos(v: u16, b: i32) str = { return u64tos(v: u64, b); };
export fn u8tos(v: u8, b: i32) str = { return u64tos(v: u64, b); };
export fn u32tos(v: u32, b: base) str = { return u64tos(v: u64, b); };
export fn u16tos(v: u16, b: base) str = { return u64tos(v: u64, b); };
export fn u8tos(v: u8, b: base) str = { return u64tos(v: u64, b); };
// digval — value of digit byte `c` under base `b`, or -1 if not a
// valid digit. Letters are accepted case-insensitively under HEX /
// HEX_UPPER; only lowercase under HEX_LOWER.
fn digval(c: u8, b: i32) i32 = {
fn digval(c: u8, b: base) i32 = {
if (c >= 48u8) { if (c <= 57u8) { return (c - 48u8): i32; }; };
if (b == HEX_LOWER) {
if (b == base.HEX_LOWER) {
if (c >= 97u8) { if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; }; };
return -1;
};
@@ -142,7 +141,7 @@ fn digval(c: u8, b: i32) i32 = {
// No locale, no whitespace, no underscores: optional leading '-' then
// digits. Returns invalid with the offending index or overflow on
// out-of-range.
export fn stoi64(s: str, b: i32) (i64 | invalid | overflow) = {
export fn stoi64(s: str, b: base) (i64 | invalid | overflow) = {
if (s.len == 0) { return 0: invalid; };
let i: i32 = 0;
let neg: bool = false;
@@ -163,7 +162,7 @@ export fn stoi64(s: str, b: i32) (i64 | invalid | overflow) = {
};
// stou64 — parse unsigned base-b number. Mirrors Hare's strconv::stou64.
export fn stou64(s: str, b: i32) (u64 | invalid | overflow) = {
export fn stou64(s: str, b: base) (u64 | invalid | overflow) = {
if (s.len == 0) { return 0: invalid; };
let nb: u64 = basenum(b): u64;
let v: u64 = 0u64;
@@ -179,7 +178,7 @@ export fn stou64(s: str, b: i32) (u64 | invalid | overflow) = {
return v;
};
export fn stoi32(s: str, b: i32) (i32 | invalid | overflow) = {
export fn stoi32(s: str, b: base) (i32 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -193,7 +192,7 @@ export fn stoi32(s: str, b: i32) (i32 | invalid | overflow) = {
return 0: invalid; // unreachable; appeases the path-cov checker
};
export fn stoi16(s: str, b: i32) (i16 | invalid | overflow) = {
export fn stoi16(s: str, b: base) (i16 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -207,7 +206,7 @@ export fn stoi16(s: str, b: i32) (i16 | invalid | overflow) = {
return 0: invalid;
};
export fn stoi8(s: str, b: i32) (i8 | invalid | overflow) = {
export fn stoi8(s: str, b: base) (i8 | invalid | overflow) = {
let r = stoi64(s, b);
match (r) {
case let v: i64 => {
@@ -221,7 +220,7 @@ export fn stoi8(s: str, b: i32) (i8 | invalid | overflow) = {
return 0: invalid;
};
export fn stou32(s: str, b: i32) (u32 | invalid | overflow) = {
export fn stou32(s: str, b: base) (u32 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -234,7 +233,7 @@ export fn stou32(s: str, b: i32) (u32 | invalid | overflow) = {
return 0: invalid;
};
export fn stou16(s: str, b: i32) (u16 | invalid | overflow) = {
export fn stou16(s: str, b: base) (u16 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -247,7 +246,7 @@ export fn stou16(s: str, b: i32) (u16 | invalid | overflow) = {
return 0: invalid;
};
export fn stou8(s: str, b: i32) (u8 | invalid | overflow) = {
export fn stou8(s: str, b: base) (u8 | invalid | overflow) = {
let r = stou64(s, b);
match (r) {
case let v: u64 => {
@@ -283,13 +282,14 @@ export fn stou8(s: str, b: i32) (u8 | invalid | overflow) = {
// the ww-side wwdump currently skips TK_FLOAT.fval while the C side
// %g-formats it. Same trick lib/ww/lex/lex.ww's parsef64 uses:
// build f64 constants via int-to-f64 casts.
let f64tos_buf: [64]u8;
export fn f64tos(v: f64) str = {
let tmp: [64]u8;
let out: i32 = 0;
let f: f64 = v;
let zero: f64 = 0: f64;
if (f < zero) {
tmp[out] = 45u8; // '-'
f64tos_buf[out] = 45u8; // '-'
out += 1;
f = -f;
};
@@ -299,12 +299,9 @@ export fn f64tos(v: f64) str = {
if (f >= cap) {
let s: str = "huge";
let k: i32 = 0;
for (k < s.len) { tmp[out] = s[k]; out += 1; k += 1; };
let buf: *u8 = os.alloc(out: u64): *u8;
let q: i32 = 0;
for (q < out) { buf[q] = tmp[q]; q += 1; };
for (k < s.len) { f64tos_buf[out] = s[k]; out += 1; k += 1; };
let r: str;
r.ptr = buf;
r.ptr = &f64tos_buf[0];
r.len = out;
return r;
};
@@ -323,32 +320,27 @@ export fn f64tos(v: f64) str = {
ip += 1;
fp = 0;
};
let intstr: str = i64tos(ip, DEC);
let intstr: str = i64tos(ip, base.DEC);
let k: i32 = 0;
for (k < intstr.len) { tmp[out] = intstr.ptr[k]; out += 1; k += 1; };
os.free(intstr.ptr: *void, intstr.len: u64);
for (k < intstr.len) { f64tos_buf[out] = intstr.ptr[k]; out += 1; k += 1; };
if (fp != 0) {
tmp[out] = 46u8; // '.'
f64tos_buf[out] = 46u8; // '.'
out += 1;
let fracstr: str = u64tos(fp: u64, DEC);
let fracstr: str = u64tos(fp: u64, base.DEC);
// Pad fractional to 6 digits with leading zeros (e.g. 0.05 →
// fp=50000, fracstr="50000", pad one '0' before).
let z: i32 = 6 - fracstr.len;
for (z > 0) { tmp[out] = 48u8; out += 1; z -= 1; };
for (z > 0) { f64tos_buf[out] = 48u8; out += 1; z -= 1; };
k = 0;
for (k < fracstr.len) { tmp[out] = fracstr.ptr[k]; out += 1; k += 1; };
os.free(fracstr.ptr: *void, fracstr.len: u64);
for (k < fracstr.len) { f64tos_buf[out] = fracstr.ptr[k]; out += 1; k += 1; };
// Trim trailing zeros in the fractional part.
for (out > 0) {
if (tmp[out - 1] != 48u8) { break; };
if (f64tos_buf[out - 1] != 48u8) { break; };
out -= 1;
};
};
let buf: *u8 = os.alloc(out: u64): *u8;
let q: i32 = 0;
for (q < out) { buf[q] = tmp[q]; q += 1; };
let r: str;
r.ptr = buf;
r.ptr = &f64tos_buf[0];
r.len = out;
return r;
};

View File

@@ -39,53 +39,46 @@ export fn hassuffix(s: str, suf: str) bool = {
return true;
};
// indexbyte — first byte position of byte `c` in `s`. Mirrors
// Hare's strings::byteindex when the needle is a single ASCII rune,
// renamed to match bytes.indexbyte and to disambiguate from Hare's
// `byteindex(haystack, needle: (str | rune))` which we don't have
// the union-arg ABI for yet.
export fn indexbyte(s: str, c: u8) (i32 | void) = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
return;
};
// rindexbyte — last byte position of byte `c` in `s`.
export fn rindexbyte(s: str, c: u8) (i32 | void) = {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
return;
};
// index — first index of `sub` in `s`. Naive scan; fine for short
// patterns and small strings, which dominate config and CLI parsing.
// Empty `sub` matches at 0.
export fn index(s: str, sub: str) (i32 | void) = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// byteindex — first byte position of `needle` in `s`. Mirrors Hare's
// strings::byteindex: a single-codepoint rune scans for the byte that
// encodes it (ASCII only here — multi-byte UTF-8 awaits utf8 encode),
// a str needle scans for the substring. Returns void if absent.
export fn byteindex(s: str, needle: (str | rune)) (i32 | void) = {
match (needle) {
case let r: rune => {
let c: u8 = r: u8;
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
if (ok) { return i; };
i += 1;
return;
};
case let sub: str => {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i += 1;
};
return;
};
};
return;
};
// contains — true iff `sub` appears in `s`. Mirrors Hare's
// strings::contains shape (byte-wise on the str-needle case).
export fn contains(s: str, sub: str) bool = {
let r: (i32 | void) = index(s, sub);
let r: (i32 | void) = byteindex(s, sub);
match (r) {
case let i: i32 => return true;
case void => return false;
@@ -129,21 +122,37 @@ export fn dup(s: str) str = {
return r;
};
// rindex — last index of `sub` in `s`. Mirrors Hare's strings::rindex
// (slice case). Empty `sub` matches at s.len.
export fn rindex(s: str, sub: str) (i32 | void) = {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
// rbyteindex — last byte position of `needle` in `s`. Mirrors Hare's
// strings::rbyteindex. Rune needle scans for the byte that encodes it
// (ASCII only); str needle scans for the substring. Empty str needle
// matches at s.len.
export fn rbyteindex(s: str, needle: (str | rune)) (i32 | void) = {
match (needle) {
case let r: rune => {
let c: u8 = r: u8;
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
if (ok) { return i; };
i -= 1;
return;
};
case let sub: str => {
if (sub.len == 0) { return s.len; };
if (sub.len > s.len) { return; };
let i: i32 = s.len - sub.len;
for (i >= 0) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i -= 1;
};
return;
};
};
return;
};

View File

@@ -269,11 +269,11 @@ fn pr(fd: i32, n: *node, d: i32) void = {
if (n.kind == nkind.N_INTLIT) {
putc1(fd, 32u8);
let s: str = strconv.u64tos(n.uval, strconv.DEC);
let s: str = strconv.u64tos(n.uval, strconv.base.DEC);
os.write(fd, s.ptr, s.len: u64);
} else { if (n.kind == nkind.N_RUNELIT) {
putc1(fd, 32u8);
let s: str = strconv.u64tos(n.uval, strconv.DEC);
let s: str = strconv.u64tos(n.uval, strconv.base.DEC);
os.write(fd, s.ptr, s.len: u64);
} else { if (
n.kind == nkind.N_STRLIT ||

View File

@@ -382,10 +382,10 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputsstr(fd, "<none>");
};
fputcbyte(fd, 58u8); // ':'
let ls: str = strconv.i64tos(t.line: i64, strconv.DEC);
let ls: str = strconv.i64tos(t.line: i64, strconv.base.DEC);
os.write(fd, ls.ptr, ls.len: u64);
fputcbyte(fd, 58u8);
let cs: str = strconv.i64tos(t.col: i64, strconv.DEC);
let cs: str = strconv.i64tos(t.col: i64, strconv.base.DEC);
os.write(fd, cs.ptr, cs.len: u64);
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
@@ -401,11 +401,11 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == tkind.TK_INT) {
fputcbyte(fd, 32u8);
let us: str = strconv.u64tos(t.uval, strconv.DEC);
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, 32u8);
let us: str = strconv.u64tos(t.uval, strconv.DEC);
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