Files
ww/lib/errors/errors.ww
Hojun-Cho dd188ca460 ww: add Hare-style is/as postfix ops on tagged unions
`e is T` returns bool (variant tag == T's index); `e as T` unwraps
to T or exit(1) on mismatch. Postfix, same precedence as `:` cast.
TK_IS / N_TYPETEST / N_TYPEASSERT appended at the tail of their
enums so every prior numeric value stays unchanged — the
990_selfhost wwdump-diff stays byte-clean.

Cgen mirrors the match-case slot-based load (tag at +0, value at
+8/+16), so an N_IDENT tagged-union local works just like a
match scrutinee. Selfhost cgen inlines the slot resolution
because the wwstage cgen drops sign bits on `*i32` output
parameters in this position.

Renames `errors.is` -> `errors.equal` (the only naming collision;
the existing comment already noted it shared shape with
strings.equal/bytes.equal).
2026-05-11 23:21:08 +09:00

31 lines
951 B
Plaintext

// errors — error type (a string) and a few sentinels. Plan 9 model:
// the empty string means OK, a non-empty string is the message.
type error = str;
def eEOF: error = "eof";
def eShortRead: error = "short read";
def eShortWrite: error = "short write";
def eClosed: error = "closed";
def eInvalid: error = "invalid argument";
def ePerm: error = "permission denied";
def eNotFound: error = "not found";
def eExists: error = "already exists";
export fn isnil(e: error) bool = {
return e.len == 0;
};
// equal — compare an error against a sentinel (or any other error).
// Pure byte equality. (Was `errors.is` before `is` became a keyword
// for tagged-union type-tests; rename matches bytes.equal / strings.equal.)
export fn equal(e: error, want: error) bool = {
if (e.len != want.len) { return false; };
let i: i32 = 0;
for (i < e.len) {
if (e[i] != want[i]) { return false; };
i += 1;
};
return true;
};