ww+wcc: graduate selfhost TK_* defs to tkind enum

`type tkind = enum i32 { TK_NONE = 0, TK_EOF = 1, ... TK_LAST = 86 }`
replaces the 87-line `def TK_*: i32 = N` cluster in lib/ww/lex/tok.ww.
Numeric values explicit so 990_selfhost's byte-diff against the C-side
`Tkind` enum still passes.

All ~270 reference sites in lib/ww and selfhost/cmd/{wcc,wwdump}
sed-renamed `TK_X` → `tkind.TK_X`. Struct fields (`tok.kind`,
`parser.curkind`) intentionally kept as `i32` — making them `tkind`
shifted some byte-positions in the cgen output and broke 990/993/995
byte-identity probes without an obvious win.

To make the rename non-cascading on every signature, type_assignable
and unify_arith in cmd/wcc/check+type relax to allow enum ↔ int
mixing when storage matches (a `tkind` value flows into an `i32`
slot and vice versa, no explicit cast). This deviates from Hare's
strict enum semantics; doc'd as an explicit pragmatic relaxation
for the compiler's internal enum-shaped kinds. External user code
can still get the type-safety benefit if they declare their
parameters with the enum type.

combined.ww files regenerated by ww build.
This commit is contained in:
2026-05-12 04:50:36 +09:00
parent fc49da44d8
commit 408ea2a322
14 changed files with 1806 additions and 1771 deletions

View File

