Files
ww/selfhost/cmd/wcc/parse.ww

987 lines
27 KiB
Plaintext

// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
//
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
// RET;` (header-only — bodies are recovered past). Unknown decls are
// chewed token-by-token until the next ';' so the diff probe can
// still anchor on partial fixtures.
//
// The full port is multi-session work — parse.c is 1,183 lines of
// hand-rolled recursive descent + Pratt expression parser. Each
// surface form lands here gradually so the AST diff in 990_selfhost
// grows toward whole-language coverage one increment at a time.
//
// Calling-convention shim: w6c can't yet pass a sub-struct field
// (e.g. p.cur.line where p.cur is a `tok` of size 76). The parser
// stores the current token as flat primitive fields rather than a
// nested `tok` struct; `refill` copies a freshly lexed token in.
use os;
use mem;
use 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.
nocast: i32,
cur_kind: i32,
cur_file: str,
cur_line: i32,
cur_col: i32,
cur_text: str,
cur_uval: u64,
};
fn refill(p: *parser) void = {
let t: tok;
lexnext(p.l, &t);
p.cur_kind = t.kind;
p.cur_file = t.file;
p.cur_line = t.line;
p.cur_col = t.col;
p.cur_text = t.text;
p.cur_uval = t.uval;
};
export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
p.l = l;
p.a = a;
p.errs = 0;
p.nocast = 0;
refill(p);
};
fn advance(p: *parser) void = { refill(p); };
fn accepttok(p: *parser, k: i32) bool = {
if (p.cur_kind == k) { advance(p); return true; };
return false;
};
fn errmsg(p: *parser, msg: str) void = {
let pre: str = "parse: ";
os.write(2, pre.ptr, pre.len: u64);
os.write(2, msg.ptr, msg.len: u64);
os.write(2, "\n".ptr, 1u64);
p.errs += 1;
};
fn expecttok(p: *parser, k: i32, what: str) bool = {
if (p.cur_kind == k) { advance(p); return true; };
errmsg(p, what);
return false;
};
// expectident — consume the current TK_IDENT and return its text.
// Returns the empty str on error (and advances to make progress).
fn expectident(p: *parser, into: *str) bool = {
if (p.cur_kind != TK_IDENT) {
errmsg(p, "expected identifier");
advance(p);
return false;
};
*into = p.cur_text;
advance(p);
return true;
};
// ---- type expressions ------------------------------------------------
//
// Currently: TNAME (single ident, no dotted path yet) and TPTR (`*T`).
// Other forms (slice, array, struct, fn, chan, tuple, tagged) will
// land in subsequent commits.
fn parsetype(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
if (p.cur_kind == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.cur_kind == TK_LBRACK) {
advance(p);
if (p.cur_kind == TK_RBRACK) {
advance(p);
let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
let n: *node = newnode(p.a, N_TARRAY, pf, pl, pc);
n.rhs = parseexpr(p);
expecttok(p, TK_RBRACK, "expected ']' in array type");
n.lhs = parsetype(p);
return n;
};
if (p.cur_kind == TK_STRUCT) {
advance(p);
expecttok(p, TK_LBRACE, "expected '{' after struct");
let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc);
let fhead: *node = nil;
let ftail: *node = nil;
for (p.cur_kind != TK_RBRACE) {
if (p.cur_kind == TK_EOF) { break; };
let fpf: str = p.cur_file;
let fpl: i32 = p.cur_line;
let fpc: i32 = p.cur_col;
let f: *node = newnode(p.a, N_TFIELD, fpf, fpl, fpc);
let fid: str;
expectident(p, &fid);
f.str = fid;
expecttok(p, TK_COLON, "expected ':' in field");
f.lhs = parsetype(p);
if (fhead == nil) { fhead = f; ftail = f; }
else { ftail.next = f; ftail = f; };
if (!accepttok(p, TK_COMMA)) { break; };
};
expecttok(p, TK_RBRACE, "expected '}' after struct fields");
n.list = fhead;
return n;
};
if (p.cur_kind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.cur_text;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
return n;
};
if (p.cur_kind == TK_LPAREN) {
// (T) or (T, T, ...) or (T | T | ...)
advance(p);
let first: *node = parsetype(p);
if (accepttok(p, TK_PIPE)) {
let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc);
let head: *node = first;
let tail: *node = first;
for (true) {
let e: *node = parsetype(p);
tail.next = e;
tail = e;
if (!accepttok(p, TK_PIPE)) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tagged-union type");
n.list = head;
return n;
};
if (!accepttok(p, TK_COMMA)) {
expecttok(p, TK_RPAREN, "expected ')' after parenthesised type");
return first;
};
let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc);
let head: *node = first;
let tail: *node = first;
for (true) {
let e: *node = parsetype(p);
tail.next = e;
tail = e;
if (!accepttok(p, TK_COMMA)) { break; };
if (p.cur_kind == TK_RPAREN) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
n.list = head;
return n;
};
if (p.cur_kind == TK_FN) {
advance(p);
expecttok(p, TK_LPAREN, "expected '(' after fn in type");
let n: *node = newnode(p.a, 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.
n.list = parseparams(p);
expecttok(p, TK_RPAREN, "expected ')' after fn type params");
n.lhs = parsetype(p);
return n;
};
errmsg(p, "expected type");
advance(p);
return newnode(p.a, N_TNAME, pf, pl, pc);
};
// ---- expressions (Pratt) ---------------------------------------------
//
// Forwards: parseexpr → parsebin → parseunary → parsepostfix(parseprimary).
// Tuple literals, match expressions, struct literals, slice [lo:hi],
// and the ?/! try operators are not yet wired — they'll arrive as the
// AST diff fixture grows to need them.
fn bprec(k: i32) i32 = {
if (k == TK_OR) { return 1; };
if (k == TK_AND) { return 2; };
if (k == TK_EQ) { return 3; };
if (k == TK_NEQ) { return 3; };
if (k == TK_LT) { return 4; };
if (k == TK_LE) { return 4; };
if (k == TK_GT) { return 4; };
if (k == TK_GE) { return 4; };
if (k == TK_PIPE) { return 5; };
if (k == TK_CARET) { return 6; };
if (k == TK_AMP) { return 7; };
if (k == TK_LSHIFT) { return 8; };
if (k == TK_RSHIFT) { return 8; };
if (k == TK_PLUS) { return 9; };
if (k == TK_MINUS) { return 9; };
if (k == TK_STAR) { return 10; };
if (k == TK_SLASH) { return 10; };
if (k == TK_PERCENT) { return 10; };
return 0;
};
fn isassignop(k: i32) bool = {
if (k == TK_ASSIGN) { return true; };
if (k == TK_PLUSEQ) { return true; };
if (k == TK_MINUSEQ) { return true; };
if (k == TK_STAREQ) { return true; };
if (k == TK_SLASHEQ) { return true; };
if (k == TK_PERCENTEQ) { return true; };
if (k == TK_AMPEQ) { return true; };
if (k == TK_PIPEEQ) { return true; };
if (k == TK_CARETEQ) { return true; };
if (k == TK_LSHIFTEQ) { return true; };
if (k == TK_RSHIFTEQ) { return true; };
return false;
};
// Forward references between parseunary/parseexpr/parsebin/parsepostfix
// are resolved by the two-pass checker — no body-less prototypes needed.
fn parseprimary(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
if (p.cur_kind == TK_INT) {
let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc);
n.uval = p.cur_uval;
n.str = p.cur_text;
advance(p);
return n;
};
if (p.cur_kind == TK_STR) {
let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc);
n.str = p.cur_text;
advance(p);
return n;
};
if (p.cur_kind == TK_RUNE) {
let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc);
n.uval = p.cur_uval;
advance(p);
return n;
};
if (p.cur_kind == TK_TRUE) {
advance(p);
return newnode(p.a, N_TRUE, pf, pl, pc);
};
if (p.cur_kind == TK_FALSE) {
advance(p);
return newnode(p.a, N_FALSE, pf, pl, pc);
};
if (p.cur_kind == TK_NIL) {
advance(p);
return newnode(p.a, N_NIL, pf, pl, pc);
};
if (p.cur_kind == TK_LPAREN) {
advance(p);
let e: *node = parseexpr(p);
// Tuple literal: (a, b, ...)
if (accepttok(p, TK_COMMA)) {
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
t.list = e;
let tail: *node = e;
for (true) {
if (p.cur_kind == TK_RPAREN) { break; };
let en: *node = parseexpr(p);
tail.next = en;
tail = en;
if (!accepttok(p, TK_COMMA)) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple");
return t;
};
expecttok(p, TK_RPAREN, "expected ')'");
return e;
};
if (p.cur_kind == TK_IDENT) {
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
n.str = p.cur_text;
advance(p);
// `IDENT {` — struct literal. Disambiguate: only consume as a
// struct lit when we're not in a context where '{' starts a
// block (e.g. `if (cond) {`). The parser is called from
// expressions, never directly from cond contexts that need a
// block; in stmt parsing, the for/if drivers consume their
// own paren/cond, so this is safe.
if (p.cur_kind == TK_LBRACE) {
advance(p);
let s: *node = newnode(p.a, N_STRUCTLIT, pf, pl, pc);
s.lhs = n;
let head: *node = nil;
let tail: *node = nil;
for (p.cur_kind != TK_RBRACE) {
if (p.cur_kind == TK_EOF) { break; };
let fpf: str = p.cur_file;
let fpl: i32 = p.cur_line;
let fpc: i32 = p.cur_col;
let id: str;
expectident(p, &id);
expecttok(p, TK_ASSIGN, "expected '=' in struct lit field");
let v: *node = parseexpr(p);
let f: *node = newnode(p.a, N_FIELD, fpf, fpl, fpc);
f.str = id;
f.lhs = v;
if (head == nil) { head = f; tail = f; }
else { tail.next = f; tail = f; };
if (!accepttok(p, TK_COMMA)) { break; };
};
expecttok(p, TK_RBRACE, "expected '}' after struct literal");
s.list = head;
return s;
};
return n;
};
if (p.cur_kind == TK_MATCH) {
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
advance(p);
expecttok(p, TK_LPAREN, "expected '(' after match");
let m: *node = newnode(p.a, N_MATCH, pf, pl, pc);
m.lhs = parseexpr(p);
expecttok(p, TK_RPAREN, "expected ')' after match scrutinee");
expecttok(p, TK_LBRACE, "expected '{' to open match body");
let head: *node = nil;
let tail: *node = nil;
for (p.cur_kind == TK_CASE) {
let cf: str = p.cur_file;
let cl: i32 = p.cur_line;
let cc: i32 = p.cur_col;
advance(p); // past `case`
let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc);
if (p.cur_kind == TK_LET) {
advance(p);
let id: str;
expectident(p, &id);
mc.str = id;
expecttok(p, TK_COLON, "expected ':' after match binding");
mc.lhs = parsetype(p);
} else { if (p.cur_kind != TK_FATARROW) {
mc.lhs = parsetype(p);
};};
expecttok(p, TK_FATARROW, "expected '=>' in match arm");
mc.body = parsestmt(p);
if (head == nil) { head = mc; tail = mc; }
else { tail.next = mc; tail = mc; };
};
expecttok(p, TK_RBRACE, "expected '}' after match body");
m.list = head;
return m;
};
errmsg(p, "expected expression");
advance(p);
return newnode(p.a, N_NONE, pf, pl, pc);
};
fn parsearglist(p: *parser, close_kind: i32, head_out: **node) void = {
*head_out = nil;
if (p.cur_kind == close_kind) { return; };
let head: *node = nil;
let tail: *node = nil;
for (true) {
let e: *node = parseexpr(p);
if (head == nil) { head = e; tail = e; }
else { tail.next = e; tail = e; };
if (!accepttok(p, TK_COMMA)) { break; };
if (p.cur_kind == close_kind) { break; };
};
*head_out = head;
};
fn parsepostfix(p: *parser, lhs: *node) *node = {
let cur: *node = lhs;
for (true) {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
if (p.cur_kind == TK_LPAREN) {
advance(p);
let n: *node = newnode(p.a, N_CALL, pf, pl, pc);
n.lhs = cur;
let arghead: *node = nil;
parsearglist(p, TK_RPAREN, &arghead);
n.list = arghead;
expecttok(p, TK_RPAREN, "expected ')' after args");
cur = n;
continue;
};
if (p.cur_kind == TK_LBRACK) {
advance(p);
// `[ : hi ]` — slice with implicit lo = 0.
if (p.cur_kind == TK_COLON) {
advance(p);
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
n.lhs = cur;
if (p.cur_kind != TK_RBRACK) {
n.cond = parseexpr(p);
};
expecttok(p, TK_RBRACK, "expected ']' in slice");
cur = n;
continue;
};
// Suppress cast inside `[...]` so ':' parses as slice
// separator rather than the postfix cast operator.
let prev: i32 = p.nocast;
p.nocast = 1;
let e: *node = parseexpr(p);
p.nocast = prev;
if (p.cur_kind == TK_COLON) {
advance(p);
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
n.lhs = cur;
n.rhs = e;
if (p.cur_kind != TK_RBRACK) {
n.cond = parseexpr(p);
};
expecttok(p, TK_RBRACK, "expected ']' in slice");
cur = n;
continue;
};
let n: *node = newnode(p.a, N_INDEX, pf, pl, pc);
n.lhs = cur;
n.rhs = e;
expecttok(p, TK_RBRACK, "expected ']' after index");
cur = n;
continue;
};
if (p.cur_kind == TK_DOT) {
advance(p);
let n: *node = newnode(p.a, N_DOT, pf, pl, pc);
n.lhs = cur;
let id: str;
expectident(p, &id);
n.str = id;
cur = n;
continue;
};
if (p.cur_kind == TK_COLON) {
if (p.nocast != 0) {
return cur;
};
advance(p);
let n: *node = newnode(p.a, N_CAST, pf, pl, pc);
n.lhs = cur;
n.rhs = parsetype(p);
cur = n;
continue;
};
break;
};
return cur;
};
fn parseunary(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
let k: i32 = p.cur_kind;
if (k == TK_MINUS) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_MINUS; n.lhs = parseunary(p);
return n;
};
if (k == TK_PLUS) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_PLUS; n.lhs = parseunary(p);
return n;
};
if (k == TK_NOT) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_NOT; n.lhs = parseunary(p);
return n;
};
if (k == TK_TILDE) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_TILDE; n.lhs = parseunary(p);
return n;
};
if (k == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_STAR; n.lhs = parseunary(p);
return n;
};
if (k == TK_AMP) {
advance(p);
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
n.op = TK_AMP; n.lhs = parseunary(p);
return n;
};
return parsepostfix(p, parseprimary(p));
};
fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
let cur: *node = lhs;
for (true) {
let op: i32 = p.cur_kind;
let pr: i32 = bprec(op);
if (pr == 0) { return cur; };
if (pr < minp) { return cur; };
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p);
let rhs: *node = parseunary(p);
for (true) {
let np: i32 = bprec(p.cur_kind);
if (np <= pr) { break; };
rhs = parsebin(p, rhs, np);
};
let n: *node = newnode(p.a, N_BIN, pf, pl, pc);
n.op = op; n.lhs = cur; n.rhs = rhs;
cur = n;
};
return cur;
};
fn parseexpr(p: *parser) *node = {
let e: *node = parsebin(p, parseunary(p), 1);
if (isassignop(p.cur_kind)) {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
let op: i32 = p.cur_kind;
advance(p);
let n: *node = newnode(p.a, N_ASSIGN, pf, pl, pc);
n.op = op;
n.lhs = e;
n.rhs = parseexpr(p); // right-associative
return n;
};
return e;
};
// ---- statements ------------------------------------------------------
//
// Subset wired today: block, let, return, if (no else-if chain), for
// (single-cond C-style), expr-stmt, defer, break, continue. Switch
// and match arms are not yet wired; tuple-let / multi-let neither.
fn parseletlocal(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `let`
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
let id: str;
expectident(p, &id);
n.str = id;
if (accepttok(p, TK_COLON)) {
n.lhs = parsetype(p);
};
if (accepttok(p, TK_ASSIGN)) {
n.rhs = parseexpr(p);
};
expecttok(p, TK_SEMI, "expected ';' after let");
return n;
};
fn parseblock(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
expecttok(p, TK_LBRACE, "expected '{' to open block");
let blk: *node = newnode(p.a, N_BLOCK, pf, pl, pc);
let head: *node = nil;
let tail: *node = nil;
for (p.cur_kind != TK_RBRACE) {
if (p.cur_kind == TK_EOF) { break; };
let s: *node = parsestmt(p);
if (s != nil) {
if (head == nil) { head = s; tail = s; }
else { tail.next = s; tail = s; };
};
};
expecttok(p, TK_RBRACE, "expected '}' to close block");
blk.list = head;
return blk;
};
fn parseif(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `if`
expecttok(p, TK_LPAREN, "expected '(' after if");
let n: *node = newnode(p.a, N_IF, pf, pl, pc);
n.cond = parseexpr(p);
expecttok(p, TK_RPAREN, "expected ')' after if condition");
n.body = parseblock(p);
if (accepttok(p, TK_ELSE)) {
if (p.cur_kind == TK_IF) {
n.els = parseif(p);
} else {
n.els = parseblock(p);
};
};
return n;
};
fn parsefor(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `for`
expecttok(p, TK_LPAREN, "expected '(' after for");
let n: *node = newnode(p.a, N_FOR, pf, pl, pc);
// Three forms (matching C parser):
// for (cond) — only cond
// for (init; cond; post) — full
// for (true) — infinite (cond is N_TRUE)
// Distinguish by counting ';'. Look at first chunk: if it's a
// `let` stmt that's the init. Otherwise, parse expr; if next is
// ';' it was cond. If we see two ';' total after init, post is
// next. Simpler: peek for `let` to decide init form.
if (p.cur_kind == TK_LET) {
n.lhs = parseletlocal(p); // init (consumes its own ';')
n.cond = parseexpr(p);
expecttok(p, TK_SEMI, "expected ';' after for cond");
n.rhs = parseexpr(p);
} else {
// Parse one expr. If next is ';', it's a 3-clause without init.
let first: *node = parseexpr(p);
if (accepttok(p, TK_SEMI)) {
// cond ; post
n.cond = first;
n.rhs = parseexpr(p);
} else {
// just (cond)
n.cond = first;
};
};
expecttok(p, TK_RPAREN, "expected ')' after for");
n.body = parseblock(p);
return n;
};
fn parsestmt(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
// `static` is allowed on local lets per Hare; we accept and skip
// it (it doesn't change the AST shape).
if (p.cur_kind == TK_STATIC) { advance(p); };
if (p.cur_kind == TK_LBRACE) {
let b: *node = parseblock(p);
expecttok(p, TK_SEMI, "expected ';' after block");
return b;
};
if (p.cur_kind == TK_LET) { return parseletlocal(p); };
if (p.cur_kind == TK_IF) {
let n: *node = parseif(p);
expecttok(p, TK_SEMI, "expected ';' after if");
return n;
};
if (p.cur_kind == TK_FOR) {
let n: *node = parsefor(p);
expecttok(p, TK_SEMI, "expected ';' after for");
return n;
};
if (p.cur_kind == TK_RETURN) {
advance(p);
let n: *node = newnode(p.a, N_RETURN, pf, pl, pc);
if (p.cur_kind != 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.cur_kind == TK_COMMA) {
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
t.list = first;
let tail: *node = first;
for (accepttok(p, TK_COMMA)) {
let e: *node = parseexpr(p);
tail.next = e;
tail = e;
};
n.lhs = t;
} else {
n.lhs = first;
};
};
expecttok(p, TK_SEMI, "expected ';' after return");
return n;
};
if (p.cur_kind == TK_DEFER) {
advance(p);
let n: *node = newnode(p.a, N_DEFER, pf, pl, pc);
n.lhs = parseexpr(p);
expecttok(p, TK_SEMI, "expected ';' after defer");
return n;
};
if (p.cur_kind == TK_BREAK) {
advance(p);
expecttok(p, TK_SEMI, "expected ';' after break");
return newnode(p.a, N_BREAK, pf, pl, pc);
};
if (p.cur_kind == TK_CONTINUE) {
advance(p);
expecttok(p, TK_SEMI, "expected ';' after continue");
return newnode(p.a, N_CONTINUE, pf, pl, pc);
};
// expression statement, or tuple-destructure multi-assign:
// a, b = expr;
// Mirrors cmd/wcc/parse.c:1015-1031. We parse the first lvalue
// with parseexpr (matches the C side); subsequent lvalues go
// through parsebin(parseunary, 1) so the `=` stays for us to
// consume — parseexpr would absorb it.
let e: *node = parseexpr(p);
if (p.cur_kind == TK_COMMA) {
let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc);
let head: *node = e;
let tail: *node = e;
for (p.cur_kind == TK_COMMA) {
advance(p);
let lv: *node = parsebin(p, parseunary(p), 1);
tail.next = lv;
tail = lv;
};
expecttok(p, TK_ASSIGN, "expected '=' after multi-assign lvalues");
m.rhs = parseexpr(p);
m.list = head;
expecttok(p, TK_SEMI, "expected ';' after multi-assign");
return m;
};
let n: *node = newnode(p.a, N_EXPRSTMT, pf, pl, pc);
n.lhs = e;
expecttok(p, TK_SEMI, "expected ';' after expression statement");
return n;
};
// ---- top-level decl parsers ------------------------------------------
fn parseuse(p: *parser) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `use`
let n: *node = newnode(p.a, N_USE, pf, pl, pc);
let id: str;
expectident(p, &id);
n.str = id;
expecttok(p, TK_SEMI, "expected ';' after use");
return n;
};
fn parsedef(p: *parser, exported: i32) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `def`
let n: *node = newnode(p.a, N_DEF, pf, pl, pc);
n.module = p.l.module;
let id: str;
expectident(p, &id);
n.str = id;
expecttok(p, TK_COLON, "expected ':' in def");
n.lhs = parsetype(p);
expecttok(p, TK_ASSIGN, "expected '=' in def");
n.rhs = parseexpr(p);
expecttok(p, TK_SEMI, "expected ';' after def");
n.exported = exported;
return n;
};
fn parselet(p: *parser, exported: i32) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `let`
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
n.module = p.l.module;
let id: str;
expectident(p, &id);
n.str = id;
if (accepttok(p, TK_COLON)) {
n.lhs = parsetype(p);
};
if (accepttok(p, TK_ASSIGN)) {
n.rhs = parseexpr(p);
};
expecttok(p, TK_SEMI, "expected ';' after let");
n.exported = exported;
return n;
};
fn parseattrs(p: *parser) *node = {
let head: *node = nil;
let tail: *node = nil;
for (p.cur_kind == TK_AT) {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p);
let a: *node = newnode(p.a, N_ATTR, pf, pl, pc);
let id: str;
expectident(p, &id);
a.str = id;
expecttok(p, TK_LPAREN, "expected '(' after attribute name");
let arghead: *node = nil;
parsearglist(p, TK_RPAREN, &arghead);
a.list = arghead;
expecttok(p, TK_RPAREN, "expected ')' after attribute args");
if (head == nil) { head = a; tail = a; }
else { tail.next = a; tail = a; };
};
return head;
};
fn parseparams(p: *parser) *node = {
if (p.cur_kind == TK_RPAREN) { return nil; };
let head: *node = nil;
let tail: *node = nil;
for (true) {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
let n: *node = newnode(p.a, 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;
expectident(p, &id);
n.str = id;
expecttok(p, TK_COLON, "expected ':' in parameter");
n.lhs = parsetype(p);
if (head == nil) { head = n; tail = n; }
else { tail.next = n; tail = n; };
if (!accepttok(p, TK_COMMA)) { break; };
if (p.cur_kind == TK_RPAREN) { break; };
};
return head;
};
fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `fn`
let n: *node = newnode(p.a, N_FNDECL, pf, pl, pc);
n.module = p.l.module;
let id: str;
expectident(p, &id);
n.str = id;
expecttok(p, TK_LPAREN, "expected '(' after fn name");
n.list = parseparams(p);
expecttok(p, TK_RPAREN, "expected ')' after params");
if (p.cur_kind != TK_ASSIGN) {
if (p.cur_kind != TK_SEMI) {
n.lhs = parsetype(p);
};
};
if (accepttok(p, TK_ASSIGN)) {
n.body = parseblock(p);
expecttok(p, TK_SEMI, "expected ';' after fn body");
} else {
// Body-less fn: FFI declaration (`fn name(args) ret;`).
expecttok(p, TK_SEMI, "expected ';' after fn header");
};
n.exported = exported;
n.attr = attrs;
return n;
};
fn parsetypedecl(p: *parser, exported: i32) *node = {
let pf: str = p.cur_file;
let pl: i32 = p.cur_line;
let pc: i32 = p.cur_col;
advance(p); // past `type`
let n: *node = newnode(p.a, N_TYPEDECL, pf, pl, pc);
n.module = p.l.module;
let id: str;
expectident(p, &id);
n.str = id;
expecttok(p, TK_ASSIGN, "expected '=' in type decl");
n.lhs = parsetype(p);
expecttok(p, TK_SEMI, "expected ';' after type decl");
n.exported = exported;
return n;
};
// ---- file-level loop -------------------------------------------------
export fn parsefile(p: *parser) *node = {
let f: *node = newnode(p.a, N_FILE, p.cur_file, p.cur_line, p.cur_col);
let head: *node = nil;
let tail: *node = nil;
for (p.cur_kind != TK_EOF) {
let attrs: *node = parseattrs(p);
let exported: i32 = 0;
if (p.cur_kind == TK_EXPORT) { exported = 1; advance(p); };
let d: *node = nil;
if (p.cur_kind == TK_USE) {
d = parseuse(p);
} else { if (p.cur_kind == TK_DEF) {
d = parsedef(p, exported);
} else { if (p.cur_kind == TK_TYPE) {
d = parsetypedecl(p, exported);
} else { if (p.cur_kind == TK_LET) {
d = parselet(p, exported);
} else { if (p.cur_kind == TK_FN) {
d = parsefn(p, exported, attrs);
} else {
// Recovery: chew tokens until next ';' or EOF, balancing
// '{' '}' pairs so internal ';'s in unfamiliar forms don't
// derail us.
for (p.cur_kind != TK_SEMI) {
if (p.cur_kind == TK_EOF) { break; };
if (p.cur_kind == TK_LBRACE) {
let depth: i32 = 0;
for (true) {
if (p.cur_kind == TK_EOF) { break; };
if (p.cur_kind == TK_LBRACE) { depth += 1; advance(p); continue; };
if (p.cur_kind == TK_RBRACE) {
depth -= 1;
advance(p);
if (depth == 0) { break; };
continue;
};
advance(p);
};
continue;
};
advance(p);
};
if (p.cur_kind == TK_SEMI) { advance(p); };
};};};};};
if (d != nil) {
if (head == nil) {
head = d;
tail = d;
} else {
tail.next = d;
tail = d;
};
};
};
f.list = head;
return f;
};