strconv: graduate to (T | invalid | overflow); add void expression

`type invalid = i32` (payload: byte index of first bad rune; mirrors
Hare's strconv::invalid = !size) and `type overflow = void` (Hare's
overflow = !void). stoi64/stou64 now return these instead of the
str-error placeholder. atoi64 dropped — lib/CLAUDE.md says graduate
in one go, don't keep both shapes around.

To produce the void variant payload, `void` is now a real
expression literal (TK_VOID kw, N_VOIDLIT). It evaluates to ty_void;
codegen emits MOVQ $0, AX. Both kinds are appended at the tail of
their enums to keep prior numeric values byte-stable for the
wwdump-diff fixtures.

check_file reorder: USE declarations are now installed in pass 1
alongside the type-decl placeholders so dotted type references
(`strconv.invalid` from a typedecl body) resolve. DEF/FN/LET silently
overwrite a USE-occupied slot — matches the old behavior where USE
silently no-op'd when a same-name fn/def existed (the conflict
manifested in selfhost main.combined.ww at `use parse;` colliding
with `export fn parse(a)`).

selfhost mirror: lib/ww/lex/tok.ww kwtab+name; lib/ww/ast.ww
N_VOIDLIT def+print; lib/ww/parse/{expr,parse}.ww TK_VOID handling;
selfhost/cmd/wcc/cgenexpr.ww N_VOIDLIT codegen.
This commit is contained in:
2026-05-12 02:02:08 +09:00
parent 1e2f55aed8
commit 4085742853
16 changed files with 250 additions and 198 deletions

View File

@@ -408,6 +408,7 @@ cexpr(Checker *c, Node *n)
case N_TRUE:
case N_FALSE: n->type = ty_untyped_bool; return n->type;
case N_NIL: n->type = ty_untyped_nil; return n->type;
case N_VOIDLIT: n->type = ty_void; return n->type;
case N_IDENT: {
if (n->str && n->str[0] == '\0')
return n->type = err(c, n->pos,
@@ -1201,8 +1202,14 @@ check_file(Checker *c, Node *file)
* For self-referential types we install the named-type placeholder
* BEFORE resolving its body; the body may legitimately mention
* the type itself (`type stream = struct { read: fn(*stream)... }`).
*/
* USE declarations are installed in this same step so dotted type
* references (`strconv.invalid`) resolve when typedecl bodies are
* walked in the next pass. */
for (Node *d = file->list; d; d = d->next) {
if (d->kind == N_USE) {
scope_define(c->cur, d->str, SK_USE, NULL, d);
continue;
}
if (d->kind != N_TYPEDECL) continue;
Type *named = type_named(c->a, d->str, NULL);
if (!scope_define(c->cur, d->str, SK_TYPE, named, d))
@@ -1221,27 +1228,40 @@ check_file(Checker *c, Node *file)
for (Node *d = file->list; d; d = d->next) {
switch (d->kind) {
case N_USE:
scope_define(c->cur, d->str, SK_USE, NULL, d);
/* already installed in pass 1; no-op here so the
* old fall-through doesn't re-define. */
break;
case N_DEF: {
Type *t = resolve_type(c, d->lhs);
d->type = t;
if (!scope_define(c->cur, d->str, SK_DEF, t, d))
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev && prev->kind == SK_USE) {
prev->kind = SK_DEF; prev->type = t; prev->decl = d;
} else if (!scope_define(c->cur, d->str, SK_DEF, t, d))
err(c, d->pos, "duplicate def %s", d->str);
break;
}
case N_FNDECL: {
Type *t = build_fn_type(c, d);
d->type = t;
if (!scope_define(c->cur, d->str, SK_FN, t, d))
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev && prev->kind == SK_USE) {
prev->kind = SK_FN; prev->type = t; prev->decl = d;
} else if (!scope_define(c->cur, d->str, SK_FN, t, d))
err(c, d->pos, "duplicate fn %s", d->str);
break;
}
case N_LET: {
Type *t = d->lhs ? resolve_type(c, d->lhs) : NULL;
d->type = t;
if (d->str && d->str[0])
scope_define(c->cur, d->str, SK_VAR, t, d);
if (d->str && d->str[0]) {
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev && prev->kind == SK_USE) {
prev->kind = SK_VAR; prev->type = t;
prev->decl = d;
} else
scope_define(c->cur, d->str, SK_VAR, t, d);
}
break;
}
default: break;