@@ -316,7 +316,7 @@ fn scanexp(l: *lex) void = {
};
fn lexnum(l: *lex, start: *pos, out: *tok) void = {
out.kind = TK_INT;
out.kind = tkind.TK_INT;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -375,9 +375,9 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
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
// care about are float-free; any tkind.TK_FLOAT seen in source
// gets a placeholder value until we wire a real parser.
out.kind = TK_FLOAT;
out.kind = tkind.TK_FLOAT;
} else {
let digs: *u8 = l.src + begin;
let dn: u64 = n;
@@ -389,7 +389,7 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
out.uval = parseint(digs, dn, base, &ok);
if (!ok) {
errat(l, start, "bad integer literal");
out.kind = TK_ERR;
out.kind = tkind.TK_ERR;
};
};
@@ -455,16 +455,16 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
// Bare '_' is the discard marker. `_x`, `_1` are normal idents.
if (n == 1u64) {
if (p[0] == 95u8) {
out.kind = TK_UNDER;
out.kind = tkind.TK_UNDER;
out.text = astrndup(l.a, p, n);
return;
};
};
let k: i32 = kwlookup(p, n: i32);
if (k != TK_NONE) {
if (k != tkind.TK_NONE) {
out.kind = k;
} else {
out.kind = TK_IDENT;
out.kind = tkind.TK_IDENT;
};
out.text = astrndup(l.a, p, n);
};
@@ -477,7 +477,7 @@ fn lexstr(l: *lex, start: *pos, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c < 0) {
errat(l, start, "unterminated string");
out.kind = TK_ERR;
out.kind = tkind.TK_ERR;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -508,7 +508,7 @@ fn lexstr(l: *lex, start: *pos, out: *tok) void = {
buf[nbi] = ch: u8;
nb += 1u64;
};
out.kind = TK_STR;
out.kind = tkind.TK_STR;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -522,7 +522,7 @@ 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.kind = tkind.TK_ERR;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -538,7 +538,7 @@ fn lexrune(l: *lex, start: *pos, out: *tok) void = {
};
if (lpeek(l, 0u64) != 39) {
errat(l, start, "rune literal missing closing '");
out.kind = TK_ERR;
out.kind = tkind.TK_ERR;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -546,7 +546,7 @@ fn lexrune(l: *lex, start: *pos, out: *tok) void = {
return;
};
lget(l);
out.kind = TK_RUNE;
out.kind = tkind.TK_RUNE;
out.file = start.file;
out.line = start.line;
out.col = start.col;
@@ -572,7 +572,7 @@ 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.kind = tkind.TK_NONE;
out.uval = 0u64;
// out.fval starts cleared by the caller's stack-local init (lex.ww
// allocates the tok with `let t: tok;` which zeroes). We avoid
@@ -586,7 +586,7 @@ export fn lexnext(l: *lex, out: *tok) void = {
if (!skipws(l)) {
let p: pos; curpos(l, &p);
emitsimple(&p, TK_EOF, out);
emitsimple(&p, tkind.TK_EOF, out);
return;
};
let start: pos; curpos(l, &start);
@@ -602,97 +602,97 @@ export fn lexnext(l: *lex, out: *tok) void = {
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 == 40) { emitsimple(&start, tkind.TK_LPAREN, out); return; };
if (c == 41) { emitsimple(&start, tkind.TK_RPAREN, out); return; };
if (c == 123) { emitsimple(&start, tkind.TK_LBRACE, out); return; };
if (c == 125) { emitsimple(&start, tkind.TK_RBRACE, out); return; };
if (c == 91) { emitsimple(&start, tkind.TK_LBRACK, out); return; };
if (c == 93) { emitsimple(&start, tkind.TK_RBRACK, out); return; };
if (c == 44) { emitsimple(&start, tkind.TK_COMMA, out); return; };
if (c == 59) { emitsimple(&start, tkind.TK_SEMI, out); return; };
if (c == 58) { emitsimple(&start, tkind.TK_COLON, out); return; };
if (c == 64) { emitsimple(&start, tkind.TK_AT, out); return; };
if (c == 63) { emitsimple(&start, tkind.TK_QUESTION, out); return; };
if (c == 126) { emitsimple(&start, tkind.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;
emitsimple(&start, tkind.TK_ELLIPSIS, out); return;
};
lget(l);
emitsimple(&start, TK_DOTDOT, out); return;
emitsimple(&start, tkind.TK_DOTDOT, out); return;
};
emitsimple(&start, TK_DOT, out); return;
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_PLUSEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_MINUSEQ, out); return; };
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, tkind.TK_ARROW, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_STAREQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_SLASHEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_PERCENTEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 38) { lget(l); emitsimple(&start, tkind.TK_AND, out); return; };
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_AMPEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 124) { lget(l); emitsimple(&start, tkind.TK_OR, out); return; };
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_PIPEEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_CARETEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_EQ, out); return; };
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, tkind.TK_FATARROW, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_NEQ, out); return; };
emitsimple(&start, tkind.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, tkind.TK_LSHIFTEQ, out); return; };
emitsimple(&start, tkind.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 (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_LE, out); return; };
if (lpeek(l, 0u64) == 45) { lget(l); emitsimple(&start, tkind.TK_LARROW, out); return; };
emitsimple(&start, tkind.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, tkind.TK_RSHIFTEQ, out); return; };
emitsimple(&start, tkind.TK_RSHIFT, out); return;
};
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_GE, out); return; };
emitsimple(&start, TK_GT, out); return;
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, tkind.TK_GE, out); return; };
emitsimple(&start, tkind.TK_GT, out); return;
};
errat(l, &start, "unexpected character");
out.kind = TK_ERR;
out.kind = tkind.TK_ERR;
setposfrom(out, &start);
let one: [1]u8;
one[0] = c: u8;

View File

