ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:
cmd/wwc/ → cmd/wcc/ libwwc.a → libwcc.a
cmd/6{c,a,l} → cmd/w6{c,a,l} binary names too
test/wwc/ → test/wcc/ 6 test files w/ w6 prefix
selfhost/cmd mirror in lockstep
bootstrap/amd64/{w6c,w6a,w6l} snapshot binaries (gitignored)
WW_6{C,A,L} → WW_W6{C,A,L} env-var overrides
Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:
ww test [path] discover *_test.ww in a directory module, run
each; single-file mode for `ww test foo.ww`
Module-by-name `ww build foo` resolves to foo.ww or foo/foo.ww
via search path (cwd : -I dirs : $WW_LIB)
Default-to-cwd `ww build` / `ww test` build the cwd module
Run pass-through `ww run path arg1 arg2` reaches the program
lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.
Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.
Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
This commit is contained in:
247
selfhost/cmd/wcc/check.ww
Normal file
247
selfhost/cmd/wcc/check.ww
Normal file
@@ -0,0 +1,247 @@
|
||||
// selfhost/cmd/wcc/check.ww — minimal port of cmd/wcc/check.c.
|
||||
//
|
||||
// Status: name-resolution + primitive-type seeding only. Full type
|
||||
// inference, conversion rules, tagged-union dispatch typing, return-
|
||||
// type checking, etc. all live in cmd/wcc/check.c (937 lines) and
|
||||
// will land here in subsequent commits.
|
||||
//
|
||||
// What this version does:
|
||||
// 1. Creates a top scope and seeds it with primitive type names so
|
||||
// `i32`, `str`, `*u8` etc. resolve.
|
||||
// 2. Walks the file's top-level decls (use/def/type/fn/let) and
|
||||
// installs Sym entries for each.
|
||||
// 3. Recursively walks fn bodies; for every N_IDENT used as an
|
||||
// expression or as a type name, looks it up and counts the
|
||||
// resolved vs. unresolved.
|
||||
// 4. Returns a summary the caller (wwdump -r) prints; the test
|
||||
// asserts unresolved == 0 on every selfhost fixture, which is
|
||||
// the floor signal that the frontend can name-resolve real ww.
|
||||
|
||||
use os;
|
||||
use mem;
|
||||
use tok;
|
||||
|
||||
type checker = struct {
|
||||
a: *arena,
|
||||
tc: *tctx,
|
||||
top: *scope,
|
||||
cur: *scope,
|
||||
nresolved: i32,
|
||||
nunresolved: i32,
|
||||
errs: i32,
|
||||
verbose: i32, // when non-zero, log each unresolved name
|
||||
};
|
||||
|
||||
// seed_primitives — install the built-in type names so `i32`, `str`,
|
||||
// etc. can be looked up like ordinary symbols.
|
||||
fn seed_primitives(c: *checker) void = {
|
||||
scope_define(c.top, "void", SK_TYPE, c.tc.ty_void, nil);
|
||||
scope_define(c.top, "bool", SK_TYPE, c.tc.ty_bool, nil);
|
||||
scope_define(c.top, "rune", SK_TYPE, c.tc.ty_rune, nil);
|
||||
scope_define(c.top, "i8", SK_TYPE, c.tc.ty_i8, nil);
|
||||
scope_define(c.top, "i16", SK_TYPE, c.tc.ty_i16, nil);
|
||||
scope_define(c.top, "i32", SK_TYPE, c.tc.ty_i32, nil);
|
||||
scope_define(c.top, "i64", SK_TYPE, c.tc.ty_i64, nil);
|
||||
scope_define(c.top, "u8", SK_TYPE, c.tc.ty_u8, nil);
|
||||
scope_define(c.top, "u16", SK_TYPE, c.tc.ty_u16, nil);
|
||||
scope_define(c.top, "u32", SK_TYPE, c.tc.ty_u32, nil);
|
||||
scope_define(c.top, "u64", SK_TYPE, c.tc.ty_u64, nil);
|
||||
scope_define(c.top, "int", SK_TYPE, c.tc.ty_int, nil);
|
||||
scope_define(c.top, "uint", SK_TYPE, c.tc.ty_uint, nil);
|
||||
scope_define(c.top, "uintptr", SK_TYPE, c.tc.ty_uintptr, nil);
|
||||
scope_define(c.top, "f32", SK_TYPE, c.tc.ty_f32, nil);
|
||||
scope_define(c.top, "f64", SK_TYPE, c.tc.ty_f64, nil);
|
||||
scope_define(c.top, "str", SK_TYPE, c.tc.ty_str, nil);
|
||||
// `nil`, `true`, `false` are keywords — handled at the lex/parser
|
||||
// level, no symbol needed.
|
||||
// `len`, `alloc`, `free` are pseudo-builtins; scope_define them so
|
||||
// their use sites resolve. The actual semantics live in cgen.
|
||||
scope_define(c.top, "len", SK_FN, nil, nil);
|
||||
scope_define(c.top, "alloc", SK_FN, nil, nil);
|
||||
scope_define(c.top, "free", SK_FN, nil, nil);
|
||||
};
|
||||
|
||||
// install_decl — install the top-level decl's name into the top scope.
|
||||
// We don't compute its type yet (that's the resolve pass) — just bind
|
||||
// the name so forward references resolve.
|
||||
fn install_decl(c: *checker, d: *node) void = {
|
||||
if (d == nil) { return; };
|
||||
let k: i32 = d.kind;
|
||||
let nm: str = d.str;
|
||||
if (k == N_USE) { scope_define(c.top, nm, SK_USE, nil, d); return; };
|
||||
if (k == N_DEF) { scope_define(c.top, nm, SK_DEF, nil, d); return; };
|
||||
if (k == N_TYPEDECL) { scope_define(c.top, nm, SK_TYPE, nil, d); return; };
|
||||
if (k == N_FNDECL) { scope_define(c.top, nm, SK_FN, nil, d); return; };
|
||||
if (k == N_LET) { scope_define(c.top, nm, SK_VAR, nil, d); return; };
|
||||
};
|
||||
|
||||
// resolve_walk — recursive AST walk that, for every N_IDENT and
|
||||
// N_TNAME seen, looks up the name and bumps the resolved/unresolved
|
||||
// counters. Local lets are installed in the current scope as soon as
|
||||
// their init/type expressions have been walked (forward use of a let
|
||||
// before its declaration would resolve to nothing — same semantics as
|
||||
// the C checker's collect-then-resolve flow within a function).
|
||||
fn resolve_walk(c: *checker, n: *node) void = {
|
||||
if (n == nil) { return; };
|
||||
let k: i32 = n.kind;
|
||||
|
||||
// `use IDENT;` — name is a module label, not a free ident.
|
||||
if (k == N_USE) { return; };
|
||||
|
||||
if (k == N_IDENT) {
|
||||
let nm: str = n.str;
|
||||
if (nm.len > 0) {
|
||||
let s: *sym = scope_lookup(c.cur, nm);
|
||||
if (s == nil) {
|
||||
c.nunresolved += 1;
|
||||
if (c.verbose != 0) {
|
||||
os.write(2, " unresolved id: ".ptr, 17u64);
|
||||
os.write(2, nm.ptr, nm.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
};
|
||||
} else { c.nresolved += 1; };
|
||||
};
|
||||
};
|
||||
|
||||
if (k == N_TNAME) {
|
||||
let nm: str = n.str;
|
||||
if (nm.len > 0) {
|
||||
let s: *sym = scope_lookup(c.cur, nm);
|
||||
if (s == nil) {
|
||||
c.nunresolved += 1;
|
||||
if (c.verbose != 0) {
|
||||
os.write(2, " unresolved tname: ".ptr, 20u64);
|
||||
os.write(2, nm.ptr, nm.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
};
|
||||
} else { c.nresolved += 1; };
|
||||
};
|
||||
};
|
||||
|
||||
// `match (e) { case let v: T => stmt; ... }` — the binding `v`
|
||||
// is declared by the case arm and visible inside its body.
|
||||
if (k == N_MCASE) {
|
||||
if (n.lhs != nil) { resolve_walk(c, n.lhs); };
|
||||
let nm: str = n.str;
|
||||
if (nm.len > 0) {
|
||||
scope_define(c.cur, nm, SK_VAR, nil, n);
|
||||
};
|
||||
if (n.body != nil) { resolve_walk(c, n.body); };
|
||||
return;
|
||||
};
|
||||
|
||||
if (k == N_DOT) {
|
||||
// Walk only the base; the .field name is a member, not a
|
||||
// free identifier.
|
||||
if (n.lhs != nil) { resolve_walk(c, n.lhs); };
|
||||
return;
|
||||
};
|
||||
|
||||
if (k == N_FIELD) {
|
||||
if (n.lhs != nil) { resolve_walk(c, n.lhs); };
|
||||
return;
|
||||
};
|
||||
|
||||
if (k == N_TFIELD) {
|
||||
if (n.lhs != nil) { resolve_walk(c, n.lhs); };
|
||||
return;
|
||||
};
|
||||
|
||||
// Walk children (mirroring ast.ww's printer descent order).
|
||||
if (n.attr != nil) { resolve_walk(c, n.attr); };
|
||||
if (n.lhs != nil) { resolve_walk(c, n.lhs); };
|
||||
if (n.rhs != nil) { resolve_walk(c, n.rhs); };
|
||||
if (n.cond != nil) { resolve_walk(c, n.cond); };
|
||||
if (n.body != nil) { resolve_walk(c, n.body); };
|
||||
if (n.els != nil) { resolve_walk(c, n.els); };
|
||||
if (n.list != nil) {
|
||||
let m: *node = n.list;
|
||||
for (m != nil) {
|
||||
resolve_walk(c, m);
|
||||
m = m.next;
|
||||
};
|
||||
};
|
||||
|
||||
// After walking children: a local `let X: T = init;` registers
|
||||
// `X` so subsequent statements can resolve it. Top-level lets
|
||||
// are installed in install_decl, so this duplicate install at
|
||||
// the file scope just no-ops (scope_define returns nil on dup).
|
||||
if (k == N_LET) {
|
||||
let nm: str = n.str;
|
||||
if (nm.len > 0) {
|
||||
scope_define(c.cur, nm, SK_VAR, nil, n);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// install_param — when entering a fn body, define its params in a
|
||||
// fresh local scope.
|
||||
fn install_params(c: *checker, params: *node) void = {
|
||||
let p: *node = params;
|
||||
for (p != nil) {
|
||||
if (p.kind == N_PARAM) {
|
||||
let nm: str = p.str;
|
||||
if (nm.len > 0) {
|
||||
scope_define(c.cur, nm, SK_PARAM, nil, p);
|
||||
};
|
||||
};
|
||||
p = p.next;
|
||||
};
|
||||
};
|
||||
|
||||
// resolve_fnbody — open a child scope for the fn, install its params,
|
||||
// then walk the body. Local lets installed by walk_stmt (a future
|
||||
// extension); for the current pass we just resolve-walk without
|
||||
// per-statement scopes.
|
||||
fn resolve_fnbody(c: *checker, fnnode: *node) void = {
|
||||
let outer: *scope = c.cur;
|
||||
c.cur = newscope(c.a, c.cur);
|
||||
install_params(c, fnnode.list);
|
||||
if (fnnode.body != nil) {
|
||||
resolve_walk(c, fnnode.body);
|
||||
};
|
||||
c.cur = outer;
|
||||
};
|
||||
|
||||
export fn check_init(c: *checker, a: *arena, tc: *tctx) void = {
|
||||
c.a = a;
|
||||
c.tc = tc;
|
||||
c.top = newscope(a, nil);
|
||||
c.cur = c.top;
|
||||
c.nresolved = 0;
|
||||
c.nunresolved = 0;
|
||||
c.errs = 0;
|
||||
c.verbose = 0;
|
||||
seed_primitives(c);
|
||||
};
|
||||
|
||||
export fn check_file(c: *checker, file: *node) void = {
|
||||
if (file == nil) { return; };
|
||||
if (file.kind != N_FILE) { return; };
|
||||
|
||||
// Pass 1: install all top-level names.
|
||||
let d: *node = file.list;
|
||||
for (d != nil) {
|
||||
install_decl(c, d);
|
||||
d = d.next;
|
||||
};
|
||||
|
||||
// Pass 2: walk decl bodies/types and resolve identifiers.
|
||||
d = file.list;
|
||||
for (d != nil) {
|
||||
let k: i32 = d.kind;
|
||||
if (k == N_FNDECL) {
|
||||
if (d.lhs != nil) { resolve_walk(c, d.lhs); }; // return type
|
||||
resolve_fnbody(c, d);
|
||||
} else { if (k == N_DEF) {
|
||||
if (d.lhs != nil) { resolve_walk(c, d.lhs); };
|
||||
if (d.rhs != nil) { resolve_walk(c, d.rhs); };
|
||||
} else { if (k == N_TYPEDECL) {
|
||||
if (d.lhs != nil) { resolve_walk(c, d.lhs); };
|
||||
} else { if (k == N_LET) {
|
||||
if (d.lhs != nil) { resolve_walk(c, d.lhs); };
|
||||
if (d.rhs != nil) { resolve_walk(c, d.rhs); };
|
||||
};};};};
|
||||
d = d.next;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user