ww: lift introspection files to lib/ww/ (ast, lex, tok, parse, typ, sym)
This commit is contained in:
@@ -437,8 +437,8 @@ export fn parseu64(s: str) (u64 | str) = {
|
||||
return v;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -927,8 +927,8 @@ export fn toupper(c: u8) u8 = {
|
||||
return c;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/lex.ww — port of cmd/wcc/lex.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -1616,8 +1616,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
|
||||
out.text = astrndup(l.a, one.ptr, 1u64);
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
|
||||
// MODULE: ww
|
||||
// lib/ww/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.
|
||||
@@ -1954,8 +1954,8 @@ export fn astprint(fd: i32, n: *node) void = {
|
||||
pr(fd, n, 0);
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/parse.ww — port of cmd/wcc/parse.c.
|
||||
//
|
||||
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
|
||||
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
|
||||
@@ -2942,8 +2942,8 @@ export fn parsefile(p: *parser) *node = {
|
||||
return f;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/type.ww — port of cmd/wcc/type.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -3273,8 +3273,8 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true; // primitives match by kind alone
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/sym.ww — port of cmd/wcc/sym.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
|
||||
@@ -1,336 +0,0 @@
|
||||
// selfhost/cmd/wcc/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.
|
||||
|
||||
use os;
|
||||
use strconv;
|
||||
use mem;
|
||||
use tok;
|
||||
|
||||
// ---- Nkind ------------------------------------------------------------
|
||||
//
|
||||
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
|
||||
// the AST diff probe in 990_selfhost works.
|
||||
|
||||
def N_NONE: i32 = 0;
|
||||
|
||||
def N_INTLIT: i32 = 1;
|
||||
def N_FLOATLIT: i32 = 2;
|
||||
def N_STRLIT: i32 = 3;
|
||||
def N_RUNELIT: i32 = 4;
|
||||
def N_TRUE: i32 = 5;
|
||||
def N_FALSE: i32 = 6;
|
||||
def N_NIL: i32 = 7;
|
||||
def N_IDENT: i32 = 8;
|
||||
|
||||
def N_BIN: i32 = 9;
|
||||
def N_UN: i32 = 10;
|
||||
def N_CALL: i32 = 11;
|
||||
def N_INDEX: i32 = 12;
|
||||
def N_DOT: i32 = 13;
|
||||
def N_CAST: i32 = 14;
|
||||
def N_STRUCTLIT:i32 = 15;
|
||||
def N_ARRLIT: i32 = 16;
|
||||
def N_FIELD: i32 = 17;
|
||||
def N_ASSIGN: i32 = 18;
|
||||
def N_ALLOC: i32 = 19;
|
||||
def N_FREE: i32 = 20;
|
||||
def N_RECV: i32 = 21;
|
||||
def N_SLICE: i32 = 22;
|
||||
def N_SPREAD: i32 = 23;
|
||||
|
||||
def N_BLOCK: i32 = 24;
|
||||
def N_EXPRSTMT: i32 = 25;
|
||||
def N_LET: i32 = 26;
|
||||
def N_RETURN: i32 = 27;
|
||||
def N_IF: i32 = 28;
|
||||
def N_FOR: i32 = 29;
|
||||
def N_FORRANGE: i32 = 30;
|
||||
def N_DEFER: i32 = 31;
|
||||
def N_BREAK: i32 = 32;
|
||||
def N_CONTINUE: i32 = 33;
|
||||
def N_SWITCH: i32 = 34;
|
||||
def N_CASE: i32 = 35;
|
||||
|
||||
def N_FILE: i32 = 36;
|
||||
def N_USE: i32 = 37;
|
||||
def N_DEF: i32 = 38;
|
||||
def N_TYPEDECL: i32 = 39;
|
||||
def N_FNDECL: i32 = 40;
|
||||
def N_PARAM: i32 = 41;
|
||||
|
||||
def N_TNAME: i32 = 42;
|
||||
def N_TPTR: i32 = 43;
|
||||
def N_TSLICE: i32 = 44;
|
||||
def N_TARRAY: i32 = 45;
|
||||
def N_TFN: i32 = 46;
|
||||
def N_TSTRUCT: i32 = 47;
|
||||
def N_TFIELD: i32 = 48;
|
||||
def N_TCHAN: i32 = 49;
|
||||
|
||||
def N_ATTR: i32 = 50;
|
||||
def N_TTUPLE: i32 = 51;
|
||||
def N_TTAGGED: i32 = 52;
|
||||
def N_TUPLE: i32 = 53;
|
||||
def N_MATCH: i32 = 54;
|
||||
def N_MCASE: i32 = 55;
|
||||
def N_TRYPROP: i32 = 56;
|
||||
def N_TRYUNW: i32 = 57;
|
||||
def N_MLET: i32 = 58;
|
||||
def N_MASSIGN: i32 = 59;
|
||||
|
||||
def N_LAST: i32 = 60;
|
||||
|
||||
// ---- Node -------------------------------------------------------------
|
||||
|
||||
type node = struct {
|
||||
kind: i32,
|
||||
file: str,
|
||||
line: i32,
|
||||
col: i32,
|
||||
op: i32, // for N_BIN / N_UN / 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", ...)
|
||||
module: str, // originating module from `// MODULE: foo`; "" if none
|
||||
};
|
||||
|
||||
export fn newnode(a: *arena, k: i32, file: str, line: i32, col: i32) *node = {
|
||||
let n: *node = amalloc(a, 208u64): *node; // ≥ struct size
|
||||
n.kind = k;
|
||||
n.file = file;
|
||||
n.line = line;
|
||||
n.col = col;
|
||||
return n;
|
||||
};
|
||||
|
||||
// ---- printer ----------------------------------------------------------
|
||||
|
||||
fn nkname(k: i32) str = {
|
||||
if (k == N_NONE) { return "none"; };
|
||||
if (k == N_INTLIT) { return "int"; };
|
||||
if (k == N_FLOATLIT) { return "float"; };
|
||||
if (k == N_STRLIT) { return "str"; };
|
||||
if (k == N_RUNELIT) { return "rune"; };
|
||||
if (k == N_TRUE) { return "true"; };
|
||||
if (k == N_FALSE) { return "false"; };
|
||||
if (k == N_NIL) { return "nil"; };
|
||||
if (k == N_IDENT) { return "id"; };
|
||||
if (k == N_BIN) { return "bin"; };
|
||||
if (k == N_UN) { return "un"; };
|
||||
if (k == N_CALL) { return "call"; };
|
||||
if (k == N_INDEX) { return "index"; };
|
||||
if (k == N_DOT) { return "dot"; };
|
||||
if (k == N_CAST) { return "cast"; };
|
||||
if (k == N_STRUCTLIT) { return "structlit"; };
|
||||
if (k == N_ARRLIT) { return "arrlit"; };
|
||||
if (k == N_FIELD) { return "field"; };
|
||||
if (k == N_ASSIGN) { return "assign"; };
|
||||
if (k == N_ALLOC) { return "alloc"; };
|
||||
if (k == N_FREE) { return "free"; };
|
||||
if (k == N_RECV) { return "recv"; };
|
||||
if (k == N_SLICE) { return "slice"; };
|
||||
if (k == N_SPREAD) { return "spread"; };
|
||||
if (k == N_BLOCK) { return "block"; };
|
||||
if (k == N_EXPRSTMT) { return "exprstmt"; };
|
||||
if (k == N_LET) { return "let"; };
|
||||
if (k == N_RETURN) { return "return"; };
|
||||
if (k == N_IF) { return "if"; };
|
||||
if (k == N_FOR) { return "for"; };
|
||||
if (k == N_FORRANGE) { return "forrange"; };
|
||||
if (k == N_DEFER) { return "defer"; };
|
||||
if (k == N_BREAK) { return "break"; };
|
||||
if (k == N_CONTINUE) { return "continue"; };
|
||||
if (k == N_SWITCH) { return "switch"; };
|
||||
if (k == N_CASE) { return "case"; };
|
||||
if (k == N_FILE) { return "file"; };
|
||||
if (k == N_USE) { return "use"; };
|
||||
if (k == N_DEF) { return "def"; };
|
||||
if (k == N_TYPEDECL) { return "typedecl"; };
|
||||
if (k == N_FNDECL) { return "fn"; };
|
||||
if (k == N_PARAM) { return "param"; };
|
||||
if (k == N_TNAME) { return "tname"; };
|
||||
if (k == N_TPTR) { return "tptr"; };
|
||||
if (k == N_TSLICE) { return "tslice"; };
|
||||
if (k == N_TARRAY) { return "tarray"; };
|
||||
if (k == N_TFN) { return "tfn"; };
|
||||
if (k == N_TSTRUCT) { return "tstruct"; };
|
||||
if (k == N_TFIELD) { return "tfield"; };
|
||||
if (k == N_TCHAN) { return "tchan"; };
|
||||
if (k == N_ATTR) { return "attr"; };
|
||||
if (k == N_TTUPLE) { return "ttuple"; };
|
||||
if (k == N_TTAGGED) { return "ttagged"; };
|
||||
if (k == N_TUPLE) { return "tuple"; };
|
||||
if (k == N_MATCH) { return "match"; };
|
||||
if (k == N_MCASE) { return "mcase"; };
|
||||
if (k == N_TRYPROP) { return "tryprop"; };
|
||||
if (k == N_TRYUNW) { return "tryunw"; };
|
||||
if (k == N_MLET) { return "mlet"; };
|
||||
if (k == N_MASSIGN) { return "massign"; };
|
||||
if (k == 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, 34u8); // '"'
|
||||
let i: i32 = 0;
|
||||
for (i < s.len) {
|
||||
let c: u8 = s[i];
|
||||
if (c == 34u8) { // '"'
|
||||
os.write(fd, "\\\"".ptr, 2u64);
|
||||
} else { if (c == 92u8) { // '\\'
|
||||
os.write(fd, "\\\\".ptr, 2u64);
|
||||
} else { if (c == 10u8) { // '\n'
|
||||
os.write(fd, "\\n".ptr, 2u64);
|
||||
} else { if (c == 9u8) { // '\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;
|
||||
};
|
||||
ind(fd, d);
|
||||
putc1(fd, 40u8); // '('
|
||||
let nm: str = nkname(n.kind);
|
||||
os.write(fd, nm.ptr, nm.len: u64);
|
||||
|
||||
if (n.kind == N_INTLIT) {
|
||||
putc1(fd, 32u8);
|
||||
let buf: [32]u8;
|
||||
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
} else { if (n.kind == N_RUNELIT) {
|
||||
putc1(fd, 32u8);
|
||||
let buf: [32]u8;
|
||||
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
} else { if (
|
||||
n.kind == N_STRLIT ||
|
||||
n.kind == N_IDENT ||
|
||||
n.kind == N_USE ||
|
||||
n.kind == N_DOT ||
|
||||
n.kind == N_DEF ||
|
||||
n.kind == N_TYPEDECL ||
|
||||
n.kind == N_FNDECL ||
|
||||
n.kind == N_PARAM ||
|
||||
n.kind == N_LET ||
|
||||
n.kind == N_TNAME ||
|
||||
n.kind == N_TFIELD ||
|
||||
n.kind == N_FIELD ||
|
||||
n.kind == 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 == N_BIN ||
|
||||
n.kind == N_UN ||
|
||||
n.kind == N_ASSIGN
|
||||
) {
|
||||
putc1(fd, 32u8);
|
||||
let on: str = tokname(n.op);
|
||||
os.write(fd, on.ptr, on.len: u64);
|
||||
};};};};
|
||||
|
||||
if (n.kind == N_FNDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == N_DEF) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == N_TYPEDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
putc1(fd, 10u8); // '\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);
|
||||
};
|
||||
@@ -1,687 +0,0 @@
|
||||
// selfhost/cmd/wcc/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.
|
||||
|
||||
use os;
|
||||
use ascii;
|
||||
use mem;
|
||||
use tok;
|
||||
|
||||
type lex = struct {
|
||||
file: str,
|
||||
src: *u8, // raw bytes; not necessarily NUL-terminated
|
||||
srclen: u64,
|
||||
lpos: u64,
|
||||
line: i32,
|
||||
col: i32,
|
||||
a: *arena,
|
||||
errs: i32,
|
||||
module: str, // current module from `// MODULE: foo` directive; "" if none
|
||||
};
|
||||
|
||||
export fn lexinit(l: *lex, a: *arena, file: str, src: *u8, len: u64) void = {
|
||||
l.file = file;
|
||||
l.src = src;
|
||||
l.srclen = len;
|
||||
l.lpos = 0u64;
|
||||
l.line = 1;
|
||||
l.col = 1;
|
||||
l.a = a;
|
||||
l.errs = 0;
|
||||
let empty: str;
|
||||
empty.ptr = nil;
|
||||
empty.len = 0;
|
||||
l.module = empty;
|
||||
};
|
||||
|
||||
// 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 == 10) { // '\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;
|
||||
};
|
||||
|
||||
// putuint — write `v` (signed, but always non-negative here) to fd 2
|
||||
// in decimal. Standalone so errat doesn't drag in fmt and create a
|
||||
// dependency cycle with strconv.
|
||||
fn putuint(fd: i32, v: i32) void = {
|
||||
let tmp: [16]u8;
|
||||
let i: i32 = 0;
|
||||
let n: i32 = v;
|
||||
for (n > 0) {
|
||||
tmp[i] = ((n % 10) + 48): u8;
|
||||
n = n / 10;
|
||||
i += 1;
|
||||
};
|
||||
if (i == 0) { tmp[0] = 48u8; i = 1; };
|
||||
let buf: [16]u8;
|
||||
let m: i32 = 0;
|
||||
for (i > 0) { i -= 1; buf[m] = tmp[i]; m += 1; };
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
};
|
||||
|
||||
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);
|
||||
putuint(2, p.line);
|
||||
os.write(2, ":".ptr, 1u64);
|
||||
putuint(2, p.col);
|
||||
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 == 32) { lget(l); continue; };
|
||||
if (c == 9) { lget(l); continue; };
|
||||
if (c == 13) { lget(l); continue; };
|
||||
if (c == 10) { lget(l); continue; };
|
||||
if (c == 47) { // '/'
|
||||
let c2: i32 = lpeek(l, 1u64);
|
||||
if (c2 == 47) {
|
||||
lget(l); lget(l); // consume '//'
|
||||
// Driver injects `// MODULE: foo` before each
|
||||
// source file's contents; capture so cgen can
|
||||
// mangle private symbols by module.
|
||||
if (lpeek(l, 0u64) == 32) { // ' '
|
||||
if (lpeek(l, 1u64) == 77) { // 'M'
|
||||
if (lpeek(l, 2u64) == 79) { // 'O'
|
||||
if (lpeek(l, 3u64) == 68) { // 'D'
|
||||
if (lpeek(l, 4u64) == 85) { // 'U'
|
||||
if (lpeek(l, 5u64) == 76) { // 'L'
|
||||
if (lpeek(l, 6u64) == 69) { // 'E'
|
||||
if (lpeek(l, 7u64) == 58) { // ':'
|
||||
if (lpeek(l, 8u64) == 32) { // ' '
|
||||
let i: i32 = 0;
|
||||
for (i < 9) { lget(l); i += 1; };
|
||||
let start: u64 = l.lpos;
|
||||
for (true) {
|
||||
let cx: i32 = lpeek(l, 0u64);
|
||||
if (cx < 0) { break; };
|
||||
if (cx == 10) { break; };
|
||||
if (cx == 13) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let n: u64 = l.lpos - start;
|
||||
l.module = astrndup(l.a, l.src + start, n);
|
||||
};};};};};};};};};
|
||||
for (true) {
|
||||
let cx: i32 = lpeek(l, 0u64);
|
||||
if (cx < 0) { return false; };
|
||||
if (cx == 10) { break; };
|
||||
lget(l);
|
||||
};
|
||||
continue;
|
||||
};
|
||||
if (c2 == 42) { // '*'
|
||||
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 == 42) {
|
||||
if (x == 47) { 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 == 95u8) { // '_'
|
||||
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; };
|
||||
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 == 110) { *out = 10; return true; };
|
||||
if (c == 116) { *out = 9; return true; };
|
||||
if (c == 114) { *out = 13; return true; };
|
||||
if (c == 92) { *out = 92; return true; };
|
||||
if (c == 39) { *out = 39; return true; };
|
||||
if (c == 34) { *out = 34; return true; };
|
||||
if (c == 48) { *out = 0; return true; };
|
||||
if (c == 97) { *out = 7; return true; };
|
||||
if (c == 98) { *out = 8; return true; };
|
||||
if (c == 102) { *out = 12; return true; };
|
||||
if (c == 118) { *out = 11; return true; };
|
||||
if (c == 120) {
|
||||
let hi: i32 = lget(l);
|
||||
let lo: i32 = lget(l);
|
||||
if (hi < 0) { return false; };
|
||||
if (lo < 0) { return false; };
|
||||
if (!ascii.ishex(hi: u8)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
if (!ascii.ishex(lo: u8)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
let h: i32 = ascii.digitval(hi: u8);
|
||||
let lv: i32 = ascii.digitval(lo: u8);
|
||||
*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: u8)) {
|
||||
if (c != 95) { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanhexrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.ishex(c: u8)) {
|
||||
if (c != 95) { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanbinrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c == 48) { lget(l); continue; };
|
||||
if (c == 49) { lget(l); continue; };
|
||||
if (c == 95) { lget(l); continue; };
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
fn scanoctrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 48) { break; };
|
||||
if (c > 55) {
|
||||
if (c != 95) { 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 != 101) { if (e != 69) { return; }; }; // 'e' or 'E'
|
||||
lget(l);
|
||||
let s: i32 = lpeek(l, 0u64);
|
||||
if (s == 43) { lget(l); }
|
||||
else { if (s == 45) { lget(l); }; };
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isdigit(c: u8)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn lexnum(l: *lex, start: *pos, out: *tok) void = {
|
||||
out.kind = 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 == 48) { // '0'
|
||||
if (c1 == 120) { // 'x'
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 88) { // 'X'
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 98) { // 'b'
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 66) { // 'B'
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 111) { // 'o'
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else { if (c1 == 79) { // 'O'
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= 48) {
|
||||
if (after <= 57) {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};};};};};};
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= 48) {
|
||||
if (after <= 57) {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
let n: u64 = l.lpos - begin;
|
||||
out.text = astrndup(l.a, l.src + begin, n);
|
||||
|
||||
if (isfloat) {
|
||||
// out.fval is already 0 from the top-of-lexnext clear.
|
||||
// We don't strtod the literal yet — the diff fixtures we
|
||||
// care about are float-free; any TK_FLOAT seen in source
|
||||
// gets a placeholder value until we wire a real parser.
|
||||
out.kind = TK_FLOAT;
|
||||
} 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 = TK_ERR;
|
||||
};
|
||||
};
|
||||
|
||||
let pc: i32 = lpeek(l, 0u64);
|
||||
if (pc >= 0) {
|
||||
if (ascii.isidstart(pc: u8)) {
|
||||
let sb: u64 = l.lpos;
|
||||
for (true) {
|
||||
let cc: i32 = lpeek(l, 0u64);
|
||||
if (cc < 0) { break; };
|
||||
if (!ascii.isidpart(cc: u8)) { 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] == 105u8) {
|
||||
if (p[1] == 56u8) { isok = true; }; // i8
|
||||
};
|
||||
if (p[0] == 117u8) {
|
||||
if (p[1] == 56u8) { isok = true; }; // u8
|
||||
};
|
||||
};
|
||||
if (sl == 3u64) {
|
||||
if (p[0] == 105u8) {
|
||||
if (p[1] == 49u8) { if (p[2] == 54u8) { isok = true; }; }; // i16
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; }; // i32
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; }; // i64
|
||||
};
|
||||
if (p[0] == 117u8) {
|
||||
if (p[1] == 49u8) { if (p[2] == 54u8) { isok = true; }; };
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; };
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; };
|
||||
};
|
||||
if (p[0] == 102u8) {
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; }; // f32
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; }; // f64
|
||||
};
|
||||
};
|
||||
if (isok) {
|
||||
out.tsuffix = astrndup(l.a, p, sl);
|
||||
} 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 (!ascii.isidpart(c: u8)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let n: u64 = l.lpos - begin;
|
||||
let p: *u8 = l.src + begin;
|
||||
let k: i32 = kwlookup(p, n: i32);
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
if (k != TK_NONE) {
|
||||
out.kind = k;
|
||||
} else {
|
||||
out.kind = TK_IDENT;
|
||||
};
|
||||
out.text = astrndup(l.a, p, n);
|
||||
};
|
||||
|
||||
fn lexstr(l: *lex, start: *pos, out: *tok) void = {
|
||||
let cap: u64 = 32u64;
|
||||
let nb: u64 = 0u64;
|
||||
let buf: *u8 = amalloc(l.a, cap): *u8;
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) {
|
||||
errat(l, start, "unterminated string");
|
||||
out.kind = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
if (c == 34) { lget(l); break; };
|
||||
let ch: i32 = 0;
|
||||
if (c == 92) {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (nb + 1u64 >= cap) {
|
||||
let ncap: u64 = cap * 2u64;
|
||||
let nb2: *u8 = amalloc(l.a, ncap): *u8;
|
||||
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 = TK_STR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let s: str;
|
||||
s.ptr = buf;
|
||||
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 = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
let ch: i32 = 0;
|
||||
if (c == 92) {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (lpeek(l, 0u64) != 39) {
|
||||
errat(l, start, "rune literal missing closing '");
|
||||
out.kind = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
lget(l);
|
||||
out.kind = TK_RUNE;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.uval = ch: u64;
|
||||
};
|
||||
|
||||
fn emitsimple(start: *pos, k: i32, 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 = 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;
|
||||
|
||||
if (!skipws(l)) {
|
||||
let p: pos; curpos(l, &p);
|
||||
emitsimple(&p, TK_EOF, out);
|
||||
return;
|
||||
};
|
||||
let start: pos; curpos(l, &start);
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
|
||||
if (c >= 0) {
|
||||
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
|
||||
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
|
||||
};
|
||||
|
||||
if (c == 34) { lget(l); lexstr(l, &start, out); return; };
|
||||
if (c == 39) { lget(l); lexrune(l, &start, out); return; };
|
||||
|
||||
lget(l);
|
||||
|
||||
if (c == 40) { emitsimple(&start, TK_LPAREN, out); return; };
|
||||
if (c == 41) { emitsimple(&start, TK_RPAREN, out); return; };
|
||||
if (c == 123) { emitsimple(&start, TK_LBRACE, out); return; };
|
||||
if (c == 125) { emitsimple(&start, TK_RBRACE, out); return; };
|
||||
if (c == 91) { emitsimple(&start, TK_LBRACK, out); return; };
|
||||
if (c == 93) { emitsimple(&start, TK_RBRACK, out); return; };
|
||||
if (c == 44) { emitsimple(&start, TK_COMMA, out); return; };
|
||||
if (c == 59) { emitsimple(&start, TK_SEMI, out); return; };
|
||||
if (c == 58) { emitsimple(&start, TK_COLON, out); return; };
|
||||
if (c == 64) { emitsimple(&start, TK_AT, out); return; };
|
||||
if (c == 63) { emitsimple(&start, TK_QUESTION, out); return; };
|
||||
if (c == 126) { emitsimple(&start, TK_TILDE, out); return; };
|
||||
|
||||
if (c == 46) { // '.'
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
if (lpeek(l, 1u64) == 46) {
|
||||
lget(l); lget(l);
|
||||
emitsimple(&start, TK_ELLIPSIS, out); return;
|
||||
};
|
||||
lget(l);
|
||||
emitsimple(&start, TK_DOTDOT, out); return;
|
||||
};
|
||||
emitsimple(&start, TK_DOT, out); return;
|
||||
};
|
||||
|
||||
if (c == 43) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PLUSEQ, out); return; };
|
||||
emitsimple(&start, TK_PLUS, out); return;
|
||||
};
|
||||
if (c == 45) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_MINUSEQ, out); return; };
|
||||
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, TK_ARROW, out); return; };
|
||||
emitsimple(&start, TK_MINUS, out); return;
|
||||
};
|
||||
if (c == 42) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_STAREQ, out); return; };
|
||||
emitsimple(&start, TK_STAR, out); return;
|
||||
};
|
||||
if (c == 47) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_SLASHEQ, out); return; };
|
||||
emitsimple(&start, TK_SLASH, out); return;
|
||||
};
|
||||
if (c == 37) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PERCENTEQ, out); return; };
|
||||
emitsimple(&start, TK_PERCENT, out); return;
|
||||
};
|
||||
if (c == 38) {
|
||||
if (lpeek(l, 0u64) == 38) { lget(l); emitsimple(&start, TK_AND, out); return; };
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_AMPEQ, out); return; };
|
||||
emitsimple(&start, TK_AMP, out); return;
|
||||
};
|
||||
if (c == 124) {
|
||||
if (lpeek(l, 0u64) == 124) { lget(l); emitsimple(&start, TK_OR, out); return; };
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PIPEEQ, out); return; };
|
||||
emitsimple(&start, TK_PIPE, out); return;
|
||||
};
|
||||
if (c == 94) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_CARETEQ, out); return; };
|
||||
emitsimple(&start, TK_CARET, out); return;
|
||||
};
|
||||
if (c == 61) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_EQ, out); return; };
|
||||
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, TK_FATARROW, out); return; };
|
||||
emitsimple(&start, TK_ASSIGN, out); return;
|
||||
};
|
||||
if (c == 33) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_NEQ, out); return; };
|
||||
emitsimple(&start, TK_NOT, out); return;
|
||||
};
|
||||
if (c == 60) {
|
||||
if (lpeek(l, 0u64) == 60) {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_LSHIFTEQ, out); return; };
|
||||
emitsimple(&start, TK_LSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_LE, out); return; };
|
||||
if (lpeek(l, 0u64) == 45) { lget(l); emitsimple(&start, TK_LARROW, out); return; };
|
||||
emitsimple(&start, TK_LT, out); return;
|
||||
};
|
||||
if (c == 62) {
|
||||
if (lpeek(l, 0u64) == 62) {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_RSHIFTEQ, out); return; };
|
||||
emitsimple(&start, TK_RSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_GE, out); return; };
|
||||
emitsimple(&start, TK_GT, out); return;
|
||||
};
|
||||
|
||||
errat(l, &start, "unexpected character");
|
||||
out.kind = TK_ERR;
|
||||
setposfrom(out, &start);
|
||||
let one: [1]u8;
|
||||
one[0] = c: u8;
|
||||
out.text = astrndup(l.a, one.ptr, 1u64);
|
||||
};
|
||||
@@ -1,986 +0,0 @@
|
||||
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
|
||||
//
|
||||
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
|
||||
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
|
||||
// RET;` (header-only — bodies are recovered past). Unknown decls are
|
||||
// chewed token-by-token until the next ';' so the diff probe can
|
||||
// still anchor on partial fixtures.
|
||||
//
|
||||
// The full port is multi-session work — parse.c is 1,183 lines of
|
||||
// hand-rolled recursive descent + Pratt expression parser. Each
|
||||
// surface form lands here gradually so the AST diff in 990_selfhost
|
||||
// grows toward whole-language coverage one increment at a time.
|
||||
//
|
||||
// Calling-convention shim: w6c can't yet pass a sub-struct field
|
||||
// (e.g. p.cur.line where p.cur is a `tok` of size 76). The parser
|
||||
// stores the current token as flat primitive fields rather than a
|
||||
// nested `tok` struct; `refill` copies a freshly lexed token in.
|
||||
|
||||
use os;
|
||||
use mem;
|
||||
use tok;
|
||||
|
||||
type parser = struct {
|
||||
l: *lex,
|
||||
a: *arena,
|
||||
errs: i32,
|
||||
// nocast: while inside `[...]` we treat ':' as the slice
|
||||
// separator, not the cast operator. Mirrors parse.c's flag.
|
||||
nocast: i32,
|
||||
cur_kind: i32,
|
||||
cur_file: str,
|
||||
cur_line: i32,
|
||||
cur_col: i32,
|
||||
cur_text: str,
|
||||
cur_uval: u64,
|
||||
};
|
||||
|
||||
fn refill(p: *parser) void = {
|
||||
let t: tok;
|
||||
lexnext(p.l, &t);
|
||||
p.cur_kind = t.kind;
|
||||
p.cur_file = t.file;
|
||||
p.cur_line = t.line;
|
||||
p.cur_col = t.col;
|
||||
p.cur_text = t.text;
|
||||
p.cur_uval = t.uval;
|
||||
};
|
||||
|
||||
export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
|
||||
p.l = l;
|
||||
p.a = a;
|
||||
p.errs = 0;
|
||||
p.nocast = 0;
|
||||
refill(p);
|
||||
};
|
||||
|
||||
fn advance(p: *parser) void = { refill(p); };
|
||||
|
||||
fn accepttok(p: *parser, k: i32) bool = {
|
||||
if (p.cur_kind == k) { advance(p); return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
fn errmsg(p: *parser, msg: str) void = {
|
||||
let pre: str = "parse: ";
|
||||
os.write(2, pre.ptr, pre.len: u64);
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
p.errs += 1;
|
||||
};
|
||||
|
||||
fn expecttok(p: *parser, k: i32, what: str) bool = {
|
||||
if (p.cur_kind == k) { advance(p); return true; };
|
||||
errmsg(p, what);
|
||||
return false;
|
||||
};
|
||||
|
||||
// expectident — consume the current TK_IDENT and return its text.
|
||||
// Returns the empty str on error (and advances to make progress).
|
||||
fn expectident(p: *parser, into: *str) bool = {
|
||||
if (p.cur_kind != TK_IDENT) {
|
||||
errmsg(p, "expected identifier");
|
||||
advance(p);
|
||||
return false;
|
||||
};
|
||||
*into = p.cur_text;
|
||||
advance(p);
|
||||
return true;
|
||||
};
|
||||
|
||||
// ---- type expressions ------------------------------------------------
|
||||
//
|
||||
// Currently: TNAME (single ident, no dotted path yet) and TPTR (`*T`).
|
||||
// Other forms (slice, array, struct, fn, chan, tuple, tagged) will
|
||||
// land in subsequent commits.
|
||||
|
||||
fn parsetype(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
|
||||
if (p.cur_kind == TK_STAR) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.cur_kind == TK_LBRACK) {
|
||||
advance(p);
|
||||
if (p.cur_kind == TK_RBRACK) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_TARRAY, pf, pl, pc);
|
||||
n.rhs = parseexpr(p);
|
||||
expecttok(p, TK_RBRACK, "expected ']' in array type");
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.cur_kind == TK_STRUCT) {
|
||||
advance(p);
|
||||
expecttok(p, TK_LBRACE, "expected '{' after struct");
|
||||
let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc);
|
||||
let fhead: *node = nil;
|
||||
let ftail: *node = nil;
|
||||
for (p.cur_kind != TK_RBRACE) {
|
||||
if (p.cur_kind == TK_EOF) { break; };
|
||||
let fpf: str = p.cur_file;
|
||||
let fpl: i32 = p.cur_line;
|
||||
let fpc: i32 = p.cur_col;
|
||||
let f: *node = newnode(p.a, N_TFIELD, fpf, fpl, fpc);
|
||||
let fid: str;
|
||||
expectident(p, &fid);
|
||||
f.str = fid;
|
||||
expecttok(p, TK_COLON, "expected ':' in field");
|
||||
f.lhs = parsetype(p);
|
||||
if (fhead == nil) { fhead = f; ftail = f; }
|
||||
else { ftail.next = f; ftail = f; };
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, TK_RBRACE, "expected '}' after struct fields");
|
||||
n.list = fhead;
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.cur_kind == TK_IDENT) {
|
||||
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
|
||||
n.str = p.cur_text;
|
||||
advance(p);
|
||||
// Dotted path collapse (pkg.Type) deferred — fixtures don't
|
||||
// need it yet.
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.cur_kind == TK_LPAREN) {
|
||||
// (T) or (T, T, ...) or (T | T | ...)
|
||||
advance(p);
|
||||
let first: *node = parsetype(p);
|
||||
if (accepttok(p, TK_PIPE)) {
|
||||
let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc);
|
||||
let head: *node = first;
|
||||
let tail: *node = first;
|
||||
for (true) {
|
||||
let e: *node = parsetype(p);
|
||||
tail.next = e;
|
||||
tail = e;
|
||||
if (!accepttok(p, TK_PIPE)) { break; };
|
||||
};
|
||||
expecttok(p, TK_RPAREN, "expected ')' in tagged-union type");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
if (!accepttok(p, TK_COMMA)) {
|
||||
expecttok(p, TK_RPAREN, "expected ')' after parenthesised type");
|
||||
return first;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc);
|
||||
let head: *node = first;
|
||||
let tail: *node = first;
|
||||
for (true) {
|
||||
let e: *node = parsetype(p);
|
||||
tail.next = e;
|
||||
tail = e;
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
if (p.cur_kind == TK_RPAREN) { break; };
|
||||
};
|
||||
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
|
||||
n.list = head;
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.cur_kind == TK_FN) {
|
||||
advance(p);
|
||||
expecttok(p, TK_LPAREN, "expected '(' after fn in type");
|
||||
let n: *node = newnode(p.a, N_TFN, pf, pl, pc);
|
||||
// Anonymous-or-named params: parseparams handles named only;
|
||||
// for fn-type expressions the C parser allows IDENT-less
|
||||
// (anonymous) params. Stub: only named params for now.
|
||||
n.list = parseparams(p);
|
||||
expecttok(p, TK_RPAREN, "expected ')' after fn type params");
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
errmsg(p, "expected type");
|
||||
advance(p);
|
||||
return newnode(p.a, N_TNAME, pf, pl, pc);
|
||||
};
|
||||
|
||||
// ---- expressions (Pratt) ---------------------------------------------
|
||||
//
|
||||
// Forwards: parseexpr → parsebin → parseunary → parsepostfix(parseprimary).
|
||||
// Tuple literals, match expressions, struct literals, slice [lo:hi],
|
||||
// and the ?/! try operators are not yet wired — they'll arrive as the
|
||||
// AST diff fixture grows to need them.
|
||||
|
||||
fn bprec(k: i32) i32 = {
|
||||
if (k == TK_OR) { return 1; };
|
||||
if (k == TK_AND) { return 2; };
|
||||
if (k == TK_EQ) { return 3; };
|
||||
if (k == TK_NEQ) { return 3; };
|
||||
if (k == TK_LT) { return 4; };
|
||||
if (k == TK_LE) { return 4; };
|
||||
if (k == TK_GT) { return 4; };
|
||||
if (k == TK_GE) { return 4; };
|
||||
if (k == TK_PIPE) { return 5; };
|
||||
if (k == TK_CARET) { return 6; };
|
||||
if (k == TK_AMP) { return 7; };
|
||||
if (k == TK_LSHIFT) { return 8; };
|
||||
if (k == TK_RSHIFT) { return 8; };
|
||||
if (k == TK_PLUS) { return 9; };
|
||||
if (k == TK_MINUS) { return 9; };
|
||||
if (k == TK_STAR) { return 10; };
|
||||
if (k == TK_SLASH) { return 10; };
|
||||
if (k == TK_PERCENT) { return 10; };
|
||||
return 0;
|
||||
};
|
||||
|
||||
fn isassignop(k: i32) bool = {
|
||||
if (k == TK_ASSIGN) { return true; };
|
||||
if (k == TK_PLUSEQ) { return true; };
|
||||
if (k == TK_MINUSEQ) { return true; };
|
||||
if (k == TK_STAREQ) { return true; };
|
||||
if (k == TK_SLASHEQ) { return true; };
|
||||
if (k == TK_PERCENTEQ) { return true; };
|
||||
if (k == TK_AMPEQ) { return true; };
|
||||
if (k == TK_PIPEEQ) { return true; };
|
||||
if (k == TK_CARETEQ) { return true; };
|
||||
if (k == TK_LSHIFTEQ) { return true; };
|
||||
if (k == TK_RSHIFTEQ) { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
// Forward references between parseunary/parseexpr/parsebin/parsepostfix
|
||||
// are resolved by the two-pass checker — no body-less prototypes needed.
|
||||
|
||||
fn parseprimary(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
|
||||
if (p.cur_kind == TK_INT) {
|
||||
let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc);
|
||||
n.uval = p.cur_uval;
|
||||
n.str = p.cur_text;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_STR) {
|
||||
let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc);
|
||||
n.str = p.cur_text;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_RUNE) {
|
||||
let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc);
|
||||
n.uval = p.cur_uval;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_TRUE) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_TRUE, pf, pl, pc);
|
||||
};
|
||||
if (p.cur_kind == TK_FALSE) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_FALSE, pf, pl, pc);
|
||||
};
|
||||
if (p.cur_kind == TK_NIL) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_NIL, pf, pl, pc);
|
||||
};
|
||||
if (p.cur_kind == TK_LPAREN) {
|
||||
advance(p);
|
||||
let e: *node = parseexpr(p);
|
||||
// Tuple literal: (a, b, ...)
|
||||
if (accepttok(p, TK_COMMA)) {
|
||||
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
||||
t.list = e;
|
||||
let tail: *node = e;
|
||||
for (true) {
|
||||
if (p.cur_kind == TK_RPAREN) { break; };
|
||||
let en: *node = parseexpr(p);
|
||||
tail.next = en;
|
||||
tail = en;
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, TK_RPAREN, "expected ')' in tuple");
|
||||
return t;
|
||||
};
|
||||
expecttok(p, TK_RPAREN, "expected ')'");
|
||||
return e;
|
||||
};
|
||||
if (p.cur_kind == TK_IDENT) {
|
||||
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
|
||||
n.str = p.cur_text;
|
||||
advance(p);
|
||||
// `IDENT {` — struct literal. Disambiguate: only consume as a
|
||||
// struct lit when we're not in a context where '{' starts a
|
||||
// block (e.g. `if (cond) {`). The parser is called from
|
||||
// expressions, never directly from cond contexts that need a
|
||||
// block; in stmt parsing, the for/if drivers consume their
|
||||
// own paren/cond, so this is safe.
|
||||
if (p.cur_kind == TK_LBRACE) {
|
||||
advance(p);
|
||||
let s: *node = newnode(p.a, N_STRUCTLIT, pf, pl, pc);
|
||||
s.lhs = n;
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.cur_kind != TK_RBRACE) {
|
||||
if (p.cur_kind == TK_EOF) { break; };
|
||||
let fpf: str = p.cur_file;
|
||||
let fpl: i32 = p.cur_line;
|
||||
let fpc: i32 = p.cur_col;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
expecttok(p, TK_ASSIGN, "expected '=' in struct lit field");
|
||||
let v: *node = parseexpr(p);
|
||||
let f: *node = newnode(p.a, N_FIELD, fpf, fpl, fpc);
|
||||
f.str = id;
|
||||
f.lhs = v;
|
||||
if (head == nil) { head = f; tail = f; }
|
||||
else { tail.next = f; tail = f; };
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
};
|
||||
expecttok(p, TK_RBRACE, "expected '}' after struct literal");
|
||||
s.list = head;
|
||||
return s;
|
||||
};
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_MATCH) {
|
||||
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
|
||||
advance(p);
|
||||
expecttok(p, TK_LPAREN, "expected '(' after match");
|
||||
let m: *node = newnode(p.a, N_MATCH, pf, pl, pc);
|
||||
m.lhs = parseexpr(p);
|
||||
expecttok(p, TK_RPAREN, "expected ')' after match scrutinee");
|
||||
expecttok(p, TK_LBRACE, "expected '{' to open match body");
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.cur_kind == TK_CASE) {
|
||||
let cf: str = p.cur_file;
|
||||
let cl: i32 = p.cur_line;
|
||||
let cc: i32 = p.cur_col;
|
||||
advance(p); // past `case`
|
||||
let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc);
|
||||
if (p.cur_kind == TK_LET) {
|
||||
advance(p);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
mc.str = id;
|
||||
expecttok(p, TK_COLON, "expected ':' after match binding");
|
||||
mc.lhs = parsetype(p);
|
||||
} else { if (p.cur_kind != TK_FATARROW) {
|
||||
mc.lhs = parsetype(p);
|
||||
};};
|
||||
expecttok(p, TK_FATARROW, "expected '=>' in match arm");
|
||||
mc.body = parsestmt(p);
|
||||
if (head == nil) { head = mc; tail = mc; }
|
||||
else { tail.next = mc; tail = mc; };
|
||||
};
|
||||
expecttok(p, TK_RBRACE, "expected '}' after match body");
|
||||
m.list = head;
|
||||
return m;
|
||||
};
|
||||
errmsg(p, "expected expression");
|
||||
advance(p);
|
||||
return newnode(p.a, N_NONE, pf, pl, pc);
|
||||
};
|
||||
|
||||
fn parsearglist(p: *parser, close_kind: i32, head_out: **node) void = {
|
||||
*head_out = nil;
|
||||
if (p.cur_kind == close_kind) { return; };
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let e: *node = parseexpr(p);
|
||||
if (head == nil) { head = e; tail = e; }
|
||||
else { tail.next = e; tail = e; };
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
if (p.cur_kind == close_kind) { break; };
|
||||
};
|
||||
*head_out = head;
|
||||
};
|
||||
|
||||
fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
let cur: *node = lhs;
|
||||
for (true) {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
if (p.cur_kind == TK_LPAREN) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_CALL, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
let arghead: *node = nil;
|
||||
parsearglist(p, TK_RPAREN, &arghead);
|
||||
n.list = arghead;
|
||||
expecttok(p, TK_RPAREN, "expected ')' after args");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.cur_kind == TK_LBRACK) {
|
||||
advance(p);
|
||||
// `[ : hi ]` — slice with implicit lo = 0.
|
||||
if (p.cur_kind == TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
if (p.cur_kind != TK_RBRACK) {
|
||||
n.cond = parseexpr(p);
|
||||
};
|
||||
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
// Suppress cast inside `[...]` so ':' parses as slice
|
||||
// separator rather than the postfix cast operator.
|
||||
let prev: i32 = p.nocast;
|
||||
p.nocast = 1;
|
||||
let e: *node = parseexpr(p);
|
||||
p.nocast = prev;
|
||||
if (p.cur_kind == TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
if (p.cur_kind != TK_RBRACK) {
|
||||
n.cond = parseexpr(p);
|
||||
};
|
||||
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_INDEX, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
expecttok(p, TK_RBRACK, "expected ']' after index");
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.cur_kind == TK_DOT) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_DOT, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.cur_kind == TK_COLON) {
|
||||
if (p.nocast != 0) {
|
||||
return cur;
|
||||
};
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_CAST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
break;
|
||||
};
|
||||
return cur;
|
||||
};
|
||||
|
||||
fn parseunary(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
let k: i32 = p.cur_kind;
|
||||
if (k == TK_MINUS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_MINUS; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == TK_PLUS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_PLUS; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == TK_NOT) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_NOT; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == TK_TILDE) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_TILDE; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == TK_STAR) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_STAR; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == TK_AMP) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
n.op = TK_AMP; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
return parsepostfix(p, parseprimary(p));
|
||||
};
|
||||
|
||||
fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
|
||||
let cur: *node = lhs;
|
||||
for (true) {
|
||||
let op: i32 = p.cur_kind;
|
||||
let pr: i32 = bprec(op);
|
||||
if (pr == 0) { return cur; };
|
||||
if (pr < minp) { return cur; };
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p);
|
||||
let rhs: *node = parseunary(p);
|
||||
for (true) {
|
||||
let np: i32 = bprec(p.cur_kind);
|
||||
if (np <= pr) { break; };
|
||||
rhs = parsebin(p, rhs, np);
|
||||
};
|
||||
let n: *node = newnode(p.a, N_BIN, pf, pl, pc);
|
||||
n.op = op; n.lhs = cur; n.rhs = rhs;
|
||||
cur = n;
|
||||
};
|
||||
return cur;
|
||||
};
|
||||
|
||||
fn parseexpr(p: *parser) *node = {
|
||||
let e: *node = parsebin(p, parseunary(p), 1);
|
||||
if (isassignop(p.cur_kind)) {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
let op: i32 = p.cur_kind;
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_ASSIGN, pf, pl, pc);
|
||||
n.op = op;
|
||||
n.lhs = e;
|
||||
n.rhs = parseexpr(p); // right-associative
|
||||
return n;
|
||||
};
|
||||
return e;
|
||||
};
|
||||
|
||||
// ---- statements ------------------------------------------------------
|
||||
//
|
||||
// Subset wired today: block, let, return, if (no else-if chain), for
|
||||
// (single-cond C-style), expr-stmt, defer, break, continue. Switch
|
||||
// and match arms are not yet wired; tuple-let / multi-let neither.
|
||||
|
||||
fn parseletlocal(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `let`
|
||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
if (accepttok(p, TK_COLON)) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
if (accepttok(p, TK_ASSIGN)) {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, TK_SEMI, "expected ';' after let");
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parseblock(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
expecttok(p, TK_LBRACE, "expected '{' to open block");
|
||||
let blk: *node = newnode(p.a, N_BLOCK, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.cur_kind != TK_RBRACE) {
|
||||
if (p.cur_kind == TK_EOF) { break; };
|
||||
let s: *node = parsestmt(p);
|
||||
if (s != nil) {
|
||||
if (head == nil) { head = s; tail = s; }
|
||||
else { tail.next = s; tail = s; };
|
||||
};
|
||||
};
|
||||
expecttok(p, TK_RBRACE, "expected '}' to close block");
|
||||
blk.list = head;
|
||||
return blk;
|
||||
};
|
||||
|
||||
fn parseif(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `if`
|
||||
expecttok(p, TK_LPAREN, "expected '(' after if");
|
||||
let n: *node = newnode(p.a, N_IF, pf, pl, pc);
|
||||
n.cond = parseexpr(p);
|
||||
expecttok(p, TK_RPAREN, "expected ')' after if condition");
|
||||
n.body = parseblock(p);
|
||||
if (accepttok(p, TK_ELSE)) {
|
||||
if (p.cur_kind == TK_IF) {
|
||||
n.els = parseif(p);
|
||||
} else {
|
||||
n.els = parseblock(p);
|
||||
};
|
||||
};
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsefor(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `for`
|
||||
expecttok(p, TK_LPAREN, "expected '(' after for");
|
||||
let n: *node = newnode(p.a, N_FOR, pf, pl, pc);
|
||||
// Three forms (matching C parser):
|
||||
// for (cond) — only cond
|
||||
// for (init; cond; post) — full
|
||||
// for (true) — infinite (cond is N_TRUE)
|
||||
// Distinguish by counting ';'. Look at first chunk: if it's a
|
||||
// `let` stmt that's the init. Otherwise, parse expr; if next is
|
||||
// ';' it was cond. If we see two ';' total after init, post is
|
||||
// next. Simpler: peek for `let` to decide init form.
|
||||
if (p.cur_kind == TK_LET) {
|
||||
n.lhs = parseletlocal(p); // init (consumes its own ';')
|
||||
n.cond = parseexpr(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after for cond");
|
||||
n.rhs = parseexpr(p);
|
||||
} else {
|
||||
// Parse one expr. If next is ';', it's a 3-clause without init.
|
||||
let first: *node = parseexpr(p);
|
||||
if (accepttok(p, TK_SEMI)) {
|
||||
// cond ; post
|
||||
n.cond = first;
|
||||
n.rhs = parseexpr(p);
|
||||
} else {
|
||||
// just (cond)
|
||||
n.cond = first;
|
||||
};
|
||||
};
|
||||
expecttok(p, TK_RPAREN, "expected ')' after for");
|
||||
n.body = parseblock(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsestmt(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
|
||||
// `static` is allowed on local lets per Hare; we accept and skip
|
||||
// it (it doesn't change the AST shape).
|
||||
if (p.cur_kind == TK_STATIC) { advance(p); };
|
||||
|
||||
if (p.cur_kind == TK_LBRACE) {
|
||||
let b: *node = parseblock(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after block");
|
||||
return b;
|
||||
};
|
||||
if (p.cur_kind == TK_LET) { return parseletlocal(p); };
|
||||
if (p.cur_kind == TK_IF) {
|
||||
let n: *node = parseif(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after if");
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_FOR) {
|
||||
let n: *node = parsefor(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after for");
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_RETURN) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_RETURN, pf, pl, pc);
|
||||
if (p.cur_kind != TK_SEMI) {
|
||||
let first: *node = parseexpr(p);
|
||||
// Hare-style multi-value: `return a, b;` becomes a
|
||||
// tuple expression so codegen sees one rvalue.
|
||||
if (p.cur_kind == TK_COMMA) {
|
||||
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
||||
t.list = first;
|
||||
let tail: *node = first;
|
||||
for (accepttok(p, TK_COMMA)) {
|
||||
let e: *node = parseexpr(p);
|
||||
tail.next = e;
|
||||
tail = e;
|
||||
};
|
||||
n.lhs = t;
|
||||
} else {
|
||||
n.lhs = first;
|
||||
};
|
||||
};
|
||||
expecttok(p, TK_SEMI, "expected ';' after return");
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_DEFER) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_DEFER, pf, pl, pc);
|
||||
n.lhs = parseexpr(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after defer");
|
||||
return n;
|
||||
};
|
||||
if (p.cur_kind == TK_BREAK) {
|
||||
advance(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after break");
|
||||
return newnode(p.a, N_BREAK, pf, pl, pc);
|
||||
};
|
||||
if (p.cur_kind == TK_CONTINUE) {
|
||||
advance(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after continue");
|
||||
return newnode(p.a, N_CONTINUE, pf, pl, pc);
|
||||
};
|
||||
// expression statement, or tuple-destructure multi-assign:
|
||||
// a, b = expr;
|
||||
// Mirrors cmd/wcc/parse.c:1015-1031. We parse the first lvalue
|
||||
// with parseexpr (matches the C side); subsequent lvalues go
|
||||
// through parsebin(parseunary, 1) so the `=` stays for us to
|
||||
// consume — parseexpr would absorb it.
|
||||
let e: *node = parseexpr(p);
|
||||
if (p.cur_kind == TK_COMMA) {
|
||||
let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc);
|
||||
let head: *node = e;
|
||||
let tail: *node = e;
|
||||
for (p.cur_kind == TK_COMMA) {
|
||||
advance(p);
|
||||
let lv: *node = parsebin(p, parseunary(p), 1);
|
||||
tail.next = lv;
|
||||
tail = lv;
|
||||
};
|
||||
expecttok(p, TK_ASSIGN, "expected '=' after multi-assign lvalues");
|
||||
m.rhs = parseexpr(p);
|
||||
m.list = head;
|
||||
expecttok(p, TK_SEMI, "expected ';' after multi-assign");
|
||||
return m;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_EXPRSTMT, pf, pl, pc);
|
||||
n.lhs = e;
|
||||
expecttok(p, TK_SEMI, "expected ';' after expression statement");
|
||||
return n;
|
||||
};
|
||||
|
||||
// ---- top-level decl parsers ------------------------------------------
|
||||
|
||||
fn parseuse(p: *parser) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `use`
|
||||
let n: *node = newnode(p.a, N_USE, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, TK_SEMI, "expected ';' after use");
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsedef(p: *parser, exported: i32) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `def`
|
||||
let n: *node = newnode(p.a, N_DEF, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, TK_COLON, "expected ':' in def");
|
||||
n.lhs = parsetype(p);
|
||||
expecttok(p, TK_ASSIGN, "expected '=' in def");
|
||||
n.rhs = parseexpr(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after def");
|
||||
n.exported = exported;
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parselet(p: *parser, exported: i32) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `let`
|
||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
if (accepttok(p, TK_COLON)) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
if (accepttok(p, TK_ASSIGN)) {
|
||||
n.rhs = parseexpr(p);
|
||||
};
|
||||
expecttok(p, TK_SEMI, "expected ';' after let");
|
||||
n.exported = exported;
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parseattrs(p: *parser) *node = {
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.cur_kind == TK_AT) {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p);
|
||||
let a: *node = newnode(p.a, N_ATTR, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
a.str = id;
|
||||
expecttok(p, TK_LPAREN, "expected '(' after attribute name");
|
||||
let arghead: *node = nil;
|
||||
parsearglist(p, TK_RPAREN, &arghead);
|
||||
a.list = arghead;
|
||||
expecttok(p, TK_RPAREN, "expected ')' after attribute args");
|
||||
if (head == nil) { head = a; tail = a; }
|
||||
else { tail.next = a; tail = a; };
|
||||
};
|
||||
return head;
|
||||
};
|
||||
|
||||
fn parseparams(p: *parser) *node = {
|
||||
if (p.cur_kind == TK_RPAREN) { return nil; };
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
let n: *node = newnode(p.a, N_PARAM, pf, pl, pc);
|
||||
// Param form: IDENT ':' type. Anonymous-type-only params (used
|
||||
// in fn type expressions) aren't yet wired here.
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, TK_COLON, "expected ':' in parameter");
|
||||
n.lhs = parsetype(p);
|
||||
if (head == nil) { head = n; tail = n; }
|
||||
else { tail.next = n; tail = n; };
|
||||
if (!accepttok(p, TK_COMMA)) { break; };
|
||||
if (p.cur_kind == TK_RPAREN) { break; };
|
||||
};
|
||||
return head;
|
||||
};
|
||||
|
||||
fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `fn`
|
||||
let n: *node = newnode(p.a, N_FNDECL, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, TK_LPAREN, "expected '(' after fn name");
|
||||
n.list = parseparams(p);
|
||||
expecttok(p, TK_RPAREN, "expected ')' after params");
|
||||
if (p.cur_kind != TK_ASSIGN) {
|
||||
if (p.cur_kind != TK_SEMI) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
};
|
||||
if (accepttok(p, TK_ASSIGN)) {
|
||||
n.body = parseblock(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after fn body");
|
||||
} else {
|
||||
// Body-less fn: FFI declaration (`fn name(args) ret;`).
|
||||
expecttok(p, TK_SEMI, "expected ';' after fn header");
|
||||
};
|
||||
n.exported = exported;
|
||||
n.attr = attrs;
|
||||
return n;
|
||||
};
|
||||
|
||||
fn parsetypedecl(p: *parser, exported: i32) *node = {
|
||||
let pf: str = p.cur_file;
|
||||
let pl: i32 = p.cur_line;
|
||||
let pc: i32 = p.cur_col;
|
||||
advance(p); // past `type`
|
||||
let n: *node = newnode(p.a, N_TYPEDECL, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
expecttok(p, TK_ASSIGN, "expected '=' in type decl");
|
||||
n.lhs = parsetype(p);
|
||||
expecttok(p, TK_SEMI, "expected ';' after type decl");
|
||||
n.exported = exported;
|
||||
return n;
|
||||
};
|
||||
|
||||
// ---- file-level loop -------------------------------------------------
|
||||
|
||||
export fn parsefile(p: *parser) *node = {
|
||||
let f: *node = newnode(p.a, N_FILE, p.cur_file, p.cur_line, p.cur_col);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
|
||||
for (p.cur_kind != TK_EOF) {
|
||||
let attrs: *node = parseattrs(p);
|
||||
let exported: i32 = 0;
|
||||
if (p.cur_kind == TK_EXPORT) { exported = 1; advance(p); };
|
||||
|
||||
let d: *node = nil;
|
||||
if (p.cur_kind == TK_USE) {
|
||||
d = parseuse(p);
|
||||
} else { if (p.cur_kind == TK_DEF) {
|
||||
d = parsedef(p, exported);
|
||||
} else { if (p.cur_kind == TK_TYPE) {
|
||||
d = parsetypedecl(p, exported);
|
||||
} else { if (p.cur_kind == TK_LET) {
|
||||
d = parselet(p, exported);
|
||||
} else { if (p.cur_kind == TK_FN) {
|
||||
d = parsefn(p, exported, attrs);
|
||||
} else {
|
||||
// Recovery: chew tokens until next ';' or EOF, balancing
|
||||
// '{' '}' pairs so internal ';'s in unfamiliar forms don't
|
||||
// derail us.
|
||||
for (p.cur_kind != TK_SEMI) {
|
||||
if (p.cur_kind == TK_EOF) { break; };
|
||||
if (p.cur_kind == TK_LBRACE) {
|
||||
let depth: i32 = 0;
|
||||
for (true) {
|
||||
if (p.cur_kind == TK_EOF) { break; };
|
||||
if (p.cur_kind == TK_LBRACE) { depth += 1; advance(p); continue; };
|
||||
if (p.cur_kind == TK_RBRACE) {
|
||||
depth -= 1;
|
||||
advance(p);
|
||||
if (depth == 0) { break; };
|
||||
continue;
|
||||
};
|
||||
advance(p);
|
||||
};
|
||||
continue;
|
||||
};
|
||||
advance(p);
|
||||
};
|
||||
if (p.cur_kind == TK_SEMI) { advance(p); };
|
||||
};};};};};
|
||||
|
||||
if (d != nil) {
|
||||
if (head == nil) {
|
||||
head = d;
|
||||
tail = d;
|
||||
} else {
|
||||
tail.next = d;
|
||||
tail = d;
|
||||
};
|
||||
};
|
||||
};
|
||||
f.list = head;
|
||||
return f;
|
||||
};
|
||||
@@ -1,113 +0,0 @@
|
||||
// selfhost/cmd/wcc/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.
|
||||
|
||||
use mem;
|
||||
use typ;
|
||||
use ast;
|
||||
|
||||
// Symbol kinds — must stay numerically aligned with cmd/wcc/ww.h Skind.
|
||||
def SK_NONE: i32 = 0;
|
||||
def SK_VAR: i32 = 1;
|
||||
def SK_PARAM: i32 = 2;
|
||||
def SK_DEF: i32 = 3;
|
||||
def SK_TYPE: i32 = 4;
|
||||
def SK_FN: i32 = 5;
|
||||
def SK_USE: i32 = 6;
|
||||
def SK_FIELD: i32 = 7;
|
||||
|
||||
type sym = struct {
|
||||
name: str,
|
||||
skind: i32,
|
||||
type_: *tinfo,
|
||||
decl: *node,
|
||||
exported: i32,
|
||||
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,
|
||||
a: *arena,
|
||||
};
|
||||
|
||||
// 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(a: *arena, parent: *scope) *scope = {
|
||||
let s: *scope = amalloc(a, 64u64): *scope;
|
||||
s.parent = parent;
|
||||
s.a = a;
|
||||
s.nbuckets = NBUCKETS;
|
||||
s.buckets = amalloc(a, (NBUCKETS: u64) * 8u64): **sym;
|
||||
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;
|
||||
};
|
||||
|
||||
export fn scopedefine(s: *scope, name: str, k: i32, t: *tinfo, decl: *node) *sym = {
|
||||
if (scopelookuplocal(s, name) != nil) { return nil; };
|
||||
let sy: *sym = amalloc(s.a, 80u64): *sym;
|
||||
sy.name = name;
|
||||
sy.skind = k;
|
||||
sy.type_ = t;
|
||||
sy.decl = decl;
|
||||
sy.scope = s;
|
||||
let h: u64 = hashstr(name);
|
||||
let bi: i32 = (h % (s.nbuckets: u64)): i32;
|
||||
sy.hashnext = s.buckets[bi];
|
||||
s.buckets[bi] = sy;
|
||||
if (s.first == nil) { s.first = sy; } else { s.last.snext = sy; };
|
||||
s.last = sy;
|
||||
return sy;
|
||||
};
|
||||
@@ -1,394 +0,0 @@
|
||||
// selfhost/cmd/wcc/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().
|
||||
|
||||
use os;
|
||||
use strconv;
|
||||
|
||||
// ---- Tkind ------------------------------------------------------------
|
||||
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
|
||||
|
||||
def TK_NONE: i32 = 0;
|
||||
def TK_EOF: i32 = 1;
|
||||
def TK_ERR: i32 = 2;
|
||||
def TK_IDENT: i32 = 3;
|
||||
def TK_INT: i32 = 4;
|
||||
def TK_FLOAT: i32 = 5;
|
||||
def TK_RUNE: i32 = 6;
|
||||
def TK_STR: i32 = 7;
|
||||
|
||||
def TK_FN: i32 = 8;
|
||||
def TK_LET: i32 = 9;
|
||||
def TK_DEF: i32 = 10;
|
||||
def TK_IF: i32 = 11;
|
||||
def TK_ELSE: i32 = 12;
|
||||
def TK_FOR: i32 = 13;
|
||||
def TK_SWITCH: i32 = 14;
|
||||
def TK_CASE: i32 = 15;
|
||||
def TK_RETURN: i32 = 16;
|
||||
def TK_USE: i32 = 17;
|
||||
def TK_TYPE: i32 = 18;
|
||||
def TK_STRUCT: i32 = 19;
|
||||
def TK_DEFER: i32 = 20;
|
||||
def TK_BREAK: i32 = 21;
|
||||
def TK_CONTINUE: i32 = 22;
|
||||
def TK_EXPORT: i32 = 23;
|
||||
def TK_PROC: i32 = 24;
|
||||
def TK_CHAN: i32 = 25;
|
||||
def TK_NIL: i32 = 26;
|
||||
def TK_TRUE: i32 = 27;
|
||||
def TK_FALSE: i32 = 28;
|
||||
def TK_AS: i32 = 29;
|
||||
def TK_STATIC: i32 = 30;
|
||||
def TK_MATCH: i32 = 31;
|
||||
|
||||
def TK_LPAREN: i32 = 32;
|
||||
def TK_RPAREN: i32 = 33;
|
||||
def TK_LBRACE: i32 = 34;
|
||||
def TK_RBRACE: i32 = 35;
|
||||
def TK_LBRACK: i32 = 36;
|
||||
def TK_RBRACK: i32 = 37;
|
||||
def TK_COMMA: i32 = 38;
|
||||
def TK_SEMI: i32 = 39;
|
||||
def TK_COLON: i32 = 40;
|
||||
def TK_DOT: i32 = 41;
|
||||
def TK_ELLIPSIS: i32 = 42;
|
||||
def TK_DOTDOT: i32 = 43;
|
||||
def TK_AT: i32 = 44;
|
||||
def TK_QUESTION: i32 = 45;
|
||||
|
||||
def TK_ASSIGN: i32 = 46;
|
||||
def TK_PLUSEQ: i32 = 47;
|
||||
def TK_MINUSEQ: i32 = 48;
|
||||
def TK_STAREQ: i32 = 49;
|
||||
def TK_SLASHEQ: i32 = 50;
|
||||
def TK_PERCENTEQ: i32 = 51;
|
||||
def TK_AMPEQ: i32 = 52;
|
||||
def TK_PIPEEQ: i32 = 53;
|
||||
def TK_CARETEQ: i32 = 54;
|
||||
def TK_LSHIFTEQ: i32 = 55;
|
||||
def TK_RSHIFTEQ: i32 = 56;
|
||||
|
||||
def TK_PLUS: i32 = 57;
|
||||
def TK_MINUS: i32 = 58;
|
||||
def TK_STAR: i32 = 59;
|
||||
def TK_SLASH: i32 = 60;
|
||||
def TK_PERCENT: i32 = 61;
|
||||
def TK_AMP: i32 = 62;
|
||||
def TK_PIPE: i32 = 63;
|
||||
def TK_CARET: i32 = 64;
|
||||
def TK_TILDE: i32 = 65;
|
||||
def TK_LSHIFT: i32 = 66;
|
||||
def TK_RSHIFT: i32 = 67;
|
||||
|
||||
def TK_EQ: i32 = 68;
|
||||
def TK_NEQ: i32 = 69;
|
||||
def TK_LT: i32 = 70;
|
||||
def TK_LE: i32 = 71;
|
||||
def TK_GT: i32 = 72;
|
||||
def TK_GE: i32 = 73;
|
||||
|
||||
def TK_AND: i32 = 74;
|
||||
def TK_OR: i32 = 75;
|
||||
def TK_NOT: i32 = 76;
|
||||
|
||||
def TK_LARROW: i32 = 77;
|
||||
def TK_ARROW: i32 = 78;
|
||||
def TK_FATARROW: i32 = 79;
|
||||
|
||||
def TK_LAST: i32 = 80;
|
||||
|
||||
// ---- 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: i32,
|
||||
file: str, // path of the source the token came from
|
||||
line: i32,
|
||||
col: i32,
|
||||
text: str, // arena-owned token text (TK_IDENT, TK_STR, TK_ERR)
|
||||
uval: u64, // TK_INT, TK_RUNE
|
||||
fval: f64, // TK_FLOAT
|
||||
tsuffix: str, // typed numeric literal suffix or empty
|
||||
};
|
||||
|
||||
// ---- keyword lookup ---------------------------------------------------
|
||||
|
||||
fn streqn(a: *u8, b: str, n: i32) bool = {
|
||||
if (b.len != n) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
// kwlookup — returns the matching TK_* keyword kind for a byte run,
|
||||
// or TK_NONE if it's an ordinary identifier. Linear search over a
|
||||
// small alphabetised list, matching cmd/wcc/tok.c.
|
||||
export fn kwlookup(p: *u8, n: i32) i32 = {
|
||||
if (streqn(p, "as", n)) { return TK_AS; };
|
||||
if (streqn(p, "break", n)) { return TK_BREAK; };
|
||||
if (streqn(p, "case", n)) { return TK_CASE; };
|
||||
if (streqn(p, "chan", n)) { return TK_CHAN; };
|
||||
if (streqn(p, "continue", n)) { return TK_CONTINUE; };
|
||||
if (streqn(p, "def", n)) { return TK_DEF; };
|
||||
if (streqn(p, "defer", n)) { return TK_DEFER; };
|
||||
if (streqn(p, "else", n)) { return TK_ELSE; };
|
||||
if (streqn(p, "export", n)) { return TK_EXPORT; };
|
||||
if (streqn(p, "false", n)) { return TK_FALSE; };
|
||||
if (streqn(p, "fn", n)) { return TK_FN; };
|
||||
if (streqn(p, "for", n)) { return TK_FOR; };
|
||||
if (streqn(p, "if", n)) { return TK_IF; };
|
||||
if (streqn(p, "let", n)) { return TK_LET; };
|
||||
if (streqn(p, "match", n)) { return TK_MATCH; };
|
||||
if (streqn(p, "nil", n)) { return TK_NIL; };
|
||||
if (streqn(p, "proc", n)) { return TK_PROC; };
|
||||
if (streqn(p, "return", n)) { return TK_RETURN; };
|
||||
if (streqn(p, "static", n)) { return TK_STATIC; };
|
||||
if (streqn(p, "struct", n)) { return TK_STRUCT; };
|
||||
if (streqn(p, "switch", n)) { return TK_SWITCH; };
|
||||
if (streqn(p, "true", n)) { return TK_TRUE; };
|
||||
if (streqn(p, "type", n)) { return TK_TYPE; };
|
||||
if (streqn(p, "use", n)) { return TK_USE; };
|
||||
return 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: i32) str = {
|
||||
if (k == TK_NONE) { return "<none>"; };
|
||||
if (k == TK_EOF) { return "EOF"; };
|
||||
if (k == TK_ERR) { return "ERR"; };
|
||||
if (k == TK_IDENT) { return "IDENT"; };
|
||||
if (k == TK_INT) { return "INT"; };
|
||||
if (k == TK_FLOAT) { return "FLOAT"; };
|
||||
if (k == TK_RUNE) { return "RUNE"; };
|
||||
if (k == TK_STR) { return "STR"; };
|
||||
|
||||
if (k == TK_FN) { return "fn"; };
|
||||
if (k == TK_LET) { return "let"; };
|
||||
if (k == TK_DEF) { return "def"; };
|
||||
if (k == TK_IF) { return "if"; };
|
||||
if (k == TK_ELSE) { return "else"; };
|
||||
if (k == TK_FOR) { return "for"; };
|
||||
if (k == TK_SWITCH) { return "switch"; };
|
||||
if (k == TK_CASE) { return "case"; };
|
||||
if (k == TK_RETURN) { return "return"; };
|
||||
if (k == TK_USE) { return "use"; };
|
||||
if (k == TK_TYPE) { return "type"; };
|
||||
if (k == TK_STRUCT) { return "struct"; };
|
||||
if (k == TK_DEFER) { return "defer"; };
|
||||
if (k == TK_BREAK) { return "break"; };
|
||||
if (k == TK_CONTINUE) { return "continue"; };
|
||||
if (k == TK_EXPORT) { return "export"; };
|
||||
if (k == TK_PROC) { return "proc"; };
|
||||
if (k == TK_CHAN) { return "chan"; };
|
||||
if (k == TK_NIL) { return "nil"; };
|
||||
if (k == TK_TRUE) { return "true"; };
|
||||
if (k == TK_FALSE) { return "false"; };
|
||||
if (k == TK_AS) { return "as"; };
|
||||
if (k == TK_STATIC) { return "static"; };
|
||||
if (k == TK_MATCH) { return "match"; };
|
||||
|
||||
if (k == TK_LPAREN) { return "("; };
|
||||
if (k == TK_RPAREN) { return ")"; };
|
||||
if (k == TK_LBRACE) { return "{"; };
|
||||
if (k == TK_RBRACE) { return "}"; };
|
||||
if (k == TK_LBRACK) { return "["; };
|
||||
if (k == TK_RBRACK) { return "]"; };
|
||||
if (k == TK_COMMA) { return ","; };
|
||||
if (k == TK_SEMI) { return ";"; };
|
||||
if (k == TK_COLON) { return ":"; };
|
||||
if (k == TK_DOT) { return "."; };
|
||||
if (k == TK_ELLIPSIS) { return "..."; };
|
||||
if (k == TK_DOTDOT) { return ".."; };
|
||||
if (k == TK_AT) { return "@"; };
|
||||
if (k == TK_QUESTION) { return "?"; };
|
||||
|
||||
if (k == TK_ASSIGN) { return "="; };
|
||||
if (k == TK_PLUSEQ) { return "+="; };
|
||||
if (k == TK_MINUSEQ) { return "-="; };
|
||||
if (k == TK_STAREQ) { return "*="; };
|
||||
if (k == TK_SLASHEQ) { return "/="; };
|
||||
if (k == TK_PERCENTEQ) { return "%="; };
|
||||
if (k == TK_AMPEQ) { return "&="; };
|
||||
if (k == TK_PIPEEQ) { return "|="; };
|
||||
if (k == TK_CARETEQ) { return "^="; };
|
||||
if (k == TK_LSHIFTEQ) { return "<<="; };
|
||||
if (k == TK_RSHIFTEQ) { return ">>="; };
|
||||
|
||||
if (k == TK_PLUS) { return "+"; };
|
||||
if (k == TK_MINUS) { return "-"; };
|
||||
if (k == TK_STAR) { return "*"; };
|
||||
if (k == TK_SLASH) { return "/"; };
|
||||
if (k == TK_PERCENT) { return "%"; };
|
||||
if (k == TK_AMP) { return "&"; };
|
||||
if (k == TK_PIPE) { return "|"; };
|
||||
if (k == TK_CARET) { return "^"; };
|
||||
if (k == TK_TILDE) { return "~"; };
|
||||
if (k == TK_LSHIFT) { return "<<"; };
|
||||
if (k == TK_RSHIFT) { return ">>"; };
|
||||
|
||||
if (k == TK_EQ) { return "=="; };
|
||||
if (k == TK_NEQ) { return "!="; };
|
||||
if (k == TK_LT) { return "<"; };
|
||||
if (k == TK_LE) { return "<="; };
|
||||
if (k == TK_GT) { return ">"; };
|
||||
if (k == TK_GE) { return ">="; };
|
||||
|
||||
if (k == TK_AND) { return "&&"; };
|
||||
if (k == TK_OR) { return "||"; };
|
||||
if (k == TK_NOT) { return "!"; };
|
||||
|
||||
if (k == TK_LARROW) { return "<-"; };
|
||||
if (k == TK_ARROW) { return "->"; };
|
||||
if (k == TK_FATARROW) { return "=>"; };
|
||||
|
||||
if (k == 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] = 92u8; // '\\'
|
||||
out[1] = 120u8; // '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, 34u8); // '"'
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
let c: u8 = p[i];
|
||||
if (c == 92u8) { // '\\'
|
||||
fputsstr(fd, "\\\\");
|
||||
} else {
|
||||
if (c == 34u8) { // '"'
|
||||
fputsstr(fd, "\\\"");
|
||||
} else {
|
||||
if (c == 10u8) { // '\n'
|
||||
fputsstr(fd, "\\n");
|
||||
} else {
|
||||
if (c == 9u8) { // '\t'
|
||||
fputsstr(fd, "\\t");
|
||||
} else {
|
||||
if (c == 13u8) { // '\r'
|
||||
fputsstr(fd, "\\r");
|
||||
} else {
|
||||
if (c < 32u8) {
|
||||
fputhex2(fd, c);
|
||||
} else {
|
||||
if (c == 127u8) {
|
||||
fputhex2(fd, c);
|
||||
} else {
|
||||
fputcbyte(fd, c);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
fputcbyte(fd, 34u8);
|
||||
};
|
||||
|
||||
// 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, 58u8); // ':'
|
||||
let buf: [32]u8;
|
||||
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
fputcbyte(fd, 58u8);
|
||||
n = strconv.i64toa(buf[0:32], t.col: i64);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
fputcbyte(fd, 32u8); // ' '
|
||||
fputsstr(fd, tokname(t.kind));
|
||||
|
||||
if (t.kind == TK_IDENT) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_STR) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_ERR) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_INT) {
|
||||
fputcbyte(fd, 32u8);
|
||||
n = strconv.u64toa(buf[0:32], t.uval);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
} else { if (t.kind == TK_RUNE) {
|
||||
fputcbyte(fd, 32u8);
|
||||
n = strconv.u64toa(buf[0:32], t.uval);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
};};};};};
|
||||
// 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, 10u8); // '\n'
|
||||
};
|
||||
@@ -1,329 +0,0 @@
|
||||
// selfhost/cmd/wcc/type.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 arena.
|
||||
|
||||
use os;
|
||||
use mem;
|
||||
|
||||
// ---- 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.
|
||||
|
||||
def TY_NONE: i32 = 0;
|
||||
def TY_VOID: i32 = 1;
|
||||
def TY_BOOL: i32 = 2;
|
||||
def TY_RUNE: i32 = 3;
|
||||
def TY_I8: i32 = 4;
|
||||
def TY_I16: i32 = 5;
|
||||
def TY_I32: i32 = 6;
|
||||
def TY_I64: i32 = 7;
|
||||
def TY_U8: i32 = 8;
|
||||
def TY_U16: i32 = 9;
|
||||
def TY_U32: i32 = 10;
|
||||
def TY_U64: i32 = 11;
|
||||
def TY_UINT: i32 = 12;
|
||||
def TY_INT: i32 = 13;
|
||||
def TY_UINTPTR: i32 = 14;
|
||||
def TY_F32: i32 = 15;
|
||||
def TY_F64: i32 = 16;
|
||||
def TY_STR: i32 = 17;
|
||||
def TY_PTR: i32 = 18;
|
||||
def TY_SLICE: i32 = 19;
|
||||
def TY_ARRAY: i32 = 20;
|
||||
def TY_STRUCT: i32 = 21;
|
||||
def TY_FN: i32 = 22;
|
||||
def TY_CHAN: i32 = 23;
|
||||
def TY_NAMED: i32 = 24;
|
||||
def TY_TUPLE: i32 = 25;
|
||||
def TY_TAGGED: i32 = 26;
|
||||
def TY_ERR: i32 = 27;
|
||||
def TY_UNTYPED_INT: i32 = 28;
|
||||
def TY_UNTYPED_FLOAT: i32 = 29;
|
||||
def TY_UNTYPED_STR: i32 = 30;
|
||||
def TY_UNTYPED_RUNE: i32 = 31;
|
||||
def TY_UNTYPED_BOOL: i32 = 32;
|
||||
def TY_UNTYPED_NIL: i32 = 33;
|
||||
|
||||
// ---- tinfo / tfield / tparam -----------------------------------------
|
||||
|
||||
type tfield = struct {
|
||||
name: str,
|
||||
type_: *tinfo,
|
||||
offset: u64,
|
||||
tnext: *tfield,
|
||||
};
|
||||
|
||||
type tparam = struct {
|
||||
name: str,
|
||||
type_: *tinfo,
|
||||
tnext: *tparam,
|
||||
};
|
||||
|
||||
type tinfo = struct {
|
||||
kind: i32,
|
||||
size: u64,
|
||||
align: u64,
|
||||
sub: *tinfo, // ptr/slice/array/chan element
|
||||
alen: u64,
|
||||
fields: *tfield,
|
||||
params: *tparam,
|
||||
ret: *tinfo,
|
||||
variadic: i32,
|
||||
name: str,
|
||||
under: *tinfo,
|
||||
};
|
||||
|
||||
// ---- tctx — the box of primitive types -------------------------------
|
||||
|
||||
type tctx = struct {
|
||||
a: *arena,
|
||||
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,
|
||||
tyf32: *tinfo,
|
||||
tyf64: *tinfo,
|
||||
tystr: *tinfo,
|
||||
tyerr: *tinfo,
|
||||
tyuntypedint: *tinfo,
|
||||
tyuntypedfloat: *tinfo,
|
||||
tyuntypedstr: *tinfo,
|
||||
tyuntypedrune: *tinfo,
|
||||
tyuntypedbool: *tinfo,
|
||||
tyuntypednil: *tinfo,
|
||||
};
|
||||
|
||||
// ---- constructors -----------------------------------------------------
|
||||
|
||||
export fn newtype(a: *arena, k: i32) *tinfo = {
|
||||
let t: *tinfo = amalloc(a, 96u64): *tinfo;
|
||||
t.kind = k;
|
||||
return t;
|
||||
};
|
||||
|
||||
fn prim(a: *arena, k: i32, nm: str, sz: u64, al: u64) *tinfo = {
|
||||
let t: *tinfo = newtype(a, k);
|
||||
t.name = nm;
|
||||
t.size = sz;
|
||||
if (al > 0u64) { t.align = al; } else { t.align = sz; };
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typesinit(c: *tctx, a: *arena) void = {
|
||||
c.a = a;
|
||||
c.tyvoid = prim(a, TY_VOID, "void", 0u64, 1u64);
|
||||
c.tybool = prim(a, TY_BOOL, "bool", 1u64, 1u64);
|
||||
c.tyrune = prim(a, TY_RUNE, "rune", 4u64, 4u64);
|
||||
c.tyi8 = prim(a, TY_I8, "i8", 1u64, 1u64);
|
||||
c.tyi16 = prim(a, TY_I16, "i16", 2u64, 2u64);
|
||||
c.tyi32 = prim(a, TY_I32, "i32", 4u64, 4u64);
|
||||
c.tyi64 = prim(a, TY_I64, "i64", 8u64, 8u64);
|
||||
c.tyu8 = prim(a, TY_U8, "u8", 1u64, 1u64);
|
||||
c.tyu16 = prim(a, TY_U16, "u16", 2u64, 2u64);
|
||||
c.tyu32 = prim(a, TY_U32, "u32", 4u64, 4u64);
|
||||
c.tyu64 = prim(a, TY_U64, "u64", 8u64, 8u64);
|
||||
c.tyint = prim(a, TY_INT, "int", 8u64, 8u64);
|
||||
c.tyuint = prim(a, TY_UINT, "uint", 8u64, 8u64);
|
||||
c.tyuintptr= prim(a, TY_UINTPTR, "uintptr", 8u64, 8u64);
|
||||
c.tyf32 = prim(a, TY_F32, "f32", 4u64, 4u64);
|
||||
c.tyf64 = prim(a, TY_F64, "f64", 8u64, 8u64);
|
||||
c.tystr = prim(a, TY_STR, "str", 16u64, 8u64);
|
||||
c.tyerr = prim(a, TY_ERR, "<err>", 0u64, 1u64);
|
||||
|
||||
c.tyuntypedint = prim(a, TY_UNTYPED_INT, "untyped_int", 0u64, 1u64);
|
||||
c.tyuntypedfloat = prim(a, TY_UNTYPED_FLOAT, "untyped_float", 0u64, 1u64);
|
||||
c.tyuntypedstr = prim(a, TY_UNTYPED_STR, "untyped_str", 0u64, 1u64);
|
||||
c.tyuntypedrune = prim(a, TY_UNTYPED_RUNE, "untyped_rune", 0u64, 1u64);
|
||||
c.tyuntypedbool = prim(a, TY_UNTYPED_BOOL, "untyped_bool", 0u64, 1u64);
|
||||
c.tyuntypednil = prim(a, TY_UNTYPED_NIL, "untyped_nil", 0u64, 1u64);
|
||||
};
|
||||
|
||||
export fn typeptr(a: *arena, sub: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(a, TY_PTR);
|
||||
t.sub = sub;
|
||||
t.size = 8u64;
|
||||
t.align = 8u64;
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typeslice(a: *arena, sub: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(a, TY_SLICE);
|
||||
t.sub = sub;
|
||||
t.size = 24u64;
|
||||
t.align = 8u64;
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typearray(a: *arena, sub: *tinfo, n: u64) *tinfo = {
|
||||
let t: *tinfo = newtype(a, TY_ARRAY);
|
||||
t.sub = sub;
|
||||
t.alen = n;
|
||||
if (sub != nil) {
|
||||
t.size = sub.size * n;
|
||||
t.align = sub.align;
|
||||
} else {
|
||||
t.align = 1u64;
|
||||
};
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typechan(a: *arena, sub: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(a, TY_CHAN);
|
||||
t.sub = sub;
|
||||
t.size = 8u64;
|
||||
t.align = 8u64;
|
||||
return t;
|
||||
};
|
||||
|
||||
export fn typenamed(a: *arena, name: str, under: *tinfo) *tinfo = {
|
||||
let t: *tinfo = newtype(a, TY_NAMED);
|
||||
t.name = name;
|
||||
t.under = under;
|
||||
if (under != nil) {
|
||||
t.size = under.size;
|
||||
t.align = under.align;
|
||||
};
|
||||
return t;
|
||||
};
|
||||
|
||||
// ---- predicates -------------------------------------------------------
|
||||
|
||||
export fn typeisint(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: i32 = t.kind;
|
||||
if (k == TY_I8) { return true; };
|
||||
if (k == TY_I16) { return true; };
|
||||
if (k == TY_I32) { return true; };
|
||||
if (k == TY_I64) { return true; };
|
||||
if (k == TY_U8) { return true; };
|
||||
if (k == TY_U16) { return true; };
|
||||
if (k == TY_U32) { return true; };
|
||||
if (k == TY_U64) { return true; };
|
||||
if (k == TY_INT) { return true; };
|
||||
if (k == TY_UINT){ return true; };
|
||||
if (k == TY_UINTPTR) { return true; };
|
||||
if (k == TY_RUNE){ return true; };
|
||||
if (k == TY_UNTYPED_INT) { return true; };
|
||||
if (k == TY_UNTYPED_RUNE) { return true; };
|
||||
if (k == TY_NAMED) { return typeisint(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
export fn typeisfloat(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: i32 = t.kind;
|
||||
if (k == TY_F32) { return true; };
|
||||
if (k == TY_F64) { return true; };
|
||||
if (k == TY_UNTYPED_FLOAT) { return true; };
|
||||
if (k == TY_NAMED) { return typeisfloat(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
export fn typeisnum(t: *tinfo) bool = {
|
||||
if (typeisint(t)) { return true; };
|
||||
return typeisfloat(t);
|
||||
};
|
||||
|
||||
export fn typeisunsigned(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: i32 = t.kind;
|
||||
if (k == TY_U8) { return true; };
|
||||
if (k == TY_U16) { return true; };
|
||||
if (k == TY_U32) { return true; };
|
||||
if (k == TY_U64) { return true; };
|
||||
if (k == TY_UINT){ return true; };
|
||||
if (k == TY_UINTPTR) { return true; };
|
||||
if (k == TY_NAMED) { return typeisunsigned(t.under); };
|
||||
return false;
|
||||
};
|
||||
|
||||
export fn typeisuntyped(t: *tinfo) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: i32 = t.kind;
|
||||
if (k == TY_UNTYPED_INT) { return true; };
|
||||
if (k == TY_UNTYPED_FLOAT) { return true; };
|
||||
if (k == TY_UNTYPED_STR) { return true; };
|
||||
if (k == TY_UNTYPED_RUNE) { return true; };
|
||||
if (k == TY_UNTYPED_BOOL) { return true; };
|
||||
if (k == 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: i32 = a.kind;
|
||||
if (k == TY_PTR) { return typeeq(a.sub, b.sub); };
|
||||
if (k == TY_SLICE) { return typeeq(a.sub, b.sub); };
|
||||
if (k == TY_CHAN) { return typeeq(a.sub, b.sub); };
|
||||
if (k == TY_ARRAY) {
|
||||
if (a.alen != b.alen) { return false; };
|
||||
return typeeq(a.sub, b.sub);
|
||||
};
|
||||
if (k == 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 == 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 == TY_NAMED) { return false; }; // nominal: only same ptr
|
||||
if (k == 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
|
||||
};
|
||||
@@ -656,7 +656,7 @@ fn scanuse(src: *u8, len: u64) (*u8, u64) = {
|
||||
// Recursively expand `path` into c.out. Imported files are emitted
|
||||
// before their importer; cycles are broken via the visited set.
|
||||
// modulename — pick the source's containing-directory basename. So
|
||||
// `lib/os/os.ww` → "os"; `selfhost/cmd/wcc/sym.ww` → "wcc". Falls back
|
||||
// `lib/os/os.ww` → "os"; `lib/ww/sym.ww` → "ww". Falls back
|
||||
// to the file's own basename (sans .ww) when there is no parent dir.
|
||||
// Returns ("",0) if `path` ends in a '/' (degenerate).
|
||||
fn modulename(path: *u8, plen: u64) (*u8, u64) = {
|
||||
|
||||
@@ -337,7 +337,7 @@ fn scanuse(src: *u8, len: u64) (*u8, u64) = {
|
||||
// Recursively expand `path` into c.out. Imported files are emitted
|
||||
// before their importer; cycles are broken via the visited set.
|
||||
// modulename — pick the source's containing-directory basename. So
|
||||
// `lib/os/os.ww` → "os"; `selfhost/cmd/wcc/sym.ww` → "wcc". Falls back
|
||||
// `lib/os/os.ww` → "os"; `lib/ww/sym.ww` → "ww". Falls back
|
||||
// to the file's own basename (sans .ww) when there is no parent dir.
|
||||
// Returns ("",0) if `path` ends in a '/' (degenerate).
|
||||
fn modulename(path: *u8, plen: u64) (*u8, u64) = {
|
||||
|
||||
@@ -437,8 +437,8 @@ export fn parseu64(s: str) (u64 | str) = {
|
||||
return v;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -927,8 +927,8 @@ export fn toupper(c: u8) u8 = {
|
||||
return c;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/lex.ww — port of cmd/wcc/lex.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -1616,8 +1616,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
|
||||
out.text = astrndup(l.a, one.ptr, 1u64);
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/ast.ww — port of cmd/wcc/ast.c (Node defs + printer).
|
||||
// MODULE: ww
|
||||
// lib/ww/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.
|
||||
@@ -1954,8 +1954,8 @@ export fn astprint(fd: i32, n: *node) void = {
|
||||
pr(fd, n, 0);
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/parse.ww — port of cmd/wcc/parse.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/parse.ww — port of cmd/wcc/parse.c.
|
||||
//
|
||||
// Status: GROWING stub. Currently handles top-level `use IDENT;`,
|
||||
// `def NAME: TYPE = LIT;`, `type NAME = TYPE;`, and `fn NAME(params)
|
||||
@@ -2942,8 +2942,8 @@ export fn parsefile(p: *parser) *node = {
|
||||
return f;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/type.ww — port of cmd/wcc/type.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
@@ -3273,8 +3273,8 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true; // primitives match by kind alone
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/sym.ww — port of cmd/wcc/sym.c.
|
||||
// MODULE: ww
|
||||
// lib/ww/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
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// `type NAME = TYPE;` (alias + struct), top-level `let NAME: TYPE = LIT;`.
|
||||
//
|
||||
// Function declarations are still recovered past — the body parser
|
||||
// is the next major chunk. See selfhost/cmd/wcc/parse.ww header.
|
||||
// is the next major chunk. See lib/ww/parse.ww header.
|
||||
|
||||
use os;
|
||||
use mem;
|
||||
|
||||
Reference in New Issue
Block a user