lib/ww + wcc + w6c + wwdump: strip *arena cascade (γ-6)

amalloc has 0 callers post-γ-2; the *arena threaded through
newnode/newscope/newtype/prim/typesinit/type{ptr,slice,array,chan,
named}/lexinit/parserinit/joindotted/checkinit/arenau64tos/cgeninit
and the scope.a / tctx.a / lex.a / parser.a / checker.a / cgen.a
fields are vestigial.

Drop `import mem;` from 15 files, remove six struct fields, strip
*arena from 14 signatures, update ~120 call sites across lib/ww +
wcc + w6c + wwdump. selfhost/test/sym_link.ww fixture drops the
newarena/freearena probe; still exits 42 on scopedefine/scopelookup.
Both main.combined.ww auto-regenerated.

Comments retidied: typ.ww "once per arena" → "once per program";
parse.ww drops "arena-build" qualifier on joindotted; sym.ww drops
mem-sibling-imports rationale.

Verified 132/132 incl. 994_w6c_ww + 995_self_rebuild byte-identity
(the primary symmetric-stages gate).
This commit is contained in:
2026-05-21 13:01:57 +09:00
parent f83e65b82a
commit 353dffb5e8
19 changed files with 543 additions and 824 deletions

View File

@@ -11,7 +11,6 @@ package ww;
import os;
import strconv;
import mem;
import tok;
// ---- Nkind ------------------------------------------------------------
@@ -127,7 +126,7 @@ type node = struct {
nmod: str, // originating module from `// MODULE: foo`; "" if none
};
export fn newnode(a: *arena, k: nkind, file: str, line: i32, col: i32) *node = {
export fn newnode(k: nkind, file: str, line: i32, col: i32) *node = {
// fval cast-init: 990's wwdump TK_FLOAT diff requires this file
// to tokenise identically through C and ww (lex.ww:382 has the
// same workaround for the cstage %g-formats vs ww-skips divergence).

View File

@@ -17,7 +17,6 @@ package lex;
// callers `import lex;` (which dir-enums lib/ww/lex/).
import os;
import ascii;
import mem;
import strings;
// isidstart / isidpart — identifier classification. Lexer-local
@@ -55,18 +54,16 @@ type lex = struct {
lpos: u64,
line: i32,
col: i32,
a: *arena,
errs: i32,
};
export fn lexinit(l: *lex, a: *arena, file: str, src: *u8, len: u64) void = {
export fn lexinit(l: *lex, file: str, src: *u8, len: u64) void = {
l.file = file;
l.src = src;
l.srclen = len;
l.lpos = 0u64;
l.line = 1;
l.col = 1;
l.a = a;
l.errs = 0;
};

View File

@@ -3,7 +3,6 @@
package parse;
import os;
import mem;
import tok;
// `import encoding.utf8;` — the driver resolves the dotted path to
@@ -16,7 +15,7 @@ fn parseuse(p: *parser) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
advance(p); // past `use`
let n: *node = newnode(p.a, nkind.N_USE, pf, pl, pc);
let n: *node = newnode(nkind.N_USE, pf, pl, pc);
n.nmod = p.curmod;
let leaf: str;
expectident(p, &leaf);
@@ -34,7 +33,7 @@ fn parsedef(p: *parser, exported: i32) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
advance(p); // past `def`
let n: *node = newnode(p.a, nkind.N_DEF, pf, pl, pc);
let n: *node = newnode(nkind.N_DEF, pf, pl, pc);
n.nmod = p.curmod;
let id: str;
expectident(p, &id);
@@ -57,7 +56,7 @@ fn parselet(p: *parser, exported: i32) *node = {
let is_const: i32 = 0;
if (p.curkind == tkind.TK_CONST) { is_const = 1; };
advance(p);
let n: *node = newnode(p.a, nkind.N_LET, pf, pl, pc);
let n: *node = newnode(nkind.N_LET, pf, pl, pc);
n.nmod = p.curmod;
let id: str;
expectbindname(p, &id);
@@ -82,7 +81,7 @@ fn parseattrs(p: *parser) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
advance(p);
let a: *node = newnode(p.a, nkind.N_ATTR, pf, pl, pc);
let a: *node = newnode(nkind.N_ATTR, pf, pl, pc);
let id: str;
expectident(p, &id);
a.str = id;
@@ -108,7 +107,7 @@ fn parseparams(p: *parser) *node = {
let pf: str = p.curfile;
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
let n: *node = newnode(p.a, nkind.N_PARAM, pf, pl, pc);
let n: *node = newnode(nkind.N_PARAM, pf, pl, pc);
// Param form: (IDENT|'_') ':' type. Anonymous-type-only params
// (used in fn type expressions) aren't yet wired here.
let id: str;
@@ -138,7 +137,7 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
advance(p); // past `fn`
let n: *node = newnode(p.a, nkind.N_FNDECL, pf, pl, pc);
let n: *node = newnode(nkind.N_FNDECL, pf, pl, pc);
n.nmod = p.curmod;
let id: str;
expectident(p, &id);
@@ -168,7 +167,7 @@ fn parsetypedecl(p: *parser, exported: i32) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
advance(p); // past `type`
let n: *node = newnode(p.a, nkind.N_TYPEDECL, pf, pl, pc);
let n: *node = newnode(nkind.N_TYPEDECL, pf, pl, pc);
n.nmod = p.curmod;
let id: str;
expectident(p, &id);

View File

@@ -3,7 +3,6 @@
package parse;
import os;
import mem;
import tok;
// streqlocal — str-to-str compare. Inlined here to avoid a cross-
@@ -24,7 +23,7 @@ fn parseprimary(p: *parser) *node = {
let pc: i32 = p.curcol;
if (p.curkind == tkind.TK_INT) {
let n: *node = newnode(p.a, nkind.N_INTLIT, pf, pl, pc);
let n: *node = newnode(nkind.N_INTLIT, pf, pl, pc);
n.uval = p.curuval;
n.str = p.curtext;
// Plumb the typed-int suffix (`42i64`, `3u8`) through to
@@ -38,7 +37,7 @@ fn parseprimary(p: *parser) *node = {
return n;
};
if (p.curkind == tkind.TK_FLOAT) {
let n: *node = newnode(p.a, nkind.N_FLOATLIT, pf, pl, pc);
let n: *node = newnode(nkind.N_FLOATLIT, pf, pl, pc);
n.fval = p.curfval;
// uval carries the IEEE 754 bit pattern — the lexer sets
// both, and cgen consumers prefer the integer view so they
@@ -50,32 +49,32 @@ fn parseprimary(p: *parser) *node = {
return n;
};
if (p.curkind == tkind.TK_STR) {
let n: *node = newnode(p.a, nkind.N_STRLIT, pf, pl, pc);
let n: *node = newnode(nkind.N_STRLIT, pf, pl, pc);
n.str = p.curtext;
advance(p);
return n;
};
if (p.curkind == tkind.TK_RUNE) {
let n: *node = newnode(p.a, nkind.N_RUNELIT, pf, pl, pc);
let n: *node = newnode(nkind.N_RUNELIT, pf, pl, pc);
n.uval = p.curuval;
advance(p);
return n;
};
if (p.curkind == tkind.TK_TRUE) {
advance(p);
return newnode(p.a, nkind.N_TRUE, pf, pl, pc);
return newnode(nkind.N_TRUE, pf, pl, pc);
};
if (p.curkind == tkind.TK_FALSE) {
advance(p);
return newnode(p.a, nkind.N_FALSE, pf, pl, pc);
return newnode(nkind.N_FALSE, pf, pl, pc);
};
if (p.curkind == tkind.TK_NIL) {
advance(p);
return newnode(p.a, nkind.N_NIL, pf, pl, pc);
return newnode(nkind.N_NIL, pf, pl, pc);
};
if (p.curkind == tkind.TK_VOID) {
advance(p);
return newnode(p.a, nkind.N_VOIDLIT, pf, pl, pc);
return newnode(nkind.N_VOIDLIT, pf, pl, pc);
};
if (p.curkind == tkind.TK_UNDER) {
// Bare `_` — valid only as a discard lvalue. Emit an N_IDENT
@@ -83,14 +82,14 @@ fn parseprimary(p: *parser) *node = {
// already 0); the checker rejects it outside lvalue
// positions.
advance(p);
return newnode(p.a, nkind.N_IDENT, pf, pl, pc);
return newnode(nkind.N_IDENT, pf, pl, pc);
};
if (p.curkind == tkind.TK_LBRACK) {
// Array literal `[a, b, c]` or `[v, w...]` (repeat suffix).
// The repeat marker is an nkind.N_FIELD node with str = "..."
// appended to the element list so cgen can detect it.
advance(p);
let n: *node = newnode(p.a, nkind.N_ARRLIT, pf, pl, pc);
let n: *node = newnode(nkind.N_ARRLIT, pf, pl, pc);
let head: *node = nil;
let tail: *node = nil;
for (p.curkind != tkind.TK_RBRACK) {
@@ -99,7 +98,7 @@ fn parseprimary(p: *parser) *node = {
if (head == nil) { head = e; tail = e; }
else { tail.next = e; tail = e; };
if (accepttok(p, tkind.TK_ELLIPSIS)) {
let rep: *node = newnode(p.a, nkind.N_FIELD,
let rep: *node = newnode(nkind.N_FIELD,
p.curfile, p.curline, p.curcol);
rep.str = "...";
tail.next = rep;
@@ -117,7 +116,7 @@ fn parseprimary(p: *parser) *node = {
let e: *node = parseexpr(p);
// Tuple literal: (a, b, ...)
if (accepttok(p, tkind.TK_COMMA)) {
let t: *node = newnode(p.a, nkind.N_TUPLE, pf, pl, pc);
let t: *node = newnode(nkind.N_TUPLE, pf, pl, pc);
t.list = e;
let tail: *node = e;
for (true) {
@@ -134,7 +133,7 @@ fn parseprimary(p: *parser) *node = {
return e;
};
if (p.curkind == tkind.TK_IDENT) {
let n: *node = newnode(p.a, nkind.N_IDENT, pf, pl, pc);
let n: *node = newnode(nkind.N_IDENT, pf, pl, pc);
n.str = p.curtext;
advance(p);
// `IDENT {` — struct literal. Disambiguate: only consume as a
@@ -145,7 +144,7 @@ fn parseprimary(p: *parser) *node = {
// own paren/cond, so this is safe.
if (p.curkind == tkind.TK_LBRACE) {
advance(p);
let s: *node = newnode(p.a, nkind.N_STRUCTLIT, pf, pl, pc);
let s: *node = newnode(nkind.N_STRUCTLIT, pf, pl, pc);
s.lhs = n;
let head: *node = nil;
let tail: *node = nil;
@@ -165,7 +164,7 @@ fn parseprimary(p: *parser) *node = {
expectident(p, &id);
expecttok(p, tkind.TK_ASSIGN, "expected '=' in struct lit field");
let v: *node = parseexpr(p);
let f: *node = newnode(p.a, nkind.N_FIELD, fpf, fpl, fpc);
let f: *node = newnode(nkind.N_FIELD, fpf, fpl, fpc);
f.str = id;
f.lhs = v;
if (head == nil) { head = f; tail = f; }
@@ -182,7 +181,7 @@ fn parseprimary(p: *parser) *node = {
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
advance(p);
expecttok(p, tkind.TK_LPAREN, "expected '(' after match");
let m: *node = newnode(p.a, nkind.N_MATCH, pf, pl, pc);
let m: *node = newnode(nkind.N_MATCH, pf, pl, pc);
m.lhs = parseexpr(p);
expecttok(p, tkind.TK_RPAREN, "expected ')' after match scrutinee");
expecttok(p, tkind.TK_LBRACE, "expected '{' to open match body");
@@ -193,7 +192,7 @@ fn parseprimary(p: *parser) *node = {
let cl: i32 = p.curline;
let cc: i32 = p.curcol;
advance(p); // past `case`
let mc: *node = newnode(p.a, nkind.N_MCASE, cf, cl, cc);
let mc: *node = newnode(nkind.N_MCASE, cf, cl, cc);
if (p.curkind == tkind.TK_LET) {
advance(p);
let id: str;
@@ -215,7 +214,7 @@ fn parseprimary(p: *parser) *node = {
};
errmsg(p, "expected expression");
advance(p);
return newnode(p.a, nkind.N_NONE, pf, pl, pc);
return newnode(nkind.N_NONE, pf, pl, pc);
};
fn parsearglist(p: *parser, closekind: tkind, headout: **node) void = {
@@ -229,7 +228,7 @@ fn parsearglist(p: *parser, closekind: tkind, headout: **node) void = {
// marker the callee/builtin can iterate over. Mirrors
// cmd/wcc/parse.c. The only consumer today is `append`.
if (accepttok(p, tkind.TK_ELLIPSIS)) {
let sp: *node = newnode(p.a, nkind.N_SPREAD, e.file, e.line, e.col);
let sp: *node = newnode(nkind.N_SPREAD, e.file, e.line, e.col);
sp.lhs = e;
e = sp;
};
@@ -249,7 +248,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
let pc: i32 = p.curcol;
if (p.curkind == tkind.TK_LPAREN) {
advance(p);
let n: *node = newnode(p.a, nkind.N_CALL, pf, pl, pc);
let n: *node = newnode(nkind.N_CALL, pf, pl, pc);
n.lhs = cur;
// size(T)/align(T): the single arg is a type expression,
// not a regular expression. Special-case at the parser.
@@ -274,7 +273,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
// `[ : hi ]` — slice with implicit lo = 0.
if (p.curkind == tkind.TK_COLON) {
advance(p);
let n: *node = newnode(p.a, nkind.N_SLICE, pf, pl, pc);
let n: *node = newnode(nkind.N_SLICE, pf, pl, pc);
n.lhs = cur;
if (p.curkind != tkind.TK_RBRACK) {
n.cond = parseexpr(p);
@@ -291,7 +290,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
p.nocast = prev;
if (p.curkind == tkind.TK_COLON) {
advance(p);
let n: *node = newnode(p.a, nkind.N_SLICE, pf, pl, pc);
let n: *node = newnode(nkind.N_SLICE, pf, pl, pc);
n.lhs = cur;
n.rhs = e;
if (p.curkind != tkind.TK_RBRACK) {
@@ -301,7 +300,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
cur = n;
continue;
};
let n: *node = newnode(p.a, nkind.N_INDEX, pf, pl, pc);
let n: *node = newnode(nkind.N_INDEX, pf, pl, pc);
n.lhs = cur;
n.rhs = e;
expecttok(p, tkind.TK_RBRACK, "expected ']' after index");
@@ -310,7 +309,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
};
if (p.curkind == tkind.TK_DOT) {
advance(p);
let n: *node = newnode(p.a, nkind.N_DOT, pf, pl, pc);
let n: *node = newnode(nkind.N_DOT, pf, pl, pc);
n.lhs = cur;
// Hare-style tuple field access: `t.0`, `t.1`. The
// numeric literal becomes the field name string so the
@@ -331,7 +330,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
return cur;
};
advance(p);
let n: *node = newnode(p.a, nkind.N_CAST, pf, pl, pc);
let n: *node = newnode(nkind.N_CAST, pf, pl, pc);
n.lhs = cur;
n.rhs = parsetype(p);
cur = n;
@@ -343,7 +342,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
// Same precedence level as the `:` cast.
if (p.curkind == tkind.TK_AS) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TYPEASSERT, pf, pl, pc);
let n: *node = newnode(nkind.N_TYPEASSERT, pf, pl, pc);
n.lhs = cur;
n.rhs = parsetype(p);
cur = n;
@@ -351,7 +350,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
};
if (p.curkind == tkind.TK_IS) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TYPETEST, pf, pl, pc);
let n: *node = newnode(nkind.N_TYPETEST, pf, pl, pc);
n.lhs = cur;
n.rhs = parsetype(p);
cur = n;
@@ -361,14 +360,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
// `e!` — abort on error variant.
if (p.curkind == tkind.TK_QUESTION) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TRYPROP, pf, pl, pc);
let n: *node = newnode(nkind.N_TRYPROP, pf, pl, pc);
n.lhs = cur;
cur = n;
continue;
};
if (p.curkind == tkind.TK_NOT) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TRYUNW, pf, pl, pc);
let n: *node = newnode(nkind.N_TRYUNW, pf, pl, pc);
n.lhs = cur;
cur = n;
continue;
@@ -385,37 +384,37 @@ fn parseunary(p: *parser) *node = {
let k: tkind = p.curkind;
if (k == tkind.TK_MINUS) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_MINUS; n.lhs = parseunary(p);
return n;
};
if (k == tkind.TK_PLUS) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_PLUS; n.lhs = parseunary(p);
return n;
};
if (k == tkind.TK_NOT) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_NOT; n.lhs = parseunary(p);
return n;
};
if (k == tkind.TK_TILDE) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_TILDE; n.lhs = parseunary(p);
return n;
};
if (k == tkind.TK_STAR) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_STAR; n.lhs = parseunary(p);
return n;
};
if (k == tkind.TK_AMP) {
advance(p);
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
let n: *node = newnode(nkind.N_UN, pf, pl, pc);
n.op = tkind.TK_AMP; n.lhs = parseunary(p);
return n;
};
@@ -439,7 +438,7 @@ fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
if (np <= pr) { break; };
rhs = parsebin(p, rhs, np);
};
let n: *node = newnode(p.a, nkind.N_BIN, pf, pl, pc);
let n: *node = newnode(nkind.N_BIN, pf, pl, pc);
n.op = op; n.lhs = cur; n.rhs = rhs;
cur = n;
};
@@ -454,7 +453,7 @@ fn parseexpr(p: *parser) *node = {
let pc: i32 = p.curcol;
let op: tkind = p.curkind;
advance(p);
let n: *node = newnode(p.a, nkind.N_ASSIGN, pf, pl, pc);
let n: *node = newnode(nkind.N_ASSIGN, pf, pl, pc);
n.op = op;
n.lhs = e;
n.rhs = parseexpr(p); // right-associative

View File

@@ -16,12 +16,10 @@ package parse;
// dir-enum when callers `import parse;` (which dir-enums
// lib/ww/parse/).
import os;
import mem;
import tok;
type parser = struct {
l: *lex,
a: *arena,
errs: i32,
// nocast: while inside `[...]` we treat ':' as the slice
// separator, not the cast operator. Mirrors parse.c's flag.
@@ -60,9 +58,8 @@ fn refill(p: *parser) void = {
p.curtsuffix = t.tsuffix;
};
export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
export fn parserinit(p: *parser, l: *lex) void = {
p.l = l;
p.a = a;
p.errs = 0;
p.nocast = 0;
refill(p);
@@ -120,10 +117,10 @@ fn expectbindname(p: *parser, into: *str) bool = {
// Other forms (slice, array, struct, fn, chan, tuple, tagged) will
// land in subsequent commits.
// joindotted — arena-build "head.tail" for dotted type-name path
// joindotted — build "head.tail" for dotted type-name path
// collapse. Mirrors aprintf in C parser; pulled local to avoid a
// cross-module dependency.
fn joindotted(a: *arena, head: str, tail: str) str = {
fn joindotted(head: str, tail: str) str = {
let n: u64 = head.len: u64 + 1u64 + tail.len: u64;
let buf: []u8 = alloc([], n + 1u64)!;
let i: u64 = 0u64;
@@ -148,14 +145,14 @@ fn parsetype(p: *parser) *node = {
if (p.curkind == tkind.TK_NOT) {
// `!T` — Hare error-flagged type wrapper.
advance(p);
let n: *node = newnode(p.a, nkind.N_TBANG, pf, pl, pc);
let n: *node = newnode(nkind.N_TBANG, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.curkind == tkind.TK_STAR) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TPTR, pf, pl, pc);
let n: *node = newnode(nkind.N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
@@ -164,11 +161,11 @@ fn parsetype(p: *parser) *node = {
advance(p);
if (p.curkind == tkind.TK_RBRACK) {
advance(p);
let n: *node = newnode(p.a, nkind.N_TSLICE, pf, pl, pc);
let n: *node = newnode(nkind.N_TSLICE, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
let n: *node = newnode(p.a, nkind.N_TARRAY, pf, pl, pc);
let n: *node = newnode(nkind.N_TARRAY, pf, pl, pc);
// `[_]T` — length inferred from initialiser. n.rhs stays nil
// as the sentinel; the cgen path for nkind.N_LET fills it from the
// array literal's element count.
@@ -185,7 +182,7 @@ fn parsetype(p: *parser) *node = {
if (p.curkind == tkind.TK_STRUCT) {
advance(p);
expecttok(p, tkind.TK_LBRACE, "expected '{' after struct");
let n: *node = newnode(p.a, nkind.N_TSTRUCT, pf, pl, pc);
let n: *node = newnode(nkind.N_TSTRUCT, pf, pl, pc);
let fhead: *node = nil;
let ftail: *node = nil;
for (p.curkind != tkind.TK_RBRACE) {
@@ -193,7 +190,7 @@ fn parsetype(p: *parser) *node = {
let fpf: str = p.curfile;
let fpl: i32 = p.curline;
let fpc: i32 = p.curcol;
let f: *node = newnode(p.a, nkind.N_TFIELD, fpf, fpl, fpc);
let f: *node = newnode(nkind.N_TFIELD, fpf, fpl, fpc);
let fid: str;
expectident(p, &fid);
f.str = fid;
@@ -214,7 +211,7 @@ fn parsetype(p: *parser) *node = {
// nkind.N_TENUMMEMBER with str=name and lhs = value expr or nil
// (auto-increment when omitted).
advance(p);
let n: *node = newnode(p.a, nkind.N_TENUM, pf, pl, pc);
let n: *node = newnode(nkind.N_TENUM, pf, pl, pc);
if (p.curkind != tkind.TK_LBRACE) {
n.lhs = parsetype(p);
};
@@ -226,7 +223,7 @@ fn parsetype(p: *parser) *node = {
let mpf: str = p.curfile;
let mpl: i32 = p.curline;
let mpc: i32 = p.curcol;
let m: *node = newnode(p.a, nkind.N_TENUMMEMBER, mpf, mpl, mpc);
let m: *node = newnode(nkind.N_TENUMMEMBER, mpf, mpl, mpc);
let mid: str;
expectident(p, &mid);
m.str = mid;
@@ -245,14 +242,14 @@ fn parsetype(p: *parser) *node = {
if (p.curkind == tkind.TK_VOID) {
// `void` keyword in type-expr context — emit as nkind.N_TNAME so
// resolution treats it like any other primitive name.
let n: *node = newnode(p.a, nkind.N_TNAME, pf, pl, pc);
let n: *node = newnode(nkind.N_TNAME, pf, pl, pc);
n.str = "void";
advance(p);
return n;
};
if (p.curkind == tkind.TK_IDENT) {
let n: *node = newnode(p.a, nkind.N_TNAME, pf, pl, pc);
let n: *node = newnode(nkind.N_TNAME, pf, pl, pc);
let acc: str = p.curtext;
advance(p);
// Dotted path collapse: pkg.Type → single TNAME with the
@@ -260,7 +257,7 @@ fn parsetype(p: *parser) *node = {
for (p.curkind == tkind.TK_DOT) {
advance(p);
if (p.curkind != tkind.TK_IDENT) { break; };
acc = joindotted(p.a, acc, p.curtext);
acc = joindotted(acc, p.curtext);
advance(p);
};
n.str = acc;
@@ -280,7 +277,7 @@ fn parsetype(p: *parser) *node = {
let first: *node = parsetype(p);
if (firstspread) { first.op = tkind.TK_ELLIPSIS; };
if (accepttok(p, tkind.TK_PIPE)) {
let n: *node = newnode(p.a, nkind.N_TTAGGED, pf, pl, pc);
let n: *node = newnode(nkind.N_TTAGGED, pf, pl, pc);
let head: *node = first;
let tail: *node = first;
for (true) {
@@ -302,7 +299,7 @@ fn parsetype(p: *parser) *node = {
expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type");
return first;
};
let n: *node = newnode(p.a, nkind.N_TTUPLE, pf, pl, pc);
let n: *node = newnode(nkind.N_TTUPLE, pf, pl, pc);
let head: *node = first;
let tail: *node = first;
for (true) {
@@ -320,7 +317,7 @@ fn parsetype(p: *parser) *node = {
if (p.curkind == tkind.TK_FN) {
advance(p);
expecttok(p, tkind.TK_LPAREN, "expected '(' after fn in type");
let n: *node = newnode(p.a, nkind.N_TFN, pf, pl, pc);
let n: *node = newnode(nkind.N_TFN, pf, pl, pc);
// Anonymous-or-named params: parseparams handles named only;
// for fn-type expressions the C parser allows IDENT-less
// (anonymous) params. Stub: only named params for now.
@@ -332,7 +329,7 @@ fn parsetype(p: *parser) *node = {
errmsg(p, "expected type");
advance(p);
return newnode(p.a, nkind.N_TNAME, pf, pl, pc);
return newnode(nkind.N_TNAME, pf, pl, pc);
};
// ---- expressions (Pratt) ---------------------------------------------
@@ -383,7 +380,7 @@ fn isassignop(k: tkind) bool = {
// are resolved by the two-pass checker — no body-less prototypes needed.
export fn parsefile(p: *parser) *node = {
let f: *node = newnode(p.a, nkind.N_FILE, p.curfile, p.curline, p.curcol);
let f: *node = newnode(nkind.N_FILE, p.curfile, p.curline, p.curcol);
let head: *node = nil;
let tail: *node = nil;
for (p.curkind != tkind.TK_EOF) {

View File

@@ -3,7 +3,6 @@
package parse;
import os;
import mem;
import tok;
fn parseletlocal(p: *parser) *node = {
@@ -20,14 +19,14 @@ fn parseletlocal(p: *parser) *node = {
// doesn't allow types here, but cmd/wcc/parse.c does).
if (p.curkind == tkind.TK_LPAREN) {
advance(p);
let m: *node = newnode(p.a, nkind.N_MLET, pf, pl, pc);
let m: *node = newnode(nkind.N_MLET, pf, pl, pc);
let head: *node = nil;
let tail: *node = nil;
for (true) {
let lpf: str = p.curfile;
let lpl: i32 = p.curline;
let lpc: i32 = p.curcol;
let l: *node = newnode(p.a, nkind.N_LET, lpf, lpl, lpc);
let l: *node = newnode(nkind.N_LET, lpf, lpl, lpc);
let id: str;
expectbindname(p, &id);
l.str = id;
@@ -50,7 +49,7 @@ fn parseletlocal(p: *parser) *node = {
return m;
};
let n: *node = newnode(p.a, nkind.N_LET, pf, pl, pc);
let n: *node = newnode(nkind.N_LET, pf, pl, pc);
let id: str;
expectbindname(p, &id);
n.str = id;
@@ -61,14 +60,14 @@ fn parseletlocal(p: *parser) *node = {
// Collects (name, type) pairs, then '=' rhs. Each binding gets
// its own nkind.N_LET; the wrapping nkind.N_MLET carries the rhs.
if (p.curkind == tkind.TK_COMMA) {
let m: *node = newnode(p.a, nkind.N_MLET, pf, pl, pc);
let m: *node = newnode(nkind.N_MLET, pf, pl, pc);
let head: *node = n;
let tail: *node = n;
for (accepttok(p, tkind.TK_COMMA)) {
let lpf: str = p.curfile;
let lpl: i32 = p.curline;
let lpc: i32 = p.curcol;
let l: *node = newnode(p.a, nkind.N_LET, lpf, lpl, lpc);
let l: *node = newnode(nkind.N_LET, lpf, lpl, lpc);
let id2: str;
expectbindname(p, &id2);
l.str = id2;
@@ -100,7 +99,7 @@ fn parseblock(p: *parser) *node = {
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
expecttok(p, tkind.TK_LBRACE, "expected '{' to open block");
let blk: *node = newnode(p.a, nkind.N_BLOCK, pf, pl, pc);
let blk: *node = newnode(nkind.N_BLOCK, pf, pl, pc);
let head: *node = nil;
let tail: *node = nil;
for (p.curkind != tkind.TK_RBRACE) {
@@ -122,7 +121,7 @@ fn parseif(p: *parser) *node = {
let pc: i32 = p.curcol;
advance(p); // past `if`
expecttok(p, tkind.TK_LPAREN, "expected '(' after if");
let n: *node = newnode(p.a, nkind.N_IF, pf, pl, pc);
let n: *node = newnode(nkind.N_IF, pf, pl, pc);
n.cond = parseexpr(p);
expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition");
n.body = parseblock(p);
@@ -162,7 +161,7 @@ fn parsefor(p: *parser) *node = {
let npf: str = p.curfile;
let npl: i32 = p.curline;
let npc: i32 = p.curcol;
let e: *node = newnode(p.a, nkind.N_IDENT, npf, npl, npc);
let e: *node = newnode(nkind.N_IDENT, npf, npl, npc);
let nm: str;
expectbindname(p, &nm);
e.str = nm;
@@ -173,7 +172,7 @@ fn parsefor(p: *parser) *node = {
};
expecttok(p, tkind.TK_RPAREN, "expected ')' in for-range names");
expecttok(p, tkind.TK_DOTDOT, "expected '..' after for-range names");
let rng: *node = newnode(p.a, nkind.N_FORRANGE, pf, pl, pc);
let rng: *node = newnode(nkind.N_FORRANGE, pf, pl, pc);
rng.list = names;
rng.lhs = parseexpr(p);
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
@@ -198,7 +197,7 @@ fn parsefor(p: *parser) *node = {
if (p.curkind == tkind.TK_DOTDOT) {
advance(p);
let rng: *node = newnode(p.a, nkind.N_FORRANGE, pf, pl, pc);
let rng: *node = newnode(nkind.N_FORRANGE, pf, pl, pc);
rng.str = nm; // "" for `_`
rng.lhs = parseexpr(p);
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
@@ -209,12 +208,12 @@ fn parsefor(p: *parser) *node = {
// Not a range — finish the let manually and continue as
// a 3-clause for-init.
let first: *node = newnode(p.a, nkind.N_LET, lpf, lpl, lpc);
let first: *node = newnode(nkind.N_LET, lpf, lpl, lpc);
first.str = nm;
if (accepttok(p, tkind.TK_COLON)) { first.lhs = parsetype(p); };
if (accepttok(p, tkind.TK_ASSIGN)) { first.rhs = parseexpr(p); };
expecttok(p, tkind.TK_SEMI, "expected ';' after for-init let");
let n: *node = newnode(p.a, nkind.N_FOR, pf, pl, pc);
let n: *node = newnode(nkind.N_FOR, pf, pl, pc);
n.lhs = first;
n.cond = parseexpr(p);
expecttok(p, tkind.TK_SEMI, "expected ';' after for cond");
@@ -229,7 +228,7 @@ fn parsefor(p: *parser) *node = {
};
// for (cond) or for (cond; post)
let n: *node = newnode(p.a, nkind.N_FOR, pf, pl, pc);
let n: *node = newnode(nkind.N_FOR, pf, pl, pc);
let first: *node = parseexpr(p);
if (accepttok(p, tkind.TK_SEMI)) {
n.cond = first;
@@ -253,7 +252,7 @@ fn parseswitch(p: *parser) *node = {
let pc: i32 = p.curcol;
advance(p); // past `switch`
expecttok(p, tkind.TK_LPAREN, "expected '(' after switch");
let n: *node = newnode(p.a, nkind.N_SWITCH, pf, pl, pc);
let n: *node = newnode(nkind.N_SWITCH, pf, pl, pc);
n.lhs = parseexpr(p);
expecttok(p, tkind.TK_RPAREN, "expected ')' after switch expression");
expecttok(p, tkind.TK_LBRACE, "expected '{' to open switch body");
@@ -264,7 +263,7 @@ fn parseswitch(p: *parser) *node = {
let cpl: i32 = p.curline;
let cpc: i32 = p.curcol;
advance(p); // past `case`
let cs: *node = newnode(p.a, nkind.N_CASE, cpf, cpl, cpc);
let cs: *node = newnode(nkind.N_CASE, cpf, cpl, cpc);
let eh: *node = nil;
let et: *node = nil;
if (p.curkind != tkind.TK_COLON) {
@@ -292,7 +291,7 @@ fn parseswitch(p: *parser) *node = {
bt = s;
};
};
let blk: *node = newnode(p.a, nkind.N_BLOCK, cpf, cpl, cpc);
let blk: *node = newnode(nkind.N_BLOCK, cpf, cpl, cpc);
blk.list = bh;
cs.body = blk;
if (head == nil) { head = cs; }
@@ -337,13 +336,13 @@ fn parsestmt(p: *parser) *node = {
};
if (p.curkind == tkind.TK_RETURN) {
advance(p);
let n: *node = newnode(p.a, nkind.N_RETURN, pf, pl, pc);
let n: *node = newnode(nkind.N_RETURN, pf, pl, pc);
if (p.curkind != tkind.TK_SEMI) {
let first: *node = parseexpr(p);
// Hare-style multi-value: `return a, b;` becomes a
// tuple expression so codegen sees one rvalue.
if (p.curkind == tkind.TK_COMMA) {
let t: *node = newnode(p.a, nkind.N_TUPLE, pf, pl, pc);
let t: *node = newnode(nkind.N_TUPLE, pf, pl, pc);
t.list = first;
let tail: *node = first;
for (accepttok(p, tkind.TK_COMMA)) {
@@ -361,14 +360,14 @@ fn parsestmt(p: *parser) *node = {
};
if (p.curkind == tkind.TK_DEFER) {
advance(p);
let n: *node = newnode(p.a, nkind.N_DEFER, pf, pl, pc);
let n: *node = newnode(nkind.N_DEFER, pf, pl, pc);
n.lhs = parseexpr(p);
expecttok(p, tkind.TK_SEMI, "expected ';' after defer");
return n;
};
if (p.curkind == tkind.TK_YIELD) {
advance(p);
let n: *node = newnode(p.a, nkind.N_YIELD, pf, pl, pc);
let n: *node = newnode(nkind.N_YIELD, pf, pl, pc);
n.lhs = parseexpr(p);
expecttok(p, tkind.TK_SEMI, "expected ';' after yield");
return n;
@@ -376,12 +375,12 @@ fn parsestmt(p: *parser) *node = {
if (p.curkind == tkind.TK_BREAK) {
advance(p);
expecttok(p, tkind.TK_SEMI, "expected ';' after break");
return newnode(p.a, nkind.N_BREAK, pf, pl, pc);
return newnode(nkind.N_BREAK, pf, pl, pc);
};
if (p.curkind == tkind.TK_CONTINUE) {
advance(p);
expecttok(p, tkind.TK_SEMI, "expected ';' after continue");
return newnode(p.a, nkind.N_CONTINUE, pf, pl, pc);
return newnode(nkind.N_CONTINUE, pf, pl, pc);
};
// expression statement, or tuple-destructure multi-assign:
// a, b = expr;
@@ -391,7 +390,7 @@ fn parsestmt(p: *parser) *node = {
// consume — parseexpr would absorb it.
let e: *node = parseexpr(p);
if (p.curkind == tkind.TK_COMMA) {
let m: *node = newnode(p.a, nkind.N_MASSIGN, pf, pl, pc);
let m: *node = newnode(nkind.N_MASSIGN, pf, pl, pc);
let head: *node = e;
let tail: *node = e;
for (p.curkind == tkind.TK_COMMA) {
@@ -406,7 +405,7 @@ fn parsestmt(p: *parser) *node = {
expecttok(p, tkind.TK_SEMI, "expected ';' after multi-assign");
return m;
};
let n: *node = newnode(p.a, nkind.N_EXPRSTMT, pf, pl, pc);
let n: *node = newnode(nkind.N_EXPRSTMT, pf, pl, pc);
n.lhs = e;
expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement");
return n;

View File

@@ -6,10 +6,6 @@
package ww;
// Sibling imports (typ, ast) auto-resolve via task #22 dir-enum
// when callers `import ww;` or pull all three separately.
import mem;
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
type skind = enum i32 {
SK_NONE = 0,
@@ -47,7 +43,6 @@ type scope = struct {
last: *sym,
buckets: **sym, // length = NBUCKETS
nbuckets: i32,
a: *arena,
};
// FNV-1a 64 — same hash the C side uses, so bucket distribution is
@@ -64,9 +59,9 @@ fn hashstr(s: str) u64 = {
return h;
};
export fn newscope(a: *arena, parent: *scope) *scope = {
export fn newscope(parent: *scope) *scope = {
let buckets_sl: []*sym = alloc([], NBUCKETS: u64)!;
let s: *scope = alloc(scope{parent=parent, first=nil, last=nil, buckets=buckets_sl.ptr, nbuckets=NBUCKETS, a=a})!;
let s: *scope = alloc(scope{parent=parent, first=nil, last=nil, buckets=buckets_sl.ptr, nbuckets=NBUCKETS})!;
return s;
};

View File

@@ -4,12 +4,11 @@
// the primitive types (tyvoid, tyi32, …); ww doesn't have writable
// global storage yet, so we bundle the primitives into a `tctx` that
// the checker passes around explicitly. typesinit fills the tctx
// once per arena.
// once per program.
package ww;
import os;
import mem;
// ---- TypeKind ---------------------------------------------------------
// Numeric values must stay aligned with cmd/wcc/ww.h TypeKind so the
@@ -118,7 +117,6 @@ type tinfocacheent = struct {
// ---- tctx — the box of primitive types -------------------------------
type tctx = struct {
a: *arena,
tyvoid: *tinfo,
tybool: *tinfo,
tyrune: *tinfo,
@@ -149,13 +147,13 @@ type tctx = struct {
// ---- constructors -----------------------------------------------------
export fn newtype(a: *arena, k: tykind) *tinfo = {
export fn newtype(k: tykind) *tinfo = {
let t: *tinfo = alloc(tinfo{kind=k, size=0u64, align=0u64, sub=nil, alen=0u64, fields=nil, params=nil, ret=nil, variadic=0, nullable=0, name="", under=nil, slotsize=0u64})!;
return t;
};
fn prim(a: *arena, k: tykind, nm: str, sz: u64, al: u64) *tinfo = {
let t: *tinfo = newtype(a, k);
fn prim(k: tykind, nm: str, sz: u64, al: u64) *tinfo = {
let t: *tinfo = newtype(k);
t.name = nm;
t.size = sz;
if (al > 0u64) { t.align = al; } else { t.align = sz; };
@@ -163,38 +161,37 @@ fn prim(a: *arena, k: tykind, nm: str, sz: u64, al: u64) *tinfo = {
return t;
};
export fn typesinit(c: *tctx, a: *arena) void = {
c.a = a;
c.tyvoid = prim(a, tykind.TY_VOID, "void", 0u64, 1u64);
c.tybool = prim(a, tykind.TY_BOOL, "bool", 1u64, 1u64);
c.tyrune = prim(a, tykind.TY_RUNE, "rune", 4u64, 4u64);
c.tyi8 = prim(a, tykind.TY_I8, "i8", 1u64, 1u64);
c.tyi16 = prim(a, tykind.TY_I16, "i16", 2u64, 2u64);
c.tyi32 = prim(a, tykind.TY_I32, "i32", 4u64, 4u64);
c.tyi64 = prim(a, tykind.TY_I64, "i64", 8u64, 8u64);
c.tyu8 = prim(a, tykind.TY_U8, "u8", 1u64, 1u64);
c.tyu16 = prim(a, tykind.TY_U16, "u16", 2u64, 2u64);
c.tyu32 = prim(a, tykind.TY_U32, "u32", 4u64, 4u64);
c.tyu64 = prim(a, tykind.TY_U64, "u64", 8u64, 8u64);
c.tyint = prim(a, tykind.TY_INT, "int", 8u64, 8u64);
c.tyuint = prim(a, tykind.TY_UINT, "uint", 8u64, 8u64);
c.tyuintptr= prim(a, tykind.TY_UINTPTR, "uintptr", 8u64, 8u64);
c.tyf32 = prim(a, tykind.TY_F32, "f32", 4u64, 4u64);
c.tyf64 = prim(a, tykind.TY_F64, "f64", 8u64, 8u64);
c.tystr = prim(a, tykind.TY_STR, "str", 16u64, 8u64); // sizelint-ok: SSoT for tystr (#64)
c.tyerr = prim(a, tykind.TY_ERR, "<err>", 0u64, 1u64);
c.tynever = prim(a, tykind.TY_NEVER, "never", 0u64, 1u64);
export fn typesinit(c: *tctx) void = {
c.tyvoid = prim(tykind.TY_VOID, "void", 0u64, 1u64);
c.tybool = prim(tykind.TY_BOOL, "bool", 1u64, 1u64);
c.tyrune = prim(tykind.TY_RUNE, "rune", 4u64, 4u64);
c.tyi8 = prim(tykind.TY_I8, "i8", 1u64, 1u64);
c.tyi16 = prim(tykind.TY_I16, "i16", 2u64, 2u64);
c.tyi32 = prim(tykind.TY_I32, "i32", 4u64, 4u64);
c.tyi64 = prim(tykind.TY_I64, "i64", 8u64, 8u64);
c.tyu8 = prim(tykind.TY_U8, "u8", 1u64, 1u64);
c.tyu16 = prim(tykind.TY_U16, "u16", 2u64, 2u64);
c.tyu32 = prim(tykind.TY_U32, "u32", 4u64, 4u64);
c.tyu64 = prim(tykind.TY_U64, "u64", 8u64, 8u64);
c.tyint = prim(tykind.TY_INT, "int", 8u64, 8u64);
c.tyuint = prim(tykind.TY_UINT, "uint", 8u64, 8u64);
c.tyuintptr= prim(tykind.TY_UINTPTR, "uintptr", 8u64, 8u64);
c.tyf32 = prim(tykind.TY_F32, "f32", 4u64, 4u64);
c.tyf64 = prim(tykind.TY_F64, "f64", 8u64, 8u64);
c.tystr = prim(tykind.TY_STR, "str", 16u64, 8u64); // sizelint-ok: SSoT for tystr (#64)
c.tyerr = prim(tykind.TY_ERR, "<err>", 0u64, 1u64);
c.tynever = prim(tykind.TY_NEVER, "never", 0u64, 1u64);
c.tyuntypedint = prim(a, tykind.TY_UNTYPED_INT, "untyped_int", 0u64, 1u64);
c.tyuntypedfloat = prim(a, tykind.TY_UNTYPED_FLOAT, "untyped_float", 0u64, 1u64);
c.tyuntypedstr = prim(a, tykind.TY_UNTYPED_STR, "untyped_str", 0u64, 1u64);
c.tyuntypedrune = prim(a, tykind.TY_UNTYPED_RUNE, "untyped_rune", 0u64, 1u64);
c.tyuntypedbool = prim(a, tykind.TY_UNTYPED_BOOL, "untyped_bool", 0u64, 1u64);
c.tyuntypednil = prim(a, tykind.TY_UNTYPED_NIL, "untyped_nil", 0u64, 1u64);
c.tyuntypedint = prim(tykind.TY_UNTYPED_INT, "untyped_int", 0u64, 1u64);
c.tyuntypedfloat = prim(tykind.TY_UNTYPED_FLOAT, "untyped_float", 0u64, 1u64);
c.tyuntypedstr = prim(tykind.TY_UNTYPED_STR, "untyped_str", 0u64, 1u64);
c.tyuntypedrune = prim(tykind.TY_UNTYPED_RUNE, "untyped_rune", 0u64, 1u64);
c.tyuntypedbool = prim(tykind.TY_UNTYPED_BOOL, "untyped_bool", 0u64, 1u64);
c.tyuntypednil = prim(tykind.TY_UNTYPED_NIL, "untyped_nil", 0u64, 1u64);
};
export fn typeptr(a: *arena, sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(a, tykind.TY_PTR);
export fn typeptr(sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(tykind.TY_PTR);
t.sub = sub;
t.size = 8u64;
t.align = 8u64;
@@ -202,8 +199,8 @@ export fn typeptr(a: *arena, sub: *tinfo) *tinfo = {
return t;
};
export fn typeslice(a: *arena, sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(a, tykind.TY_SLICE);
export fn typeslice(sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(tykind.TY_SLICE);
t.sub = sub;
t.size = 24u64; // sizelint-ok: SSoT for slice header (#64)
t.align = 8u64;
@@ -211,8 +208,8 @@ export fn typeslice(a: *arena, sub: *tinfo) *tinfo = {
return t;
};
export fn typearray(a: *arena, sub: *tinfo, n: u64) *tinfo = {
let t: *tinfo = newtype(a, tykind.TY_ARRAY);
export fn typearray(sub: *tinfo, n: u64) *tinfo = {
let t: *tinfo = newtype(tykind.TY_ARRAY);
t.sub = sub;
t.alen = n;
if (sub != nil) {
@@ -229,8 +226,8 @@ export fn typearray(a: *arena, sub: *tinfo, n: u64) *tinfo = {
return t;
};
export fn typechan(a: *arena, sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(a, tykind.TY_CHAN);
export fn typechan(sub: *tinfo) *tinfo = {
let t: *tinfo = newtype(tykind.TY_CHAN);
t.sub = sub;
t.size = 8u64;
t.align = 8u64;
@@ -238,8 +235,8 @@ export fn typechan(a: *arena, sub: *tinfo) *tinfo = {
return t;
};
export fn typenamed(a: *arena, name: str, under: *tinfo) *tinfo = {
let t: *tinfo = newtype(a, tykind.TY_NAMED);
export fn typenamed(name: str, under: *tinfo) *tinfo = {
let t: *tinfo = newtype(tykind.TY_NAMED);
t.name = name;
t.under = under;
if (under != nil) {