@@ -12,108 +12,110 @@
use os;
use strconv;
// ---- Tkind ------------------------------------------------------------
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
// ---- tkind ------------------------------------------------------------
// Mirror of the C `Tkind` enum in cmd/wcc/ww.h. Numeric values are
// explicit and must stay in sync — the 990_selfhost test diffs wwdump
// output against the C side, byte for byte.
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;
type tkind = enum i32 {
TK_NONE = 0,
TK_EOF = 1,
TK_ERR = 2,
TK_IDENT = 3,
TK_INT = 4,
TK_FLOAT = 5,
TK_RUNE = 6,
TK_STR = 7,
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_CONST: i32 = 32;
def TK_UNDER: i32 = 33;
TK_FN = 8,
TK_LET = 9,
TK_DEF = 10,
TK_IF = 11,
TK_ELSE = 12,
TK_FOR = 13,
TK_SWITCH = 14,
TK_CASE = 15,
TK_RETURN = 16,
TK_USE = 17,
TK_TYPE = 18,
TK_STRUCT = 19,
TK_DEFER = 20,
TK_BREAK = 21,
TK_CONTINUE = 22,
TK_EXPORT = 23,
TK_PROC = 24,
TK_CHAN = 25,
TK_NIL = 26,
TK_TRUE = 27,
TK_FALSE = 28,
TK_AS = 29,
TK_STATIC = 30,
TK_MATCH = 31,
TK_CONST = 32,
TK_UNDER = 33,
def TK_LPAREN: i32 = 34;
def TK_RPAREN: i32 = 35;
def TK_LBRACE: i32 = 36;
def TK_RBRACE: i32 = 37;
def TK_LBRACK: i32 = 38;
def TK_RBRACK: i32 = 39;
def TK_COMMA: i32 = 40;
def TK_SEMI: i32 = 41;
def TK_COLON: i32 = 42;
def TK_DOT: i32 = 43;
def TK_ELLIPSIS: i32 = 44;
def TK_DOTDOT: i32 = 45;
def TK_AT: i32 = 46;
def TK_QUESTION: i32 = 47;
TK_LPAREN = 34,
TK_RPAREN = 35,
TK_LBRACE = 36,
TK_RBRACE = 37,
TK_LBRACK = 38,
TK_RBRACK = 39,
TK_COMMA = 40,
TK_SEMI = 41,
TK_COLON = 42,
TK_DOT = 43,
TK_ELLIPSIS = 44,
TK_DOTDOT = 45,
TK_AT = 46,
TK_QUESTION = 47,
def TK_ASSIGN: i32 = 48;
def TK_PLUSEQ: i32 = 49;
def TK_MINUSEQ: i32 = 50;
def TK_STAREQ: i32 = 51;
def TK_SLASHEQ: i32 = 52;
def TK_PERCENTEQ: i32 = 53;
def TK_AMPEQ: i32 = 54;
def TK_PIPEEQ: i32 = 55;
def TK_CARETEQ: i32 = 56;
def TK_LSHIFTEQ: i32 = 57;
def TK_RSHIFTEQ: i32 = 58;
TK_ASSIGN = 48,
TK_PLUSEQ = 49,
TK_MINUSEQ = 50,
TK_STAREQ = 51,
TK_SLASHEQ = 52,
TK_PERCENTEQ = 53,
TK_AMPEQ = 54,
TK_PIPEEQ = 55,
TK_CARETEQ = 56,
TK_LSHIFTEQ = 57,
TK_RSHIFTEQ = 58,
def TK_PLUS: i32 = 59;
def TK_MINUS: i32 = 60;
def TK_STAR: i32 = 61;
def TK_SLASH: i32 = 62;
def TK_PERCENT: i32 = 63;
def TK_AMP: i32 = 64;
def TK_PIPE: i32 = 65;
def TK_CARET: i32 = 66;
def TK_TILDE: i32 = 67;
def TK_LSHIFT: i32 = 68;
def TK_RSHIFT: i32 = 69;
TK_PLUS = 59,
TK_MINUS = 60,
TK_STAR = 61,
TK_SLASH = 62,
TK_PERCENT = 63,
TK_AMP = 64,
TK_PIPE = 65,
TK_CARET = 66,
TK_TILDE = 67,
TK_LSHIFT = 68,
TK_RSHIFT = 69,
def TK_EQ: i32 = 70;
def TK_NEQ: i32 = 71;
def TK_LT: i32 = 72;
def TK_LE: i32 = 73;
def TK_GT: i32 = 74;
def TK_GE: i32 = 75;
TK_EQ = 70,
TK_NEQ = 71,
TK_LT = 72,
TK_LE = 73,
TK_GT = 74,
TK_GE = 75,
def TK_AND: i32 = 76;
def TK_OR: i32 = 77;
def TK_NOT: i32 = 78;
TK_AND = 76,
TK_OR = 77,
TK_NOT = 78,
def TK_LARROW: i32 = 79;
def TK_ARROW: i32 = 80;
def TK_FATARROW: i32 = 81;
TK_LARROW = 79,
TK_ARROW = 80,
TK_FATARROW = 81,
// Appended at the tail (not grouped with the keyword block) so every
// pre-existing TK_* value stays unchanged — the 990_selfhost test
// diffs wwdump output against the C side, byte for byte.
def TK_IS: i32 = 82;
def TK_VOID: i32 = 83;
def TK_YIELD: i32 = 84;
def TK_ENUM: i32 = 85;
def TK_LAST: i32 = 86;
// Tail-appended values — keeps every prior TK_* numeric value
// stable for the 990_selfhost byte-diff against the C side.
TK_IS = 82,
TK_VOID = 83,
TK_YIELD = 84,
TK_ENUM = 85,
TK_LAST = 86,
};
// ---- Pos / Tok --------------------------------------------------------
//
@@ -132,13 +134,14 @@ type pos = struct {
};
type tok = struct {
kind: i32,
kind: i32, // holds a `tkind` value; bare i32 so the cgen's
// fixed 8B/struct-field layout matches the C side
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
text: str, // arena-owned token text (tkind.TK_IDENT, tkind.TK_STR, tkind.TK_ERR)
uval: u64, // tkind.TK_INT, tkind.TK_RUNE
fval: f64, // tkind.TK_FLOAT
tsuffix: str, // typed numeric literal suffix or empty
};
@@ -155,39 +158,39 @@ fn streqn(a: *u8, b: str, n: i32) bool = {
};
// kwlookup — returns the matching TK_* keyword kind for a byte run,
// or TK_NONE if it's an ordinary identifier. Linear search over a
// or tkind.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, "const", n)) { return TK_CONST; };
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, "enum", n)) { return TK_ENUM; };
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, "is", n)) { return TK_IS; };
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; };
if (streqn(p, "void", n)) { return TK_VOID; };
if (streqn(p, "yield", n)) { return TK_YIELD; };
return TK_NONE;
if (streqn(p, "as", n)) { return tkind.TK_AS; };
if (streqn(p, "break", n)) { return tkind.TK_BREAK; };
if (streqn(p, "case", n)) { return tkind.TK_CASE; };
if (streqn(p, "chan", n)) { return tkind.TK_CHAN; };
if (streqn(p, "const", n)) { return tkind.TK_CONST; };
if (streqn(p, "continue", n)) { return tkind.TK_CONTINUE; };
if (streqn(p, "def", n)) { return tkind.TK_DEF; };
if (streqn(p, "defer", n)) { return tkind.TK_DEFER; };
if (streqn(p, "else", n)) { return tkind.TK_ELSE; };
if (streqn(p, "enum", n)) { return tkind.TK_ENUM; };
if (streqn(p, "export", n)) { return tkind.TK_EXPORT; };
if (streqn(p, "false", n)) { return tkind.TK_FALSE; };
if (streqn(p, "fn", n)) { return tkind.TK_FN; };
if (streqn(p, "for", n)) { return tkind.TK_FOR; };
if (streqn(p, "if", n)) { return tkind.TK_IF; };
if (streqn(p, "is", n)) { return tkind.TK_IS; };
if (streqn(p, "let", n)) { return tkind.TK_LET; };
if (streqn(p, "match", n)) { return tkind.TK_MATCH; };
if (streqn(p, "nil", n)) { return tkind.TK_NIL; };
if (streqn(p, "proc", n)) { return tkind.TK_PROC; };
if (streqn(p, "return", n)) { return tkind.TK_RETURN; };
if (streqn(p, "static", n)) { return tkind.TK_STATIC; };
if (streqn(p, "struct", n)) { return tkind.TK_STRUCT; };
if (streqn(p, "switch", n)) { return tkind.TK_SWITCH; };
if (streqn(p, "true", n)) { return tkind.TK_TRUE; };
if (streqn(p, "type", n)) { return tkind.TK_TYPE; };
if (streqn(p, "use", n)) { return tkind.TK_USE; };
if (streqn(p, "void", n)) { return tkind.TK_VOID; };
if (streqn(p, "yield", n)) { return tkind.TK_YIELD; };
return tkind.TK_NONE;
};
// ---- tokname ----------------------------------------------------------
@@ -196,101 +199,101 @@ export fn kwlookup(p: *u8, n: i32) i32 = {
// 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 == tkind.TK_NONE) { return "<none>"; };
if (k == tkind.TK_EOF) { return "EOF"; };
if (k == tkind.TK_ERR) { return "ERR"; };
if (k == tkind.TK_IDENT) { return "IDENT"; };
if (k == tkind.TK_INT) { return "INT"; };
if (k == tkind.TK_FLOAT) { return "FLOAT"; };
if (k == tkind.TK_RUNE) { return "RUNE"; };
if (k == tkind.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_IS) { return "is"; };
if (k == TK_VOID) { return "void"; };
if (k == TK_YIELD) { return "yield"; };
if (k == TK_STATIC) { return "static"; };
if (k == TK_MATCH) { return "match"; };
if (k == TK_CONST) { return "const"; };
if (k == TK_UNDER) { return "_"; };
if (k == TK_ENUM) { return "enum"; };
if (k == tkind.TK_FN) { return "fn"; };
if (k == tkind.TK_LET) { return "let"; };
if (k == tkind.TK_DEF) { return "def"; };
if (k == tkind.TK_IF) { return "if"; };
if (k == tkind.TK_ELSE) { return "else"; };
if (k == tkind.TK_FOR) { return "for"; };
if (k == tkind.TK_SWITCH) { return "switch"; };
if (k == tkind.TK_CASE) { return "case"; };
if (k == tkind.TK_RETURN) { return "return"; };
if (k == tkind.TK_USE) { return "use"; };
if (k == tkind.TK_TYPE) { return "type"; };
if (k == tkind.TK_STRUCT) { return "struct"; };
if (k == tkind.TK_DEFER) { return "defer"; };
if (k == tkind.TK_BREAK) { return "break"; };
if (k == tkind.TK_CONTINUE) { return "continue"; };
if (k == tkind.TK_EXPORT) { return "export"; };
if (k == tkind.TK_PROC) { return "proc"; };
if (k == tkind.TK_CHAN) { return "chan"; };
if (k == tkind.TK_NIL) { return "nil"; };
if (k == tkind.TK_TRUE) { return "true"; };
if (k == tkind.TK_FALSE) { return "false"; };
if (k == tkind.TK_AS) { return "as"; };
if (k == tkind.TK_IS) { return "is"; };
if (k == tkind.TK_VOID) { return "void"; };
if (k == tkind.TK_YIELD) { return "yield"; };
if (k == tkind.TK_STATIC) { return "static"; };
if (k == tkind.TK_MATCH) { return "match"; };
if (k == tkind.TK_CONST) { return "const"; };
if (k == tkind.TK_UNDER) { return "_"; };
if (k == tkind.TK_ENUM) { return "enum"; };
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 == tkind.TK_LPAREN) { return "("; };
if (k == tkind.TK_RPAREN) { return ")"; };
if (k == tkind.TK_LBRACE) { return "{"; };
if (k == tkind.TK_RBRACE) { return "}"; };
if (k == tkind.TK_LBRACK) { return "["; };
if (k == tkind.TK_RBRACK) { return "]"; };
if (k == tkind.TK_COMMA) { return ","; };
if (k == tkind.TK_SEMI) { return ";"; };
if (k == tkind.TK_COLON) { return ":"; };
if (k == tkind.TK_DOT) { return "."; };
if (k == tkind.TK_ELLIPSIS) { return "..."; };
if (k == tkind.TK_DOTDOT) { return ".."; };
if (k == tkind.TK_AT) { return "@"; };
if (k == tkind.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 == tkind.TK_ASSIGN) { return "="; };
if (k == tkind.TK_PLUSEQ) { return "+="; };
if (k == tkind.TK_MINUSEQ) { return "-="; };
if (k == tkind.TK_STAREQ) { return "*="; };
if (k == tkind.TK_SLASHEQ) { return "/="; };
if (k == tkind.TK_PERCENTEQ) { return "%="; };
if (k == tkind.TK_AMPEQ) { return "&="; };
if (k == tkind.TK_PIPEEQ) { return "|="; };
if (k == tkind.TK_CARETEQ) { return "^="; };
if (k == tkind.TK_LSHIFTEQ) { return "<<="; };
if (k == tkind.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 == tkind.TK_PLUS) { return "+"; };
if (k == tkind.TK_MINUS) { return "-"; };
if (k == tkind.TK_STAR) { return "*"; };
if (k == tkind.TK_SLASH) { return "/"; };
if (k == tkind.TK_PERCENT) { return "%"; };
if (k == tkind.TK_AMP) { return "&"; };
if (k == tkind.TK_PIPE) { return "|"; };
if (k == tkind.TK_CARET) { return "^"; };
if (k == tkind.TK_TILDE) { return "~"; };
if (k == tkind.TK_LSHIFT) { return "<<"; };
if (k == tkind.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 == tkind.TK_EQ) { return "=="; };
if (k == tkind.TK_NEQ) { return "!="; };
if (k == tkind.TK_LT) { return "<"; };
if (k == tkind.TK_LE) { return "<="; };
if (k == tkind.TK_GT) { return ">"; };
if (k == tkind.TK_GE) { return ">="; };
if (k == TK_AND) { return "&&"; };
if (k == TK_OR) { return "||"; };
if (k == TK_NOT) { return "!"; };
if (k == tkind.TK_AND) { return "&&"; };
if (k == tkind.TK_OR) { return "||"; };
if (k == tkind.TK_NOT) { return "!"; };
if (k == TK_LARROW) { return "<-"; };
if (k == TK_ARROW) { return "->"; };
if (k == TK_FATARROW) { return "=>"; };
if (k == tkind.TK_LARROW) { return "<-"; };
if (k == tkind.TK_ARROW) { return "->"; };
if (k == tkind.TK_FATARROW) { return "=>"; };
if (k == TK_LAST) { return "<last>"; };
if (k == tkind.TK_LAST) { return "<last>"; };
return "<?>";
};
@@ -389,25 +392,25 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
if (t.kind == TK_IDENT) {
if (t.kind == tkind.TK_IDENT) {
fputcbyte(fd, 32u8);
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_STR) {
} else { if (t.kind == tkind.TK_STR) {
fputcbyte(fd, 32u8);
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_ERR) {
} else { if (t.kind == tkind.TK_ERR) {
fputcbyte(fd, 32u8);
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_INT) {
} else { if (t.kind == tkind.TK_INT) {
fputcbyte(fd, 32u8);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
} else { if (t.kind == TK_RUNE) {
} else { if (t.kind == tkind.TK_RUNE) {
fputcbyte(fd, 32u8);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
};};};};};
// TK_FLOAT is intentionally not handled here — %g formatting
// tkind.TK_FLOAT is intentionally not handled here — %g formatting
// won't byte-match across implementations. Diff fixtures must
// be float-free until we implement a stable float formatter.