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).
This commit is contained in:
@@ -306,6 +306,26 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
// Hare-style postfix:
|
||||
// `e as T` — assert lhs is variant T (abort otherwise) → T
|
||||
// `e is T` — bool: does lhs currently hold variant T?
|
||||
// Same precedence level as the `:` cast.
|
||||
if (p.curkind == TK_AS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TYPEASSERT, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == TK_IS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TYPETEST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
break;
|
||||
};
|
||||
return cur;
|
||||
|
||||
Reference in New Issue
Block a user