ww: split parse.ww into parse/{parse,expr,stmt,decl}.ww submodule

This commit is contained in:
2026-05-11 16:48:58 +09:00
parent 72dfb6ac8d
commit 8ffe6dbee6
11 changed files with 1573 additions and 1555 deletions

View File

@@ -1954,267 +1954,13 @@ export fn astprint(fd: i32, n: *node) void = {
pr(fd, n, 0);
};
// MODULE: ww
// lib/ww/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.
// MODULE: parse
// lib/ww/parse/expr.ww — expression parsing, split out of parse.ww.
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,
curkind: i32,
curfile: str,
curline: i32,
curcol: i32,
curtext: str,
curuval: u64,
};
fn refill(p: *parser) void = {
let t: tok;
lexnext(p.l, &t);
p.curkind = t.kind;
p.curfile = t.file;
p.curline = t.line;
p.curcol = t.col;
p.curtext = t.text;
p.curuval = 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.curkind == 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.curkind == 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.curkind != TK_IDENT) {
errmsg(p, "expected identifier");
advance(p);
return false;
};
*into = p.curtext;
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.curfile;
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
if (p.curkind == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.curkind == TK_LBRACK) {
advance(p);
if (p.curkind == 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.curkind == 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.curkind != TK_RBRACE) {
if (p.curkind == TK_EOF) { break; };
let fpf: str = p.curfile;
let fpl: i32 = p.curline;
let fpc: i32 = p.curcol;
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.curkind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.curtext;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
return n;
};
if (p.curkind == 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.curkind == TK_RPAREN) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
n.list = head;
return n;
};
if (p.curkind == 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.curfile;
let pl: i32 = p.curline;
@@ -2532,11 +2278,13 @@ fn parseexpr(p: *parser) *node = {
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.
// MODULE: parse
// lib/ww/parse/stmt.ww — statement parsing, split out of parse.ww.
use os;
use mem;
use tok;
fn parseletlocal(p: *parser) *node = {
let pf: str = p.curfile;
@@ -2730,7 +2478,13 @@ fn parsestmt(p: *parser) *node = {
return n;
};
// ---- top-level decl parsers ------------------------------------------
// MODULE: parse
// lib/ww/parse/decl.ww — declaration parsing, split out of parse.ww.
use os;
use mem;
use tok;
fn parseuse(p: *parser) *node = {
let pf: str = p.curfile;
@@ -2879,7 +2633,264 @@ fn parsetypedecl(p: *parser, exported: i32) *node = {
return n;
};
// ---- file-level loop -------------------------------------------------
// MODULE: parse
// lib/ww/parse/parse.ww — port of cmd/wcc/parse.c (entry + plumbing).
//
// Split into Hare-style submodule: parse.ww (here) holds the parser
// struct, lexer plumbing, parsetype, parsefile (entry). Expression,
// statement, and declaration parsers live in expr.ww, stmt.ww,
// decl.ww respectively — all in the same `parse` module.
//
// 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;
use expr;
use stmt;
use decl;
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,
curkind: i32,
curfile: str,
curline: i32,
curcol: i32,
curtext: str,
curuval: u64,
};
fn refill(p: *parser) void = {
let t: tok;
lexnext(p.l, &t);
p.curkind = t.kind;
p.curfile = t.file;
p.curline = t.line;
p.curcol = t.col;
p.curtext = t.text;
p.curuval = 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.curkind == 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.curkind == 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.curkind != TK_IDENT) {
errmsg(p, "expected identifier");
advance(p);
return false;
};
*into = p.curtext;
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.curfile;
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
if (p.curkind == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.curkind == TK_LBRACK) {
advance(p);
if (p.curkind == 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.curkind == 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.curkind != TK_RBRACE) {
if (p.curkind == TK_EOF) { break; };
let fpf: str = p.curfile;
let fpl: i32 = p.curline;
let fpc: i32 = p.curcol;
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.curkind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.curtext;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
return n;
};
if (p.curkind == 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.curkind == TK_RPAREN) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
n.list = head;
return n;
};
if (p.curkind == 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.
export fn parsefile(p: *parser) *node = {
let f: *node = newnode(p.a, N_FILE, p.curfile, p.curline, p.curcol);

View File

@@ -1954,267 +1954,13 @@ export fn astprint(fd: i32, n: *node) void = {
pr(fd, n, 0);
};
// MODULE: ww
// lib/ww/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.
// MODULE: parse
// lib/ww/parse/expr.ww — expression parsing, split out of parse.ww.
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,
curkind: i32,
curfile: str,
curline: i32,
curcol: i32,
curtext: str,
curuval: u64,
};
fn refill(p: *parser) void = {
let t: tok;
lexnext(p.l, &t);
p.curkind = t.kind;
p.curfile = t.file;
p.curline = t.line;
p.curcol = t.col;
p.curtext = t.text;
p.curuval = 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.curkind == 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.curkind == 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.curkind != TK_IDENT) {
errmsg(p, "expected identifier");
advance(p);
return false;
};
*into = p.curtext;
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.curfile;
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
if (p.curkind == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.curkind == TK_LBRACK) {
advance(p);
if (p.curkind == 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.curkind == 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.curkind != TK_RBRACE) {
if (p.curkind == TK_EOF) { break; };
let fpf: str = p.curfile;
let fpl: i32 = p.curline;
let fpc: i32 = p.curcol;
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.curkind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.curtext;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
return n;
};
if (p.curkind == 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.curkind == TK_RPAREN) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
n.list = head;
return n;
};
if (p.curkind == 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.curfile;
let pl: i32 = p.curline;
@@ -2532,11 +2278,13 @@ fn parseexpr(p: *parser) *node = {
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.
// MODULE: parse
// lib/ww/parse/stmt.ww — statement parsing, split out of parse.ww.
use os;
use mem;
use tok;
fn parseletlocal(p: *parser) *node = {
let pf: str = p.curfile;
@@ -2730,7 +2478,13 @@ fn parsestmt(p: *parser) *node = {
return n;
};
// ---- top-level decl parsers ------------------------------------------
// MODULE: parse
// lib/ww/parse/decl.ww — declaration parsing, split out of parse.ww.
use os;
use mem;
use tok;
fn parseuse(p: *parser) *node = {
let pf: str = p.curfile;
@@ -2879,7 +2633,264 @@ fn parsetypedecl(p: *parser, exported: i32) *node = {
return n;
};
// ---- file-level loop -------------------------------------------------
// MODULE: parse
// lib/ww/parse/parse.ww — port of cmd/wcc/parse.c (entry + plumbing).
//
// Split into Hare-style submodule: parse.ww (here) holds the parser
// struct, lexer plumbing, parsetype, parsefile (entry). Expression,
// statement, and declaration parsers live in expr.ww, stmt.ww,
// decl.ww respectively — all in the same `parse` module.
//
// 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;
use expr;
use stmt;
use decl;
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,
curkind: i32,
curfile: str,
curline: i32,
curcol: i32,
curtext: str,
curuval: u64,
};
fn refill(p: *parser) void = {
let t: tok;
lexnext(p.l, &t);
p.curkind = t.kind;
p.curfile = t.file;
p.curline = t.line;
p.curcol = t.col;
p.curtext = t.text;
p.curuval = 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.curkind == 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.curkind == 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.curkind != TK_IDENT) {
errmsg(p, "expected identifier");
advance(p);
return false;
};
*into = p.curtext;
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.curfile;
let pl: i32 = p.curline;
let pc: i32 = p.curcol;
if (p.curkind == TK_STAR) {
advance(p);
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
n.lhs = parsetype(p);
return n;
};
if (p.curkind == TK_LBRACK) {
advance(p);
if (p.curkind == 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.curkind == 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.curkind != TK_RBRACE) {
if (p.curkind == TK_EOF) { break; };
let fpf: str = p.curfile;
let fpl: i32 = p.curline;
let fpc: i32 = p.curcol;
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.curkind == TK_IDENT) {
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
n.str = p.curtext;
advance(p);
// Dotted path collapse (pkg.Type) deferred — fixtures don't
// need it yet.
return n;
};
if (p.curkind == 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.curkind == TK_RPAREN) { break; };
};
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
n.list = head;
return n;
};
if (p.curkind == 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.
export fn parsefile(p: *parser) *node = {
let f: *node = newnode(p.a, N_FILE, p.curfile, p.curline, p.curcol);