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:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user