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.
228 lines
6.0 KiB
Plaintext
228 lines
6.0 KiB
Plaintext
// strings — operations over the immutable str type ({ *u8, len }).
|
|
// Mirrors Hare's strings::; `len` and `is-empty` aren't functions
|
|
// (callers use `s.len` and `s.len == 0` directly).
|
|
|
|
use os;
|
|
|
|
// compare — bytewise three-way comparison: negative if a<b, 0 if equal,
|
|
// positive if a>b. Matches Hare's strings::compare. ASCII-order, not
|
|
// locale-aware. Callers that just need equality use `compare(a, b) == 0`.
|
|
export fn compare(a: str, b: str) i32 = {
|
|
let n: i32 = a.len;
|
|
if (b.len < n) { n = b.len; };
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
if (a[i] != b[i]) { return (a[i]: i32) - (b[i]: i32); };
|
|
i += 1;
|
|
};
|
|
return a.len - b.len;
|
|
};
|
|
|
|
export fn hasprefix(s: str, p: str) bool = {
|
|
if (p.len > s.len) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < p.len) {
|
|
if (s[i] != p[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
export fn hassuffix(s: str, suf: str) bool = {
|
|
if (suf.len > s.len) { return false; };
|
|
let off: i32 = s.len - suf.len;
|
|
let i: i32 = 0;
|
|
for (i < suf.len) {
|
|
if (s[off + i] != suf[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
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) = byteindex(s, sub);
|
|
match (r) {
|
|
case let i: i32 => return true;
|
|
case void => return false;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// concat — joins two strings into a fresh str. Caller owns the
|
|
// returned str's storage; release via `os.free(r.ptr, r.len)`. Mirrors
|
|
// Hare's strings::concat shape.
|
|
export fn concat(a: str, b: str) str = {
|
|
let total: i32 = a.len + b.len;
|
|
let buf: *u8 = os.alloc(total: u64): *u8;
|
|
let i: i32 = 0;
|
|
for (i < a.len) { buf[i] = a[i]; i += 1; };
|
|
let j: i32 = 0;
|
|
for (j < b.len) { buf[a.len + j] = b[j]; j += 1; };
|
|
let r: str;
|
|
r.ptr = buf;
|
|
r.len = total;
|
|
return r;
|
|
};
|
|
|
|
// dup — duplicate a string into a fresh allocation. Caller owns the
|
|
// returned str's storage; release via `os.free(r.ptr, r.len)`. Mirrors
|
|
// Hare's strings::dup shape — Hare returns `(str | nomem)`, ww doesn't
|
|
// have nomem (os.alloc aborts on OOM), so we return plain `str`.
|
|
//
|
|
// Empty input yields a `{nil, 0}` str — Hare returns the static empty
|
|
// string; same observable result.
|
|
export fn dup(s: str) str = {
|
|
let r: str;
|
|
r.ptr = nil;
|
|
r.len = 0;
|
|
if (s.len == 0) { return r; };
|
|
let buf: *u8 = os.alloc(s.len: u64): *u8;
|
|
let i: i32 = 0;
|
|
for (i < s.len) { buf[i] = s[i]; i += 1; };
|
|
r.ptr = buf;
|
|
r.len = s.len;
|
|
return r;
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
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;
|
|
};
|
|
|
|
// sub — borrowed substring `s[start..end]`. Mirrors Hare's
|
|
// strings::sub. Caller must ensure 0 <= start <= end <= s.len; out-of-
|
|
// range indices are clamped silently here, where Hare aborts.
|
|
export fn sub(s: str, start: i32, end: i32) str = {
|
|
let lo: i32 = start;
|
|
let hi: i32 = end;
|
|
if (lo < 0) { lo = 0; };
|
|
if (hi > s.len) { hi = s.len; };
|
|
if (hi < lo) { hi = lo; };
|
|
let r: str;
|
|
r.ptr = s.ptr + (lo: u64);
|
|
r.len = hi - lo;
|
|
return r;
|
|
};
|
|
|
|
// trimprefix — `s` with `pre` stripped from the front, or `s`
|
|
// unchanged if it doesn't start with `pre`. Returns a borrowed view.
|
|
// Mirrors Hare's strings::trimprefix.
|
|
export fn trimprefix(s: str, pre: str) str = {
|
|
if (!hasprefix(s, pre)) { return s; };
|
|
let r: str;
|
|
r.ptr = s.ptr + (pre.len: u64);
|
|
r.len = s.len - pre.len;
|
|
return r;
|
|
};
|
|
|
|
// trimsuffix — `s` with `suf` stripped from the end, or `s` unchanged
|
|
// if it doesn't end with `suf`. Returns a borrowed view. Mirrors
|
|
// Hare's strings::trimsuffix.
|
|
export fn trimsuffix(s: str, suf: str) str = {
|
|
if (!hassuffix(s, suf)) { return s; };
|
|
let r: str;
|
|
r.ptr = s.ptr;
|
|
r.len = s.len - suf.len;
|
|
return r;
|
|
};
|
|
|
|
// ltrimbyte / rtrimbyte / trimbyte — strip occurrences of a single
|
|
// byte from the left, right, or both ends. Returns a borrowed view.
|
|
// Hare's strings::ltrim / rtrim / trim take a rune varargs set; ww's
|
|
// subset takes a single byte (the common ASCII case).
|
|
export fn ltrimbyte(s: str, c: u8) str = {
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
if (s[i] != c) { break; };
|
|
i += 1;
|
|
};
|
|
let r: str;
|
|
r.ptr = s.ptr + (i: u64);
|
|
r.len = s.len - i;
|
|
return r;
|
|
};
|
|
|
|
export fn rtrimbyte(s: str, c: u8) str = {
|
|
let n: i32 = s.len;
|
|
for (n > 0) {
|
|
if (s[n - 1] != c) { break; };
|
|
n -= 1;
|
|
};
|
|
let r: str;
|
|
r.ptr = s.ptr;
|
|
r.len = n;
|
|
return r;
|
|
};
|
|
|
|
export fn trimbyte(s: str, c: u8) str = {
|
|
return rtrimbyte(ltrimbyte(s, c), c);
|
|
};
|