lib/ww,wcc: consolidate frontend into one syntax package (Go-compiler model, #74)
The ww compiler frontend was split across packages lex (lex+tok), ww
(ast+sym+typ), and parse — mirroring Hare's ref/hare/hare/{ast,lex,parse}.
That split's only payoff is third-party reuse, which ww has zero of: the
frontend is consumed by exactly one client, the wcc backend. The split's
cost is a wide cross-package export surface — every fn over a sibling
package's type must export it, and under separate compilation that
re-triggers check_exported_type, plus a phantom `import tok;` (tok lives
in package lex). Consolidate into ONE package lib/ww/syntax/, modelled on
Go's cmd/compile/internal/syntax. The 9 files move in (package syntax);
the intra-frontend mutual references become same-package; wcc and the
tool mains import syntax. No cstage C change (the C frontend mangles from
the source package clause). Internal data shapes (AST kinds, token model,
lexer/parser state) still mirror ref/hare/hare per rule 6/12 — only the
module decomposition collapses; the stdlib is untouched.
USER-approved (#74); spec .ai/rob-frontend-reorg.md (drew2 fidelity-
confirmed). Rule-6 carve-out documented in CLAUDE.md. Dissolves the tok
phantom import; collapses the intra-frontend export sprawl. Byte-id
rebaseline (lex.X/parse.X/ww.X -> syntax.X); cs==ww held. The residual
syntax->wcc export surface (10 types) + the unqualified-ref question are
separate follow-ups (#72/#75).
This commit is contained in:
389
lib/ww/syntax/ast.ww
Normal file
389
lib/ww/syntax/ast.ww
Normal file
@@ -0,0 +1,389 @@
|
||||
// lib/ww/syntax/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
|
||||
//
|
||||
// Status: AST printer is fully ported. Constructor `newnode` is here.
|
||||
// The parser (parse.ww) is currently minimal — see its file header.
|
||||
//
|
||||
// Calling-convention shim: same as tok/lex — `node` is too big to pass
|
||||
// by value (8 *node pointers + 2 strs + a few ints), so callers always
|
||||
// hand around `*node`. Only `newnode` allocates and returns a *node.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
import strconv;
|
||||
|
||||
// ---- Nkind ------------------------------------------------------------
|
||||
//
|
||||
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
|
||||
// the AST diff probe in 990_selfhost works.
|
||||
|
||||
// Mirror of the C `Nkind` enum in cmd/wcc/ww.h. Numeric values are
|
||||
// explicit and must stay in sync — the 990_selfhost test diffs
|
||||
// astprint against the C side byte-for-byte. Tail-appended entries
|
||||
// (TYPETEST onward) preserve every prior N_* value.
|
||||
type nkind = enum i32 {
|
||||
N_NONE = 0,
|
||||
|
||||
N_INTLIT = 1,
|
||||
N_FLOATLIT = 2,
|
||||
N_STRLIT = 3,
|
||||
N_RUNELIT = 4,
|
||||
N_TRUE = 5,
|
||||
N_FALSE = 6,
|
||||
N_NIL = 7,
|
||||
N_IDENT = 8,
|
||||
|
||||
N_BIN = 9,
|
||||
N_UN = 10,
|
||||
N_CALL = 11,
|
||||
N_INDEX = 12,
|
||||
N_DOT = 13,
|
||||
N_CAST = 14,
|
||||
N_STRUCTLIT = 15,
|
||||
N_ARRLIT = 16,
|
||||
N_FIELD = 17,
|
||||
N_ASSIGN = 18,
|
||||
N_ALLOC = 19,
|
||||
N_FREE = 20,
|
||||
N_RECV = 21,
|
||||
N_SLICE = 22,
|
||||
N_SPREAD = 23,
|
||||
|
||||
N_BLOCK = 24,
|
||||
N_EXPRSTMT = 25,
|
||||
N_LET = 26,
|
||||
N_RETURN = 27,
|
||||
N_IF = 28,
|
||||
N_FOR = 29,
|
||||
N_FORRANGE = 30,
|
||||
N_DEFER = 31,
|
||||
N_BREAK = 32,
|
||||
N_CONTINUE = 33,
|
||||
N_SWITCH = 34,
|
||||
N_CASE = 35,
|
||||
|
||||
N_FILE = 36,
|
||||
N_USE = 37,
|
||||
N_DEF = 38,
|
||||
N_TYPEDECL = 39,
|
||||
N_FNDECL = 40,
|
||||
N_PARAM = 41,
|
||||
|
||||
N_TNAME = 42,
|
||||
N_TPTR = 43,
|
||||
N_TSLICE = 44,
|
||||
N_TARRAY = 45,
|
||||
N_TFN = 46,
|
||||
N_TSTRUCT = 47,
|
||||
N_TFIELD = 48,
|
||||
N_TCHAN = 49,
|
||||
|
||||
N_ATTR = 50,
|
||||
N_TTUPLE = 51,
|
||||
N_TTAGGED = 52,
|
||||
N_TUPLE = 53,
|
||||
N_MATCH = 54,
|
||||
N_MCASE = 55,
|
||||
N_TRYPROP = 56,
|
||||
N_TRYUNW = 57,
|
||||
N_MLET = 58,
|
||||
N_MASSIGN = 59,
|
||||
|
||||
N_TYPETEST = 60,
|
||||
N_TYPEASSERT = 61,
|
||||
N_VOIDLIT = 62,
|
||||
N_TBANG = 63,
|
||||
N_YIELD = 64,
|
||||
N_TENUM = 65,
|
||||
N_TENUMMEMBER = 66,
|
||||
|
||||
// N_TPARAM — chain wrapper for N_TTUPLE.list elements. Mirror of
|
||||
// cstage's Tparam (cmd/wcc/check.c:1437-1451) lifted to the AST so
|
||||
// `exprtype` can return shared element-type nodes (sym.decl.lhs,
|
||||
// struct field's .lhs, another N_TTUPLE's .list element) without
|
||||
// corrupting source ASTs by reusing their .next. .lhs holds the
|
||||
// element type AST (possibly shared); .next chains within the
|
||||
// parent N_TTUPLE.list. Cstage keeps Tparam at the Type-layer; ww
|
||||
// has no separate type layer for tuple chains, so the wrapper sits
|
||||
// at the AST layer. Other node fields are unused. Never appears
|
||||
// outside an N_TTUPLE.list; astprint unwraps transparently to keep
|
||||
// the 990 -a byte-diff against cstage.
|
||||
N_TPARAM = 67,
|
||||
|
||||
N_LAST = 68,
|
||||
};
|
||||
|
||||
// ---- Node -------------------------------------------------------------
|
||||
|
||||
type node = struct {
|
||||
kind: nkind,
|
||||
file: str,
|
||||
line: i32,
|
||||
col: i32,
|
||||
op: tkind, // for nkind.N_BIN / nkind.N_UN / nkind.N_ASSIGN
|
||||
str: str,
|
||||
uval: u64,
|
||||
fval: f64,
|
||||
lhs: *node,
|
||||
rhs: *node,
|
||||
cond: *node,
|
||||
body: *node,
|
||||
els: *node,
|
||||
list: *node,
|
||||
next: *node,
|
||||
attr: *node,
|
||||
exported: i32, // bool — `export` keyword present
|
||||
type_: *void, // filled in by checker; type.ww treats it as *tinfo
|
||||
tsuffix: str, // typed numeric literal suffix ("i32", "u64", ...)
|
||||
nmod: str, // originating module from `// MODULE: foo`; "" if none
|
||||
usepath: str, // on an N_USE: full dotted IMPORT path vs leaf alias
|
||||
// in `str` (M1 #22); "" otherwise
|
||||
imported: i32, // M1 #22: decl reached via `//ww:module <path>` import
|
||||
// boundary (vs root/primary); gates root-only bare main
|
||||
};
|
||||
|
||||
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).
|
||||
let n: *node = alloc(node{kind=k, file=file, line=line, col=col, op=tkind.TK_NONE, str="", uval=0u64, fval=0: f64, lhs=nil, rhs=nil, cond=nil, body=nil, els=nil, list=nil, next=nil, attr=nil, exported=0, type_=nil, tsuffix="", nmod="", usepath="", imported=0})!;
|
||||
return n;
|
||||
};
|
||||
|
||||
// ---- printer ----------------------------------------------------------
|
||||
|
||||
export fn nkname(k: nkind) str = {
|
||||
switch (k) {
|
||||
case nkind.N_NONE: return "none";
|
||||
case nkind.N_INTLIT: return "int";
|
||||
case nkind.N_FLOATLIT: return "float";
|
||||
case nkind.N_STRLIT: return "str";
|
||||
case nkind.N_RUNELIT: return "rune";
|
||||
case nkind.N_TRUE: return "true";
|
||||
case nkind.N_FALSE: return "false";
|
||||
case nkind.N_NIL: return "nil";
|
||||
case nkind.N_IDENT: return "id";
|
||||
|
||||
case nkind.N_BIN: return "bin";
|
||||
case nkind.N_UN: return "un";
|
||||
case nkind.N_CALL: return "call";
|
||||
case nkind.N_INDEX: return "index";
|
||||
case nkind.N_DOT: return "dot";
|
||||
case nkind.N_CAST: return "cast";
|
||||
case nkind.N_STRUCTLIT: return "structlit";
|
||||
case nkind.N_ARRLIT: return "arrlit";
|
||||
case nkind.N_FIELD: return "field";
|
||||
case nkind.N_ASSIGN: return "assign";
|
||||
case nkind.N_ALLOC: return "alloc";
|
||||
case nkind.N_FREE: return "free";
|
||||
case nkind.N_RECV: return "recv";
|
||||
case nkind.N_SLICE: return "slice";
|
||||
case nkind.N_SPREAD: return "spread";
|
||||
|
||||
case nkind.N_BLOCK: return "block";
|
||||
case nkind.N_EXPRSTMT: return "exprstmt";
|
||||
case nkind.N_LET: return "let";
|
||||
case nkind.N_RETURN: return "return";
|
||||
case nkind.N_IF: return "if";
|
||||
case nkind.N_FOR: return "for";
|
||||
case nkind.N_FORRANGE: return "forrange";
|
||||
case nkind.N_DEFER: return "defer";
|
||||
case nkind.N_BREAK: return "break";
|
||||
case nkind.N_CONTINUE: return "continue";
|
||||
case nkind.N_SWITCH: return "switch";
|
||||
case nkind.N_CASE: return "case";
|
||||
|
||||
case nkind.N_FILE: return "file";
|
||||
case nkind.N_USE: return "use";
|
||||
case nkind.N_DEF: return "def";
|
||||
case nkind.N_TYPEDECL: return "typedecl";
|
||||
case nkind.N_FNDECL: return "fn";
|
||||
case nkind.N_PARAM: return "param";
|
||||
|
||||
case nkind.N_TNAME: return "tname";
|
||||
case nkind.N_TPTR: return "tptr";
|
||||
case nkind.N_TSLICE: return "tslice";
|
||||
case nkind.N_TARRAY: return "tarray";
|
||||
case nkind.N_TFN: return "tfn";
|
||||
case nkind.N_TSTRUCT: return "tstruct";
|
||||
case nkind.N_TFIELD: return "tfield";
|
||||
case nkind.N_TCHAN: return "tchan";
|
||||
|
||||
case nkind.N_ATTR: return "attr";
|
||||
case nkind.N_TTUPLE: return "ttuple";
|
||||
case nkind.N_TTAGGED: return "ttagged";
|
||||
case nkind.N_TUPLE: return "tuple";
|
||||
case nkind.N_MATCH: return "match";
|
||||
case nkind.N_MCASE: return "mcase";
|
||||
case nkind.N_TRYPROP: return "tryprop";
|
||||
case nkind.N_TRYUNW: return "tryunw";
|
||||
case nkind.N_MLET: return "mlet";
|
||||
case nkind.N_MASSIGN: return "massign";
|
||||
|
||||
case nkind.N_TYPETEST: return "typetest";
|
||||
case nkind.N_TYPEASSERT: return "typeassert";
|
||||
case nkind.N_VOIDLIT: return "voidlit";
|
||||
case nkind.N_TBANG: return "tbang";
|
||||
case nkind.N_YIELD: return "yield";
|
||||
case nkind.N_TENUM: return "tenum";
|
||||
case nkind.N_TENUMMEMBER: return "tenummember";
|
||||
case nkind.N_TPARAM: return "tparam";
|
||||
case nkind.N_LAST: return "last";
|
||||
};
|
||||
return "?";
|
||||
};
|
||||
|
||||
fn ind(fd: i32, d: i32) void = {
|
||||
let i: i32 = 0;
|
||||
for (i < d) {
|
||||
os.write(fd, " ".ptr, 2u64);
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
fn putc1(fd: i32, b: u8) void = {
|
||||
let buf: [1]u8;
|
||||
buf[0] = b;
|
||||
os.write(fd, buf.ptr, 1u64);
|
||||
};
|
||||
|
||||
fn putq(fd: i32, s: str) void = {
|
||||
putc1(fd, '"');
|
||||
let i: i32 = 0;
|
||||
for (i < s.len) {
|
||||
let c: u8 = s[i];
|
||||
if (c == '"') {
|
||||
os.write(fd, "\\\"".ptr, 2u64);
|
||||
} else { if (c == '\\') {
|
||||
os.write(fd, "\\\\".ptr, 2u64);
|
||||
} else { if (c == '\n') {
|
||||
os.write(fd, "\\n".ptr, 2u64);
|
||||
} else { if (c == '\t') {
|
||||
os.write(fd, "\\t".ptr, 2u64);
|
||||
} else { if (c < 32u8) {
|
||||
let hi: u8 = c >> 4u8;
|
||||
let lo: u8 = c & 15u8;
|
||||
let h: u8 = 0u8;
|
||||
let l: u8 = 0u8;
|
||||
if (hi < 10u8) { h = hi + 48u8; } else { h = (hi - 10u8) + 97u8; };
|
||||
if (lo < 10u8) { l = lo + 48u8; } else { l = (lo - 10u8) + 97u8; };
|
||||
let buf: [4]u8;
|
||||
buf[0] = 92u8;
|
||||
buf[1] = 120u8;
|
||||
buf[2] = h;
|
||||
buf[3] = l;
|
||||
os.write(fd, buf.ptr, 4u64);
|
||||
} else {
|
||||
putc1(fd, c);
|
||||
};};};};};
|
||||
i += 1;
|
||||
};
|
||||
putc1(fd, 34u8);
|
||||
};
|
||||
|
||||
fn pr(fd: i32, n: *node, d: i32) void = {
|
||||
if (n == nil) {
|
||||
ind(fd, d);
|
||||
os.write(fd, "()\n".ptr, 3u64);
|
||||
return;
|
||||
};
|
||||
// N_TPARAM wraps an N_TTUPLE.list element so exprtype can return
|
||||
// shared element-type nodes without corrupting their .next chain.
|
||||
// Cstage has no AST-level wrapper, so unwrap here to keep the 990
|
||||
// -a byte-diff with cstage's astprint.
|
||||
if (n.kind == nkind.N_TPARAM) {
|
||||
pr(fd, n.lhs, d);
|
||||
return;
|
||||
};
|
||||
ind(fd, d);
|
||||
putc1(fd, '(');
|
||||
let nm: str = nkname(n.kind);
|
||||
os.write(fd, nm.ptr, nm.len: u64);
|
||||
|
||||
if (n.kind == nkind.N_INTLIT) {
|
||||
putc1(fd, 32u8);
|
||||
let s: str = strconv.u64tos(n.uval, strconv.base.DEC);
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
} else { if (n.kind == nkind.N_RUNELIT) {
|
||||
putc1(fd, 32u8);
|
||||
let s: str = strconv.u64tos(n.uval, strconv.base.DEC);
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
} else { if (
|
||||
n.kind == nkind.N_STRLIT ||
|
||||
n.kind == nkind.N_IDENT ||
|
||||
n.kind == nkind.N_USE ||
|
||||
n.kind == nkind.N_DOT ||
|
||||
n.kind == nkind.N_DEF ||
|
||||
n.kind == nkind.N_TYPEDECL ||
|
||||
n.kind == nkind.N_FNDECL ||
|
||||
n.kind == nkind.N_PARAM ||
|
||||
n.kind == nkind.N_LET ||
|
||||
n.kind == nkind.N_TNAME ||
|
||||
n.kind == nkind.N_TFIELD ||
|
||||
n.kind == nkind.N_TENUMMEMBER ||
|
||||
n.kind == nkind.N_FIELD ||
|
||||
n.kind == nkind.N_ATTR
|
||||
) {
|
||||
// Match C ast.c: print the str field whenever it's non-nil,
|
||||
// even if its length is zero (e.g. an empty STRLIT prints
|
||||
// `(str ""`).
|
||||
let s: str = n.str;
|
||||
if (s.ptr != nil) {
|
||||
putc1(fd, 32u8);
|
||||
putq(fd, s);
|
||||
};
|
||||
} else { if (
|
||||
n.kind == nkind.N_BIN ||
|
||||
n.kind == nkind.N_UN ||
|
||||
n.kind == nkind.N_ASSIGN
|
||||
) {
|
||||
putc1(fd, 32u8);
|
||||
let on: str = tokname(n.op);
|
||||
os.write(fd, on.ptr, on.len: u64);
|
||||
};};};};
|
||||
|
||||
if (n.kind == nkind.N_FNDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == nkind.N_DEF) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == nkind.N_TYPEDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
putc1(fd, '\n');
|
||||
|
||||
if (n.attr != nil) {
|
||||
ind(fd, d + 1);
|
||||
os.write(fd, "(@\n".ptr, 3u64);
|
||||
let m: *node = n.attr;
|
||||
for (m != nil) {
|
||||
pr(fd, m, d + 2);
|
||||
m = m.next;
|
||||
};
|
||||
ind(fd, d + 1);
|
||||
os.write(fd, ")\n".ptr, 2u64);
|
||||
};
|
||||
if (n.lhs != nil) { pr(fd, n.lhs, d + 1); };
|
||||
if (n.rhs != nil) { pr(fd, n.rhs, d + 1); };
|
||||
if (n.cond != nil) { pr(fd, n.cond, d + 1); };
|
||||
if (n.body != nil) { pr(fd, n.body, d + 1); };
|
||||
if (n.els != nil) { pr(fd, n.els, d + 1); };
|
||||
if (n.list != nil) {
|
||||
ind(fd, d + 1);
|
||||
os.write(fd, "(list\n".ptr, 6u64);
|
||||
let m: *node = n.list;
|
||||
for (m != nil) {
|
||||
pr(fd, m, d + 2);
|
||||
m = m.next;
|
||||
};
|
||||
ind(fd, d + 1);
|
||||
os.write(fd, ")\n".ptr, 2u64);
|
||||
};
|
||||
ind(fd, d);
|
||||
os.write(fd, ")\n".ptr, 2u64);
|
||||
};
|
||||
|
||||
export fn astprint(fd: i32, n: *node) void = {
|
||||
pr(fd, n, 0);
|
||||
};
|
||||
102
lib/ww/syntax/asttest.ww
Normal file
102
lib/ww/syntax/asttest.ww
Normal file
@@ -0,0 +1,102 @@
|
||||
// asttest — functional-equivalence pin for [[nkname]] after the
|
||||
// if-ladder → switch fold (struct fold S2). Run with
|
||||
// `ww run -I lib/ww lib/ww/syntax/asttest.ww`.
|
||||
//
|
||||
// nkname is checked against every nkind value (the full ladder the
|
||||
// switch replaced) plus the out-of-band fallback ("?"). A failing row
|
||||
// aborts via the assert/abort builtin (task #5 @test conversion).
|
||||
// `package main` + bare `import ast` mirrors wwdump (the external
|
||||
// astprint consumer).
|
||||
|
||||
package main;
|
||||
|
||||
import syntax;
|
||||
|
||||
|
||||
fn checkname(k: nkind, want: str) void = {
|
||||
assert(!(nkname(k) != want));
|
||||
};
|
||||
|
||||
// One row per nkind value — same string the if-ladder returned. The
|
||||
// final row pins the unknown-kind fallback ("?").
|
||||
@test fn nkname_cases() void = {
|
||||
checkname(nkind.N_NONE, "none");
|
||||
checkname(nkind.N_INTLIT, "int");
|
||||
checkname(nkind.N_FLOATLIT, "float");
|
||||
checkname(nkind.N_STRLIT, "str");
|
||||
checkname(nkind.N_RUNELIT, "rune");
|
||||
checkname(nkind.N_TRUE, "true");
|
||||
checkname(nkind.N_FALSE, "false");
|
||||
checkname(nkind.N_NIL, "nil");
|
||||
checkname(nkind.N_IDENT, "id");
|
||||
|
||||
checkname(nkind.N_BIN, "bin");
|
||||
checkname(nkind.N_UN, "un");
|
||||
checkname(nkind.N_CALL, "call");
|
||||
checkname(nkind.N_INDEX, "index");
|
||||
checkname(nkind.N_DOT, "dot");
|
||||
checkname(nkind.N_CAST, "cast");
|
||||
checkname(nkind.N_STRUCTLIT, "structlit");
|
||||
checkname(nkind.N_ARRLIT, "arrlit");
|
||||
checkname(nkind.N_FIELD, "field");
|
||||
checkname(nkind.N_ASSIGN, "assign");
|
||||
checkname(nkind.N_ALLOC, "alloc");
|
||||
checkname(nkind.N_FREE, "free");
|
||||
checkname(nkind.N_RECV, "recv");
|
||||
checkname(nkind.N_SLICE, "slice");
|
||||
checkname(nkind.N_SPREAD, "spread");
|
||||
|
||||
checkname(nkind.N_BLOCK, "block");
|
||||
checkname(nkind.N_EXPRSTMT, "exprstmt");
|
||||
checkname(nkind.N_LET, "let");
|
||||
checkname(nkind.N_RETURN, "return");
|
||||
checkname(nkind.N_IF, "if");
|
||||
checkname(nkind.N_FOR, "for");
|
||||
checkname(nkind.N_FORRANGE, "forrange");
|
||||
checkname(nkind.N_DEFER, "defer");
|
||||
checkname(nkind.N_BREAK, "break");
|
||||
checkname(nkind.N_CONTINUE, "continue");
|
||||
checkname(nkind.N_SWITCH, "switch");
|
||||
checkname(nkind.N_CASE, "case");
|
||||
|
||||
checkname(nkind.N_FILE, "file");
|
||||
checkname(nkind.N_USE, "use");
|
||||
checkname(nkind.N_DEF, "def");
|
||||
checkname(nkind.N_TYPEDECL, "typedecl");
|
||||
checkname(nkind.N_FNDECL, "fn");
|
||||
checkname(nkind.N_PARAM, "param");
|
||||
|
||||
checkname(nkind.N_TNAME, "tname");
|
||||
checkname(nkind.N_TPTR, "tptr");
|
||||
checkname(nkind.N_TSLICE, "tslice");
|
||||
checkname(nkind.N_TARRAY, "tarray");
|
||||
checkname(nkind.N_TFN, "tfn");
|
||||
checkname(nkind.N_TSTRUCT, "tstruct");
|
||||
checkname(nkind.N_TFIELD, "tfield");
|
||||
checkname(nkind.N_TCHAN, "tchan");
|
||||
|
||||
checkname(nkind.N_ATTR, "attr");
|
||||
checkname(nkind.N_TTUPLE, "ttuple");
|
||||
checkname(nkind.N_TTAGGED, "ttagged");
|
||||
checkname(nkind.N_TUPLE, "tuple");
|
||||
checkname(nkind.N_MATCH, "match");
|
||||
checkname(nkind.N_MCASE, "mcase");
|
||||
checkname(nkind.N_TRYPROP, "tryprop");
|
||||
checkname(nkind.N_TRYUNW, "tryunw");
|
||||
checkname(nkind.N_MLET, "mlet");
|
||||
checkname(nkind.N_MASSIGN, "massign");
|
||||
|
||||
checkname(nkind.N_TYPETEST, "typetest");
|
||||
checkname(nkind.N_TYPEASSERT, "typeassert");
|
||||
checkname(nkind.N_VOIDLIT, "voidlit");
|
||||
checkname(nkind.N_TBANG, "tbang");
|
||||
checkname(nkind.N_YIELD, "yield");
|
||||
checkname(nkind.N_TENUM, "tenum");
|
||||
checkname(nkind.N_TENUMMEMBER, "tenummember");
|
||||
checkname(nkind.N_TPARAM, "tparam");
|
||||
checkname(nkind.N_LAST, "last");
|
||||
|
||||
// Unknown kind → the post-switch fallback. N_LAST is the highest
|
||||
// named value (68); 69 is out of band, exercising the "?" tail.
|
||||
checkname(69: nkind, "?");
|
||||
};
|
||||
193
lib/ww/syntax/decl.ww
Normal file
193
lib/ww/syntax/decl.ww
Normal file
@@ -0,0 +1,193 @@
|
||||
// lib/ww/syntax/decl.ww — declaration parsing, split out of parse.ww.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
import strings;
|
||||
|
||||
// `import encoding.utf8;` — the driver resolves the dotted path to
|
||||
// a directory; only the leaf (`utf8`) is needed downstream as the
|
||||
// module bareword for n_use → decl disambiguation, mirroring Hare's
|
||||
// `use encoding::utf8;` → `utf8::name` (ref/hare/hare/ast/import.ha:7
|
||||
// stores `[]str` but identifier-resolution uses the last component).
|
||||
fn parseuse(p: *parser) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `use`
|
||||
let n: *node = newnode(nkind.N_USE, pf, pl, pc);
|
||||
n.nmod = p.curmod;
|
||||
// M1 #22: accumulate the full dotted import path (n.usepath) for the
|
||||
// checker's path-keyed module match; n.str stays the leaf alias the
|
||||
// user writes (`utf8.x`).
|
||||
let leaf: str;
|
||||
expectident(p, &leaf);
|
||||
let path: str = leaf;
|
||||
for (p.curkind == tkind.TK_DOT) {
|
||||
advance(p); // past `.`
|
||||
expectident(p, &leaf);
|
||||
path = strings.concat(path, ".", leaf);
|
||||
};
|
||||
n.str = leaf;
|
||||
n.usepath = path;
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after use");
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsedef(p: *parser, exported: i32) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `def`
|
||||
let n: *node = newnode(nkind.N_DEF, pf, pl, pc);
|
||||
n.nmod = p.curmod;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, tkind.TK_COLON, "expected ':' in def");
|
||||
n.lhs = parsetype(p);
|
||||
// A value-LESS `def X: T;` is an interface prototype (an aggregate-
|
||||
// init `.wwi` def whose DATA lives in the defining package — BUG-2 /
|
||||
// task #71), mirroring the fn bodyless-prototype arm. rhs stays nil;
|
||||
// the checker fold pass and cgen emit_defs/emit_lets already guard
|
||||
// the rhs==nil shape.
|
||||
if (accepttok(p, tkind.TK_ASSIGN)) {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after def");
|
||||
n.exported = exported;
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parselet(p: *parser, exported: i32) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
// Accept `let` or `const`. Const-bound bindings are marked via
|
||||
// n.op = tkind.TK_CONST so the checker can reject reassignment.
|
||||
let is_const: i32 = 0;
|
||||
if (p.curkind == tkind.TK_CONST) { is_const = 1; };
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_LET, pf, pl, pc);
|
||||
n.nmod = p.curmod;
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
n.str = id;
|
||||
if (accepttok(p, tkind.TK_COLON)) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
if (accepttok(p, tkind.TK_ASSIGN)) {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after let");
|
||||
n.exported = exported;
|
||||
if (is_const != 0) { n.op = tkind.TK_CONST; };
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parseattrs(p: *parser) *node = {
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind == tkind.TK_AT) {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p);
|
||||
let a: *node = newnode(nkind.N_ATTR, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
a.str = id;
|
||||
// `@name(args...)` for FFI-style attrs; `@name` for marker-
|
||||
// only attrs like @test (no parens).
|
||||
if (accepttok(p, tkind.TK_LPAREN)) {
|
||||
let arghead: *node = nil;
|
||||
parsearglist(p, tkind.TK_RPAREN, &arghead);
|
||||
a.list = arghead;
|
||||
expecttok(p, tkind.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.curkind == tkind.TK_RPAREN) { return nil; };
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
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;
|
||||
expectbindname(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, tkind.TK_COLON, "expected ':' in parameter");
|
||||
n.lhs = parsetype(p);
|
||||
// Hare-style variadic: `name: T...`. Marker on n.op so check
|
||||
// promotes the param's type to []T and call sites gather /
|
||||
// forward. Mirrors cmd/wcc/parse.c parseparams.
|
||||
if (accepttok(p, tkind.TK_ELLIPSIS)) {
|
||||
n.op = tkind.TK_ELLIPSIS;
|
||||
};
|
||||
if (head == nil) { head = n; tail = n; }
|
||||
else { tail.next = n; tail = n; };
|
||||
if (n.op == tkind.TK_ELLIPSIS) {
|
||||
break; // variadic must be the last param
|
||||
};
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
if (p.curkind == tkind.TK_RPAREN) { break; };
|
||||
};
|
||||
return head;
|
||||
};
|
||||
|
||||
fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `fn`
|
||||
let n: *node = newnode(nkind.N_FNDECL, pf, pl, pc);
|
||||
n.nmod = p.curmod;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after fn name");
|
||||
n.list = parseparams(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after params");
|
||||
if (p.curkind != tkind.TK_ASSIGN) {
|
||||
if (p.curkind != tkind.TK_SEMI) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
};
|
||||
if (accepttok(p, tkind.TK_ASSIGN)) {
|
||||
n.body = parseblock(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after fn body");
|
||||
} else {
|
||||
// Body-less fn: FFI declaration (`fn name(args) ret;`).
|
||||
expecttok(p, tkind.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.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `type`
|
||||
let n: *node = newnode(nkind.N_TYPEDECL, pf, pl, pc);
|
||||
n.nmod = p.curmod;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, tkind.TK_ASSIGN, "expected '=' in type decl");
|
||||
n.lhs = parsetype(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after type decl");
|
||||
n.exported = exported;
|
||||
return n;
|
||||
};
|
||||
|
||||
467
lib/ww/syntax/expr.ww
Normal file
467
lib/ww/syntax/expr.ww
Normal file
@@ -0,0 +1,467 @@
|
||||
// lib/ww/syntax/expr.ww — expression parsing, split out of parse.ww.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
|
||||
// streqlocal — str-to-str compare. Inlined here to avoid a cross-
|
||||
// module `use sym;` for one call site.
|
||||
fn streqlocal(a: str, b: str) bool = {
|
||||
if (a.len != b.len) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < a.len) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
fn parseprimary(p: *parser) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
|
||||
if (p.curkind == tkind.TK_INT) {
|
||||
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
|
||||
// the node. Cgen's rhstargetname reads tsuffix to pick the
|
||||
// matching tagged-union variant; without this, typed-int
|
||||
// rhs of `h.e = 42i64;` falls through to the "first non-str
|
||||
// variant" fallback and writes tag 0. Mirror of cmd/wcc/
|
||||
// parse.c parseprimary TK_INT.
|
||||
n.tsuffix = p.curtsuffix;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_FLOAT) {
|
||||
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
|
||||
// don't need a float ABI to materialise the constant.
|
||||
n.uval = p.curuval;
|
||||
n.str = p.curtext;
|
||||
n.tsuffix = p.curtsuffix;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_STR) {
|
||||
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(nkind.N_RUNELIT, pf, pl, pc);
|
||||
n.uval = p.curuval;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_TRUE) {
|
||||
advance(p);
|
||||
return newnode(nkind.N_TRUE, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_FALSE) {
|
||||
advance(p);
|
||||
return newnode(nkind.N_FALSE, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_NIL) {
|
||||
advance(p);
|
||||
return newnode(nkind.N_NIL, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_VOID) {
|
||||
advance(p);
|
||||
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
|
||||
// with empty str (newnode zeroes the node, so str.len is
|
||||
// already 0); the checker rejects it outside lvalue
|
||||
// positions.
|
||||
advance(p);
|
||||
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(nkind.N_ARRLIT, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACK) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
let e: *node = parseexpr(p);
|
||||
if (head == nil) { head = e; tail = e; }
|
||||
else { tail.next = e; tail = e; };
|
||||
if (accepttok(p, tkind.TK_ELLIPSIS)) {
|
||||
let rep: *node = newnode(nkind.N_FIELD,
|
||||
p.curfile, p.curline, p.curcol);
|
||||
rep.str = "...";
|
||||
tail.next = rep;
|
||||
tail = rep;
|
||||
break;
|
||||
};
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACK, "expected ']' after array literal");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
let e: *node = parseexpr(p);
|
||||
// Tuple literal: (a, b, ...)
|
||||
if (accepttok(p, tkind.TK_COMMA)) {
|
||||
let t: *node = newnode(nkind.N_TUPLE, pf, pl, pc);
|
||||
t.list = e;
|
||||
let tail: *node = e;
|
||||
// Parse each element BEFORE the RPAREN-break so `(a,)`
|
||||
// (a single elem + trailing comma) is a loud parse error;
|
||||
// a trailing comma is legal only after >=2 elems. Mirror
|
||||
// cstage cmd/wcc/parse.c:552-558 loop order.
|
||||
for (true) {
|
||||
let en: *node = parseexpr(p);
|
||||
tail.next = en;
|
||||
tail = en;
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
if (p.curkind == tkind.TK_RPAREN) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple");
|
||||
return t;
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')'");
|
||||
return e;
|
||||
};
|
||||
if (p.curkind == tkind.TK_IDENT) {
|
||||
let n: *node = newnode(nkind.N_IDENT, pf, pl, pc);
|
||||
n.str = p.curtext;
|
||||
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.curkind == tkind.TK_LBRACE) {
|
||||
advance(p);
|
||||
let s: *node = newnode(nkind.N_STRUCTLIT, pf, pl, pc);
|
||||
s.lhs = n;
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
// Trailing `...` autofill marker. Stash on s.op so
|
||||
// cgen can zero-fill the slot before per-field stores.
|
||||
if (p.curkind == tkind.TK_ELLIPSIS) {
|
||||
advance(p);
|
||||
s.op = tkind.TK_ELLIPSIS;
|
||||
break;
|
||||
};
|
||||
let fpf: str = p.curfile;
|
||||
let fpl: i32 = p.curline;
|
||||
let fpc: i32 = p.curcol;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
expecttok(p, tkind.TK_ASSIGN, "expected '=' in struct lit field");
|
||||
let v: *node = parseexpr(p);
|
||||
let f: *node = newnode(nkind.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, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACE, "expected '}' after struct literal");
|
||||
s.list = head;
|
||||
return s;
|
||||
};
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_MATCH) {
|
||||
// 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(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");
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind == tkind.TK_CASE) {
|
||||
let cf: str = p.curfile;
|
||||
let cl: i32 = p.curline;
|
||||
let cc: i32 = p.curcol;
|
||||
advance(p); // past `case`
|
||||
let mc: *node = newnode(nkind.N_MCASE, cf, cl, cc);
|
||||
if (p.curkind == tkind.TK_LET) {
|
||||
advance(p);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
mc.str = id;
|
||||
expecttok(p, tkind.TK_COLON, "expected ':' after match binding");
|
||||
mc.lhs = parsetype(p);
|
||||
} else { if (p.curkind != tkind.TK_FATARROW) {
|
||||
mc.lhs = parsetype(p);
|
||||
};};
|
||||
expecttok(p, tkind.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, tkind.TK_RBRACE, "expected '}' after match body");
|
||||
m.list = head;
|
||||
return m;
|
||||
};
|
||||
errmsg(p, "expected expression");
|
||||
advance(p);
|
||||
return newnode(nkind.N_NONE, pf, pl, pc);
|
||||
};
|
||||
|
||||
fn parsearglist(p: *parser, closekind: tkind, headout: **node) void = {
|
||||
*headout = nil;
|
||||
if (p.curkind == closekind) { return; };
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let e: *node = parseexpr(p);
|
||||
// Hare-style spread: `expr...` in an arg slot becomes a
|
||||
// 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(nkind.N_SPREAD, e.file, e.line, e.col);
|
||||
sp.lhs = e;
|
||||
e = sp;
|
||||
};
|
||||
if (head == nil) { head = e; tail = e; }
|
||||
else { tail.next = e; tail = e; };
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
if (p.curkind == closekind) { break; };
|
||||
};
|
||||
*headout = head;
|
||||
};
|
||||
|
||||
fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
let cur: *node = lhs;
|
||||
for (true) {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
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.
|
||||
let is_typeop: i32 = 0;
|
||||
if (cur.kind == nkind.N_IDENT) {
|
||||
if (streqlocal(cur.str, "size")) { is_typeop = 1; };
|
||||
if (streqlocal(cur.str, "align")) { is_typeop = 1; };
|
||||
};
|
||||
if (is_typeop != 0) {
|
||||
n.list = parsetype(p);
|
||||
} else {
|
||||
let arghead: *node = nil;
|
||||
parsearglist(p, tkind.TK_RPAREN, &arghead);
|
||||
n.list = arghead;
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after args");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == tkind.TK_LBRACK) {
|
||||
advance(p);
|
||||
// `[ : hi ]` — slice with implicit lo = 0.
|
||||
if (p.curkind == tkind.TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
if (p.curkind != tkind.TK_RBRACK) {
|
||||
n.cond = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.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.curkind == tkind.TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
if (p.curkind != tkind.TK_RBRACK) {
|
||||
n.cond = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACK, "expected ']' in slice");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
let n: *node = newnode(nkind.N_INDEX, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
expecttok(p, tkind.TK_RBRACK, "expected ']' after index");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == tkind.TK_DOT) {
|
||||
advance(p);
|
||||
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
|
||||
// cgen tuple-positional path matches `cmd/wcc/parse.c`.
|
||||
if (p.curkind == tkind.TK_INT) {
|
||||
n.str = p.curtext;
|
||||
advance(p);
|
||||
} else {
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
};
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == tkind.TK_COLON) {
|
||||
if (p.nocast != 0) {
|
||||
return cur;
|
||||
};
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_CAST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
// Hare-style postfix:
|
||||
// `e as T` — assert lhs is variant T (abort otherwise) → T
|
||||
// `e is T` — bool: does lhs currently hold variant T?
|
||||
// Same precedence level as the `:` cast.
|
||||
if (p.curkind == tkind.TK_AS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_TYPEASSERT, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == tkind.TK_IS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_TYPETEST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
// `e?` — propagate error variant up the stack.
|
||||
// `e!` — abort on error variant.
|
||||
if (p.curkind == tkind.TK_QUESTION) {
|
||||
advance(p);
|
||||
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(nkind.N_TRYUNW, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
break;
|
||||
};
|
||||
return cur;
|
||||
};
|
||||
|
||||
fn parseunary(p: *parser) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
let k: tkind = p.curkind;
|
||||
if (k == tkind.TK_MINUS) {
|
||||
advance(p);
|
||||
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(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(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(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(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(nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.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: tkind = p.curkind;
|
||||
let pr: i32 = bprec(op);
|
||||
if (pr == 0) { return cur; };
|
||||
if (pr < minp) { return cur; };
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p);
|
||||
let rhs: *node = parseunary(p);
|
||||
for (true) {
|
||||
let np: i32 = bprec(p.curkind);
|
||||
if (np <= pr) { break; };
|
||||
rhs = parsebin(p, rhs, np);
|
||||
};
|
||||
let n: *node = newnode(nkind.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.curkind)) {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
let op: tkind = p.curkind;
|
||||
advance(p);
|
||||
let n: *node = newnode(nkind.N_ASSIGN, pf, pl, pc);
|
||||
n.op = op;
|
||||
n.lhs = e;
|
||||
n.rhs = parseexpr(p); // right-associative
|
||||
return n;
|
||||
};
|
||||
return e;
|
||||
};
|
||||
|
||||
878
lib/ww/syntax/lex.ww
Normal file
878
lib/ww/syntax/lex.ww
Normal file
@@ -0,0 +1,878 @@
|
||||
// lib/ww/syntax/lex.ww — port of cmd/wcc/lex.c.
|
||||
//
|
||||
// The DFA, the helpers, and the order of decisions all mirror the C
|
||||
// version exactly. The 990_selfhost test diffs the resulting token
|
||||
// stream against the C-side wwdump byte-for-byte; any divergence is
|
||||
// a port bug.
|
||||
//
|
||||
// Calling-convention note: w6c can't yet pass or return structs >16
|
||||
// bytes by value, so `tok` and `pos` are passed by pointer (out
|
||||
// params). The C version passes `Tok` by value; we differ here only
|
||||
// in shape, not in observable behaviour. Token kind values stay
|
||||
// numerically identical.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
import ascii;
|
||||
import strings;
|
||||
import strconv;
|
||||
|
||||
// isidstart / isidpart — identifier classification. Lexer-local
|
||||
// because the "alpha or '_' / alnum or '_'" set isn't part of Hare's
|
||||
// ascii::; ascii::isalpha + the '_' check live here instead.
|
||||
fn isidstart(c: rune) bool = {
|
||||
if (ascii.isalpha(c)) { return true; };
|
||||
if (c == '_') { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
fn isidpart(c: rune) bool = {
|
||||
if (ascii.isalnum(c)) { return true; };
|
||||
if (c == '_') { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
// hexval — value of `c` as a hex digit (0..15) or void if not a hex
|
||||
// digit. Used by string-literal `\xHH` escapes.
|
||||
fn hexval(c: rune) (i32 | void) = {
|
||||
if (ascii.isdigit(c)) { return (c - '0'): i32; };
|
||||
if (c >= 'A') {
|
||||
if (c <= 'F') { return ((c - 'A') + 10): i32; };
|
||||
};
|
||||
if (c >= 'a') {
|
||||
if (c <= 'f') { return ((c - 'a') + 10): i32; };
|
||||
};
|
||||
return;
|
||||
};
|
||||
|
||||
type lex = struct {
|
||||
file: str,
|
||||
src: *u8, // raw bytes; not necessarily NUL-terminated
|
||||
srclen: u64,
|
||||
lpos: u64,
|
||||
line: i32,
|
||||
col: i32,
|
||||
errs: i32,
|
||||
// a `//ww:module-reset` directive was seen in the last skipped run;
|
||||
// lexnext emits TK_MODRESET before the next real token (#16 opt-B).
|
||||
modreset: i32,
|
||||
// a `//ww:module <path>` directive was seen in the last skipped run;
|
||||
// lexnext emits TK_MODPATH carrying this dotted path (M1 #22).
|
||||
modpathset: i32,
|
||||
modpath: str,
|
||||
// a `//ww:module-reset <path>` directive was seen; the next TK_MODRESET
|
||||
// carries this dotted path so the sep primary body mangles on the path,
|
||||
// not its leaf clause (#57).
|
||||
modresetpathset: i32,
|
||||
modresetpath: str,
|
||||
};
|
||||
|
||||
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.errs = 0;
|
||||
l.modreset = 0;
|
||||
l.modpathset = 0;
|
||||
l.modresetpathset = 0;
|
||||
};
|
||||
|
||||
// srcb — byte at offset; helper that lifts the cast out of indexing.
|
||||
fn srcb(l: *lex, off: u64) i32 = {
|
||||
let i: i32 = off: i32;
|
||||
let b: u8 = l.src[i];
|
||||
return b: i32;
|
||||
};
|
||||
|
||||
fn lpeek(l: *lex, ahead: u64) i32 = {
|
||||
let p: u64 = l.lpos + ahead;
|
||||
if (p >= l.srclen) { return -1; };
|
||||
return srcb(l, p);
|
||||
};
|
||||
|
||||
fn lget(l: *lex) i32 = {
|
||||
if (l.lpos >= l.srclen) { return -1; };
|
||||
let c: i32 = srcb(l, l.lpos);
|
||||
l.lpos += 1u64;
|
||||
if (c == '\n') {
|
||||
l.line += 1;
|
||||
l.col = 1;
|
||||
} else {
|
||||
l.col += 1;
|
||||
};
|
||||
return c;
|
||||
};
|
||||
|
||||
fn curpos(l: *lex, out: *pos) void = {
|
||||
out.file = l.file;
|
||||
out.line = l.line;
|
||||
out.col = l.col;
|
||||
};
|
||||
|
||||
fn errat(l: *lex, p: *pos, msg: str) void = {
|
||||
let pf: str = p.file;
|
||||
os.write(2, pf.ptr, pf.len: u64);
|
||||
os.write(2, ":".ptr, 1u64);
|
||||
let ls: str = strconv.u64tos(p.line: u64, strconv.base.DEC);
|
||||
os.write(2, ls.ptr, ls.len: u64);
|
||||
os.write(2, ":".ptr, 1u64);
|
||||
let cs: str = strconv.u64tos(p.col: u64, strconv.base.DEC);
|
||||
os.write(2, cs.ptr, cs.len: u64);
|
||||
os.write(2, ": error: ".ptr, 9u64);
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
l.errs += 1;
|
||||
};
|
||||
|
||||
fn skipws(l: *lex) bool = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { return false; };
|
||||
if (c == ' ') { lget(l); continue; };
|
||||
if (c == '\t') { lget(l); continue; };
|
||||
if (c == '\r') { lget(l); continue; };
|
||||
if (c == '\n') { lget(l); continue; };
|
||||
if (c == '/') {
|
||||
let c2: i32 = lpeek(l, 1u64);
|
||||
if (c2 == '/') {
|
||||
lget(l); lget(l); // consume '//'
|
||||
// #16 opt-B: recognize the driver's curmod-reset
|
||||
// boundary directive `//ww:module-reset` (whole
|
||||
// line) and flag it; lexnext emits TK_MODRESET.
|
||||
// The body is then skipped like any comment.
|
||||
// Mirrors cstage lex.c skipws. Compare via lpeek
|
||||
// (no consume) so the skip loop below is unchanged.
|
||||
let pre: str = "ww:module";
|
||||
let di: i32 = 0;
|
||||
let matched: bool = true;
|
||||
for (di < pre.len) {
|
||||
if (lpeek(l, di: u64) != pre[di]: i32) {
|
||||
matched = false; break;
|
||||
};
|
||||
di += 1;
|
||||
};
|
||||
if (matched) {
|
||||
let nx: i32 = lpeek(l, pre.len: u64);
|
||||
if (nx == '-') {
|
||||
let rest: str = "-reset";
|
||||
let rj: i32 = 0;
|
||||
let rm: bool = true;
|
||||
for (rj < rest.len) {
|
||||
if (lpeek(l, (pre.len + rj): u64)
|
||||
!= rest[rj]: i32) {
|
||||
rm = false; break;
|
||||
};
|
||||
rj += 1;
|
||||
};
|
||||
if (rm) {
|
||||
let af: i32 = lpeek(l,
|
||||
(pre.len + rest.len): u64);
|
||||
if (af == '\n') { l.modreset = 1; }
|
||||
else { if (af < 0) { l.modreset = 1; }
|
||||
else { if (af == ' ' || af == '\t') {
|
||||
// `//ww:module-reset <path>` — sep
|
||||
// primary body tagged by its full
|
||||
// dotted import path (#57).
|
||||
let k: u64 =
|
||||
(pre.len + rest.len): u64;
|
||||
for (true) {
|
||||
let sc: i32 = lpeek(l, k);
|
||||
if (sc == ' ' || sc == '\t') {
|
||||
k += 1u64; continue;
|
||||
};
|
||||
break;
|
||||
};
|
||||
let s0: u64 = k;
|
||||
for (true) {
|
||||
let pc: i32 = lpeek(l, k);
|
||||
if (pc < 0) { break; };
|
||||
if (pc == '\n' || pc == '\r'
|
||||
|| pc == ' '
|
||||
|| pc == '\t') {
|
||||
break;
|
||||
};
|
||||
k += 1u64;
|
||||
};
|
||||
l.modreset = 1;
|
||||
if (k > s0) {
|
||||
let view: str;
|
||||
view.ptr =
|
||||
l.src + l.lpos + s0;
|
||||
view.len = (k - s0): i32;
|
||||
l.modresetpath =
|
||||
strings.dup(view);
|
||||
l.modresetpathset = 1;
|
||||
};
|
||||
}; }; };
|
||||
};
|
||||
} else { if (nx == ' ' || nx == '\t') {
|
||||
// `//ww:module <path>` — M1 import boundary.
|
||||
let k: u64 = pre.len: u64;
|
||||
for (true) {
|
||||
let sc: i32 = lpeek(l, k);
|
||||
if (sc == ' ' || sc == '\t') {
|
||||
k += 1u64; continue;
|
||||
};
|
||||
break;
|
||||
};
|
||||
let s0: u64 = k;
|
||||
for (true) {
|
||||
let pc: i32 = lpeek(l, k);
|
||||
if (pc < 0) { break; };
|
||||
if (pc == '\n' || pc == '\r'
|
||||
|| pc == ' ' || pc == '\t') {
|
||||
break;
|
||||
};
|
||||
k += 1u64;
|
||||
};
|
||||
if (k > s0) {
|
||||
let view: str;
|
||||
view.ptr = l.src + l.lpos + s0;
|
||||
view.len = (k - s0): i32;
|
||||
l.modpath = strings.dup(view);
|
||||
l.modpathset = 1;
|
||||
};
|
||||
}; };
|
||||
};
|
||||
for (true) {
|
||||
let cx: i32 = lpeek(l, 0u64);
|
||||
if (cx < 0) { return false; };
|
||||
if (cx == '\n') { break; };
|
||||
lget(l);
|
||||
};
|
||||
continue;
|
||||
};
|
||||
if (c2 == '*') {
|
||||
lget(l); lget(l);
|
||||
let prev: i32 = -1;
|
||||
for (true) {
|
||||
let x: i32 = lget(l);
|
||||
if (x < 0) {
|
||||
let cp: pos;
|
||||
curpos(l, &cp);
|
||||
errat(l, &cp, "unterminated /* comment");
|
||||
return false;
|
||||
};
|
||||
if (prev == '*') {
|
||||
if (x == '/') { break; };
|
||||
};
|
||||
prev = x;
|
||||
};
|
||||
continue;
|
||||
};
|
||||
};
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
fn parseint(p: *u8, n: u64, base: i32, ok: *bool) u64 = {
|
||||
let v: u64 = 0u64;
|
||||
let got: bool = false;
|
||||
let i: u64 = 0u64;
|
||||
for (i < n) {
|
||||
let ix: i32 = i: i32;
|
||||
let c: u8 = p[ix];
|
||||
if (c == '_') {
|
||||
i += 1u64;
|
||||
continue;
|
||||
};
|
||||
let d: i32 = -1;
|
||||
if (c >= 48u8) {
|
||||
if (c <= 57u8) { d = (c - 48u8): i32; };
|
||||
};
|
||||
if (d < 0) {
|
||||
if (c >= 97u8) {
|
||||
if (c <= 102u8) { d = ((c - 97u8) + 10u8): i32; };
|
||||
};
|
||||
};
|
||||
if (d < 0) {
|
||||
if (c >= 65u8) {
|
||||
if (c <= 70u8) { d = ((c - 65u8) + 10u8): i32; };
|
||||
};
|
||||
};
|
||||
if (d < 0) { *ok = false; return 0u64; };
|
||||
if (d >= base) { *ok = false; return 0u64; };
|
||||
if (v > ~0u64 / (base: u64)) { *ok = false; return 0u64; };
|
||||
v = v * (base: u64) + (d: u64);
|
||||
got = true;
|
||||
i += 1u64;
|
||||
};
|
||||
*ok = got;
|
||||
return v;
|
||||
};
|
||||
|
||||
fn escape(l: *lex, out: *i32) bool = {
|
||||
let c: i32 = lget(l);
|
||||
if (c < 0) { return false; };
|
||||
if (c == 'n') { *out = '\n'; return true; };
|
||||
if (c == 't') { *out = '\t'; return true; };
|
||||
if (c == 'r') { *out = '\r'; return true; };
|
||||
if (c == '\\') { *out = '\\'; return true; };
|
||||
if (c == '\'') { *out = '\''; return true; };
|
||||
if (c == '"') { *out = '"'; return true; };
|
||||
if (c == '0') { *out = '\0'; return true; };
|
||||
if (c == 'a') { *out = '\a'; return true; };
|
||||
if (c == 'b') { *out = '\b'; return true; };
|
||||
if (c == 'f') { *out = '\f'; return true; };
|
||||
if (c == 'v') { *out = '\v'; return true; };
|
||||
if (c == 'x') {
|
||||
let hi: i32 = lget(l);
|
||||
let lo: i32 = lget(l);
|
||||
if (hi < 0) { return false; };
|
||||
if (lo < 0) { return false; };
|
||||
if (!ascii.isxdigit(hi: rune)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
if (!ascii.isxdigit(lo: rune)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
// Hex digits already validated by isxdigit above — `!`
|
||||
// (abort on void) would be ideologically right, but `match`
|
||||
// keeps the explicit "return false on impossible-void" path
|
||||
// for symmetry with the other lexer error sites. Use `!`
|
||||
// once we have a panic-with-position helper.
|
||||
let h: i32 = hexval(hi: rune)!;
|
||||
let lv: i32 = hexval(lo: rune)!;
|
||||
*out = (h << 4) | lv;
|
||||
return true;
|
||||
};
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad escape");
|
||||
return false;
|
||||
};
|
||||
|
||||
// scandecimalrun — consume a run of decimal digits and underscores.
|
||||
fn scandecimalrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isdigit(c: rune)) {
|
||||
if (c != '_') { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanhexrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isxdigit(c: rune)) {
|
||||
if (c != '_') { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanbinrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c == '0') { lget(l); continue; };
|
||||
if (c == '1') { lget(l); continue; };
|
||||
if (c == '_') { lget(l); continue; };
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
fn scanoctrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < '0') { break; };
|
||||
if (c > '7') {
|
||||
if (c != '_') { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
// scanexp — consume the [eE][+-]?[0-9]+ tail of a float, if present.
|
||||
fn scanexp(l: *lex) void = {
|
||||
let e: i32 = lpeek(l, 0u64);
|
||||
if (e != 'e') { if (e != 'E') { return; }; };
|
||||
lget(l);
|
||||
let s: i32 = lpeek(l, 0u64);
|
||||
if (s == '+') { lget(l); }
|
||||
else { if (s == '-') { lget(l); }; };
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isdigit(c: rune)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn lexnum(l: *lex, start: *pos, out: *tok) void = {
|
||||
out.kind = tkind.TK_INT;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let begin: u64 = l.lpos;
|
||||
let base: i32 = 10;
|
||||
let isfloat: bool = false;
|
||||
|
||||
let c0: i32 = lpeek(l, 0u64);
|
||||
let c1: i32 = lpeek(l, 1u64);
|
||||
|
||||
if (c0 == '0') {
|
||||
if (c1 == 'x') {
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 'X') {
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 'b') {
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 'B') {
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 'o') {
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else { if (c1 == 'O') {
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == '.') {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= '0') {
|
||||
if (after <= '9') {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};};};};};};
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == '.') {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= '0') {
|
||||
if (after <= '9') {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
let n: u64 = l.lpos - begin;
|
||||
let view: str;
|
||||
view.ptr = l.src + begin;
|
||||
view.len = n: i32;
|
||||
out.text = strings.dup(view);
|
||||
|
||||
if (isfloat) {
|
||||
out.kind = tkind.TK_FLOAT;
|
||||
// Strip underscores from the digits (Hare allows 1_000.5)
|
||||
// before parsing — match what cmd/wcc/lex.c does with
|
||||
// strtod over a cleaned buffer.
|
||||
let clean: []u8 = alloc([], n + 1u64)!;
|
||||
let i: u64 = 0u64;
|
||||
let j: u64 = 0u64;
|
||||
for (i < n) {
|
||||
let b: u8 = l.src[begin + i];
|
||||
if (b != '_') {
|
||||
clean[j] = b;
|
||||
j += 1u64;
|
||||
};
|
||||
i += 1u64;
|
||||
};
|
||||
clean[j] = 0u8;
|
||||
let cleanv: str;
|
||||
cleanv.ptr = clean.ptr;
|
||||
cleanv.len = j: i32;
|
||||
// strconv's correctly-rounded decimal engine — cstage folds
|
||||
// via strtod, and a leaner pow-10 fold here was 1-2 ULP off
|
||||
// on long-mantissa/extreme literals (cs≠ww DATA bits, #62).
|
||||
// `0: f64` cast, not a 0.0 literal: 990's wwdump diff relies
|
||||
// on this file tokenising identically through C and ww, and
|
||||
// the C dumper %g-formats TK_FLOAT.fval while the ww dumper
|
||||
// skips it.
|
||||
// Retained divergence (task #21): SUBNORMAL literals are
|
||||
// accepted here correctly-rounded (Hare stof semantics)
|
||||
// but rejected by cstage (glibc strtod flags partial
|
||||
// underflow with ERANGE).
|
||||
let fv: f64 = 0: f64;
|
||||
match (strconv.stof64(cleanv, strconv.base.DEC)) {
|
||||
case let v: f64 => { fv = v; };
|
||||
case let e: strconv.invalid => {
|
||||
errat(l, start, "bad float literal");
|
||||
};
|
||||
case let e: strconv.overflow => {
|
||||
errat(l, start, "bad float literal");
|
||||
};
|
||||
};
|
||||
out.fval = fv;
|
||||
// Stash the IEEE bits in uval — cgen consumers read floats
|
||||
// as integers (n.uval) to avoid an SSE round-trip when
|
||||
// materialising the constant.
|
||||
let pu: *u64 = (&fv): *u64;
|
||||
out.uval = *pu;
|
||||
} else {
|
||||
let digs: *u8 = l.src + begin;
|
||||
let dn: u64 = n;
|
||||
if (base != 10) {
|
||||
digs = digs + 2u64;
|
||||
dn -= 2u64;
|
||||
};
|
||||
let ok: bool = false;
|
||||
out.uval = parseint(digs, dn, base, &ok);
|
||||
if (!ok) {
|
||||
errat(l, start, "bad integer literal");
|
||||
out.kind = tkind.TK_ERR;
|
||||
};
|
||||
};
|
||||
|
||||
let pc: i32 = lpeek(l, 0u64);
|
||||
if (pc >= 0) {
|
||||
if (isidstart(pc: rune)) {
|
||||
let sb: u64 = l.lpos;
|
||||
for (true) {
|
||||
let cc: i32 = lpeek(l, 0u64);
|
||||
if (cc < 0) { break; };
|
||||
if (!isidpart(cc: rune)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let sl: u64 = l.lpos - sb;
|
||||
let p: *u8 = l.src + sb;
|
||||
let isok: bool = false;
|
||||
if (sl == 2u64) {
|
||||
if (p[0] == 'i') {
|
||||
if (p[1] == '8') { isok = true; }; // i8
|
||||
};
|
||||
if (p[0] == 'u') {
|
||||
if (p[1] == '8') { isok = true; }; // u8
|
||||
};
|
||||
};
|
||||
if (sl == 3u64) {
|
||||
if (p[0] == 'i') {
|
||||
if (p[1] == '1') { if (p[2] == '6') { isok = true; }; }; // i16
|
||||
if (p[1] == '3') { if (p[2] == '2') { isok = true; }; }; // i32
|
||||
if (p[1] == '6') { if (p[2] == '4') { isok = true; }; }; // i64
|
||||
};
|
||||
if (p[0] == 'u') {
|
||||
if (p[1] == '1') { if (p[2] == '6') { isok = true; }; };
|
||||
if (p[1] == '3') { if (p[2] == '2') { isok = true; }; };
|
||||
if (p[1] == '6') { if (p[2] == '4') { isok = true; }; };
|
||||
};
|
||||
if (p[0] == 'f') {
|
||||
if (p[1] == '3') { if (p[2] == '2') { isok = true; }; }; // f32
|
||||
if (p[1] == '6') { if (p[2] == '4') { isok = true; }; }; // f64
|
||||
};
|
||||
};
|
||||
if (isok) {
|
||||
let view: str;
|
||||
view.ptr = p;
|
||||
view.len = sl: i32;
|
||||
out.tsuffix = strings.dup(view);
|
||||
} else {
|
||||
l.lpos = sb;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
fn lexident(l: *lex, start: *pos, out: *tok) void = {
|
||||
let begin: u64 = l.lpos;
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!isidpart(c: rune)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let n: u64 = l.lpos - begin;
|
||||
let p: *u8 = l.src + begin;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
// Bare '_' is the discard marker. `_x`, `_1` are normal idents.
|
||||
if (n == 1u64) {
|
||||
if (p[0] == '_') {
|
||||
out.kind = tkind.TK_UNDER;
|
||||
let view: str;
|
||||
view.ptr = p;
|
||||
view.len = n: i32;
|
||||
out.text = strings.dup(view);
|
||||
return;
|
||||
};
|
||||
};
|
||||
let k: tkind = kwlookup(p, n: i32);
|
||||
if (k != tkind.TK_NONE) {
|
||||
out.kind = k;
|
||||
} else {
|
||||
out.kind = tkind.TK_IDENT;
|
||||
};
|
||||
let view: str;
|
||||
view.ptr = p;
|
||||
view.len = n: i32;
|
||||
out.text = strings.dup(view);
|
||||
};
|
||||
|
||||
fn lexstr(l: *lex, start: *pos, out: *tok) void = {
|
||||
let cap: u64 = 32u64;
|
||||
let nb: u64 = 0u64;
|
||||
let buf: []u8 = alloc([], cap)!;
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) {
|
||||
errat(l, start, "unterminated string");
|
||||
out.kind = tkind.TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let view: str;
|
||||
view.ptr = "".ptr;
|
||||
view.len = 0;
|
||||
out.text = strings.dup(view);
|
||||
return;
|
||||
};
|
||||
if (c == '"') { lget(l); break; };
|
||||
let ch: i32 = 0;
|
||||
if (c == '\\') {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (nb + 1u64 >= cap) {
|
||||
let ncap: u64 = cap * 2u64;
|
||||
let nb2: []u8 = alloc([], ncap)!;
|
||||
let i: u64 = 0u64;
|
||||
for (i < nb) {
|
||||
let ix: i32 = i: i32;
|
||||
nb2[ix] = buf[ix];
|
||||
i += 1u64;
|
||||
};
|
||||
buf = nb2;
|
||||
cap = ncap;
|
||||
};
|
||||
let nbi: i32 = nb: i32;
|
||||
buf[nbi] = ch: u8;
|
||||
nb += 1u64;
|
||||
};
|
||||
out.kind = tkind.TK_STR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let s: str;
|
||||
s.ptr = buf.ptr;
|
||||
s.len = nb: i32;
|
||||
out.text = s;
|
||||
};
|
||||
|
||||
fn lexrune(l: *lex, start: *pos, out: *tok) void = {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) {
|
||||
errat(l, start, "unterminated rune");
|
||||
out.kind = tkind.TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let view: str;
|
||||
view.ptr = "".ptr;
|
||||
view.len = 0;
|
||||
out.text = strings.dup(view);
|
||||
return;
|
||||
};
|
||||
let ch: i32 = 0;
|
||||
if (c == '\\') {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (lpeek(l, 0u64) != '\'') {
|
||||
errat(l, start, "rune literal missing closing '");
|
||||
out.kind = tkind.TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let view: str;
|
||||
view.ptr = "".ptr;
|
||||
view.len = 0;
|
||||
out.text = strings.dup(view);
|
||||
return;
|
||||
};
|
||||
lget(l);
|
||||
out.kind = tkind.TK_RUNE;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.uval = ch: u64;
|
||||
};
|
||||
|
||||
fn emitsimple(start: *pos, k: tkind, out: *tok) void = {
|
||||
out.kind = k;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
};
|
||||
|
||||
// setposfrom — copy file/line/col from a *pos into a tok. Used by
|
||||
// the err-token path where we already have a pos.
|
||||
fn setposfrom(out: *tok, p: *pos) void = {
|
||||
out.file = p.file;
|
||||
out.line = p.line;
|
||||
out.col = p.col;
|
||||
};
|
||||
|
||||
export fn lexnext(l: *lex, out: *tok) void = {
|
||||
// Reset the out token so callers can rely on stale fields being
|
||||
// cleared (they only inspect kind, pos, text, uval, fval, tsuffix
|
||||
// per kind).
|
||||
out.kind = tkind.TK_NONE;
|
||||
out.uval = 0u64;
|
||||
// out.fval starts cleared by the caller's stack-local init (lex.ww
|
||||
// allocates the tok with `let t: tok;` which zeroes). We avoid
|
||||
// writing a 0.0 literal here so this file itself stays float-free
|
||||
// and the C/ww wwdump diff over it is byte-identical.
|
||||
let empty: str;
|
||||
empty.ptr = nil;
|
||||
empty.len = 0;
|
||||
out.text = empty;
|
||||
out.tsuffix = empty;
|
||||
|
||||
let more: bool = skipws(l);
|
||||
let start: pos; curpos(l, &start);
|
||||
// A `//ww:module-reset` seen in the skipped run surfaces as its own
|
||||
// token before the next real one (#16 opt-B boundary reset).
|
||||
if (l.modreset != 0) {
|
||||
l.modreset = 0;
|
||||
emitsimple(&start, tkind.TK_MODRESET, out);
|
||||
// path-carrying reset → text=path (#57); bare reset → text empty
|
||||
if (l.modresetpathset != 0) {
|
||||
l.modresetpathset = 0;
|
||||
out.text = l.modresetpath;
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (l.modpathset != 0) {
|
||||
l.modpathset = 0;
|
||||
emitsimple(&start, tkind.TK_MODPATH, out);
|
||||
out.text = l.modpath;
|
||||
return;
|
||||
};
|
||||
if (!more) {
|
||||
emitsimple(&start, tkind.TK_EOF, out);
|
||||
return;
|
||||
};
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
|
||||
if (c >= 0) {
|
||||
if (isidstart(c: rune)) { lexident(l, &start, out); return; };
|
||||
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
|
||||
};
|
||||
|
||||
if (c == '"') { lget(l); lexstr(l, &start, out); return; };
|
||||
if (c == '\'') { lget(l); lexrune(l, &start, out); return; };
|
||||
|
||||
lget(l);
|
||||
|
||||
if (c == '(') { emitsimple(&start, tkind.TK_LPAREN, out); return; };
|
||||
if (c == ')') { emitsimple(&start, tkind.TK_RPAREN, out); return; };
|
||||
if (c == '{') { emitsimple(&start, tkind.TK_LBRACE, out); return; };
|
||||
if (c == '}') { emitsimple(&start, tkind.TK_RBRACE, out); return; };
|
||||
if (c == '[') { emitsimple(&start, tkind.TK_LBRACK, out); return; };
|
||||
if (c == ']') { emitsimple(&start, tkind.TK_RBRACK, out); return; };
|
||||
if (c == ',') { emitsimple(&start, tkind.TK_COMMA, out); return; };
|
||||
if (c == ';') { emitsimple(&start, tkind.TK_SEMI, out); return; };
|
||||
if (c == ':') { emitsimple(&start, tkind.TK_COLON, out); return; };
|
||||
if (c == '@') { emitsimple(&start, tkind.TK_AT, out); return; };
|
||||
if (c == '?') { emitsimple(&start, tkind.TK_QUESTION, out); return; };
|
||||
if (c == '~') { emitsimple(&start, tkind.TK_TILDE, out); return; };
|
||||
|
||||
if (c == '.') {
|
||||
if (lpeek(l, 0u64) == '.') {
|
||||
if (lpeek(l, 1u64) == '.') {
|
||||
lget(l); lget(l);
|
||||
emitsimple(&start, tkind.TK_ELLIPSIS, out); return;
|
||||
};
|
||||
lget(l);
|
||||
emitsimple(&start, tkind.TK_DOTDOT, out); return;
|
||||
};
|
||||
emitsimple(&start, tkind.TK_DOT, out); return;
|
||||
};
|
||||
|
||||
if (c == '+') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_PLUSEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_PLUS, out); return;
|
||||
};
|
||||
if (c == '-') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_MINUSEQ, out); return; };
|
||||
if (lpeek(l, 0u64) == '>') { lget(l); emitsimple(&start, tkind.TK_ARROW, out); return; };
|
||||
emitsimple(&start, tkind.TK_MINUS, out); return;
|
||||
};
|
||||
if (c == '*') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_STAREQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_STAR, out); return;
|
||||
};
|
||||
if (c == '/') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_SLASHEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_SLASH, out); return;
|
||||
};
|
||||
if (c == '%') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_PERCENTEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_PERCENT, out); return;
|
||||
};
|
||||
if (c == '&') {
|
||||
if (lpeek(l, 0u64) == '&') { lget(l); emitsimple(&start, tkind.TK_AND, out); return; };
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_AMPEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_AMP, out); return;
|
||||
};
|
||||
if (c == '|') {
|
||||
if (lpeek(l, 0u64) == '|') { lget(l); emitsimple(&start, tkind.TK_OR, out); return; };
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_PIPEEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_PIPE, out); return;
|
||||
};
|
||||
if (c == '^') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_CARETEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_CARET, out); return;
|
||||
};
|
||||
if (c == '=') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_EQ, out); return; };
|
||||
if (lpeek(l, 0u64) == '>') { lget(l); emitsimple(&start, tkind.TK_FATARROW, out); return; };
|
||||
emitsimple(&start, tkind.TK_ASSIGN, out); return;
|
||||
};
|
||||
if (c == '!') {
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_NEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_NOT, out); return;
|
||||
};
|
||||
if (c == '<') {
|
||||
if (lpeek(l, 0u64) == '<') {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_LSHIFTEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_LSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_LE, out); return; };
|
||||
if (lpeek(l, 0u64) == '-') { lget(l); emitsimple(&start, tkind.TK_LARROW, out); return; };
|
||||
emitsimple(&start, tkind.TK_LT, out); return;
|
||||
};
|
||||
if (c == '>') {
|
||||
if (lpeek(l, 0u64) == '>') {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_RSHIFTEQ, out); return; };
|
||||
emitsimple(&start, tkind.TK_RSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == '=') { lget(l); emitsimple(&start, tkind.TK_GE, out); return; };
|
||||
emitsimple(&start, tkind.TK_GT, out); return;
|
||||
};
|
||||
|
||||
errat(l, &start, "unexpected character");
|
||||
out.kind = tkind.TK_ERR;
|
||||
setposfrom(out, &start);
|
||||
let one: [1]u8;
|
||||
one[0] = c: u8;
|
||||
let view: str;
|
||||
view.ptr = one.ptr;
|
||||
view.len = 1;
|
||||
out.text = strings.dup(view);
|
||||
};
|
||||
545
lib/ww/syntax/parse.ww
Normal file
545
lib/ww/syntax/parse.ww
Normal file
@@ -0,0 +1,545 @@
|
||||
// lib/ww/syntax/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.
|
||||
|
||||
package syntax;
|
||||
|
||||
// Sibling files (expr, stmt, decl) are the same `package syntax`,
|
||||
// auto-resolved via task #22 dir-enum of lib/ww/syntax/.
|
||||
import os;
|
||||
import strings;
|
||||
|
||||
type parser = struct {
|
||||
l: *lex,
|
||||
errs: i32,
|
||||
// nocast: while inside `[...]` we treat ':' as the slice
|
||||
// separator, not the cast operator. Mirrors parse.c's flag.
|
||||
nocast: i32,
|
||||
curkind: tkind,
|
||||
curfile: str,
|
||||
curline: i32,
|
||||
curcol: i32,
|
||||
curtext: str,
|
||||
curuval: u64,
|
||||
curfval: f64,
|
||||
// curtsuffix: typed numeric literal suffix ("i32", "u64", ...) on
|
||||
// the current TK_INT / TK_FLOAT token, or empty. Parseprimary
|
||||
// copies this onto the N_INTLIT / N_FLOATLIT node so cgen's
|
||||
// rhstargetname can map `42i64` to the i64 variant of a tagged
|
||||
// union without falling back to "first non-str variant" (which
|
||||
// silently picked tag 0 for typed-int literals; see #10).
|
||||
curtsuffix: str,
|
||||
// curmod: the most-recent `module foo;` declaration. Each
|
||||
// top-level decl is stamped with this value; on concatenated
|
||||
// multi-file streams successive `module` decls mark per-file
|
||||
// section boundaries. Mirrors cstage Parser.curmod.
|
||||
curmod: str,
|
||||
// M1 #22: active `//ww:module <path>` dotted import path. While set,
|
||||
// decls stamp nmod=pathmod and imported=1, and the in-file `package`
|
||||
// clause is an assertion. "" means inactive (root/primary).
|
||||
pathmod: str,
|
||||
// #57: active `//ww:module-reset <path>` dotted path. Mangles decls on
|
||||
// the path WITHOUT imported=1 (primary-ness for -c and the #32 bare-main
|
||||
// rule stay intact), and the in-file `package` clause asserts (leaf ==
|
||||
// last component) instead of overwriting curmod. "" means inactive.
|
||||
resetmod: str,
|
||||
};
|
||||
|
||||
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;
|
||||
p.curfval = t.fval;
|
||||
p.curtsuffix = t.tsuffix;
|
||||
};
|
||||
|
||||
export fn parserinit(p: *parser, l: *lex) void = {
|
||||
p.l = l;
|
||||
p.errs = 0;
|
||||
p.nocast = 0;
|
||||
p.pathmod = "";
|
||||
p.resetmod = "";
|
||||
refill(p);
|
||||
};
|
||||
|
||||
fn advance(p: *parser) void = { refill(p); };
|
||||
|
||||
fn accepttok(p: *parser, k: tkind) bool = {
|
||||
if (p.curkind == k) { advance(p); return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
fn errmsg(p: *parser, msg: str) void = {
|
||||
let pre = "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: tkind, what: str) bool = {
|
||||
if (p.curkind == k) { advance(p); return true; };
|
||||
errmsg(p, what);
|
||||
return false;
|
||||
};
|
||||
|
||||
// expectident — consume the current tkind.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 != tkind.TK_IDENT) {
|
||||
errmsg(p, "expected identifier");
|
||||
advance(p);
|
||||
return false;
|
||||
};
|
||||
*into = p.curtext;
|
||||
advance(p);
|
||||
return true;
|
||||
};
|
||||
|
||||
// expectbindname — like expectident but also accepts a bare `_`
|
||||
// discard marker. On `_`, returns "" so the checker skips
|
||||
// scope_define for the binding.
|
||||
fn expectbindname(p: *parser, into: *str) bool = {
|
||||
if (p.curkind == tkind.TK_UNDER) {
|
||||
*into = "";
|
||||
advance(p);
|
||||
return true;
|
||||
};
|
||||
return expectident(p, into);
|
||||
};
|
||||
|
||||
// ---- 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.
|
||||
|
||||
// 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(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;
|
||||
let j: i32 = 0;
|
||||
for (j < head.len) { buf[i] = head[j]; i += 1u64; j += 1; };
|
||||
buf[i] = '.';
|
||||
i += 1u64;
|
||||
j = 0;
|
||||
for (j < tail.len) { buf[i] = tail[j]; i += 1u64; j += 1; };
|
||||
buf[i] = 0u8;
|
||||
let r: str;
|
||||
r.ptr = buf.ptr;
|
||||
r.len = n: i32;
|
||||
return r;
|
||||
};
|
||||
|
||||
fn parsetype(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
|
||||
if (p.curkind == tkind.TK_NOT) {
|
||||
// `!T` — Hare error-flagged type wrapper.
|
||||
advance(p);
|
||||
let n = newnode(nkind.N_TBANG, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_STAR) {
|
||||
advance(p);
|
||||
let n = newnode(nkind.N_TPTR, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_LBRACK) {
|
||||
advance(p);
|
||||
if (p.curkind == tkind.TK_RBRACK) {
|
||||
advance(p);
|
||||
let n = newnode(nkind.N_TSLICE, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
let n = 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.
|
||||
if (p.curkind == tkind.TK_UNDER) {
|
||||
advance(p);
|
||||
} else {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACK, "expected ']' in array type");
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_STRUCT) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' after struct");
|
||||
let n = newnode(nkind.N_TSTRUCT, pf, pl, pc);
|
||||
let fhead: *node = nil;
|
||||
let ftail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
let fpf = p.curfile;
|
||||
let fpl = p.curline;
|
||||
let fpc = p.curcol;
|
||||
let f = newnode(nkind.N_TFIELD, fpf, fpl, fpc);
|
||||
let fid: str;
|
||||
expectident(p, &fid);
|
||||
f.str = fid;
|
||||
expecttok(p, tkind.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, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACE, "expected '}' after struct fields");
|
||||
n.list = fhead;
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_ENUM) {
|
||||
// `enum [storage] { NAME [= expr], ... }`
|
||||
// Storage defaults to i32 (lhs == nil). Each member is an
|
||||
// nkind.N_TENUMMEMBER with str=name and lhs = value expr or nil
|
||||
// (auto-increment when omitted).
|
||||
advance(p);
|
||||
let n = newnode(nkind.N_TENUM, pf, pl, pc);
|
||||
if (p.curkind != tkind.TK_LBRACE) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' after enum");
|
||||
let mhead: *node = nil;
|
||||
let mtail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
let mpf = p.curfile;
|
||||
let mpl = p.curline;
|
||||
let mpc = p.curcol;
|
||||
let m = newnode(nkind.N_TENUMMEMBER, mpf, mpl, mpc);
|
||||
let mid: str;
|
||||
expectident(p, &mid);
|
||||
m.str = mid;
|
||||
if (accepttok(p, tkind.TK_ASSIGN)) {
|
||||
m.lhs = parseexpr(p);
|
||||
};
|
||||
if (mhead == nil) { mhead = m; mtail = m; }
|
||||
else { mtail.next = m; mtail = m; };
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACE, "expected '}' after enum members");
|
||||
n.list = mhead;
|
||||
return n;
|
||||
};
|
||||
|
||||
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 = newnode(nkind.N_TNAME, pf, pl, pc);
|
||||
n.str = "void";
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_IDENT) {
|
||||
let n = newnode(nkind.N_TNAME, pf, pl, pc);
|
||||
let acc = p.curtext;
|
||||
advance(p);
|
||||
// Dotted path collapse: pkg.Type → single TNAME with the
|
||||
// joined string. Mirrors C parsetype's loop.
|
||||
for (p.curkind == tkind.TK_DOT) {
|
||||
advance(p);
|
||||
if (p.curkind != tkind.TK_IDENT) { break; };
|
||||
acc = joindotted(acc, p.curtext);
|
||||
advance(p);
|
||||
};
|
||||
n.str = acc;
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
// (T) or (T, T, ...) or (T | T | ...)
|
||||
//
|
||||
// Each tagged variant may be prefixed with `...` to mark a
|
||||
// spread — when the variant resolves to another tagged union
|
||||
// its variants are flattened into the enclosing union. We
|
||||
// tag the spread on node.op = TK_ELLIPSIS so resolve_type
|
||||
// can distinguish intent. Mirrors C parsetype.
|
||||
advance(p);
|
||||
let firstspread = accepttok(p, tkind.TK_ELLIPSIS);
|
||||
let first = parsetype(p);
|
||||
if (firstspread) { first.op = tkind.TK_ELLIPSIS; };
|
||||
if (accepttok(p, tkind.TK_PIPE)) {
|
||||
let n = newnode(nkind.N_TTAGGED, pf, pl, pc);
|
||||
let head = first;
|
||||
let tail = first;
|
||||
for (true) {
|
||||
let spread = accepttok(p, tkind.TK_ELLIPSIS);
|
||||
let e = parsetype(p);
|
||||
if (spread) { e.op = tkind.TK_ELLIPSIS; };
|
||||
tail.next = e;
|
||||
tail = e;
|
||||
if (!accepttok(p, tkind.TK_PIPE)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' in tagged-union type");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
if (firstspread) {
|
||||
errmsg(p, "spread '...' only valid before tagged-union variants");
|
||||
};
|
||||
if (!accepttok(p, tkind.TK_COMMA)) {
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type");
|
||||
return first;
|
||||
};
|
||||
// Wrap each element in N_TPARAM so the chain owns its .next.
|
||||
// Mirrors cstage's Tparam (cmd/wcc/check.c:1437-1451); lifted
|
||||
// to the AST layer here because wwstage has no separate type
|
||||
// layer, and exprtype must return shared element-type nodes
|
||||
// (sym.decl.lhs, struct field's .lhs, another N_TTUPLE element)
|
||||
// without corrupting source ASTs.
|
||||
let n = newnode(nkind.N_TTUPLE, pf, pl, pc);
|
||||
let firstwrap = newnode(nkind.N_TPARAM, pf, pl, pc);
|
||||
firstwrap.lhs = first;
|
||||
let head = firstwrap;
|
||||
let tail = firstwrap;
|
||||
for (true) {
|
||||
let e = parsetype(p);
|
||||
let w = newnode(nkind.N_TPARAM, e.file, e.line, e.col);
|
||||
w.lhs = e;
|
||||
tail.next = w;
|
||||
tail = w;
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
if (p.curkind == tkind.TK_RPAREN) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple type");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_FN) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after fn in type");
|
||||
let n = 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.
|
||||
n.list = parseparams(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after fn type params");
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
errmsg(p, "expected type");
|
||||
advance(p);
|
||||
return newnode(nkind.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: tkind) i32 = {
|
||||
if (k == tkind.TK_OR) { return 1; };
|
||||
if (k == tkind.TK_AND) { return 2; };
|
||||
if (k == tkind.TK_EQ) { return 3; };
|
||||
if (k == tkind.TK_NEQ) { return 3; };
|
||||
if (k == tkind.TK_LT) { return 4; };
|
||||
if (k == tkind.TK_LE) { return 4; };
|
||||
if (k == tkind.TK_GT) { return 4; };
|
||||
if (k == tkind.TK_GE) { return 4; };
|
||||
if (k == tkind.TK_PIPE) { return 5; };
|
||||
if (k == tkind.TK_CARET) { return 6; };
|
||||
if (k == tkind.TK_AMP) { return 7; };
|
||||
if (k == tkind.TK_LSHIFT) { return 8; };
|
||||
if (k == tkind.TK_RSHIFT) { return 8; };
|
||||
if (k == tkind.TK_PLUS) { return 9; };
|
||||
if (k == tkind.TK_MINUS) { return 9; };
|
||||
if (k == tkind.TK_STAR) { return 10; };
|
||||
if (k == tkind.TK_SLASH) { return 10; };
|
||||
if (k == tkind.TK_PERCENT) { return 10; };
|
||||
return 0;
|
||||
};
|
||||
|
||||
fn isassignop(k: tkind) bool = {
|
||||
if (k == tkind.TK_ASSIGN) { return true; };
|
||||
if (k == tkind.TK_PLUSEQ) { return true; };
|
||||
if (k == tkind.TK_MINUSEQ) { return true; };
|
||||
if (k == tkind.TK_STAREQ) { return true; };
|
||||
if (k == tkind.TK_SLASHEQ) { return true; };
|
||||
if (k == tkind.TK_PERCENTEQ) { return true; };
|
||||
if (k == tkind.TK_AMPEQ) { return true; };
|
||||
if (k == tkind.TK_PIPEEQ) { return true; };
|
||||
if (k == tkind.TK_CARETEQ) { return true; };
|
||||
if (k == tkind.TK_LSHIFTEQ) { return true; };
|
||||
if (k == tkind.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 = newnode(nkind.N_FILE, p.curfile, p.curline, p.curcol);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_EOF) {
|
||||
// `package foo;` — each contributing source's section in a
|
||||
// concatenated stream begins with one. Single-file inputs
|
||||
// may omit it (curmod stays empty; decls treated as primary).
|
||||
//
|
||||
// Retained divergence from brief: strict missing-`package`
|
||||
// error softened to silent-default — 63 inline-source test
|
||||
// wrappers depend on the soft behavior. See task #23 for
|
||||
// the wrapper migration that unblocks the strict check.
|
||||
// Rule 7 + rule 8 documentation.
|
||||
if (p.curkind == tkind.TK_MODULE) {
|
||||
advance(p);
|
||||
let name: str;
|
||||
expectident(p, &name);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after module name");
|
||||
if (p.pathmod.len != 0 || p.resetmod.len != 0) {
|
||||
// M1 #22: while an import path is active the in-file
|
||||
// `package` clause is an ASSERTION — its leaf must
|
||||
// equal the path's last component; it does NOT
|
||||
// overwrite the path-derived module. #57 extends this
|
||||
// to the sep primary-reset path (resetmod): the dotted
|
||||
// reset path is authoritative, the clause asserts.
|
||||
let active: str = p.pathmod;
|
||||
if (p.pathmod.len == 0) { active = p.resetmod; };
|
||||
let (pre, post) = strings.rcut(active, ".");
|
||||
let last: str = post;
|
||||
if (post.len == 0) { last = active; };
|
||||
if (strings.compare(name, last) != 0) {
|
||||
errmsg(p, "package does not match import path");
|
||||
};
|
||||
} else {
|
||||
p.curmod = name;
|
||||
};
|
||||
continue;
|
||||
};
|
||||
// `//ww:module <path>` — M1 #22 import boundary. The following
|
||||
// file's decls mangle on the full dotted import path, not the
|
||||
// leaf `package` clause, and are flagged imported (gates the
|
||||
// root-only bare-`main` rule, #32).
|
||||
if (p.curkind == tkind.TK_MODPATH) {
|
||||
p.pathmod = p.curtext;
|
||||
p.curmod = p.curtext;
|
||||
p.resetmod = "";
|
||||
advance(p);
|
||||
continue;
|
||||
};
|
||||
// `//ww:module-reset` — bundle boundary before a package-less
|
||||
// file. Reset curmod to "" so the file's decls (and its own
|
||||
// `import os;`) are attributed to the primary module, not the
|
||||
// preceding bundled package. Codegen-neutral: "" curmod keeps
|
||||
// bare symbols. (#16 option-B; closes task #11.)
|
||||
// Driver-emitted ONLY before package-less files; a hand-placed
|
||||
// directive after a mid-file `package` would strip subsequent
|
||||
// decls to bare — that usage is deliberate-only.
|
||||
if (p.curkind == tkind.TK_MODRESET) {
|
||||
// #57: a path-carrying reset (sep primary body) mangles decls
|
||||
// on the dotted path so definer == importer, but leaves
|
||||
// imported==0 (curmod set, pathmod "") so -c primary-ness and
|
||||
// the #32 bare-main rule are intact; the body's `package`
|
||||
// clause then asserts (resetmod). A bare reset is the
|
||||
// root/package-less boundary: curmod "" → bare, today's path.
|
||||
let rp: str = p.curtext;
|
||||
advance(p);
|
||||
p.pathmod = "";
|
||||
if (rp.len != 0) {
|
||||
p.curmod = rp;
|
||||
p.resetmod = rp;
|
||||
} else {
|
||||
let empty: str;
|
||||
empty.ptr = nil;
|
||||
empty.len = 0;
|
||||
p.curmod = empty;
|
||||
p.resetmod = "";
|
||||
};
|
||||
continue;
|
||||
};
|
||||
let attrs = parseattrs(p);
|
||||
let exported: i32 = 0;
|
||||
if (p.curkind == tkind.TK_EXPORT) { exported = 1; advance(p); };
|
||||
|
||||
let d: *node = nil;
|
||||
if (p.curkind == tkind.TK_USE) {
|
||||
d = parseuse(p);
|
||||
} else { if (p.curkind == tkind.TK_DEF) {
|
||||
d = parsedef(p, exported);
|
||||
} else { if (p.curkind == tkind.TK_TYPE) {
|
||||
d = parsetypedecl(p, exported);
|
||||
} else { if (p.curkind == tkind.TK_LET) {
|
||||
d = parselet(p, exported);
|
||||
} else { if (p.curkind == tkind.TK_CONST) {
|
||||
d = parselet(p, exported);
|
||||
} else { if (p.curkind == tkind.TK_FN) {
|
||||
d = parsefn(p, exported, attrs);
|
||||
} else {
|
||||
// cstage parse.c:1395-1400 errorf+p->errs++ on the
|
||||
// default arm: an unknown top-level construct is a loud
|
||||
// reject, not a silent skip. ww chews the whole decl at
|
||||
// once (below), so one error per fallback entry.
|
||||
errmsg(p, "expected top-level decl");
|
||||
// Recovery: chew tokens until next ';' or EOF, balancing
|
||||
// '{' '}' pairs so internal ';'s in unfamiliar forms don't
|
||||
// derail us.
|
||||
for (p.curkind != tkind.TK_SEMI) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
if (p.curkind == tkind.TK_LBRACE) {
|
||||
let depth: i32 = 0;
|
||||
for (true) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
if (p.curkind == tkind.TK_LBRACE) { depth += 1; advance(p); continue; };
|
||||
if (p.curkind == tkind.TK_RBRACE) {
|
||||
depth -= 1;
|
||||
advance(p);
|
||||
if (depth == 0) { break; };
|
||||
continue;
|
||||
};
|
||||
advance(p);
|
||||
};
|
||||
continue;
|
||||
};
|
||||
advance(p);
|
||||
};
|
||||
if (p.curkind == tkind.TK_SEMI) { advance(p); };
|
||||
};};};};};};
|
||||
|
||||
if (d != nil) {
|
||||
// M1 #22: flag decls reached via an import-path boundary
|
||||
// (gates the root-only bare-`main` rule, #32).
|
||||
if (p.pathmod.len != 0) { d.imported = 1; };
|
||||
if (head == nil) {
|
||||
head = d;
|
||||
tail = d;
|
||||
} else {
|
||||
tail.next = d;
|
||||
tail = d;
|
||||
};
|
||||
};
|
||||
};
|
||||
f.list = head;
|
||||
return f;
|
||||
};
|
||||
412
lib/ww/syntax/stmt.ww
Normal file
412
lib/ww/syntax/stmt.ww
Normal file
@@ -0,0 +1,412 @@
|
||||
// lib/ww/syntax/stmt.ww — statement parsing, split out of parse.ww.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
|
||||
fn parseletlocal(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
// `let` or `const`. Const-bound locals are marked via n.op = tkind.TK_CONST.
|
||||
let is_const: i32 = 0;
|
||||
if (p.curkind == tkind.TK_CONST) { is_const = 1; };
|
||||
advance(p);
|
||||
|
||||
// Hare-style tuple destructure: `let (a, b) = expr;`.
|
||||
// Types are optional per binding (matches C parser; Hare itself
|
||||
// doesn't allow types here, but cmd/wcc/parse.c does).
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
let m = newnode(nkind.N_MLET, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let lpf = p.curfile;
|
||||
let lpl = p.curline;
|
||||
let lpc = p.curcol;
|
||||
let l = newnode(nkind.N_LET, lpf, lpl, lpc);
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
l.str = id;
|
||||
if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); };
|
||||
if (head == nil) { head = l; }
|
||||
else { tail.next = l; };
|
||||
tail = l;
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' in let destructure");
|
||||
expecttok(p, tkind.TK_ASSIGN, "expected '=' after let destructure");
|
||||
m.rhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after let");
|
||||
m.list = head;
|
||||
if (is_const != 0) {
|
||||
m.op = tkind.TK_CONST;
|
||||
let lc = head;
|
||||
for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; };
|
||||
};
|
||||
return m;
|
||||
};
|
||||
|
||||
let n = newnode(nkind.N_LET, pf, pl, pc);
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
n.str = id;
|
||||
if (accepttok(p, tkind.TK_COLON)) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
// Comma-multi-let: `let n, s = call();` (ww extension over Hare).
|
||||
// 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 = newnode(nkind.N_MLET, pf, pl, pc);
|
||||
let head = n;
|
||||
let tail = n;
|
||||
for (accepttok(p, tkind.TK_COMMA)) {
|
||||
let lpf = p.curfile;
|
||||
let lpl = p.curline;
|
||||
let lpc = p.curcol;
|
||||
let l = newnode(nkind.N_LET, lpf, lpl, lpc);
|
||||
let id2: str;
|
||||
expectbindname(p, &id2);
|
||||
l.str = id2;
|
||||
if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); };
|
||||
tail.next = l;
|
||||
tail = l;
|
||||
};
|
||||
expecttok(p, tkind.TK_ASSIGN, "expected '=' after let names");
|
||||
m.rhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after let");
|
||||
m.list = head;
|
||||
if (is_const != 0) {
|
||||
m.op = tkind.TK_CONST;
|
||||
let lc = head;
|
||||
for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; };
|
||||
};
|
||||
return m;
|
||||
};
|
||||
if (accepttok(p, tkind.TK_ASSIGN)) {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after let");
|
||||
if (is_const != 0) { n.op = tkind.TK_CONST; };
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parseblock(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' to open block");
|
||||
let blk = newnode(nkind.N_BLOCK, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
let s = parsestmt(p);
|
||||
if (s != nil) {
|
||||
if (head == nil) { head = s; tail = s; }
|
||||
else { tail.next = s; tail = s; };
|
||||
};
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACE, "expected '}' to close block");
|
||||
blk.list = head;
|
||||
return blk;
|
||||
};
|
||||
|
||||
fn parseif(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
advance(p); // past `if`
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after if");
|
||||
let n = newnode(nkind.N_IF, pf, pl, pc);
|
||||
n.cond = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition");
|
||||
n.body = parseblock(p);
|
||||
if (accepttok(p, tkind.TK_ELSE)) {
|
||||
if (p.curkind == tkind.TK_IF) {
|
||||
n.els = parseif(p);
|
||||
} else {
|
||||
n.els = parseblock(p);
|
||||
};
|
||||
};
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsefor(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
advance(p); // past `for`
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after for");
|
||||
|
||||
// Four forms (matching C parser):
|
||||
// for (cond) — only cond
|
||||
// for (init; cond; post) — C-style 3-clause
|
||||
// for (let x .. expr) — Hare-style range, single binding
|
||||
// for (let (a, b) .. expr) — range with tuple destructure
|
||||
// Range and 3-clause both lead with `let`, so we commit to consuming
|
||||
// `let` then disambiguate by looking at what follows.
|
||||
if (p.curkind == tkind.TK_LET) {
|
||||
advance(p); // past `let`
|
||||
|
||||
// Tuple destructure: `for (let (a, b) .. expr)`.
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
let names: *node = nil;
|
||||
let ntail: *node = nil;
|
||||
for (true) {
|
||||
let npf = p.curfile;
|
||||
let npl = p.curline;
|
||||
let npc = p.curcol;
|
||||
let e = newnode(nkind.N_IDENT, npf, npl, npc);
|
||||
let nm: str;
|
||||
expectbindname(p, &nm);
|
||||
e.str = nm;
|
||||
if (names == nil) { names = e; }
|
||||
else { ntail.next = e; };
|
||||
ntail = e;
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' in for-range names");
|
||||
expecttok(p, tkind.TK_DOTDOT, "expected '..' after for-range names");
|
||||
let rng = newnode(nkind.N_FORRANGE, pf, pl, pc);
|
||||
rng.list = names;
|
||||
rng.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
|
||||
rng.body = parseblock(p);
|
||||
if (accepttok(p, tkind.TK_ELSE)) { rng.els = parseblock(p); };
|
||||
return rng;
|
||||
};
|
||||
|
||||
// Single binding range or C-style let-init. We need to consume
|
||||
// the IDENT/UNDER to know which: if followed by '..' it's a
|
||||
// range; otherwise build a synthetic LET for the C-style for-init
|
||||
// with the consumed name baked in.
|
||||
if (p.curkind == tkind.TK_IDENT || p.curkind == tkind.TK_UNDER) {
|
||||
let isunder = (p.curkind == tkind.TK_UNDER);
|
||||
let nm: str;
|
||||
nm.ptr = nil; nm.len = 0;
|
||||
if (!isunder) { nm = p.curtext; };
|
||||
let lpf = p.curfile;
|
||||
let lpl = p.curline;
|
||||
let lpc = p.curcol;
|
||||
advance(p); // consume IDENT/UNDER
|
||||
|
||||
if (p.curkind == tkind.TK_DOTDOT) {
|
||||
advance(p);
|
||||
let rng = newnode(nkind.N_FORRANGE, pf, pl, pc);
|
||||
rng.str = nm; // "" for `_`
|
||||
rng.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
|
||||
rng.body = parseblock(p);
|
||||
if (accepttok(p, tkind.TK_ELSE)) { rng.els = parseblock(p); };
|
||||
return rng;
|
||||
};
|
||||
|
||||
// Not a range — finish the let manually and continue as
|
||||
// a 3-clause for-init.
|
||||
let first = 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 = newnode(nkind.N_FOR, pf, pl, pc);
|
||||
n.lhs = first;
|
||||
n.cond = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after for cond");
|
||||
n.rhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
|
||||
n.body = parseblock(p);
|
||||
if (accepttok(p, tkind.TK_ELSE)) { n.els = parseblock(p); };
|
||||
return n;
|
||||
};
|
||||
|
||||
errmsg(p, "expected name after 'let' in for");
|
||||
};
|
||||
|
||||
// for (cond) or for (cond; post)
|
||||
let n = newnode(nkind.N_FOR, pf, pl, pc);
|
||||
let first = parseexpr(p);
|
||||
if (accepttok(p, tkind.TK_SEMI)) {
|
||||
n.cond = first;
|
||||
n.rhs = parseexpr(p);
|
||||
} else {
|
||||
n.cond = first;
|
||||
};
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after for");
|
||||
n.body = parseblock(p);
|
||||
// Optional `else { ... }` — runs at normal cond-false exit; skipped
|
||||
// by break. Hare's "did the loop find it?" idiom.
|
||||
if (accepttok(p, tkind.TK_ELSE)) {
|
||||
n.els = parseblock(p);
|
||||
};
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parseswitch(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
advance(p); // past `switch`
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after switch");
|
||||
let n = 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");
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind == tkind.TK_CASE) {
|
||||
let cpf = p.curfile;
|
||||
let cpl = p.curline;
|
||||
let cpc = p.curcol;
|
||||
advance(p); // past `case`
|
||||
let cs = newnode(nkind.N_CASE, cpf, cpl, cpc);
|
||||
let eh: *node = nil;
|
||||
let et: *node = nil;
|
||||
if (p.curkind != tkind.TK_COLON) {
|
||||
p.nocast = 1;
|
||||
for (true) {
|
||||
let e = parseexpr(p);
|
||||
if (eh == nil) { eh = e; }
|
||||
else { et.next = e; };
|
||||
et = e;
|
||||
if (!accepttok(p, tkind.TK_COMMA)) { break; };
|
||||
};
|
||||
p.nocast = 0;
|
||||
};
|
||||
cs.list = eh;
|
||||
expecttok(p, tkind.TK_COLON, "expected ':' after case label");
|
||||
let bh: *node = nil;
|
||||
let bt: *node = nil;
|
||||
for (p.curkind != tkind.TK_CASE) {
|
||||
if (p.curkind == tkind.TK_RBRACE) { break; };
|
||||
if (p.curkind == tkind.TK_EOF) { break; };
|
||||
let s = parsestmt(p);
|
||||
if (s != nil) {
|
||||
if (bh == nil) { bh = s; }
|
||||
else { bt.next = s; };
|
||||
bt = s;
|
||||
};
|
||||
};
|
||||
let blk = newnode(nkind.N_BLOCK, cpf, cpl, cpc);
|
||||
blk.list = bh;
|
||||
cs.body = blk;
|
||||
if (head == nil) { head = cs; }
|
||||
else { tail.next = cs; };
|
||||
tail = cs;
|
||||
};
|
||||
expecttok(p, tkind.TK_RBRACE, "expected '}' to close switch");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsestmt(p: *parser) *node = {
|
||||
let pf = p.curfile;
|
||||
let pl = p.curline;
|
||||
let pc = p.curcol;
|
||||
|
||||
// `static` is allowed on local lets per Hare; we accept and skip
|
||||
// it (it doesn't change the AST shape).
|
||||
if (p.curkind == tkind.TK_STATIC) { advance(p); };
|
||||
|
||||
if (p.curkind == tkind.TK_LBRACE) {
|
||||
let b = parseblock(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after block");
|
||||
return b;
|
||||
};
|
||||
if (p.curkind == tkind.TK_LET) { return parseletlocal(p); };
|
||||
if (p.curkind == tkind.TK_CONST) { return parseletlocal(p); };
|
||||
if (p.curkind == tkind.TK_IF) {
|
||||
let n = parseif(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after if");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_FOR) {
|
||||
let n = parsefor(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after for");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_SWITCH) {
|
||||
let n = parseswitch(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after switch");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_RETURN) {
|
||||
advance(p);
|
||||
let n = newnode(nkind.N_RETURN, pf, pl, pc);
|
||||
if (p.curkind != tkind.TK_SEMI) {
|
||||
let first = 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 = newnode(nkind.N_TUPLE, pf, pl, pc);
|
||||
t.list = first;
|
||||
let tail = first;
|
||||
for (accepttok(p, tkind.TK_COMMA)) {
|
||||
let e = parseexpr(p);
|
||||
tail.next = e;
|
||||
tail = e;
|
||||
};
|
||||
n.lhs = t;
|
||||
} else {
|
||||
n.lhs = first;
|
||||
};
|
||||
};
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after return");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_DEFER) {
|
||||
advance(p);
|
||||
let n = 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 = newnode(nkind.N_YIELD, pf, pl, pc);
|
||||
n.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after yield");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_BREAK) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after break");
|
||||
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(nkind.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 = parseexpr(p);
|
||||
if (p.curkind == tkind.TK_COMMA) {
|
||||
let m = newnode(nkind.N_MASSIGN, pf, pl, pc);
|
||||
let head = e;
|
||||
let tail = e;
|
||||
for (p.curkind == tkind.TK_COMMA) {
|
||||
advance(p);
|
||||
let lv = parsebin(p, parseunary(p), 1);
|
||||
tail.next = lv;
|
||||
tail = lv;
|
||||
};
|
||||
expecttok(p, tkind.TK_ASSIGN, "expected '=' after multi-assign lvalues");
|
||||
m.rhs = parseexpr(p);
|
||||
m.list = head;
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after multi-assign");
|
||||
return m;
|
||||
};
|
||||
let n = newnode(nkind.N_EXPRSTMT, pf, pl, pc);
|
||||
n.lhs = e;
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement");
|
||||
return n;
|
||||
};
|
||||
|
||||
333
lib/ww/syntax/sym.ww
Normal file
333
lib/ww/syntax/sym.ww
Normal file
@@ -0,0 +1,333 @@
|
||||
// lib/ww/syntax/sym.ww — port of cmd/wcc/sym.c.
|
||||
//
|
||||
// Per-scope hashtable, chained to the parent. Lookup walks up.
|
||||
// Plan 9 / Hare flavoured. Duplicate definitions in the same scope
|
||||
// return nil; the caller flags the error.
|
||||
|
||||
package syntax;
|
||||
|
||||
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
|
||||
type skind = enum i32 {
|
||||
SK_NONE = 0,
|
||||
SK_VAR = 1,
|
||||
SK_PARAM = 2,
|
||||
SK_DEF = 3,
|
||||
SK_TYPE = 4,
|
||||
SK_FN = 5,
|
||||
SK_USE = 6,
|
||||
SK_FIELD = 7,
|
||||
};
|
||||
|
||||
type sym = struct {
|
||||
name: str,
|
||||
skind: skind,
|
||||
type_: *tinfo,
|
||||
decl: *node,
|
||||
exported: i32,
|
||||
is_const: i32, // const-bound (assignment rejected)
|
||||
use_alias: i32, // #30: this value/type decl ALSO names an imported
|
||||
// module (the fnmatch.fnmatch / random.random shape).
|
||||
// Set when installtop promotes a same-leaf SK_USE in
|
||||
// place; the N_DOT guards treat such a sym as a module
|
||||
// for `name.member`. Mirror cstage Sym.use_alias
|
||||
// (cmd/wcc/check.c:2831-2951 promote + 87/1337 guards).
|
||||
mod: str, // importing module's bareword for symbols
|
||||
// from a `use`-imported module; "" for primary
|
||||
// (root) compilation unit symbols. Used by
|
||||
// scopelookupinmodule to disambiguate same-leaf-
|
||||
// name types coming from different imports.
|
||||
snext: *sym, // iteration order
|
||||
hashnext: *sym, // hash bucket chain
|
||||
scope: *scope,
|
||||
};
|
||||
|
||||
def NBUCKETS: i32 = 16;
|
||||
|
||||
type scope = struct {
|
||||
parent: *scope,
|
||||
first: *sym,
|
||||
last: *sym,
|
||||
buckets: **sym, // length = NBUCKETS
|
||||
nbuckets: i32,
|
||||
};
|
||||
|
||||
// FNV-1a 64 — same hash the C side uses, so bucket distribution is
|
||||
// identical when both walk a scope in declaration order.
|
||||
fn hashstr(s: str) u64 = {
|
||||
let h: u64 = 14695981039346656037u64;
|
||||
let i: i32 = 0;
|
||||
for (i < s.len) {
|
||||
let c: u8 = s[i];
|
||||
h = h ^ (c: u64);
|
||||
h = h * 1099511628211u64;
|
||||
i += 1;
|
||||
};
|
||||
return h;
|
||||
};
|
||||
|
||||
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})!;
|
||||
return s;
|
||||
};
|
||||
|
||||
export fn streq(a: str, b: str) bool = {
|
||||
if (a.len != b.len) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < a.len) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
export fn scopelookuplocal(s: *scope, name: str) *sym = {
|
||||
if (s == nil) { return nil; };
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
let b: *sym = s.buckets[bi];
|
||||
for (b != nil) {
|
||||
let bn: str = b.name;
|
||||
if (streq(bn, name)) { return b; };
|
||||
b = b.hashnext;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
export fn scopelookup(s: *scope, name: str) *sym = {
|
||||
for (s != nil) {
|
||||
let r: *sym = scopelookuplocal(s, name);
|
||||
if (r != nil) { return r; };
|
||||
s = s.parent;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
// scopelookuptype — find an SK_TYPE entry by name, same-module preferred.
|
||||
//
|
||||
// Same FNV bucket + hashnext chain + parent walk as scopelookup, with
|
||||
// an `skind == SK_TYPE` filter. Used to disambiguate the bare-TNAME
|
||||
// vs imported-module-bareword collision: when scopelookup returns the
|
||||
// SK_USE sym for a leaf that ALSO names a type (a same-name `import X;`
|
||||
// SK_USE shadowing a struct X declared in another module), the resolver
|
||||
// needs the type entry — the struct's mod may differ from the leaf so
|
||||
// scopelookupinmodule(c, leaf, leaf) won't find it.
|
||||
//
|
||||
// #58/#50: within each scope, Pass-1 prefers an SK_TYPE whose `sym.mod`
|
||||
// matches `mod`; Pass-2 falls back to the first SK_TYPE regardless of
|
||||
// mod (chain-first, the prior behavior). scopedefineinmodule PREPENDS,
|
||||
// so chain-first = last-registered — when two modules export the same
|
||||
// type leaf the bare walk silently picked the newest-installed one,
|
||||
// install-order-dependent, while cstage is deterministic on cur_mod.
|
||||
// Mirrors cstage cmd/wcc/sym.c scope_lookup_type(s, mod, name) (the
|
||||
// kind-filtered + mod-preferring single walk); sole caller passes
|
||||
// c.curmod. i32-correct: streq throughout, only `.len > 0` guards (the
|
||||
// reverted attempt compared str.len as u64 — str.len is i32).
|
||||
export fn scopelookuptype(s: *scope, mod: str, name: str) *sym = {
|
||||
let p: *scope = s;
|
||||
for (p != nil) {
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (p.nbuckets: u64)): i32;
|
||||
let b: *sym = p.buckets[bi];
|
||||
let fallback: *sym = nil;
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.skind == skind.SK_TYPE) {
|
||||
if (mod.len > 0) {
|
||||
if (b.mod.len > 0) {
|
||||
if (streq(b.mod, mod)) {
|
||||
return b;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (fallback == nil) { fallback = b; };
|
||||
};
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
if (fallback != nil) { return fallback; };
|
||||
p = p.parent;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
// scopelookupuselocal — find a same-leaf SK_USE entry within ONE scope.
|
||||
//
|
||||
// Same FNV bucket + hashnext chain as scopelookuplocal, with a
|
||||
// `skind == SK_USE` filter and NO parent walk. The dot-lhs twin of
|
||||
// scopelookuptype: when a `use mod;` and a colliding top-level
|
||||
// `fn mod` / `type mod` of the same leaf coexist (random.random,
|
||||
// fnmatch.fnmatch), the mod-preferring scopelookupprefer returns the
|
||||
// SK_FN/SK_TYPE whose mod matches the importing unit's package, masking
|
||||
// the SK_USE. A dot-lhs `mod.x` must resolve `mod` to the SK_USE for the
|
||||
// module-qualified arm to fire, so the resolver re-resolves through this
|
||||
// filter — keyed on the scope where scopelookupprefer LANDED — when it
|
||||
// lands on a non-USE same-leaf entry.
|
||||
//
|
||||
// Single-scope (not a parent walk) so a local binding that shares a leaf
|
||||
// with a top-level `use` keeps value semantics: scopelookupprefer
|
||||
// resolves the local in its inner scope, whose bucket holds no SK_USE,
|
||||
// so this returns nil and the dot stays field access. Only a genuine
|
||||
// same-scope coexistence (top-level use + top-level type/fn) re-resolves.
|
||||
//
|
||||
// #30 (design reversal): this serves the DISTINCT-mod two-sym case only —
|
||||
// a `fn fnmatch` (mod="fnmatch") coexisting with `import fnmatch`'s SK_USE
|
||||
// (mod=""), where scopelookupprefer lands on the value and the dot re-
|
||||
// resolves to the SK_USE here. The SAME-mod collision (a primary-package
|
||||
// decl whose leaf also names a bundled module — `type sym` vs `import sym`,
|
||||
// `@test fn ascii` vs the fnmatch->ascii bundle floor) is NO LONGER left to
|
||||
// two coexisting syms: that ripples into every bare-ref resolver (a missed
|
||||
// site is a byte-id-consistent-but-wrong cat-A risk the gate can't prove
|
||||
// away). Instead installtop now PROMOTES the SK_USE in place to the value
|
||||
// kind with use_alias=1 (selfhost/cmd/wcc/check.ww installtop), mirroring
|
||||
// cstage's Sym.use_alias promote exactly (cmd/wcc/check.c:2831-2951); the
|
||||
// N_DOT guards honor `skind == SK_USE || use_alias` directly. ONE
|
||||
// correctly-kinded sym → all resolvers correct by construction. Cite:
|
||||
// task #30; project memory module_type_name_collision (cstage 2026-05-13).
|
||||
export fn scopelookupuselocal(s: *scope, name: str) *sym = {
|
||||
if (s == nil) { return nil; };
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
let b: *sym = s.buckets[bi];
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.skind == skind.SK_USE) { return b; };
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
// scopelookupinmodule — module-filtered chain walk.
|
||||
//
|
||||
// Same FNV bucket + hashnext chain + parent walk as scopelookup, plus
|
||||
// a `b.mod.len > 0 && streq(b.mod, mod)` filter. When `mod` is empty
|
||||
// we fall back to unfiltered scopelookup semantics, so callers that
|
||||
// don't care about disambiguation get the default.
|
||||
//
|
||||
// Used by the dot-prefixed type-name lookup in selfhost/cmd/wcc/
|
||||
// check.ww to pick the right same-leaf-name type when two imports
|
||||
// each export it (`bufio.stream` vs `io.stream`).
|
||||
export fn scopelookupinmodule(s: *scope, mod: str, name: str) *sym = {
|
||||
if (mod.len == 0) { return scopelookup(s, name); };
|
||||
for (s != nil) {
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
let b: *sym = s.buckets[bi];
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.mod.len > 0) {
|
||||
if (streq(b.mod, mod)) {
|
||||
return b;
|
||||
};
|
||||
};
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
s = s.parent;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
// scopelookupprefer — bare-leaf lookup with same-module preference.
|
||||
//
|
||||
// Walks the same FNV bucket + hashnext chain + parent walk scopelookup
|
||||
// uses. Within each scope's bucket: Pass 1 prefers entries whose
|
||||
// `sym.mod` matches `mod`; Pass 2 falls back to the first match
|
||||
// regardless of mod (same semantics as scopelookup). We only descend
|
||||
// to the parent scope when the current scope has no matching entry at
|
||||
// all — so a local binding in a closer scope still shadows a same-name
|
||||
// fn from a parent scope, even when the parent entry mod-matches.
|
||||
//
|
||||
// When `mod` is empty we just call scopelookup — there's no module
|
||||
// identity to prefer.
|
||||
//
|
||||
// Used at bare-leaf lookup sites inside a known current module so that
|
||||
// a bare `read` inside lib/os resolves to os.read rather than the
|
||||
// io.read that happens to hash earlier into the flat scope. Mirrors
|
||||
// cmd/wcc/sym.c scope_lookup_prefer.
|
||||
export fn scopelookupprefer(s: *scope, mod: str, name: str) *sym = {
|
||||
if (mod.len == 0) { return scopelookup(s, name); };
|
||||
let p: *scope = s;
|
||||
for (p != nil) {
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (p.nbuckets: u64)): i32;
|
||||
let b: *sym = p.buckets[bi];
|
||||
let fallback: *sym = nil;
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.mod.len > 0) {
|
||||
if (streq(b.mod, mod)) {
|
||||
return b;
|
||||
};
|
||||
};
|
||||
if (fallback == nil) { fallback = b; };
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
if (fallback != nil) { return fallback; };
|
||||
p = p.parent;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
export fn scopedefine(s: *scope, name: str, k: skind, t: *tinfo, decl: *node) *sym = {
|
||||
let empty: str;
|
||||
return scopedefineinmodule(s, name, empty, k, t, decl);
|
||||
};
|
||||
|
||||
// scopedefineinmodule — bucket insert with per-mod dedup.
|
||||
//
|
||||
// Same insertion as scopedefine, but the duplicate-rejection key is
|
||||
// (name, mod) rather than name alone. This lets two imports each
|
||||
// register their own `stream` SK_TYPE in the flat scope, and lets the
|
||||
// primary register `stream` (mod="") alongside imported `stream`s.
|
||||
//
|
||||
// Within a single (name, mod) pair the first registration wins; later
|
||||
// attempts return nil and the caller can flag the error.
|
||||
export fn scopedefineinmodule(s: *scope, name: str, mod: str, k: skind, t: *tinfo, decl: *node) *sym = {
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
let b: *sym = s.buckets[bi];
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.mod.len == 0) {
|
||||
if (mod.len == 0) { return nil; };
|
||||
} else {
|
||||
if (mod.len > 0) {
|
||||
if (streq(b.mod, mod)) { return nil; };
|
||||
};
|
||||
};
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
let sy: *sym = alloc(sym{name=name, skind=k, type_=t, decl=decl, exported=0, is_const=0, use_alias=0, mod=mod, snext=nil, hashnext=s.buckets[bi], scope=s})!;
|
||||
s.buckets[bi] = sy;
|
||||
if (s.first == nil) { s.first = sy; } else { s.last.snext = sy; };
|
||||
s.last = sy;
|
||||
return sy;
|
||||
};
|
||||
|
||||
// scopesamekeysym — the entry scopedefineinmodule(name, mod) treats as a
|
||||
// duplicate (same name, same mod-key), or nil if the key is free. Lets a
|
||||
// caller that got a nil from scopedefineinmodule learn WHAT it collided
|
||||
// with (e.g. a pre-seeded builtin vs a genuine user redeclaration). The
|
||||
// match logic mirrors scopedefineinmodule's reject branch exactly.
|
||||
export fn scopesamekeysym(s: *scope, name: str, mod: str) *sym = {
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
let b: *sym = s.buckets[bi];
|
||||
for (b != nil) {
|
||||
if (streq(b.name, name)) {
|
||||
if (b.mod.len == 0) {
|
||||
if (mod.len == 0) { return b; };
|
||||
} else {
|
||||
if (mod.len > 0) {
|
||||
if (streq(b.mod, mod)) { return b; };
|
||||
};
|
||||
};
|
||||
};
|
||||
b = b.hashnext;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
403
lib/ww/syntax/tok.ww
Normal file
403
lib/ww/syntax/tok.ww
Normal file
@@ -0,0 +1,403 @@
|
||||
// lib/ww/syntax/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
|
||||
// Tok / Pos shapes from cmd/wcc/ww.h.
|
||||
//
|
||||
// Token kind values must stay numerically equal to the C side: the
|
||||
// 990_selfhost test diffs ww-side wwdump output against C-side
|
||||
// wwdump output, byte-for-byte. Reordering this list shifts the
|
||||
// integers and breaks the diff.
|
||||
//
|
||||
// Bottom of file: tokprint, which emits one token per line in a
|
||||
// format identical to cmd/wcc/tok.c:tokprint().
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
import strconv;
|
||||
import strings;
|
||||
|
||||
// ---- tkind ------------------------------------------------------------
|
||||
// Mirror of the C `Tkind` enum in cmd/wcc/ww.h. Numeric values are
|
||||
// explicit and must stay in sync — the 990_selfhost test diffs wwdump
|
||||
// output against the C side, byte for byte.
|
||||
|
||||
type tkind = enum i32 {
|
||||
TK_NONE = 0,
|
||||
TK_EOF = 1,
|
||||
TK_ERR = 2,
|
||||
TK_IDENT = 3,
|
||||
TK_INT = 4,
|
||||
TK_FLOAT = 5,
|
||||
TK_RUNE = 6,
|
||||
TK_STR = 7,
|
||||
|
||||
TK_FN = 8,
|
||||
TK_LET = 9,
|
||||
TK_DEF = 10,
|
||||
TK_IF = 11,
|
||||
TK_ELSE = 12,
|
||||
TK_FOR = 13,
|
||||
TK_SWITCH = 14,
|
||||
TK_CASE = 15,
|
||||
TK_RETURN = 16,
|
||||
TK_USE = 17,
|
||||
TK_TYPE = 18,
|
||||
TK_STRUCT = 19,
|
||||
TK_DEFER = 20,
|
||||
TK_BREAK = 21,
|
||||
TK_CONTINUE = 22,
|
||||
TK_EXPORT = 23,
|
||||
TK_PROC = 24,
|
||||
TK_CHAN = 25,
|
||||
TK_NIL = 26,
|
||||
TK_TRUE = 27,
|
||||
TK_FALSE = 28,
|
||||
TK_AS = 29,
|
||||
TK_STATIC = 30,
|
||||
TK_MATCH = 31,
|
||||
TK_CONST = 32,
|
||||
TK_UNDER = 33,
|
||||
|
||||
TK_LPAREN = 34,
|
||||
TK_RPAREN = 35,
|
||||
TK_LBRACE = 36,
|
||||
TK_RBRACE = 37,
|
||||
TK_LBRACK = 38,
|
||||
TK_RBRACK = 39,
|
||||
TK_COMMA = 40,
|
||||
TK_SEMI = 41,
|
||||
TK_COLON = 42,
|
||||
TK_DOT = 43,
|
||||
TK_ELLIPSIS = 44,
|
||||
TK_DOTDOT = 45,
|
||||
TK_AT = 46,
|
||||
TK_QUESTION = 47,
|
||||
|
||||
TK_ASSIGN = 48,
|
||||
TK_PLUSEQ = 49,
|
||||
TK_MINUSEQ = 50,
|
||||
TK_STAREQ = 51,
|
||||
TK_SLASHEQ = 52,
|
||||
TK_PERCENTEQ = 53,
|
||||
TK_AMPEQ = 54,
|
||||
TK_PIPEEQ = 55,
|
||||
TK_CARETEQ = 56,
|
||||
TK_LSHIFTEQ = 57,
|
||||
TK_RSHIFTEQ = 58,
|
||||
|
||||
TK_PLUS = 59,
|
||||
TK_MINUS = 60,
|
||||
TK_STAR = 61,
|
||||
TK_SLASH = 62,
|
||||
TK_PERCENT = 63,
|
||||
TK_AMP = 64,
|
||||
TK_PIPE = 65,
|
||||
TK_CARET = 66,
|
||||
TK_TILDE = 67,
|
||||
TK_LSHIFT = 68,
|
||||
TK_RSHIFT = 69,
|
||||
|
||||
TK_EQ = 70,
|
||||
TK_NEQ = 71,
|
||||
TK_LT = 72,
|
||||
TK_LE = 73,
|
||||
TK_GT = 74,
|
||||
TK_GE = 75,
|
||||
|
||||
TK_AND = 76,
|
||||
TK_OR = 77,
|
||||
TK_NOT = 78,
|
||||
|
||||
TK_LARROW = 79,
|
||||
TK_ARROW = 80,
|
||||
TK_FATARROW = 81,
|
||||
|
||||
// Tail-appended values — keeps every prior TK_* numeric value
|
||||
// stable for the 990_selfhost byte-diff against the C side.
|
||||
TK_IS = 82,
|
||||
TK_VOID = 83,
|
||||
TK_YIELD = 84,
|
||||
TK_ENUM = 85,
|
||||
TK_MODULE = 86, // `module foo;` — directory-as-module decl
|
||||
TK_MODRESET = 87, // `//ww:module-reset` driver bundle boundary:
|
||||
// reset curmod to "" before a package-less file
|
||||
// (#16 option-B; cstage TK_MODRESET twin)
|
||||
TK_MODPATH = 88, // `//ww:module <dotted-path>` driver import
|
||||
// boundary; decls mangle on the path, not the
|
||||
// leaf `package` clause (M1 #22; cstage twin)
|
||||
TK_LAST = 89,
|
||||
};
|
||||
|
||||
// ---- Pos / Tok --------------------------------------------------------
|
||||
//
|
||||
// `pos` is used at error-reporting boundaries; we always pass it via
|
||||
// *pos so the value never gets struct-copied (w6c can't yet copy a
|
||||
// 24-byte struct).
|
||||
//
|
||||
// `tok` is flat — file/line/col live directly on the token rather than
|
||||
// nested inside a `pos` field. Same reason: nested struct field
|
||||
// assignment isn't supported, and flat primitives are.
|
||||
|
||||
type pos = struct {
|
||||
file: str,
|
||||
line: i32,
|
||||
col: i32,
|
||||
};
|
||||
|
||||
type tok = struct {
|
||||
kind: tkind,
|
||||
file: str, // path of the source the token came from
|
||||
line: i32,
|
||||
col: i32,
|
||||
text: str, // arena-owned token text (tkind.TK_IDENT, tkind.TK_STR, tkind.TK_ERR)
|
||||
uval: u64, // tkind.TK_INT, tkind.TK_RUNE
|
||||
fval: f64, // tkind.TK_FLOAT
|
||||
tsuffix: str, // typed numeric literal suffix or empty
|
||||
};
|
||||
|
||||
// ---- keyword lookup ---------------------------------------------------
|
||||
|
||||
// keep alphabetised, so kwlookup is easy to read — mirrors the C twin
|
||||
// cmd/wcc/tok.c:18-47. Two parallel arrays, not a [N]kwent array-of-
|
||||
// struct: a str *inside* an aggregate element is the filed #18 follow-up
|
||||
// (str-in-aggregate). `let`, not `def`: #18's module-level [N]str static
|
||||
// init is scoped to the DATAW (`let`) directive (A_DATAR needs a DATAW
|
||||
// holder); `def [N]str` is the same filed follow-up. kwkinds is a plain
|
||||
// [N]tkind enum byte-array (pre-#18 path). Explicit [30], NOT [_]:
|
||||
// [_] static-init silently miscompiles to a zero-length array in-tree
|
||||
// (probe at cc69daf — len() returns 0, no diagnostic); filed bug.
|
||||
let kwnames: [30]str = [
|
||||
"as", "break", "case", "chan", "const", "continue", "def", "defer",
|
||||
"else", "enum", "export", "false", "fn", "for", "if", "is",
|
||||
"import", "let", "match", "nil", "package", "proc", "return",
|
||||
"static", "struct", "switch", "true", "type", "void", "yield",
|
||||
];
|
||||
let kwkinds: [30]tkind = [
|
||||
tkind.TK_AS, tkind.TK_BREAK, tkind.TK_CASE, tkind.TK_CHAN,
|
||||
tkind.TK_CONST, tkind.TK_CONTINUE, tkind.TK_DEF, tkind.TK_DEFER,
|
||||
tkind.TK_ELSE, tkind.TK_ENUM, tkind.TK_EXPORT, tkind.TK_FALSE,
|
||||
tkind.TK_FN, tkind.TK_FOR, tkind.TK_IF, tkind.TK_IS,
|
||||
tkind.TK_USE, tkind.TK_LET, tkind.TK_MATCH, tkind.TK_NIL,
|
||||
tkind.TK_MODULE, tkind.TK_PROC, tkind.TK_RETURN, tkind.TK_STATIC,
|
||||
tkind.TK_STRUCT, tkind.TK_SWITCH, tkind.TK_TRUE, tkind.TK_TYPE,
|
||||
tkind.TK_VOID, tkind.TK_YIELD,
|
||||
];
|
||||
|
||||
// kwlookup — returns the matching TK_* keyword kind for a byte run,
|
||||
// or tkind.TK_NONE if it's an ordinary identifier. Linear scan over the
|
||||
// table, matching cmd/wcc/tok.c:kwlookup (N=30, no hash).
|
||||
export fn kwlookup(p: *u8, n: i32) tkind = {
|
||||
let cand: str;
|
||||
cand.ptr = p;
|
||||
cand.len = n;
|
||||
let i: i32 = 0;
|
||||
for (i < len(kwnames)) {
|
||||
if (strings.compare(cand, kwnames[i]) == 0) {
|
||||
return kwkinds[i];
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
return tkind.TK_NONE;
|
||||
};
|
||||
|
||||
// ---- tokname ----------------------------------------------------------
|
||||
//
|
||||
// Returns the canonical printable spelling for a token kind. Matches
|
||||
// the C tokname()'s output exactly so wwdump output diffs cleanly.
|
||||
|
||||
export fn tokname(k: tkind) str = {
|
||||
switch (k) {
|
||||
case tkind.TK_NONE: return "<none>";
|
||||
case tkind.TK_EOF: return "EOF";
|
||||
case tkind.TK_ERR: return "ERR";
|
||||
case tkind.TK_IDENT: return "IDENT";
|
||||
case tkind.TK_INT: return "INT";
|
||||
case tkind.TK_FLOAT: return "FLOAT";
|
||||
case tkind.TK_RUNE: return "RUNE";
|
||||
case tkind.TK_STR: return "STR";
|
||||
|
||||
case tkind.TK_FN: return "fn";
|
||||
case tkind.TK_LET: return "let";
|
||||
case tkind.TK_DEF: return "def";
|
||||
case tkind.TK_IF: return "if";
|
||||
case tkind.TK_ELSE: return "else";
|
||||
case tkind.TK_FOR: return "for";
|
||||
case tkind.TK_SWITCH: return "switch";
|
||||
case tkind.TK_CASE: return "case";
|
||||
case tkind.TK_RETURN: return "return";
|
||||
case tkind.TK_USE: return "import";
|
||||
case tkind.TK_TYPE: return "type";
|
||||
case tkind.TK_STRUCT: return "struct";
|
||||
case tkind.TK_DEFER: return "defer";
|
||||
case tkind.TK_BREAK: return "break";
|
||||
case tkind.TK_CONTINUE: return "continue";
|
||||
case tkind.TK_EXPORT: return "export";
|
||||
case tkind.TK_PROC: return "proc";
|
||||
case tkind.TK_CHAN: return "chan";
|
||||
case tkind.TK_NIL: return "nil";
|
||||
case tkind.TK_TRUE: return "true";
|
||||
case tkind.TK_FALSE: return "false";
|
||||
case tkind.TK_AS: return "as";
|
||||
case tkind.TK_IS: return "is";
|
||||
case tkind.TK_VOID: return "void";
|
||||
case tkind.TK_YIELD: return "yield";
|
||||
case tkind.TK_STATIC: return "static";
|
||||
case tkind.TK_MATCH: return "match";
|
||||
case tkind.TK_CONST: return "const";
|
||||
case tkind.TK_UNDER: return "_";
|
||||
case tkind.TK_ENUM: return "enum";
|
||||
case tkind.TK_MODULE: return "package";
|
||||
case tkind.TK_MODRESET: return "//ww:module-reset";
|
||||
case tkind.TK_MODPATH: return "//ww:module";
|
||||
|
||||
case tkind.TK_LPAREN: return "(";
|
||||
case tkind.TK_RPAREN: return ")";
|
||||
case tkind.TK_LBRACE: return "{";
|
||||
case tkind.TK_RBRACE: return "}";
|
||||
case tkind.TK_LBRACK: return "[";
|
||||
case tkind.TK_RBRACK: return "]";
|
||||
case tkind.TK_COMMA: return ",";
|
||||
case tkind.TK_SEMI: return ";";
|
||||
case tkind.TK_COLON: return ":";
|
||||
case tkind.TK_DOT: return ".";
|
||||
case tkind.TK_ELLIPSIS: return "...";
|
||||
case tkind.TK_DOTDOT: return "..";
|
||||
case tkind.TK_AT: return "@";
|
||||
case tkind.TK_QUESTION: return "?";
|
||||
|
||||
case tkind.TK_ASSIGN: return "=";
|
||||
case tkind.TK_PLUSEQ: return "+=";
|
||||
case tkind.TK_MINUSEQ: return "-=";
|
||||
case tkind.TK_STAREQ: return "*=";
|
||||
case tkind.TK_SLASHEQ: return "/=";
|
||||
case tkind.TK_PERCENTEQ: return "%=";
|
||||
case tkind.TK_AMPEQ: return "&=";
|
||||
case tkind.TK_PIPEEQ: return "|=";
|
||||
case tkind.TK_CARETEQ: return "^=";
|
||||
case tkind.TK_LSHIFTEQ: return "<<=";
|
||||
case tkind.TK_RSHIFTEQ: return ">>=";
|
||||
|
||||
case tkind.TK_PLUS: return "+";
|
||||
case tkind.TK_MINUS: return "-";
|
||||
case tkind.TK_STAR: return "*";
|
||||
case tkind.TK_SLASH: return "/";
|
||||
case tkind.TK_PERCENT: return "%";
|
||||
case tkind.TK_AMP: return "&";
|
||||
case tkind.TK_PIPE: return "|";
|
||||
case tkind.TK_CARET: return "^";
|
||||
case tkind.TK_TILDE: return "~";
|
||||
case tkind.TK_LSHIFT: return "<<";
|
||||
case tkind.TK_RSHIFT: return ">>";
|
||||
|
||||
case tkind.TK_EQ: return "==";
|
||||
case tkind.TK_NEQ: return "!=";
|
||||
case tkind.TK_LT: return "<";
|
||||
case tkind.TK_LE: return "<=";
|
||||
case tkind.TK_GT: return ">";
|
||||
case tkind.TK_GE: return ">=";
|
||||
|
||||
case tkind.TK_AND: return "&&";
|
||||
case tkind.TK_OR: return "||";
|
||||
case tkind.TK_NOT: return "!";
|
||||
|
||||
case tkind.TK_LARROW: return "<-";
|
||||
case tkind.TK_ARROW: return "->";
|
||||
case tkind.TK_FATARROW: return "=>";
|
||||
|
||||
case tkind.TK_LAST: return "<last>";
|
||||
};
|
||||
return "<?>";
|
||||
};
|
||||
|
||||
// ---- writer for tokprint ----------------------------------------------
|
||||
//
|
||||
// fputq mirrors cmd/wcc/tok.c:fputq — quote the string with C-style
|
||||
// escapes for \, ", \n, \t, \r and \xNN for other non-printables.
|
||||
|
||||
fn fputcbyte(fd: i32, b: u8) void = {
|
||||
let buf: [1]u8;
|
||||
buf[0] = b;
|
||||
os.write(fd, buf.ptr, 1u64);
|
||||
};
|
||||
|
||||
fn fputsstr(fd: i32, s: str) void = {
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
fn hexchar(n: u8) u8 = {
|
||||
if (n < 10u8) { return n + 48u8; }; // '0'..'9'
|
||||
return (n - 10u8) + 97u8; // 'a'..'f'
|
||||
};
|
||||
|
||||
fn fputhex2(fd: i32, b: u8) void = {
|
||||
let out: [4]u8;
|
||||
out[0] = '\\';
|
||||
out[1] = 'x';
|
||||
out[2] = hexchar(b >> 4u8);
|
||||
out[3] = hexchar(b & 15u8);
|
||||
os.write(fd, out.ptr, 4u64);
|
||||
};
|
||||
|
||||
fn fputq(fd: i32, p: *u8, n: i32) void = {
|
||||
fputcbyte(fd, '"');
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
let c: u8 = p[i];
|
||||
switch (c) {
|
||||
case '\\': fputsstr(fd, "\\\\");
|
||||
case '"': fputsstr(fd, "\\\"");
|
||||
case '\n': fputsstr(fd, "\\n");
|
||||
case '\t': fputsstr(fd, "\\t");
|
||||
case '\r': fputsstr(fd, "\\r");
|
||||
case:
|
||||
if (c < ' ' || c == 127u8) {
|
||||
fputhex2(fd, c);
|
||||
} else {
|
||||
fputcbyte(fd, c);
|
||||
};
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
fputcbyte(fd, '"');
|
||||
};
|
||||
|
||||
// tokprint — write one token line to fd. Format must match
|
||||
// cmd/wcc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
|
||||
// "<file>:<line>:<col> <kindname>[ <value>]\n"
|
||||
//
|
||||
// Takes `t` by pointer because w6c can't yet pass a >16-byte struct
|
||||
// by value; the C version takes Tok by value.
|
||||
export fn tokprint(fd: i32, t: *tok) void = {
|
||||
// Chained-dot field reads (`t.x.y`) on str sub-fields aren't yet
|
||||
// reduced by w6c — `t.x.y` returns the whole str. Lift the str
|
||||
// fields into locals so we can use the str pseudo-field path.
|
||||
let tfile: str = t.file;
|
||||
let ttext: str = t.text;
|
||||
if (tfile.len > 0) {
|
||||
fputsstr(fd, tfile);
|
||||
} else {
|
||||
fputsstr(fd, "<none>");
|
||||
};
|
||||
fputcbyte(fd, ':');
|
||||
let ls: str = strconv.i64tos(t.line: i64, strconv.base.DEC);
|
||||
os.write(fd, ls.ptr, ls.len: u64);
|
||||
fputcbyte(fd, ':');
|
||||
let cs: str = strconv.i64tos(t.col: i64, strconv.base.DEC);
|
||||
os.write(fd, cs.ptr, cs.len: u64);
|
||||
fputcbyte(fd, ' ');
|
||||
fputsstr(fd, tokname(t.kind));
|
||||
|
||||
switch (t.kind) {
|
||||
case tkind.TK_IDENT, tkind.TK_STR, tkind.TK_ERR:
|
||||
fputcbyte(fd, ' ');
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
case tkind.TK_INT, tkind.TK_RUNE:
|
||||
fputcbyte(fd, ' ');
|
||||
let us: str = strconv.u64tos(t.uval, strconv.base.DEC);
|
||||
os.write(fd, us.ptr, us.len: u64);
|
||||
};
|
||||
// tkind.TK_FLOAT is intentionally not handled here — %g formatting
|
||||
// won't byte-match across implementations. Diff fixtures must
|
||||
// be float-free until we implement a stable float formatter.
|
||||
|
||||
fputcbyte(fd, '\n');
|
||||
};
|
||||
310
lib/ww/syntax/toktest.ww
Normal file
310
lib/ww/syntax/toktest.ww
Normal file
@@ -0,0 +1,310 @@
|
||||
// toktest — functional-equivalence pin for [[tokname]], [[kwlookup]]
|
||||
// (struct fold S1) and [[tokprint]]/fputq (struct fold S9, the
|
||||
// if-ladder → switch folds in tok.ww).
|
||||
// Run with `ww run -I lib/ww lib/ww/syntax/toktest.ww`.
|
||||
//
|
||||
// tokname is checked against every tkind value (the full ladder the
|
||||
// switch replaced, plus the unknown-kind fallback); kwlookup is
|
||||
// checked against every keyword it recognises plus non-keyword and
|
||||
// near-miss (prefix/superstring/exact-width) identifiers.
|
||||
// tokprint is driven over a temp file: it pins the kind-dispatch
|
||||
// switch (STR/IDENT/ERR vs INT/RUNE vs the value-less default) and,
|
||||
// through the STR text, fputq's full escape switch (\\, ", \n, \t,
|
||||
// \r, the c<0x20 and c==0x7f \xNN arms, and the printable tail) —
|
||||
// branches the 990_selfhost corpus does not exercise (source tokens
|
||||
// hold raw `\`+`n`, never a literal control byte).
|
||||
// A failing row aborts via the assert/abort builtin (task #5 @test
|
||||
// conversion); per-row exit-code pinpoint is intentionally dropped (the
|
||||
// abort reports the file, not the row; drew-t2-conversion-spec sec.5).
|
||||
// `package main` + bare `import tok/lex` mirrors wwdump (the only other
|
||||
// external lex consumer).
|
||||
|
||||
package main;
|
||||
|
||||
import os;
|
||||
import syntax;
|
||||
|
||||
|
||||
fn checkname(k: tkind, want: str) void = {
|
||||
assert(!(tokname(k) != want));
|
||||
};
|
||||
|
||||
// One row per tkind value — same string the if-ladder returned. The
|
||||
// final row pins the unknown-kind fallback ("<?>").
|
||||
@test fn tokname_cases() void = {
|
||||
checkname(tkind.TK_NONE, "<none>");
|
||||
checkname(tkind.TK_EOF, "EOF");
|
||||
checkname(tkind.TK_ERR, "ERR");
|
||||
checkname(tkind.TK_IDENT, "IDENT");
|
||||
checkname(tkind.TK_INT, "INT");
|
||||
checkname(tkind.TK_FLOAT, "FLOAT");
|
||||
checkname(tkind.TK_RUNE, "RUNE");
|
||||
checkname(tkind.TK_STR, "STR");
|
||||
|
||||
checkname(tkind.TK_FN, "fn");
|
||||
checkname(tkind.TK_LET, "let");
|
||||
checkname(tkind.TK_DEF, "def");
|
||||
checkname(tkind.TK_IF, "if");
|
||||
checkname(tkind.TK_ELSE, "else");
|
||||
checkname(tkind.TK_FOR, "for");
|
||||
checkname(tkind.TK_SWITCH, "switch");
|
||||
checkname(tkind.TK_CASE, "case");
|
||||
checkname(tkind.TK_RETURN, "return");
|
||||
checkname(tkind.TK_USE, "import");
|
||||
checkname(tkind.TK_TYPE, "type");
|
||||
checkname(tkind.TK_STRUCT, "struct");
|
||||
checkname(tkind.TK_DEFER, "defer");
|
||||
checkname(tkind.TK_BREAK, "break");
|
||||
checkname(tkind.TK_CONTINUE, "continue");
|
||||
checkname(tkind.TK_EXPORT, "export");
|
||||
checkname(tkind.TK_PROC, "proc");
|
||||
checkname(tkind.TK_CHAN, "chan");
|
||||
checkname(tkind.TK_NIL, "nil");
|
||||
checkname(tkind.TK_TRUE, "true");
|
||||
checkname(tkind.TK_FALSE, "false");
|
||||
checkname(tkind.TK_AS, "as");
|
||||
checkname(tkind.TK_IS, "is");
|
||||
checkname(tkind.TK_VOID, "void");
|
||||
checkname(tkind.TK_YIELD, "yield");
|
||||
checkname(tkind.TK_STATIC, "static");
|
||||
checkname(tkind.TK_MATCH, "match");
|
||||
checkname(tkind.TK_CONST, "const");
|
||||
checkname(tkind.TK_UNDER, "_");
|
||||
checkname(tkind.TK_ENUM, "enum");
|
||||
checkname(tkind.TK_MODULE, "package");
|
||||
|
||||
checkname(tkind.TK_LPAREN, "(");
|
||||
checkname(tkind.TK_RPAREN, ")");
|
||||
checkname(tkind.TK_LBRACE, "{");
|
||||
checkname(tkind.TK_RBRACE, "}");
|
||||
checkname(tkind.TK_LBRACK, "[");
|
||||
checkname(tkind.TK_RBRACK, "]");
|
||||
checkname(tkind.TK_COMMA, ",");
|
||||
checkname(tkind.TK_SEMI, ";");
|
||||
checkname(tkind.TK_COLON, ":");
|
||||
checkname(tkind.TK_DOT, ".");
|
||||
checkname(tkind.TK_ELLIPSIS, "...");
|
||||
checkname(tkind.TK_DOTDOT, "..");
|
||||
checkname(tkind.TK_AT, "@");
|
||||
checkname(tkind.TK_QUESTION, "?");
|
||||
|
||||
checkname(tkind.TK_ASSIGN, "=");
|
||||
checkname(tkind.TK_PLUSEQ, "+=");
|
||||
checkname(tkind.TK_MINUSEQ, "-=");
|
||||
checkname(tkind.TK_STAREQ, "*=");
|
||||
checkname(tkind.TK_SLASHEQ, "/=");
|
||||
checkname(tkind.TK_PERCENTEQ, "%=");
|
||||
checkname(tkind.TK_AMPEQ, "&=");
|
||||
checkname(tkind.TK_PIPEEQ, "|=");
|
||||
checkname(tkind.TK_CARETEQ, "^=");
|
||||
checkname(tkind.TK_LSHIFTEQ, "<<=");
|
||||
checkname(tkind.TK_RSHIFTEQ, ">>=");
|
||||
|
||||
checkname(tkind.TK_PLUS, "+");
|
||||
checkname(tkind.TK_MINUS, "-");
|
||||
checkname(tkind.TK_STAR, "*");
|
||||
checkname(tkind.TK_SLASH, "/");
|
||||
checkname(tkind.TK_PERCENT, "%");
|
||||
checkname(tkind.TK_AMP, "&");
|
||||
checkname(tkind.TK_PIPE, "|");
|
||||
checkname(tkind.TK_CARET, "^");
|
||||
checkname(tkind.TK_TILDE, "~");
|
||||
checkname(tkind.TK_LSHIFT, "<<");
|
||||
checkname(tkind.TK_RSHIFT, ">>");
|
||||
|
||||
checkname(tkind.TK_EQ, "==");
|
||||
checkname(tkind.TK_NEQ, "!=");
|
||||
checkname(tkind.TK_LT, "<");
|
||||
checkname(tkind.TK_LE, "<=");
|
||||
checkname(tkind.TK_GT, ">");
|
||||
checkname(tkind.TK_GE, ">=");
|
||||
|
||||
checkname(tkind.TK_AND, "&&");
|
||||
checkname(tkind.TK_OR, "||");
|
||||
checkname(tkind.TK_NOT, "!");
|
||||
|
||||
checkname(tkind.TK_LARROW, "<-");
|
||||
checkname(tkind.TK_ARROW, "->");
|
||||
checkname(tkind.TK_FATARROW, "=>");
|
||||
|
||||
checkname(tkind.TK_MODRESET, "//ww:module-reset");
|
||||
checkname(tkind.TK_MODPATH, "//ww:module");
|
||||
checkname(tkind.TK_LAST, "<last>");
|
||||
|
||||
// Unknown kind → the post-switch fallback. TK_LAST is the highest
|
||||
// named value (89, after TK_MODPATH=88 landed); 90 is out of band,
|
||||
// exercising the "<?>" tail.
|
||||
checkname(90: tkind, "<?>");
|
||||
};
|
||||
|
||||
fn checkkw(s: str, want: tkind) void = {
|
||||
assert(!(kwlookup(s.ptr, s.len) != want));
|
||||
};
|
||||
|
||||
// Table-driven (parallel-array idiom; tuple rows blocked by #111). The
|
||||
// kw rows pin all 30 keywords kwlookup recognises, 1:1 with the
|
||||
// kwnames/kwkinds table (and cmd/wcc/tok.c:18-47), incl. the two remaps
|
||||
// import->TK_USE and package->TK_MODULE. The nk rows pin the
|
||||
// fall-through to TK_NONE. Explicit dims, NOT [_]: [_] static-init silently
|
||||
// miscompiles to a zero-length array in-tree (probe, cc69daf), which
|
||||
// would void the loop body — the very hole a table test must not have.
|
||||
@test fn kwlookup_cases() void = {
|
||||
let kwin: [30]str = [
|
||||
"as", "break", "case", "chan", "const", "continue", "def", "defer",
|
||||
"else", "enum", "export", "false", "fn", "for", "if", "is",
|
||||
"import", "let", "match", "nil", "package", "proc", "return",
|
||||
"static", "struct", "switch", "true", "type", "void", "yield",
|
||||
];
|
||||
let kwexp: [30]tkind = [
|
||||
tkind.TK_AS, tkind.TK_BREAK, tkind.TK_CASE, tkind.TK_CHAN,
|
||||
tkind.TK_CONST, tkind.TK_CONTINUE, tkind.TK_DEF, tkind.TK_DEFER,
|
||||
tkind.TK_ELSE, tkind.TK_ENUM, tkind.TK_EXPORT, tkind.TK_FALSE,
|
||||
tkind.TK_FN, tkind.TK_FOR, tkind.TK_IF, tkind.TK_IS,
|
||||
tkind.TK_USE, tkind.TK_LET, tkind.TK_MATCH, tkind.TK_NIL,
|
||||
tkind.TK_MODULE, tkind.TK_PROC, tkind.TK_RETURN, tkind.TK_STATIC,
|
||||
tkind.TK_STRUCT, tkind.TK_SWITCH, tkind.TK_TRUE, tkind.TK_TYPE,
|
||||
tkind.TK_VOID, tkind.TK_YIELD,
|
||||
];
|
||||
let i: i32 = 0;
|
||||
for (i < len(kwin)) {
|
||||
checkkw(kwin[i], kwexp[i]);
|
||||
i += 1;
|
||||
};
|
||||
|
||||
// Non-keywords that must fall through to TK_NONE. Rows exercise the
|
||||
// length-guard + bytewise reject inside strings.compare: keyword
|
||||
// SUPERSTRINGS (longer, shared prefix), proper PREFIXES of a keyword
|
||||
// (shorter, shared leading bytes), exact-length non-keywords at the
|
||||
// 2/3/4-byte keyword widths, and Hare bmap "keywords" (alloc/len/
|
||||
// size/append/assert) that are deliberately NOT ww lexer keywords.
|
||||
let nk: [18]str = [
|
||||
"xyzzy", // ordinary identifier
|
||||
"fns", // superstring of "fn"
|
||||
"ifx", // superstring of "if"
|
||||
"iffy", // superstring of "if"
|
||||
"form", // superstring of "for"
|
||||
"fora", // superstring of "for"
|
||||
"asx", // superstring of "as"
|
||||
"i", // proper prefix of if / is / import
|
||||
"co", // proper prefix of const / continue
|
||||
"swit", // proper prefix of switch
|
||||
"xx", // len-2 non-kw (as/fn/if/is width)
|
||||
"zzz", // len-3 non-kw (def/for/nil width)
|
||||
"abcd", // len-4 non-kw (case/enum/true/type width)
|
||||
"alloc", // Hare bmap keyword, NOT a ww lexer keyword
|
||||
"len", // Hare bmap keyword, NOT a ww lexer keyword
|
||||
"size", // Hare bmap keyword, NOT a ww lexer keyword
|
||||
"append", // Hare bmap keyword, NOT a ww lexer keyword
|
||||
"assert", // Hare bmap keyword, NOT a ww lexer keyword
|
||||
];
|
||||
let j: i32 = 0;
|
||||
for (j < len(nk)) {
|
||||
checkkw(nk[j], tkind.TK_NONE);
|
||||
j += 1;
|
||||
};
|
||||
};
|
||||
|
||||
// checkprint — tokprint `t` to a freshly-rewound fd, read the bytes
|
||||
// back, and assert they equal `want`. The fd is RDWR; we lseek to 0
|
||||
// before each write so earlier (possibly longer) content past want.len
|
||||
// is irrelevant — only want.len bytes from offset 0 are compared.
|
||||
fn checkprint(fd: i32, t: *tok, want: str) void = {
|
||||
assert(!(os.lseek(fd, 0i64, os.whence.SET) != 0i64));
|
||||
tokprint(fd, t);
|
||||
assert(!(os.lseek(fd, 0i64, os.whence.SET) != 0i64));
|
||||
let rbuf: [256]u8;
|
||||
let z: i32 = 0;
|
||||
for (z < 256) { rbuf[z] = 0u8; z += 1; };
|
||||
let rd: i64 = os.read(fd, &rbuf[0], want.len: u64);
|
||||
assert(!(rd != want.len: i64));
|
||||
let j: i32 = 0;
|
||||
for (j < want.len) {
|
||||
assert(!(rbuf[j] != want.ptr[j]));
|
||||
j += 1;
|
||||
};
|
||||
};
|
||||
|
||||
// One token per fputq/tokprint dispatch branch. The STR text packs
|
||||
// every fputq escape: \\ " \n \t \r, a c<0x20 byte (0x01), c==0x7f,
|
||||
// and a printable ('A'). `want` spells the exact emitted line.
|
||||
@test fn tokprint_cases() void = {
|
||||
let flags: os.flag = os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC;
|
||||
let fd: i32 = os.open("/tmp/ww_s9_tok.tmp", flags, 384i32);
|
||||
assert(!(fd < 0));
|
||||
|
||||
let t: tok;
|
||||
t.file = "t";
|
||||
t.line = 1;
|
||||
t.col = 1;
|
||||
|
||||
t.kind = tkind.TK_STR;
|
||||
t.text = "\\\"\n\t\r\x01\x7fA";
|
||||
checkprint(fd, &t, "t:1:1 STR \"\\\\\\\"\\n\\t\\r\\x01\\x7fA\"\n");
|
||||
|
||||
t.kind = tkind.TK_IDENT;
|
||||
t.text = "name";
|
||||
checkprint(fd, &t, "t:1:1 IDENT \"name\"\n");
|
||||
|
||||
t.kind = tkind.TK_ERR;
|
||||
t.text = "oops";
|
||||
checkprint(fd, &t, "t:1:1 ERR \"oops\"\n");
|
||||
|
||||
t.kind = tkind.TK_INT;
|
||||
t.uval = 42u64;
|
||||
checkprint(fd, &t, "t:1:1 INT 42\n");
|
||||
|
||||
t.kind = tkind.TK_RUNE;
|
||||
t.uval = 65u64;
|
||||
checkprint(fd, &t, "t:1:1 RUNE 65\n");
|
||||
|
||||
// Default arm: a kind outside the switch appends no value.
|
||||
t.kind = tkind.TK_NONE;
|
||||
checkprint(fd, &t, "t:1:1 <none>\n");
|
||||
|
||||
assert(!(os.close(fd) != 0));
|
||||
assert(!(os.remove("/tmp/ww_s9_tok.tmp") != 0));
|
||||
};
|
||||
|
||||
fn checkfloat(src: str, want: u64) void = {
|
||||
let l: lex;
|
||||
lexinit(&l, "t", src.ptr, src.len: u64);
|
||||
let t: tok;
|
||||
lexnext(&l, &t);
|
||||
assert(!(t.kind != tkind.TK_FLOAT));
|
||||
assert(!(t.uval != want));
|
||||
};
|
||||
|
||||
// #62 pin: lexnum's float fold routes through strconv.stof64 and must
|
||||
// produce the IEEE-754 correctly-rounded bits cstage gets from strtod
|
||||
// — any rounding slip is a cs≠ww DATA divergence. Vectors pinned
|
||||
// against a C strtod oracle. Rows cover the classes the retired
|
||||
// pow-10 fold got wrong: >19-digit mantissas (its i64 accumulator
|
||||
// overflowed), DBL_MIN/DBL_MAX extremes, and the decimal-fraction
|
||||
// 1-ULP double-rounding cases; plus halfway-to-even, exponent forms,
|
||||
// the underscore strip, the 53-digit exact-halfway pair at the 2^-53
|
||||
// boundary (tie rounds to even, tie+1 rounds up — also the only
|
||||
// >19-digit FRACTION rows), and an exact power of two.
|
||||
@test fn floatfold_cases() void = {
|
||||
checkfloat("1.0000000000000002", 0x3FF0000000000001u64);
|
||||
checkfloat("9007199254740993.0", 0x4340000000000000u64);
|
||||
checkfloat("1.2345e67", 0x4DDD4E421712C0B7u64);
|
||||
checkfloat("0.1", 0x3FB999999999999Au64);
|
||||
checkfloat("1.1", 0x3FF199999999999Au64);
|
||||
checkfloat("123456789012345678901234567890.0", 0x45F8EE90FF6C373Eu64);
|
||||
checkfloat("2.2250738585072014e-308", 0x0010000000000000u64);
|
||||
checkfloat("0.3", 0x3FD3333333333333u64);
|
||||
checkfloat("3.141592653589793", 0x400921FB54442D18u64);
|
||||
checkfloat("1.7976931348623157e308", 0x7FEFFFFFFFFFFFFFu64);
|
||||
checkfloat("1.7976931348623158e308", 0x7FEFFFFFFFFFFFFFu64);
|
||||
checkfloat("7.2057594037927933e16", 0x4370000000000000u64);
|
||||
checkfloat("1000000000000000000000.0", 0x444B1AE4D6E2EF50u64);
|
||||
checkfloat("1_000.5", 0x408F440000000000u64);
|
||||
checkfloat("1.00000000000000011102230246251565404236316680908203125",
|
||||
0x3FF0000000000000u64);
|
||||
checkfloat("1.00000000000000011102230246251565404236316680908203126",
|
||||
0x3FF0000000000001u64);
|
||||
checkfloat("4503599627370497.5", 0x4330000000000002u64);
|
||||
checkfloat("0.5", 0x3FE0000000000000u64);
|
||||
checkfloat("1.0e308", 0x7FE1CCF385EBC8A0u64);
|
||||
checkfloat("2.225073858507202e-308", 0x0010000000000001u64);
|
||||
};
|
||||
615
lib/ww/syntax/typ.ww
Normal file
615
lib/ww/syntax/typ.ww
Normal file
@@ -0,0 +1,615 @@
|
||||
// lib/ww/syntax/typ.ww — port of cmd/wcc/type.c.
|
||||
//
|
||||
// Status: full structural port. The C version uses module-globals for
|
||||
// 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 program.
|
||||
|
||||
package syntax;
|
||||
|
||||
import os;
|
||||
|
||||
// ---- TypeKind ---------------------------------------------------------
|
||||
// Numeric values must stay aligned with cmd/wcc/ww.h TypeKind so the
|
||||
// next diff signal (typed-AST printer / cgen) can compare across the
|
||||
// two implementations.
|
||||
|
||||
// Mirror of the C `TypeKind` enum in cmd/wcc/ww.h. Numeric values
|
||||
// are explicit and must stay in sync — the selfhost selfcheck and
|
||||
// typed-AST printers depend on matching numeric layout.
|
||||
type tykind = enum i32 {
|
||||
TY_NONE = 0,
|
||||
TY_VOID = 1,
|
||||
TY_BOOL = 2,
|
||||
TY_RUNE = 3,
|
||||
TY_I8 = 4,
|
||||
TY_I16 = 5,
|
||||
TY_I32 = 6,
|
||||
TY_I64 = 7,
|
||||
TY_U8 = 8,
|
||||
TY_U16 = 9,
|
||||
TY_U32 = 10,
|
||||
TY_U64 = 11,
|
||||
TY_UINT = 12,
|
||||
TY_INT = 13,
|
||||
TY_UINTPTR = 14,
|
||||
TY_F32 = 15,
|
||||
TY_F64 = 16,
|
||||
TY_STR = 17,
|
||||
TY_PTR = 18,
|
||||
TY_SLICE = 19,
|
||||
TY_ARRAY = 20,
|
||||
TY_STRUCT = 21,
|
||||
TY_FN = 22,
|
||||
TY_CHAN = 23,
|
||||
TY_NAMED = 24,
|
||||
TY_TUPLE = 25,
|
||||
TY_TAGGED = 26,
|
||||
TY_ERR = 27,
|
||||
TY_NEVER = 28,
|
||||
TY_UNTYPED_INT = 29,
|
||||
TY_UNTYPED_FLOAT = 30,
|
||||
TY_UNTYPED_STR = 31,
|
||||
TY_UNTYPED_RUNE = 32,
|
||||
TY_UNTYPED_BOOL = 33,
|
||||
TY_UNTYPED_NIL = 34,
|
||||
// Tail-appended values keep prior TY_* stable for the byte-diff
|
||||
// against cmd/wcc/ww.h.
|
||||
TY_ENUM = 35,
|
||||
TY_SIZE = 36, // #85 fold-1; mirrors TY_UINTPTR (8/8 amd64)
|
||||
TY_OPAQUE = 37, // #108(a); abstract + unsized, behind indirection only
|
||||
};
|
||||
|
||||
// #108(a): unsized sentinel for abstract types (tinfo.size / .align).
|
||||
// Mirrors harec SIZE_UNDEFINED = (size_t)-1 (ref/harec/include/types.h
|
||||
// :58) and cstage cmd/wcc/ww.h; not 0, so a bare opaque local can't
|
||||
// fabricate a 0-byte slot. Value == U64_MAX.
|
||||
def SIZE_UNDEFINED: u64 = 18446744073709551615;
|
||||
|
||||
// ---- tinfo / tfield / tparam -----------------------------------------
|
||||
|
||||
type tfield = struct {
|
||||
name: str,
|
||||
type_: *tinfo,
|
||||
offset: u64,
|
||||
tnext: *tfield,
|
||||
};
|
||||
|
||||
type tparam = struct {
|
||||
name: str,
|
||||
type_: *tinfo,
|
||||
// #61a: per-variant `!T` error mark for TY_TAGGED variants.
|
||||
// cstage-MIRROR divergence: harec carries no per-variant flag —
|
||||
// it models `!T` as a distinct STORAGE_ERROR type node
|
||||
// (ref/harec/include/types.h:144, src/types.c:151-159
|
||||
// type_is_error). wwstage tinfo has no iserror field
|
||||
// (check.ww TTAGGED arm), so the bit rides the shared param
|
||||
// struct instead, matching cstage Type.iserror semantics.
|
||||
// Faithful STORAGE_ERROR-node port filed as #62.
|
||||
iserror: bool,
|
||||
tnext: *tparam,
|
||||
};
|
||||
|
||||
// #57 A.6.3i-phase-1: tuple positional element. Distinct from tfield
|
||||
// (named, struct member) per harec's split at ref/harec/include/types.h
|
||||
// :109-115 (struct_field) vs :122-126 (type_tuple) — tuple positionals
|
||||
// carry no name (positional only) and a separate next-link. Rule-12
|
||||
// sea-of-stars mirrors Hare's structural choice; the empty-name idiom
|
||||
// from #50's TTAGGED-on-tparam would conflate two semantic axes
|
||||
// (variants can be named; positionals never can).
|
||||
type ttupleelem = struct {
|
||||
type_: *tinfo,
|
||||
offset: u64,
|
||||
tnext: *ttupleelem,
|
||||
};
|
||||
|
||||
type tinfo = struct {
|
||||
kind: tykind,
|
||||
size: u64,
|
||||
align: u64,
|
||||
sub: *tinfo, // ptr/slice/array/chan element
|
||||
alen: u64,
|
||||
fields: *tfield,
|
||||
params: *tparam,
|
||||
tupleelems: *ttupleelem, // #57 A.6.3i-phase-1: TY_TUPLE
|
||||
// positional chain (harec types.h:122-126
|
||||
// `struct type_tuple`). Distinct slot from
|
||||
// .fields so struct-member vs tuple-
|
||||
// positional stay axis-separated.
|
||||
ret: *tinfo,
|
||||
variadic: i32,
|
||||
nullable: i32, // #61 A.3: TY_TAGGED `(*T | void)` fold collapses to
|
||||
// 8B ptr slot (null is the void variant). Mirrors
|
||||
// cstage Type.nullable (cmd/wcc/ww.h:430-433).
|
||||
name: str,
|
||||
under: *tinfo,
|
||||
resolving: i32, // #62/#69: TY_NAMED demand-resolution cycle guard.
|
||||
// Mirrors cstage Type.resolving (cmd/wcc/ww.h)
|
||||
// and harec idecl->in_progress (ref/harec/src/
|
||||
// check.c:4767): set while the alias body
|
||||
// resolves; a VALUE-position read of an
|
||||
// in-progress named is a true type cycle and
|
||||
// loud-rejects. Pointer positions never read
|
||||
// size, so legal self-refs stay accepted.
|
||||
slotsize: u64, // #61 A.5: stack-slot SSoT split from `size`.
|
||||
// `size` stays natural (Hare-faithful);
|
||||
// `slotsize` carries the slot-padded width
|
||||
// cgen's let/struct-field layout demands.
|
||||
// For primitives/ptr/slice/chan/fn/str/tagged
|
||||
// `slotsize == size`; struct + tuple + array
|
||||
// of struct diverge — see check.ww tinfo-
|
||||
// fornode + cgenutil.ww registerstruct.
|
||||
// Pad-to-8 of narrow primitives in let slots
|
||||
// still lives at slotsize()'s read site;
|
||||
// graduating it here would break `[N]i32`
|
||||
// stride (4*N stays natural).
|
||||
};
|
||||
|
||||
// #61 audit §1.8 / Rob+Drew convergence 2026-05-20: memoizes
|
||||
// tinfofornode lookups keyed by AST pointer. perf #18: the original
|
||||
// flat prepend-only list made the cache-MISS scan O(N) per call ->
|
||||
// O(N2) over a compile (91% of all wwstage instructions on a 5k-line
|
||||
// input). Now a node-ptr hash index, mirroring sym.ww scope.buckets
|
||||
// (rule-12): cnext chains WITHIN a bucket; lookup/bind hash then touch
|
||||
// only one bucket -> O(1) amortized. Identical lookup results (same
|
||||
// *tinfo for the same node), so emitted asm is byte-identical.
|
||||
type tinfocacheent = struct {
|
||||
key: *node,
|
||||
val: *tinfo,
|
||||
cnext: *tinfocacheent,
|
||||
};
|
||||
|
||||
// Tuning knob, NOT a type size (rule-13 N/A): power-of-two so the
|
||||
// bucket index is a MASK, not a mod. ~5400 nodes on a big input ->
|
||||
// well under one entry/bucket. Mirror of sym.ww:38 NBUCKETS (16),
|
||||
// scaled up — sym's 16 would give ~340-deep chains here.
|
||||
def NBUCKETS_TINFO: u64 = 8192u64;
|
||||
|
||||
// ---- tctx — the box of primitive types -------------------------------
|
||||
|
||||
type tctx = struct {
|
||||
tyvoid: *tinfo,
|
||||
tybool: *tinfo,
|
||||
tyrune: *tinfo,
|
||||
tyi8: *tinfo,
|
||||
tyi16: *tinfo,
|
||||
tyi32: *tinfo,
|
||||
tyi64: *tinfo,
|
||||
tyu8: *tinfo,
|
||||
tyu16: *tinfo,
|
||||
tyu32: *tinfo,
|
||||
tyu64: *tinfo,
|
||||
tyint: *tinfo,
|
||||
tyuint: *tinfo,
|
||||
tyuintptr: *tinfo,
|
||||
tysize: *tinfo,
|
||||
tyopaque: *tinfo,
|
||||
tyf32: *tinfo,
|
||||
tyf64: *tinfo,
|
||||
tystr: *tinfo,
|
||||
tyerr: *tinfo,
|
||||
tynever: *tinfo,
|
||||
tyuntypedint: *tinfo,
|
||||
tyuntypedfloat: *tinfo,
|
||||
tyuntypedstr: *tinfo,
|
||||
tyuntypedrune: *tinfo,
|
||||
tyuntypedbool: *tinfo,
|
||||
tyuntypednil: *tinfo,
|
||||
tinfobuckets: **tinfocacheent, // length NBUCKETS_TINFO; node-ptr hash index
|
||||
};
|
||||
|
||||
// ---- constructors -----------------------------------------------------
|
||||
|
||||
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, tupleelems=nil, ret=nil, variadic=0, nullable=0, name="", under=nil, slotsize=0u64})!;
|
||||
return t;
|
||||
};
|
||||
|
||||
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; };
|
||||
t.slotsize = sz;
|
||||
return t;
|
||||
};
|
||||
|
||||
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.tysize = prim(tykind.TY_SIZE, "size", 8u64, 8u64); // #85
|
||||
c.tyf32 = prim(tykind.TY_F32, "f32", 4u64, 4u64);
|
||||
c.tyf64 = prim(tykind.TY_F64, "f64", 8u64, 8u64);
|
||||
// str IS []u8: { *u8, len, cap } — 24B, 3-reg ABI (#1/Phase 3).
|
||||
// Size sourced from a u8-slice's size (typeslice SSoT) so str and
|
||||
// []u8 can never drift; no second hardcoded 24. Mirrors cstage
|
||||
// type.c `type_slice(a, ty_u8)->size`.
|
||||
//
|
||||
// The slice tinfo MUST land in a local first: the inline form
|
||||
// `typeslice(c.tyu8).size` triggers a cgen bug — `call().field`
|
||||
// where the call returns a *pointer* emits no deref (it uses the
|
||||
// returned pointer AS the field value), so tystr.size would become
|
||||
// a heap address → runaway slot-size loops. Filed as task #6
|
||||
// (cstage cgen.c N_DOT base=N_CALL-returning-pointer + wwstage
|
||||
// cgdot mirror); retained here as a local until that lands.
|
||||
let u8slice: *tinfo = typeslice(c.tyu8);
|
||||
c.tystr = prim(tykind.TY_STR, "str", u8slice.size, 8u64);
|
||||
c.tystr.sub = c.tyu8; // str IS []u8: element is u8 (Phase 2 F1)
|
||||
c.tyerr = prim(tykind.TY_ERR, "<err>", 0u64, 1u64);
|
||||
c.tynever = prim(tykind.TY_NEVER, "never", 0u64, 1u64);
|
||||
// #108(a): abstract + unsized. SIZE_UNDEFINED (not 0) blocks a bare
|
||||
// `let x: opaque` 0-byte slot; legal only behind indirection.
|
||||
// Mirrors cstage type.c ty_opaque (harec types.c:1446).
|
||||
c.tyopaque = prim(tykind.TY_OPAQUE, "opaque", SIZE_UNDEFINED, SIZE_UNDEFINED);
|
||||
|
||||
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);
|
||||
let tib: []*tinfocacheent = alloc([], NBUCKETS_TINFO)!; // mirror sym.ww:63
|
||||
c.tinfobuckets = tib.ptr;
|
||||
};
|
||||
|
||||
export fn typeptr(sub: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(tykind.TY_PTR);
|
||||
t.sub = sub;
|
||||
t.size = 8u64;
|
||||
t.align = 8u64;
|
||||
t.slotsize = 8u64;
|
||||
return t;
|
||||
};
|
||||
|
||||
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;
|
||||
t.slotsize = 24u64; // sizelint-ok: SSoT for slice slotsize (#64)
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typearray(sub: *tinfo, n: u64) *tinfo = {
|
||||
let t: *tinfo = newtype(tykind.TY_ARRAY);
|
||||
t.sub = sub;
|
||||
t.alen = n;
|
||||
if (sub != nil) {
|
||||
t.size = sub.size * n;
|
||||
t.align = sub.align;
|
||||
// #61 A.5: ti.slotsize = stride * elen using the element's
|
||||
// slot-padded width. Primitives have slotsize == size so
|
||||
// `[N]i32` stride stays 4 (natural); structs have padded
|
||||
// slotsize so `[N]Triplet` stride lifts to 16.
|
||||
t.slotsize = sub.slotsize * n;
|
||||
} else {
|
||||
t.align = 1u64;
|
||||
};
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typechan(sub: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(tykind.TY_CHAN);
|
||||
t.sub = sub;
|
||||
t.size = 8u64;
|
||||
t.align = 8u64;
|
||||
t.slotsize = 8u64;
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typenamed(name: str, under: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(tykind.TY_NAMED);
|
||||
t.name = name;
|
||||
t.under = under; // peel-ok: construction
|
||||
if (under != nil) {
|
||||
t.size = under.size;
|
||||
t.align = under.align;
|
||||
t.slotsize = under.slotsize;
|
||||
};
|
||||
return t;
|
||||
};
|
||||
|
||||
// #61 audit §1.8 — A.1 infrastructure: tinfocache lookup/bind. Keyed
|
||||
// by AST node-pointer so two different N_TNAME("i32") nodes get
|
||||
// independent entries that both resolve to c.tyi32. Used by
|
||||
// tinfofornode in check.ww; cgen still reads sizes via primtypesize
|
||||
// until A.2+ graduates each walker family.
|
||||
// Node ptrs are 8+-aligned, so the low 3-4 bits are always zero —
|
||||
// shift right 4 before masking or every 16th bucket would cluster.
|
||||
fn tinfobucket(key: *node) u64 = {
|
||||
return ((key: u64) >> 4u64) & (NBUCKETS_TINFO - 1u64);
|
||||
};
|
||||
|
||||
export fn tinfocachelookup(c: *tctx, key: *node) *tinfo = {
|
||||
let bi: u64 = tinfobucket(key);
|
||||
let e: *tinfocacheent = c.tinfobuckets[bi];
|
||||
for (e != nil) {
|
||||
if (e.key == key) { return e.val; };
|
||||
e = e.cnext;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
export fn tinfocachebind(c: *tctx, key: *node, val: *tinfo) void = {
|
||||
let bi: u64 = tinfobucket(key);
|
||||
let e: *tinfocacheent = alloc(tinfocacheent{key=key, val=val, cnext=c.tinfobuckets[bi]})!;
|
||||
c.tinfobuckets[bi] = e;
|
||||
};
|
||||
|
||||
// ---- predicates -------------------------------------------------------
|
||||
|
||||
export fn typeisint(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_I8) { return true; };
|
||||
if (k == tykind.TY_I16) { return true; };
|
||||
if (k == tykind.TY_I32) { return true; };
|
||||
if (k == tykind.TY_I64) { return true; };
|
||||
if (k == tykind.TY_U8) { return true; };
|
||||
if (k == tykind.TY_U16) { return true; };
|
||||
if (k == tykind.TY_U32) { return true; };
|
||||
if (k == tykind.TY_U64) { return true; };
|
||||
if (k == tykind.TY_INT) { return true; };
|
||||
if (k == tykind.TY_UINT){ return true; };
|
||||
if (k == tykind.TY_UINTPTR) { return true; };
|
||||
if (k == tykind.TY_SIZE) { return true; };
|
||||
if (k == tykind.TY_RUNE){ return true; };
|
||||
if (k == tykind.TY_UNTYPED_INT) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_RUNE) { return true; };
|
||||
if (k == tykind.TY_ENUM) { return typeisint(t.sub); };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisint(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
export fn typeisfloat(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_F32) { return true; };
|
||||
if (k == tykind.TY_F64) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_FLOAT) { return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisfloat(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
export fn typeisnum(t: *tinfo) bool = {
|
||||
if (typeisint(t)) { return true; };
|
||||
return typeisfloat(t);
|
||||
};
|
||||
|
||||
// TY_RUNE is unsigned: Unicode codepoint (0..0x10FFFF) zero-extends on
|
||||
// sub-word load (MOVL, not MOVSXD). TY_ENUM recurses on .sub so a
|
||||
// `type k = enum u32 {…}` reads as unsigned. Cite cstage type.c:178
|
||||
// `type_isunsigned`; rule 10 keeps wwstage aligned down to cstage.
|
||||
export fn typeisunsigned(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_U8) { return true; };
|
||||
if (k == tykind.TY_U16) { return true; };
|
||||
if (k == tykind.TY_U32) { return true; };
|
||||
if (k == tykind.TY_U64) { return true; };
|
||||
if (k == tykind.TY_UINT){ return true; };
|
||||
if (k == tykind.TY_UINTPTR) { return true; };
|
||||
if (k == tykind.TY_SIZE) { return true; };
|
||||
if (k == tykind.TY_RUNE){ return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisunsigned(t.under); };
|
||||
if (k == tykind.TY_ENUM) { return typeisunsigned(t.sub); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeissigned — does this type need sign-extension on a sub-word
|
||||
// (1/2/4B) load? Mirrors cstage cgen.c:240 `fld_issigned`. Cgen-facing
|
||||
// predicate (TY_BOOL is unsigned for storage purposes — 0/1 → MOVZBQ),
|
||||
// so it doesn't simply mirror `!typeisunsigned`. Pair-of-`is*`
|
||||
// convention follows ref/hare/types/ helpers.
|
||||
export fn typeissigned(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind == tykind.TY_BOOL) { return false; };
|
||||
if (typeisunsigned(t)) { return false; };
|
||||
return typeisint(t);
|
||||
};
|
||||
|
||||
// typeisstr — TY_STR (and TY_UNTYPED_STR for literals pre-default).
|
||||
// Cite cstage cgen.c:159 `type_isstr` — single TY_NAMED peel, accepts
|
||||
// the same untyped form. ww walks the .under chain so alias-of-alias
|
||||
// (`type s2 = s1; type s1 = str;`) lands the same way.
|
||||
export fn typeisstr(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_STR) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_STR) { return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisstr(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeisslice — TY_SLICE. Cite cstage cgen.c:174 `type_isslice`.
|
||||
export fn typeisslice(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_SLICE) { return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisslice(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeistagged — TY_TAGGED (alias-aware). Cite cstage cgen.c:516
|
||||
// `type_istagged` — same single-peel shape. The node-keyed wwstage
|
||||
// helper this replaces also unwrapped a leading N_TBANG so
|
||||
// `type error = !(invalid | overflow);` registered as tagged. Post-
|
||||
// A.6.2 the TBANG unwrap is handled by tinfofornode (check.ww:1145-
|
||||
// 1152 returns the inner tinfo unchanged) so we recover the cstage
|
||||
// semantics with the bare kind check + NAMED chase.
|
||||
export fn typeistagged(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_TAGGED) { return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeistagged(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeisf32 — narrower-than-typeisfloat: only TY_F32 (after alias
|
||||
// chase). Cite cstage cgen.c:188 `type_isf32`. Used to pick MOVSS vs
|
||||
// MOVSD and the SS-variant arithmetic / cast opcodes.
|
||||
export fn typeisf32(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_F32) { return true; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisf32(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeisnullable — TY_TAGGED with the `(*T | void)` one-word fold.
|
||||
// Cite cstage cgen.c:396 `type_isnullable`. The .nullable flag is
|
||||
// stamped by tinfofornode (check.ww:1309-1318) when the two-variant
|
||||
// shape matches.
|
||||
export fn typeisnullable(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_TAGGED) { return t.nullable != 0; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeisnullable(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeis8byteprim — does this type take exactly one 8-byte stack
|
||||
// slot (ptr / fn / chan / 64-bit int / scalar primitive padded up to
|
||||
// 8 / `[N]T` whose natural width is 8) rather than a wider aggregate?
|
||||
// Mirrors the ladder cstage's cgen.c N_LET zero-init takes on `sz==8`
|
||||
// (cmd/wcc/check.c sizing + cgen.c N_LET). The node-keyed wwstage
|
||||
// helper this replaces predates tinfo and AST-walked TBANG / TNAME
|
||||
// alias chains; tinfofornode now collapses TBANG (check.ww:1145) and
|
||||
// TY_NAMED.under carries the chain, so the tinfo walk handles every
|
||||
// shape the AST walker did.
|
||||
export fn typeis8byteprim(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_PTR) { return true; };
|
||||
if (k == tykind.TY_FN) { return true; };
|
||||
if (k == tykind.TY_CHAN) { return true; };
|
||||
if (k == tykind.TY_SLICE) { return false; };
|
||||
if (k == tykind.TY_TUPLE) { return false; };
|
||||
if (k == tykind.TY_TAGGED) { return false; };
|
||||
if (k == tykind.TY_STR) { return false; };
|
||||
if (k == tykind.TY_STRUCT) { return false; };
|
||||
if (k == tykind.TY_ARRAY) { return t.size == 8u64; };
|
||||
// peel-ok: recursive chase
|
||||
if (k == tykind.TY_NAMED) { return typeis8byteprim(t.under); };
|
||||
// Remaining: primitives (i8/u8/.../i64/u64/bool/rune/f32/f64/
|
||||
// int/uint/uintptr) and TY_VOID. All slot-pad to 8 and zero-init
|
||||
// in cstage's `sz==8` branch.
|
||||
return true;
|
||||
};
|
||||
|
||||
export fn typeisuntyped(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: tykind = t.kind;
|
||||
if (k == tykind.TY_UNTYPED_INT) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_FLOAT) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_STR) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_RUNE) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_BOOL) { return true; };
|
||||
if (k == tykind.TY_UNTYPED_NIL) { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeeq — structural equality. Named types compare nominally.
|
||||
export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
if (a == b) { return true; };
|
||||
if (a == nil) { return false; };
|
||||
if (b == nil) { return false; };
|
||||
if (a.kind != b.kind) { return false; };
|
||||
let k: tykind = a.kind;
|
||||
if (k == tykind.TY_PTR) { return typeeq(a.sub, b.sub); };
|
||||
if (k == tykind.TY_SLICE) { return typeeq(a.sub, b.sub); };
|
||||
if (k == tykind.TY_CHAN) { return typeeq(a.sub, b.sub); };
|
||||
if (k == tykind.TY_ARRAY) {
|
||||
if (a.alen != b.alen) { return false; };
|
||||
return typeeq(a.sub, b.sub);
|
||||
};
|
||||
if (k == tykind.TY_FN) {
|
||||
if (a.variadic != b.variadic) { return false; };
|
||||
if (!typeeq(a.ret, b.ret)) { return false; };
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_STRUCT) {
|
||||
let fa: *tfield = a.fields;
|
||||
let fb: *tfield = b.fields;
|
||||
for (true) {
|
||||
if (fa == nil) { if (fb == nil) { return true; }; return false; };
|
||||
if (fb == nil) { return false; };
|
||||
let na: str = fa.name;
|
||||
let nb: str = fb.name;
|
||||
if (na.len != nb.len) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < na.len) {
|
||||
if (na[i] != nb[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
if (!typeeq(fa.type_, fb.type_)) { return false; };
|
||||
fa = fa.tnext;
|
||||
fb = fb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_NAMED) { return false; }; // nominal: only same ptr
|
||||
if (k == tykind.TY_TAGGED) {
|
||||
// Structural: variant lists match position-by-position, and
|
||||
// the nullable `(*T|void)` fold is part of identity. Mirrors
|
||||
// cstage type_eq's TY_TAGGED arm (cmd/wcc/type.c:271-284);
|
||||
// the missing branch let any two tagged unions compare equal
|
||||
// (fell through to the primitive `return true`), which
|
||||
// #218's cgvariantmatch structural fallback was the first
|
||||
// caller to exercise.
|
||||
if (a.nullable != b.nullable) { return false; };
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_TUPLE) {
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
return true; // primitives match by kind alone
|
||||
};
|
||||
Reference in New Issue
Block a user