diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index 0003d499..86d822fa 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -531,6 +531,17 @@ unify_arith(Checker *c, Pos p, Type *a, Type *b) if (type_isuntyped(a) && type_assignable(b, a)) return b; if (type_isuntyped(b) && type_assignable(a, b)) return a; if (type_eq(a, b)) return a; + /* Enum ↔ integer storage: pair `(tkind, i32)` operands unify to + * the storage int. Mirrors the same relaxation in type_assignable; + * lets `cur.kind == TK_FN` typecheck without a cast on either side. */ + { + Type *au = (a->kind == TY_NAMED) ? a->under : a; + Type *bu = (b->kind == TY_NAMED) ? b->under : b; + if (au && au->kind == TY_ENUM && type_isint(b) && + type_eq(au->sub, b)) return b; + if (bu && bu->kind == TY_ENUM && type_isint(a) && + type_eq(a, bu->sub)) return a; + } return err(c, p, "operands have differing types %s and %s", type_name(c->a, a), type_name(c->a, b)); } diff --git a/cmd/wcc/type.c b/cmd/wcc/type.c index 80a28fb1..b7312484 100644 --- a/cmd/wcc/type.c +++ b/cmd/wcc/type.c @@ -295,6 +295,21 @@ type_assignable(Type *dst, Type *src) if (dst->kind == TY_NAMED && type_eq(dst->under, src)) return 1; if (src->kind == TY_NAMED && type_eq(dst, src->under)) return 1; + /* Enum ↔ integer storage: bare i32 flows into a `tkind` slot and + * vice-versa as long as the storage type matches. Hare-strict + * would require an explicit cast, but the ww frontend's tkind / + * nkind enums have hundreds of `let k: i32 = expr` sites we'd + * otherwise have to migrate in lockstep — the relaxation is + * explicit and limited to int-typed enums. */ + { + Type *du = (dst->kind == TY_NAMED) ? dst->under : dst; + Type *su = (src->kind == TY_NAMED) ? src->under : src; + if (du && du->kind == TY_ENUM && type_isint(src) && + type_eq(du->sub, src)) return 1; + if (su && su->kind == TY_ENUM && type_isint(dst) && + type_eq(dst, su->sub)) return 1; + } + /* Tuple-to-tuple: element-wise assignable. */ if (dst->kind == TY_TUPLE && src->kind == TY_TUPLE) { Tparam *pa = dst->params, *pb = src->params; diff --git a/lib/ww/lex/lex.ww b/lib/ww/lex/lex.ww index 3614f9db..d819cf2a 100644 --- a/lib/ww/lex/lex.ww +++ b/lib/ww/lex/lex.ww @@ -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; diff --git a/lib/ww/lex/tok.ww b/lib/ww/lex/tok.ww index 398c7a45..5e3ac0c9 100644 --- a/lib/ww/lex/tok.ww +++ b/lib/ww/lex/tok.ww @@ -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 ""; }; - 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 ""; }; + 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 ""; }; + if (k == tkind.TK_LAST) { return ""; }; 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. diff --git a/lib/ww/parse/decl.ww b/lib/ww/parse/decl.ww index 8452a57a..45009c49 100644 --- a/lib/ww/parse/decl.ww +++ b/lib/ww/parse/decl.ww @@ -13,7 +13,7 @@ fn parseuse(p: *parser) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_SEMI, "expected ';' after use"); + expecttok(p, tkind.TK_SEMI, "expected ';' after use"); return n; }; @@ -27,11 +27,11 @@ fn parsedef(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in def"); + expecttok(p, tkind.TK_COLON, "expected ':' in def"); n.lhs = parsetype(p); - expecttok(p, TK_ASSIGN, "expected '=' in def"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in def"); n.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after def"); + expecttok(p, tkind.TK_SEMI, "expected ';' after def"); n.exported = exported; return n; }; @@ -41,31 +41,31 @@ fn parselet(p: *parser, exported: i32) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; // Accept `let` or `const`. Const-bound bindings are marked via - // n.op = TK_CONST so the checker can reject reassignment. + // n.op = tkind.TK_CONST so the checker can reject reassignment. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); let n: *node = newnode(p.a, N_LET, pf, pl, pc); n.module = p.l.module; let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); n.exported = exported; - if (is_const != 0) { n.op = TK_CONST; }; + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; fn parseattrs(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_AT) { + for (p.curkind == tkind.TK_AT) { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; @@ -76,11 +76,11 @@ fn parseattrs(p: *parser) *node = { a.str = id; // `@name(args...)` for FFI-style attrs; `@name` for marker- // only attrs like @test (no parens). - if (accepttok(p, TK_LPAREN)) { + if (accepttok(p, tkind.TK_LPAREN)) { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); a.list = arghead; - expecttok(p, TK_RPAREN, "expected ')' after attribute args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after attribute args"); }; if (head == nil) { head = a; tail = a; } else { tail.next = a; tail = a; }; @@ -89,7 +89,7 @@ fn parseattrs(p: *parser) *node = { }; fn parseparams(p: *parser) *node = { - if (p.curkind == TK_RPAREN) { return nil; }; + if (p.curkind == tkind.TK_RPAREN) { return nil; }; let head: *node = nil; let tail: *node = nil; for (true) { @@ -102,12 +102,12 @@ fn parseparams(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in parameter"); + expecttok(p, tkind.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.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; return head; }; @@ -122,20 +122,20 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_LPAREN, "expected '(' after fn name"); + expecttok(p, tkind.TK_LPAREN, "expected '(' after fn name"); n.list = parseparams(p); - expecttok(p, TK_RPAREN, "expected ')' after params"); - if (p.curkind != TK_ASSIGN) { - if (p.curkind != TK_SEMI) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after params"); + if (p.curkind != tkind.TK_ASSIGN) { + if (p.curkind != tkind.TK_SEMI) { n.lhs = parsetype(p); }; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.body = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after fn body"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn body"); } else { // Body-less fn: FFI declaration (`fn name(args) ret;`). - expecttok(p, TK_SEMI, "expected ';' after fn header"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn header"); }; n.exported = exported; n.attr = attrs; @@ -152,9 +152,9 @@ fn parsetypedecl(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_ASSIGN, "expected '=' in type decl"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in type decl"); n.lhs = parsetype(p); - expecttok(p, TK_SEMI, "expected ';' after type decl"); + expecttok(p, tkind.TK_SEMI, "expected ';' after type decl"); n.exported = exported; return n; }; diff --git a/lib/ww/parse/expr.ww b/lib/ww/parse/expr.ww index 36af7066..08917b06 100644 --- a/lib/ww/parse/expr.ww +++ b/lib/ww/parse/expr.ww @@ -21,42 +21,42 @@ fn parseprimary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc); n.uval = p.curuval; n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_STR) { + if (p.curkind == tkind.TK_STR) { let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc); n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_RUNE) { + if (p.curkind == tkind.TK_RUNE) { let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc); n.uval = p.curuval; advance(p); return n; }; - if (p.curkind == TK_TRUE) { + if (p.curkind == tkind.TK_TRUE) { advance(p); return newnode(p.a, N_TRUE, pf, pl, pc); }; - if (p.curkind == TK_FALSE) { + if (p.curkind == tkind.TK_FALSE) { advance(p); return newnode(p.a, N_FALSE, pf, pl, pc); }; - if (p.curkind == TK_NIL) { + if (p.curkind == tkind.TK_NIL) { advance(p); return newnode(p.a, N_NIL, pf, pl, pc); }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { advance(p); return newnode(p.a, N_VOIDLIT, pf, pl, pc); }; - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { // Bare `_` — valid only as a discard lvalue. Emit an N_IDENT // with empty str; the checker rejects it outside lvalue // positions. @@ -66,7 +66,7 @@ fn parseprimary(p: *parser) *node = { n.str = empty; return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { // Array literal `[a, b, c]` or `[v, w...]` (repeat suffix). // The repeat marker is an N_FIELD node with str = "..." // appended to the element list so cgen can detect it. @@ -74,12 +74,12 @@ fn parseprimary(p: *parser) *node = { let n: *node = newnode(p.a, N_ARRLIT, pf, pl, pc); let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_RBRACK) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACK) { + if (p.curkind == tkind.TK_EOF) { break; }; let e: *node = parseexpr(p); if (head == nil) { head = e; tail = e; } else { tail.next = e; tail = e; }; - if (accepttok(p, TK_ELLIPSIS)) { + if (accepttok(p, tkind.TK_ELLIPSIS)) { let rep: *node = newnode(p.a, N_FIELD, p.curfile, p.curline, p.curcol); rep.str = "..."; @@ -87,34 +87,34 @@ fn parseprimary(p: *parser) *node = { tail = rep; break; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACK, "expected ']' after array literal"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after array literal"); n.list = head; return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let e: *node = parseexpr(p); // Tuple literal: (a, b, ...) - if (accepttok(p, TK_COMMA)) { + if (accepttok(p, tkind.TK_COMMA)) { let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc); t.list = e; let tail: *node = e; for (true) { - if (p.curkind == TK_RPAREN) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; let en: *node = parseexpr(p); tail.next = en; tail = en; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple"); return t; }; - expecttok(p, TK_RPAREN, "expected ')'"); + expecttok(p, tkind.TK_RPAREN, "expected ')'"); return e; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_IDENT, pf, pl, pc); n.str = p.curtext; advance(p); @@ -124,19 +124,19 @@ fn parseprimary(p: *parser) *node = { // expressions, never directly from cond contexts that need a // block; in stmt parsing, the for/if drivers consume their // own paren/cond, so this is safe. - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; // Trailing `...` autofill marker. Stash on s.op so // cgen can zero-fill the slot before per-field stores. - if (p.curkind == TK_ELLIPSIS) { + if (p.curkind == tkind.TK_ELLIPSIS) { advance(p); - s.op = TK_ELLIPSIS; + s.op = tkind.TK_ELLIPSIS; break; }; let fpf: str = p.curfile; @@ -144,53 +144,53 @@ fn parseprimary(p: *parser) *node = { let fpc: i32 = p.curcol; let id: str; expectident(p, &id); - expecttok(p, TK_ASSIGN, "expected '=' in struct lit field"); + expecttok(p, tkind.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; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct literal"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct literal"); s.list = head; return s; }; return n; }; - if (p.curkind == TK_MATCH) { + if (p.curkind == tkind.TK_MATCH) { // match (e) { case let v: T => stmt; case T => stmt; case => stmt; }; advance(p); - expecttok(p, TK_LPAREN, "expected '(' after match"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after match scrutinee"); + expecttok(p, tkind.TK_LBRACE, "expected '{' to open match body"); let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_CASE) { + for (p.curkind == tkind.TK_CASE) { let cf: str = p.curfile; let cl: i32 = p.curline; let cc: i32 = p.curcol; advance(p); // past `case` let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc); - if (p.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { advance(p); let id: str; expectident(p, &id); mc.str = id; - expecttok(p, TK_COLON, "expected ':' after match binding"); + expecttok(p, tkind.TK_COLON, "expected ':' after match binding"); mc.lhs = parsetype(p); - } else { if (p.curkind != TK_FATARROW) { + } else { if (p.curkind != tkind.TK_FATARROW) { mc.lhs = parsetype(p); };}; - expecttok(p, TK_FATARROW, "expected '=>' in match arm"); + expecttok(p, tkind.TK_FATARROW, "expected '=>' in match arm"); mc.body = parsestmt(p); if (head == nil) { head = mc; tail = mc; } else { tail.next = mc; tail = mc; }; }; - expecttok(p, TK_RBRACE, "expected '}' after match body"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after match body"); m.list = head; return m; }; @@ -208,7 +208,7 @@ fn parsearglist(p: *parser, closekind: i32, headout: **node) void = { 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 (!accepttok(p, tkind.TK_COMMA)) { break; }; if (p.curkind == closekind) { break; }; }; *headout = head; @@ -220,7 +220,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let n: *node = newnode(p.a, N_CALL, pf, pl, pc); n.lhs = cur; @@ -235,24 +235,24 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { n.list = parsetype(p); } else { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); n.list = arghead; }; - expecttok(p, TK_RPAREN, "expected ')' after args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after args"); cur = n; continue; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); // `[ : hi ]` — slice with implicit lo = 0. - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in slice"); cur = n; continue; }; @@ -262,33 +262,33 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { p.nocast = 1; let e: *node = parseexpr(p); p.nocast = prev; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; n.rhs = e; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after index"); cur = n; continue; }; - if (p.curkind == TK_DOT) { + if (p.curkind == tkind.TK_DOT) { advance(p); let n: *node = newnode(p.a, N_DOT, pf, pl, pc); n.lhs = cur; // Hare-style tuple field access: `t.0`, `t.1`. The // numeric literal becomes the field name string so the // cgen tuple-positional path matches `cmd/wcc/parse.c`. - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { n.str = p.curtext; advance(p); } else { @@ -299,7 +299,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { if (p.nocast != 0) { return cur; }; @@ -314,7 +314,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { // `e as T` — assert lhs is variant T (abort otherwise) → T // `e is T` — bool: does lhs currently hold variant T? // Same precedence level as the `:` cast. - if (p.curkind == TK_AS) { + if (p.curkind == tkind.TK_AS) { advance(p); let n: *node = newnode(p.a, N_TYPEASSERT, pf, pl, pc); n.lhs = cur; @@ -322,7 +322,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_IS) { + if (p.curkind == tkind.TK_IS) { advance(p); let n: *node = newnode(p.a, N_TYPETEST, pf, pl, pc); n.lhs = cur; @@ -332,14 +332,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { }; // `e?` — propagate error variant up the stack. // `e!` — abort on error variant. - if (p.curkind == TK_QUESTION) { + if (p.curkind == tkind.TK_QUESTION) { advance(p); let n: *node = newnode(p.a, N_TRYPROP, pf, pl, pc); n.lhs = cur; cur = n; continue; }; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_TRYUNW, pf, pl, pc); n.lhs = cur; @@ -356,40 +356,40 @@ fn parseunary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; let k: i32 = p.curkind; - if (k == TK_MINUS) { + if (k == tkind.TK_MINUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_MINUS; n.lhs = parseunary(p); + n.op = tkind.TK_MINUS; n.lhs = parseunary(p); return n; }; - if (k == TK_PLUS) { + if (k == tkind.TK_PLUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_PLUS; n.lhs = parseunary(p); + n.op = tkind.TK_PLUS; n.lhs = parseunary(p); return n; }; - if (k == TK_NOT) { + if (k == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_NOT; n.lhs = parseunary(p); + n.op = tkind.TK_NOT; n.lhs = parseunary(p); return n; }; - if (k == TK_TILDE) { + if (k == tkind.TK_TILDE) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_TILDE; n.lhs = parseunary(p); + n.op = tkind.TK_TILDE; n.lhs = parseunary(p); return n; }; - if (k == TK_STAR) { + if (k == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_STAR; n.lhs = parseunary(p); + n.op = tkind.TK_STAR; n.lhs = parseunary(p); return n; }; - if (k == TK_AMP) { + if (k == tkind.TK_AMP) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_AMP; n.lhs = parseunary(p); + n.op = tkind.TK_AMP; n.lhs = parseunary(p); return n; }; return parsepostfix(p, parseprimary(p)); diff --git a/lib/ww/parse/parse.ww b/lib/ww/parse/parse.ww index c99872c7..da905a0b 100644 --- a/lib/ww/parse/parse.ww +++ b/lib/ww/parse/parse.ww @@ -24,7 +24,7 @@ type parser = struct { // nocast: while inside `[...]` we treat ':' as the slice // separator, not the cast operator. Mirrors parse.c's flag. nocast: i32, - curkind: i32, + curkind: i32, // holds a `tkind` value (relaxation lets it mix) curfile: str, curline: i32, curcol: i32, @@ -72,10 +72,10 @@ fn expecttok(p: *parser, k: i32, what: str) bool = { return false; }; -// expectident — consume the current TK_IDENT and return its text. +// expectident — consume the current tkind.TK_IDENT and return its text. // Returns the empty str on error (and advances to make progress). fn expectident(p: *parser, into: *str) bool = { - if (p.curkind != TK_IDENT) { + if (p.curkind != tkind.TK_IDENT) { errmsg(p, "expected identifier"); advance(p); return false; @@ -89,7 +89,7 @@ fn expectident(p: *parser, into: *str) bool = { // discard marker. On `_`, returns "" so the checker skips // scope_define for the binding. fn expectbindname(p: *parser, into: *str) bool = { - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { let empty: str; *into = empty; advance(p); @@ -129,7 +129,7 @@ fn parsetype(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { // `!T` — Hare error-flagged type wrapper. advance(p); let n: *node = newnode(p.a, N_TBANG, pf, pl, pc); @@ -137,16 +137,16 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_STAR) { + if (p.curkind == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_TPTR, pf, pl, pc); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); - if (p.curkind == TK_RBRACK) { + if (p.curkind == tkind.TK_RBRACK) { advance(p); let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc); n.lhs = parsetype(p); @@ -156,24 +156,24 @@ fn parsetype(p: *parser) *node = { // `[_]T` — length inferred from initialiser. n.rhs stays nil // as the sentinel; the cgen path for N_LET fills it from the // array literal's element count. - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { advance(p); } else { n.rhs = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in array type"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in array type"); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_STRUCT) { + if (p.curkind == tkind.TK_STRUCT) { advance(p); - expecttok(p, TK_LBRACE, "expected '{' after struct"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after struct"); let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc); let fhead: *node = nil; let ftail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let fpf: str = p.curfile; let fpl: i32 = p.curline; let fpc: i32 = p.curcol; @@ -181,32 +181,32 @@ fn parsetype(p: *parser) *node = { let fid: str; expectident(p, &fid); f.str = fid; - expecttok(p, TK_COLON, "expected ':' in field"); + expecttok(p, tkind.TK_COLON, "expected ':' in field"); f.lhs = parsetype(p); if (fhead == nil) { fhead = f; ftail = f; } else { ftail.next = f; ftail = f; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct fields"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct fields"); n.list = fhead; return n; }; - if (p.curkind == TK_ENUM) { + if (p.curkind == tkind.TK_ENUM) { // `enum [storage] { NAME [= expr], ... }` // Storage defaults to i32 (lhs == nil). Each member is an // N_TENUMMEMBER with str=name and lhs = value expr or nil // (auto-increment when omitted). advance(p); let n: *node = newnode(p.a, N_TENUM, pf, pl, pc); - if (p.curkind != TK_LBRACE) { + if (p.curkind != tkind.TK_LBRACE) { n.lhs = parsetype(p); }; - expecttok(p, TK_LBRACE, "expected '{' after enum"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after enum"); let mhead: *node = nil; let mtail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let mpf: str = p.curfile; let mpl: i32 = p.curline; let mpc: i32 = p.curcol; @@ -214,19 +214,19 @@ fn parsetype(p: *parser) *node = { let mid: str; expectident(p, &mid); m.str = mid; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { m.lhs = parseexpr(p); }; if (mhead == nil) { mhead = m; mtail = m; } else { mtail.next = m; mtail = m; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after enum members"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after enum members"); n.list = mhead; return n; }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { // `void` keyword in type-expr context — emit as N_TNAME so // resolution treats it like any other primitive name. let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); @@ -235,15 +235,15 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); let acc: str = p.curtext; advance(p); // Dotted path collapse: pkg.Type → single TNAME with the // joined string. Mirrors C parsetype's loop. - for (p.curkind == TK_DOT) { + for (p.curkind == tkind.TK_DOT) { advance(p); - if (p.curkind != TK_IDENT) { break; }; + if (p.curkind != tkind.TK_IDENT) { break; }; acc = joindotted(p.a, acc, p.curtext); advance(p); }; @@ -251,11 +251,11 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { // (T) or (T, T, ...) or (T | T | ...) advance(p); let first: *node = parsetype(p); - if (accepttok(p, TK_PIPE)) { + if (accepttok(p, tkind.TK_PIPE)) { let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc); let head: *node = first; let tail: *node = first; @@ -263,14 +263,14 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_PIPE)) { break; }; + if (!accepttok(p, tkind.TK_PIPE)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tagged-union type"); + expecttok(p, tkind.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"); + if (!accepttok(p, tkind.TK_COMMA)) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type"); return first; }; let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc); @@ -280,23 +280,23 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_COMMA)) { break; }; - if (p.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple type"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple type"); n.list = head; return n; }; - if (p.curkind == TK_FN) { + if (p.curkind == tkind.TK_FN) { advance(p); - expecttok(p, TK_LPAREN, "expected '(' after fn in type"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after fn type params"); n.lhs = parsetype(p); return n; }; @@ -314,39 +314,39 @@ fn parsetype(p: *parser) *node = { // 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; }; + if (k == tkind.TK_OR) { return 1; }; + if (k == tkind.TK_AND) { return 2; }; + if (k == tkind.TK_EQ) { return 3; }; + if (k == tkind.TK_NEQ) { return 3; }; + if (k == tkind.TK_LT) { return 4; }; + if (k == tkind.TK_LE) { return 4; }; + if (k == tkind.TK_GT) { return 4; }; + if (k == tkind.TK_GE) { return 4; }; + if (k == tkind.TK_PIPE) { return 5; }; + if (k == tkind.TK_CARET) { return 6; }; + if (k == tkind.TK_AMP) { return 7; }; + if (k == tkind.TK_LSHIFT) { return 8; }; + if (k == tkind.TK_RSHIFT) { return 8; }; + if (k == tkind.TK_PLUS) { return 9; }; + if (k == tkind.TK_MINUS) { return 9; }; + if (k == tkind.TK_STAR) { return 10; }; + if (k == tkind.TK_SLASH) { return 10; }; + if (k == tkind.TK_PERCENT) { return 10; }; return 0; }; fn isassignop(k: 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; }; + if (k == tkind.TK_ASSIGN) { return true; }; + if (k == tkind.TK_PLUSEQ) { return true; }; + if (k == tkind.TK_MINUSEQ) { return true; }; + if (k == tkind.TK_STAREQ) { return true; }; + if (k == tkind.TK_SLASHEQ) { return true; }; + if (k == tkind.TK_PERCENTEQ) { return true; }; + if (k == tkind.TK_AMPEQ) { return true; }; + if (k == tkind.TK_PIPEEQ) { return true; }; + if (k == tkind.TK_CARETEQ) { return true; }; + if (k == tkind.TK_LSHIFTEQ) { return true; }; + if (k == tkind.TK_RSHIFTEQ) { return true; }; return false; }; @@ -358,36 +358,36 @@ export fn parsefile(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_EOF) { + for (p.curkind != tkind.TK_EOF) { let attrs: *node = parseattrs(p); let exported: i32 = 0; - if (p.curkind == TK_EXPORT) { exported = 1; advance(p); }; + if (p.curkind == tkind.TK_EXPORT) { exported = 1; advance(p); }; let d: *node = nil; - if (p.curkind == TK_USE) { + if (p.curkind == tkind.TK_USE) { d = parseuse(p); - } else { if (p.curkind == TK_DEF) { + } else { if (p.curkind == tkind.TK_DEF) { d = parsedef(p, exported); - } else { if (p.curkind == TK_TYPE) { + } else { if (p.curkind == tkind.TK_TYPE) { d = parsetypedecl(p, exported); - } else { if (p.curkind == TK_LET) { + } else { if (p.curkind == tkind.TK_LET) { d = parselet(p, exported); - } else { if (p.curkind == TK_CONST) { + } else { if (p.curkind == tkind.TK_CONST) { d = parselet(p, exported); - } else { if (p.curkind == TK_FN) { + } else { if (p.curkind == tkind.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.curkind != TK_SEMI) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { + for (p.curkind != tkind.TK_SEMI) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { let depth: i32 = 0; for (true) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { depth += 1; advance(p); continue; }; - if (p.curkind == TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { depth += 1; advance(p); continue; }; + if (p.curkind == tkind.TK_RBRACE) { depth -= 1; advance(p); if (depth == 0) { break; }; @@ -399,7 +399,7 @@ export fn parsefile(p: *parser) *node = { }; advance(p); }; - if (p.curkind == TK_SEMI) { advance(p); }; + if (p.curkind == tkind.TK_SEMI) { advance(p); }; };};};};};}; if (d != nil) { diff --git a/lib/ww/parse/stmt.ww b/lib/ww/parse/stmt.ww index ddba0929..2d4f7063 100644 --- a/lib/ww/parse/stmt.ww +++ b/lib/ww/parse/stmt.ww @@ -8,15 +8,15 @@ fn parseletlocal(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - // `let` or `const`. Const-bound locals are marked via n.op = TK_CONST. + // `let` or `const`. Const-bound locals are marked via n.op = tkind.TK_CONST. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); // Hare-style tuple destructure: `let (a, b) = expr;`. // Types are optional per binding (matches C parser; Hare itself // doesn't allow types here, but cmd/wcc/parse.c does). - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = nil; @@ -29,21 +29,21 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); l.str = id; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; if (head == nil) { head = l; } else { tail.next = l; }; tail = l; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in let destructure"); - expecttok(p, TK_ASSIGN, "expected '=' after let destructure"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in let destructure"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let destructure"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; @@ -52,17 +52,17 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; // Comma-multi-let: `let n, s = call();` (ww extension over Hare). // Collects (name, type) pairs, then '=' rhs. Each binding gets // its own N_LET; the wrapping N_MLET carries the rhs. - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = n; let tail: *node = n; - for (accepttok(p, TK_COMMA)) { + for (accepttok(p, tkind.TK_COMMA)) { let lpf: str = p.curfile; let lpl: i32 = p.curline; let lpc: i32 = p.curcol; @@ -70,26 +70,26 @@ fn parseletlocal(p: *parser) *node = { let id2: str; expectbindname(p, &id2); l.str = id2; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; tail.next = l; tail = l; }; - expecttok(p, TK_ASSIGN, "expected '=' after let names"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let names"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); - if (is_const != 0) { n.op = TK_CONST; }; + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; @@ -97,19 +97,19 @@ fn parseblock(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - expecttok(p, TK_LBRACE, "expected '{' to open block"); + expecttok(p, tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_RBRACE, "expected '}' to close block"); blk.list = head; return blk; }; @@ -119,13 +119,13 @@ fn parseif(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `if` - expecttok(p, TK_LPAREN, "expected '(' after if"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition"); n.body = parseblock(p); - if (accepttok(p, TK_ELSE)) { - if (p.curkind == TK_IF) { + if (accepttok(p, tkind.TK_ELSE)) { + if (p.curkind == tkind.TK_IF) { n.els = parseif(p); } else { n.els = parseblock(p); @@ -139,7 +139,7 @@ fn parsefor(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `for` - expecttok(p, TK_LPAREN, "expected '(' after for"); + expecttok(p, tkind.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 @@ -149,15 +149,15 @@ fn parsefor(p: *parser) *node = { // `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.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { n.lhs = parseletlocal(p); // init (consumes its own ';') n.cond = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after for cond"); + expecttok(p, tkind.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)) { + if (accepttok(p, tkind.TK_SEMI)) { // cond ; post n.cond = first; n.rhs = parseexpr(p); @@ -166,11 +166,11 @@ fn parsefor(p: *parser) *node = { n.cond = first; }; }; - expecttok(p, TK_RPAREN, "expected ')' after for"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after for"); n.body = parseblock(p); // Optional `else { ... }` — runs at normal cond-false exit; skipped // by break. Hare's "did the loop find it?" idiom. - if (accepttok(p, TK_ELSE)) { + if (accepttok(p, tkind.TK_ELSE)) { n.els = parseblock(p); }; return n; @@ -183,37 +183,37 @@ fn parsestmt(p: *parser) *node = { // `static` is allowed on local lets per Hare; we accept and skip // it (it doesn't change the AST shape). - if (p.curkind == TK_STATIC) { advance(p); }; + if (p.curkind == tkind.TK_STATIC) { advance(p); }; - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.TK_LBRACE) { let b: *node = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after block"); + expecttok(p, tkind.TK_SEMI, "expected ';' after block"); return b; }; - if (p.curkind == TK_LET) { return parseletlocal(p); }; - if (p.curkind == TK_CONST) { return parseletlocal(p); }; - if (p.curkind == TK_IF) { + if (p.curkind == tkind.TK_LET) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_CONST) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_IF) { let n: *node = parseif(p); - expecttok(p, TK_SEMI, "expected ';' after if"); + expecttok(p, tkind.TK_SEMI, "expected ';' after if"); return n; }; - if (p.curkind == TK_FOR) { + if (p.curkind == tkind.TK_FOR) { let n: *node = parsefor(p); - expecttok(p, TK_SEMI, "expected ';' after for"); + expecttok(p, tkind.TK_SEMI, "expected ';' after for"); return n; }; - if (p.curkind == TK_RETURN) { + if (p.curkind == tkind.TK_RETURN) { advance(p); let n: *node = newnode(p.a, N_RETURN, pf, pl, pc); - if (p.curkind != TK_SEMI) { + if (p.curkind != tkind.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.curkind == TK_COMMA) { + if (p.curkind == tkind.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)) { + for (accepttok(p, tkind.TK_COMMA)) { let e: *node = parseexpr(p); tail.next = e; tail = e; @@ -223,31 +223,31 @@ fn parsestmt(p: *parser) *node = { n.lhs = first; }; }; - expecttok(p, TK_SEMI, "expected ';' after return"); + expecttok(p, tkind.TK_SEMI, "expected ';' after return"); return n; }; - if (p.curkind == TK_DEFER) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after defer"); return n; }; - if (p.curkind == TK_YIELD) { + if (p.curkind == tkind.TK_YIELD) { advance(p); let n: *node = newnode(p.a, N_YIELD, pf, pl, pc); n.lhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after yield"); + expecttok(p, tkind.TK_SEMI, "expected ';' after yield"); return n; }; - if (p.curkind == TK_BREAK) { + if (p.curkind == tkind.TK_BREAK) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after break"); + expecttok(p, tkind.TK_SEMI, "expected ';' after break"); return newnode(p.a, N_BREAK, pf, pl, pc); }; - if (p.curkind == TK_CONTINUE) { + if (p.curkind == tkind.TK_CONTINUE) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after continue"); + expecttok(p, tkind.TK_SEMI, "expected ';' after continue"); return newnode(p.a, N_CONTINUE, pf, pl, pc); }; // expression statement, or tuple-destructure multi-assign: @@ -257,25 +257,25 @@ fn parsestmt(p: *parser) *node = { // through parsebin(parseunary, 1) so the `=` stays for us to // consume — parseexpr would absorb it. let e: *node = parseexpr(p); - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc); let head: *node = e; let tail: *node = e; - for (p.curkind == TK_COMMA) { + for (p.curkind == tkind.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"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after multi-assign lvalues"); m.rhs = parseexpr(p); m.list = head; - expecttok(p, TK_SEMI, "expected ';' after multi-assign"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement"); return n; }; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 6ded58d4..c67f04e2 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -459,108 +459,110 @@ export fn stou64(s: str) (u64 | invalid | overflow) = { 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 -------------------------------------------------------- // @@ -579,13 +581,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 }; @@ -602,39 +605,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 ---------------------------------------------------------- @@ -643,101 +646,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 ""; }; - 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 ""; }; + 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 ""; }; + if (k == tkind.TK_LAST) { return ""; }; return ""; }; @@ -836,25 +839,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. @@ -1274,7 +1277,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; @@ -1333,9 +1336,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; @@ -1347,7 +1350,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; }; }; @@ -1413,16 +1416,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); }; @@ -1435,7 +1438,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; @@ -1466,7 +1469,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; @@ -1480,7 +1483,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; @@ -1496,7 +1499,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; @@ -1504,7 +1507,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; @@ -1530,7 +1533,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 @@ -1544,7 +1547,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); @@ -1560,97 +1563,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; @@ -2037,42 +2040,42 @@ fn parseprimary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc); n.uval = p.curuval; n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_STR) { + if (p.curkind == tkind.TK_STR) { let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc); n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_RUNE) { + if (p.curkind == tkind.TK_RUNE) { let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc); n.uval = p.curuval; advance(p); return n; }; - if (p.curkind == TK_TRUE) { + if (p.curkind == tkind.TK_TRUE) { advance(p); return newnode(p.a, N_TRUE, pf, pl, pc); }; - if (p.curkind == TK_FALSE) { + if (p.curkind == tkind.TK_FALSE) { advance(p); return newnode(p.a, N_FALSE, pf, pl, pc); }; - if (p.curkind == TK_NIL) { + if (p.curkind == tkind.TK_NIL) { advance(p); return newnode(p.a, N_NIL, pf, pl, pc); }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { advance(p); return newnode(p.a, N_VOIDLIT, pf, pl, pc); }; - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { // Bare `_` — valid only as a discard lvalue. Emit an N_IDENT // with empty str; the checker rejects it outside lvalue // positions. @@ -2082,7 +2085,7 @@ fn parseprimary(p: *parser) *node = { n.str = empty; return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { // Array literal `[a, b, c]` or `[v, w...]` (repeat suffix). // The repeat marker is an N_FIELD node with str = "..." // appended to the element list so cgen can detect it. @@ -2090,12 +2093,12 @@ fn parseprimary(p: *parser) *node = { let n: *node = newnode(p.a, N_ARRLIT, pf, pl, pc); let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_RBRACK) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACK) { + if (p.curkind == tkind.TK_EOF) { break; }; let e: *node = parseexpr(p); if (head == nil) { head = e; tail = e; } else { tail.next = e; tail = e; }; - if (accepttok(p, TK_ELLIPSIS)) { + if (accepttok(p, tkind.TK_ELLIPSIS)) { let rep: *node = newnode(p.a, N_FIELD, p.curfile, p.curline, p.curcol); rep.str = "..."; @@ -2103,34 +2106,34 @@ fn parseprimary(p: *parser) *node = { tail = rep; break; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACK, "expected ']' after array literal"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after array literal"); n.list = head; return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let e: *node = parseexpr(p); // Tuple literal: (a, b, ...) - if (accepttok(p, TK_COMMA)) { + if (accepttok(p, tkind.TK_COMMA)) { let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc); t.list = e; let tail: *node = e; for (true) { - if (p.curkind == TK_RPAREN) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; let en: *node = parseexpr(p); tail.next = en; tail = en; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple"); return t; }; - expecttok(p, TK_RPAREN, "expected ')'"); + expecttok(p, tkind.TK_RPAREN, "expected ')'"); return e; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_IDENT, pf, pl, pc); n.str = p.curtext; advance(p); @@ -2140,19 +2143,19 @@ fn parseprimary(p: *parser) *node = { // expressions, never directly from cond contexts that need a // block; in stmt parsing, the for/if drivers consume their // own paren/cond, so this is safe. - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; // Trailing `...` autofill marker. Stash on s.op so // cgen can zero-fill the slot before per-field stores. - if (p.curkind == TK_ELLIPSIS) { + if (p.curkind == tkind.TK_ELLIPSIS) { advance(p); - s.op = TK_ELLIPSIS; + s.op = tkind.TK_ELLIPSIS; break; }; let fpf: str = p.curfile; @@ -2160,53 +2163,53 @@ fn parseprimary(p: *parser) *node = { let fpc: i32 = p.curcol; let id: str; expectident(p, &id); - expecttok(p, TK_ASSIGN, "expected '=' in struct lit field"); + expecttok(p, tkind.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; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct literal"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct literal"); s.list = head; return s; }; return n; }; - if (p.curkind == TK_MATCH) { + if (p.curkind == tkind.TK_MATCH) { // match (e) { case let v: T => stmt; case T => stmt; case => stmt; }; advance(p); - expecttok(p, TK_LPAREN, "expected '(' after match"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after match scrutinee"); + expecttok(p, tkind.TK_LBRACE, "expected '{' to open match body"); let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_CASE) { + for (p.curkind == tkind.TK_CASE) { let cf: str = p.curfile; let cl: i32 = p.curline; let cc: i32 = p.curcol; advance(p); // past `case` let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc); - if (p.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { advance(p); let id: str; expectident(p, &id); mc.str = id; - expecttok(p, TK_COLON, "expected ':' after match binding"); + expecttok(p, tkind.TK_COLON, "expected ':' after match binding"); mc.lhs = parsetype(p); - } else { if (p.curkind != TK_FATARROW) { + } else { if (p.curkind != tkind.TK_FATARROW) { mc.lhs = parsetype(p); };}; - expecttok(p, TK_FATARROW, "expected '=>' in match arm"); + expecttok(p, tkind.TK_FATARROW, "expected '=>' in match arm"); mc.body = parsestmt(p); if (head == nil) { head = mc; tail = mc; } else { tail.next = mc; tail = mc; }; }; - expecttok(p, TK_RBRACE, "expected '}' after match body"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after match body"); m.list = head; return m; }; @@ -2224,7 +2227,7 @@ fn parsearglist(p: *parser, closekind: i32, headout: **node) void = { 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 (!accepttok(p, tkind.TK_COMMA)) { break; }; if (p.curkind == closekind) { break; }; }; *headout = head; @@ -2236,7 +2239,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let n: *node = newnode(p.a, N_CALL, pf, pl, pc); n.lhs = cur; @@ -2251,24 +2254,24 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { n.list = parsetype(p); } else { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); n.list = arghead; }; - expecttok(p, TK_RPAREN, "expected ')' after args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after args"); cur = n; continue; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); // `[ : hi ]` — slice with implicit lo = 0. - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in slice"); cur = n; continue; }; @@ -2278,33 +2281,33 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { p.nocast = 1; let e: *node = parseexpr(p); p.nocast = prev; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; n.rhs = e; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after index"); cur = n; continue; }; - if (p.curkind == TK_DOT) { + if (p.curkind == tkind.TK_DOT) { advance(p); let n: *node = newnode(p.a, N_DOT, pf, pl, pc); n.lhs = cur; // Hare-style tuple field access: `t.0`, `t.1`. The // numeric literal becomes the field name string so the // cgen tuple-positional path matches `cmd/wcc/parse.c`. - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { n.str = p.curtext; advance(p); } else { @@ -2315,7 +2318,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { if (p.nocast != 0) { return cur; }; @@ -2330,7 +2333,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { // `e as T` — assert lhs is variant T (abort otherwise) → T // `e is T` — bool: does lhs currently hold variant T? // Same precedence level as the `:` cast. - if (p.curkind == TK_AS) { + if (p.curkind == tkind.TK_AS) { advance(p); let n: *node = newnode(p.a, N_TYPEASSERT, pf, pl, pc); n.lhs = cur; @@ -2338,7 +2341,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_IS) { + if (p.curkind == tkind.TK_IS) { advance(p); let n: *node = newnode(p.a, N_TYPETEST, pf, pl, pc); n.lhs = cur; @@ -2348,14 +2351,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { }; // `e?` — propagate error variant up the stack. // `e!` — abort on error variant. - if (p.curkind == TK_QUESTION) { + if (p.curkind == tkind.TK_QUESTION) { advance(p); let n: *node = newnode(p.a, N_TRYPROP, pf, pl, pc); n.lhs = cur; cur = n; continue; }; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_TRYUNW, pf, pl, pc); n.lhs = cur; @@ -2372,40 +2375,40 @@ fn parseunary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; let k: i32 = p.curkind; - if (k == TK_MINUS) { + if (k == tkind.TK_MINUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_MINUS; n.lhs = parseunary(p); + n.op = tkind.TK_MINUS; n.lhs = parseunary(p); return n; }; - if (k == TK_PLUS) { + if (k == tkind.TK_PLUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_PLUS; n.lhs = parseunary(p); + n.op = tkind.TK_PLUS; n.lhs = parseunary(p); return n; }; - if (k == TK_NOT) { + if (k == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_NOT; n.lhs = parseunary(p); + n.op = tkind.TK_NOT; n.lhs = parseunary(p); return n; }; - if (k == TK_TILDE) { + if (k == tkind.TK_TILDE) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_TILDE; n.lhs = parseunary(p); + n.op = tkind.TK_TILDE; n.lhs = parseunary(p); return n; }; - if (k == TK_STAR) { + if (k == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_STAR; n.lhs = parseunary(p); + n.op = tkind.TK_STAR; n.lhs = parseunary(p); return n; }; - if (k == TK_AMP) { + if (k == tkind.TK_AMP) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_AMP; n.lhs = parseunary(p); + n.op = tkind.TK_AMP; n.lhs = parseunary(p); return n; }; return parsepostfix(p, parseprimary(p)); @@ -2464,15 +2467,15 @@ fn parseletlocal(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - // `let` or `const`. Const-bound locals are marked via n.op = TK_CONST. + // `let` or `const`. Const-bound locals are marked via n.op = tkind.TK_CONST. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); // Hare-style tuple destructure: `let (a, b) = expr;`. // Types are optional per binding (matches C parser; Hare itself // doesn't allow types here, but cmd/wcc/parse.c does). - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = nil; @@ -2485,21 +2488,21 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); l.str = id; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; if (head == nil) { head = l; } else { tail.next = l; }; tail = l; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in let destructure"); - expecttok(p, TK_ASSIGN, "expected '=' after let destructure"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in let destructure"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let destructure"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; @@ -2508,17 +2511,17 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; // Comma-multi-let: `let n, s = call();` (ww extension over Hare). // Collects (name, type) pairs, then '=' rhs. Each binding gets // its own N_LET; the wrapping N_MLET carries the rhs. - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = n; let tail: *node = n; - for (accepttok(p, TK_COMMA)) { + for (accepttok(p, tkind.TK_COMMA)) { let lpf: str = p.curfile; let lpl: i32 = p.curline; let lpc: i32 = p.curcol; @@ -2526,26 +2529,26 @@ fn parseletlocal(p: *parser) *node = { let id2: str; expectbindname(p, &id2); l.str = id2; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; tail.next = l; tail = l; }; - expecttok(p, TK_ASSIGN, "expected '=' after let names"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let names"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); - if (is_const != 0) { n.op = TK_CONST; }; + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; @@ -2553,19 +2556,19 @@ fn parseblock(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - expecttok(p, TK_LBRACE, "expected '{' to open block"); + expecttok(p, tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_RBRACE, "expected '}' to close block"); blk.list = head; return blk; }; @@ -2575,13 +2578,13 @@ fn parseif(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `if` - expecttok(p, TK_LPAREN, "expected '(' after if"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition"); n.body = parseblock(p); - if (accepttok(p, TK_ELSE)) { - if (p.curkind == TK_IF) { + if (accepttok(p, tkind.TK_ELSE)) { + if (p.curkind == tkind.TK_IF) { n.els = parseif(p); } else { n.els = parseblock(p); @@ -2595,7 +2598,7 @@ fn parsefor(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `for` - expecttok(p, TK_LPAREN, "expected '(' after for"); + expecttok(p, tkind.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 @@ -2605,15 +2608,15 @@ fn parsefor(p: *parser) *node = { // `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.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { n.lhs = parseletlocal(p); // init (consumes its own ';') n.cond = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after for cond"); + expecttok(p, tkind.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)) { + if (accepttok(p, tkind.TK_SEMI)) { // cond ; post n.cond = first; n.rhs = parseexpr(p); @@ -2622,11 +2625,11 @@ fn parsefor(p: *parser) *node = { n.cond = first; }; }; - expecttok(p, TK_RPAREN, "expected ')' after for"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after for"); n.body = parseblock(p); // Optional `else { ... }` — runs at normal cond-false exit; skipped // by break. Hare's "did the loop find it?" idiom. - if (accepttok(p, TK_ELSE)) { + if (accepttok(p, tkind.TK_ELSE)) { n.els = parseblock(p); }; return n; @@ -2639,37 +2642,37 @@ fn parsestmt(p: *parser) *node = { // `static` is allowed on local lets per Hare; we accept and skip // it (it doesn't change the AST shape). - if (p.curkind == TK_STATIC) { advance(p); }; + if (p.curkind == tkind.TK_STATIC) { advance(p); }; - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.TK_LBRACE) { let b: *node = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after block"); + expecttok(p, tkind.TK_SEMI, "expected ';' after block"); return b; }; - if (p.curkind == TK_LET) { return parseletlocal(p); }; - if (p.curkind == TK_CONST) { return parseletlocal(p); }; - if (p.curkind == TK_IF) { + if (p.curkind == tkind.TK_LET) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_CONST) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_IF) { let n: *node = parseif(p); - expecttok(p, TK_SEMI, "expected ';' after if"); + expecttok(p, tkind.TK_SEMI, "expected ';' after if"); return n; }; - if (p.curkind == TK_FOR) { + if (p.curkind == tkind.TK_FOR) { let n: *node = parsefor(p); - expecttok(p, TK_SEMI, "expected ';' after for"); + expecttok(p, tkind.TK_SEMI, "expected ';' after for"); return n; }; - if (p.curkind == TK_RETURN) { + if (p.curkind == tkind.TK_RETURN) { advance(p); let n: *node = newnode(p.a, N_RETURN, pf, pl, pc); - if (p.curkind != TK_SEMI) { + if (p.curkind != tkind.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.curkind == TK_COMMA) { + if (p.curkind == tkind.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)) { + for (accepttok(p, tkind.TK_COMMA)) { let e: *node = parseexpr(p); tail.next = e; tail = e; @@ -2679,31 +2682,31 @@ fn parsestmt(p: *parser) *node = { n.lhs = first; }; }; - expecttok(p, TK_SEMI, "expected ';' after return"); + expecttok(p, tkind.TK_SEMI, "expected ';' after return"); return n; }; - if (p.curkind == TK_DEFER) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after defer"); return n; }; - if (p.curkind == TK_YIELD) { + if (p.curkind == tkind.TK_YIELD) { advance(p); let n: *node = newnode(p.a, N_YIELD, pf, pl, pc); n.lhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after yield"); + expecttok(p, tkind.TK_SEMI, "expected ';' after yield"); return n; }; - if (p.curkind == TK_BREAK) { + if (p.curkind == tkind.TK_BREAK) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after break"); + expecttok(p, tkind.TK_SEMI, "expected ';' after break"); return newnode(p.a, N_BREAK, pf, pl, pc); }; - if (p.curkind == TK_CONTINUE) { + if (p.curkind == tkind.TK_CONTINUE) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after continue"); + expecttok(p, tkind.TK_SEMI, "expected ';' after continue"); return newnode(p.a, N_CONTINUE, pf, pl, pc); }; // expression statement, or tuple-destructure multi-assign: @@ -2713,25 +2716,25 @@ fn parsestmt(p: *parser) *node = { // through parsebin(parseunary, 1) so the `=` stays for us to // consume — parseexpr would absorb it. let e: *node = parseexpr(p); - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc); let head: *node = e; let tail: *node = e; - for (p.curkind == TK_COMMA) { + for (p.curkind == tkind.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"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after multi-assign lvalues"); m.rhs = parseexpr(p); m.list = head; - expecttok(p, TK_SEMI, "expected ';' after multi-assign"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement"); return n; }; @@ -2752,7 +2755,7 @@ fn parseuse(p: *parser) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_SEMI, "expected ';' after use"); + expecttok(p, tkind.TK_SEMI, "expected ';' after use"); return n; }; @@ -2766,11 +2769,11 @@ fn parsedef(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in def"); + expecttok(p, tkind.TK_COLON, "expected ':' in def"); n.lhs = parsetype(p); - expecttok(p, TK_ASSIGN, "expected '=' in def"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in def"); n.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after def"); + expecttok(p, tkind.TK_SEMI, "expected ';' after def"); n.exported = exported; return n; }; @@ -2780,31 +2783,31 @@ fn parselet(p: *parser, exported: i32) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; // Accept `let` or `const`. Const-bound bindings are marked via - // n.op = TK_CONST so the checker can reject reassignment. + // n.op = tkind.TK_CONST so the checker can reject reassignment. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); let n: *node = newnode(p.a, N_LET, pf, pl, pc); n.module = p.l.module; let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); n.exported = exported; - if (is_const != 0) { n.op = TK_CONST; }; + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; fn parseattrs(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_AT) { + for (p.curkind == tkind.TK_AT) { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; @@ -2815,11 +2818,11 @@ fn parseattrs(p: *parser) *node = { a.str = id; // `@name(args...)` for FFI-style attrs; `@name` for marker- // only attrs like @test (no parens). - if (accepttok(p, TK_LPAREN)) { + if (accepttok(p, tkind.TK_LPAREN)) { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); a.list = arghead; - expecttok(p, TK_RPAREN, "expected ')' after attribute args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after attribute args"); }; if (head == nil) { head = a; tail = a; } else { tail.next = a; tail = a; }; @@ -2828,7 +2831,7 @@ fn parseattrs(p: *parser) *node = { }; fn parseparams(p: *parser) *node = { - if (p.curkind == TK_RPAREN) { return nil; }; + if (p.curkind == tkind.TK_RPAREN) { return nil; }; let head: *node = nil; let tail: *node = nil; for (true) { @@ -2841,12 +2844,12 @@ fn parseparams(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in parameter"); + expecttok(p, tkind.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.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; return head; }; @@ -2861,20 +2864,20 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_LPAREN, "expected '(' after fn name"); + expecttok(p, tkind.TK_LPAREN, "expected '(' after fn name"); n.list = parseparams(p); - expecttok(p, TK_RPAREN, "expected ')' after params"); - if (p.curkind != TK_ASSIGN) { - if (p.curkind != TK_SEMI) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after params"); + if (p.curkind != tkind.TK_ASSIGN) { + if (p.curkind != tkind.TK_SEMI) { n.lhs = parsetype(p); }; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.body = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after fn body"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn body"); } else { // Body-less fn: FFI declaration (`fn name(args) ret;`). - expecttok(p, TK_SEMI, "expected ';' after fn header"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn header"); }; n.exported = exported; n.attr = attrs; @@ -2891,9 +2894,9 @@ fn parsetypedecl(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_ASSIGN, "expected '=' in type decl"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in type decl"); n.lhs = parsetype(p); - expecttok(p, TK_SEMI, "expected ';' after type decl"); + expecttok(p, tkind.TK_SEMI, "expected ';' after type decl"); n.exported = exported; return n; }; @@ -2926,7 +2929,7 @@ type parser = struct { // nocast: while inside `[...]` we treat ':' as the slice // separator, not the cast operator. Mirrors parse.c's flag. nocast: i32, - curkind: i32, + curkind: i32, // holds a `tkind` value (relaxation lets it mix) curfile: str, curline: i32, curcol: i32, @@ -2974,10 +2977,10 @@ fn expecttok(p: *parser, k: i32, what: str) bool = { return false; }; -// expectident — consume the current TK_IDENT and return its text. +// expectident — consume the current tkind.TK_IDENT and return its text. // Returns the empty str on error (and advances to make progress). fn expectident(p: *parser, into: *str) bool = { - if (p.curkind != TK_IDENT) { + if (p.curkind != tkind.TK_IDENT) { errmsg(p, "expected identifier"); advance(p); return false; @@ -2991,7 +2994,7 @@ fn expectident(p: *parser, into: *str) bool = { // discard marker. On `_`, returns "" so the checker skips // scope_define for the binding. fn expectbindname(p: *parser, into: *str) bool = { - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { let empty: str; *into = empty; advance(p); @@ -3031,7 +3034,7 @@ fn parsetype(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { // `!T` — Hare error-flagged type wrapper. advance(p); let n: *node = newnode(p.a, N_TBANG, pf, pl, pc); @@ -3039,16 +3042,16 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_STAR) { + if (p.curkind == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_TPTR, pf, pl, pc); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); - if (p.curkind == TK_RBRACK) { + if (p.curkind == tkind.TK_RBRACK) { advance(p); let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc); n.lhs = parsetype(p); @@ -3058,24 +3061,24 @@ fn parsetype(p: *parser) *node = { // `[_]T` — length inferred from initialiser. n.rhs stays nil // as the sentinel; the cgen path for N_LET fills it from the // array literal's element count. - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { advance(p); } else { n.rhs = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in array type"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in array type"); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_STRUCT) { + if (p.curkind == tkind.TK_STRUCT) { advance(p); - expecttok(p, TK_LBRACE, "expected '{' after struct"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after struct"); let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc); let fhead: *node = nil; let ftail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let fpf: str = p.curfile; let fpl: i32 = p.curline; let fpc: i32 = p.curcol; @@ -3083,32 +3086,32 @@ fn parsetype(p: *parser) *node = { let fid: str; expectident(p, &fid); f.str = fid; - expecttok(p, TK_COLON, "expected ':' in field"); + expecttok(p, tkind.TK_COLON, "expected ':' in field"); f.lhs = parsetype(p); if (fhead == nil) { fhead = f; ftail = f; } else { ftail.next = f; ftail = f; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct fields"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct fields"); n.list = fhead; return n; }; - if (p.curkind == TK_ENUM) { + if (p.curkind == tkind.TK_ENUM) { // `enum [storage] { NAME [= expr], ... }` // Storage defaults to i32 (lhs == nil). Each member is an // N_TENUMMEMBER with str=name and lhs = value expr or nil // (auto-increment when omitted). advance(p); let n: *node = newnode(p.a, N_TENUM, pf, pl, pc); - if (p.curkind != TK_LBRACE) { + if (p.curkind != tkind.TK_LBRACE) { n.lhs = parsetype(p); }; - expecttok(p, TK_LBRACE, "expected '{' after enum"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after enum"); let mhead: *node = nil; let mtail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let mpf: str = p.curfile; let mpl: i32 = p.curline; let mpc: i32 = p.curcol; @@ -3116,19 +3119,19 @@ fn parsetype(p: *parser) *node = { let mid: str; expectident(p, &mid); m.str = mid; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { m.lhs = parseexpr(p); }; if (mhead == nil) { mhead = m; mtail = m; } else { mtail.next = m; mtail = m; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after enum members"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after enum members"); n.list = mhead; return n; }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { // `void` keyword in type-expr context — emit as N_TNAME so // resolution treats it like any other primitive name. let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); @@ -3137,15 +3140,15 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); let acc: str = p.curtext; advance(p); // Dotted path collapse: pkg.Type → single TNAME with the // joined string. Mirrors C parsetype's loop. - for (p.curkind == TK_DOT) { + for (p.curkind == tkind.TK_DOT) { advance(p); - if (p.curkind != TK_IDENT) { break; }; + if (p.curkind != tkind.TK_IDENT) { break; }; acc = joindotted(p.a, acc, p.curtext); advance(p); }; @@ -3153,11 +3156,11 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { // (T) or (T, T, ...) or (T | T | ...) advance(p); let first: *node = parsetype(p); - if (accepttok(p, TK_PIPE)) { + if (accepttok(p, tkind.TK_PIPE)) { let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc); let head: *node = first; let tail: *node = first; @@ -3165,14 +3168,14 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_PIPE)) { break; }; + if (!accepttok(p, tkind.TK_PIPE)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tagged-union type"); + expecttok(p, tkind.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"); + if (!accepttok(p, tkind.TK_COMMA)) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type"); return first; }; let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc); @@ -3182,23 +3185,23 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_COMMA)) { break; }; - if (p.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple type"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple type"); n.list = head; return n; }; - if (p.curkind == TK_FN) { + if (p.curkind == tkind.TK_FN) { advance(p); - expecttok(p, TK_LPAREN, "expected '(' after fn in type"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after fn type params"); n.lhs = parsetype(p); return n; }; @@ -3216,39 +3219,39 @@ fn parsetype(p: *parser) *node = { // 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; }; + if (k == tkind.TK_OR) { return 1; }; + if (k == tkind.TK_AND) { return 2; }; + if (k == tkind.TK_EQ) { return 3; }; + if (k == tkind.TK_NEQ) { return 3; }; + if (k == tkind.TK_LT) { return 4; }; + if (k == tkind.TK_LE) { return 4; }; + if (k == tkind.TK_GT) { return 4; }; + if (k == tkind.TK_GE) { return 4; }; + if (k == tkind.TK_PIPE) { return 5; }; + if (k == tkind.TK_CARET) { return 6; }; + if (k == tkind.TK_AMP) { return 7; }; + if (k == tkind.TK_LSHIFT) { return 8; }; + if (k == tkind.TK_RSHIFT) { return 8; }; + if (k == tkind.TK_PLUS) { return 9; }; + if (k == tkind.TK_MINUS) { return 9; }; + if (k == tkind.TK_STAR) { return 10; }; + if (k == tkind.TK_SLASH) { return 10; }; + if (k == tkind.TK_PERCENT) { return 10; }; return 0; }; fn isassignop(k: 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; }; + if (k == tkind.TK_ASSIGN) { return true; }; + if (k == tkind.TK_PLUSEQ) { return true; }; + if (k == tkind.TK_MINUSEQ) { return true; }; + if (k == tkind.TK_STAREQ) { return true; }; + if (k == tkind.TK_SLASHEQ) { return true; }; + if (k == tkind.TK_PERCENTEQ) { return true; }; + if (k == tkind.TK_AMPEQ) { return true; }; + if (k == tkind.TK_PIPEEQ) { return true; }; + if (k == tkind.TK_CARETEQ) { return true; }; + if (k == tkind.TK_LSHIFTEQ) { return true; }; + if (k == tkind.TK_RSHIFTEQ) { return true; }; return false; }; @@ -3260,36 +3263,36 @@ export fn parsefile(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_EOF) { + for (p.curkind != tkind.TK_EOF) { let attrs: *node = parseattrs(p); let exported: i32 = 0; - if (p.curkind == TK_EXPORT) { exported = 1; advance(p); }; + if (p.curkind == tkind.TK_EXPORT) { exported = 1; advance(p); }; let d: *node = nil; - if (p.curkind == TK_USE) { + if (p.curkind == tkind.TK_USE) { d = parseuse(p); - } else { if (p.curkind == TK_DEF) { + } else { if (p.curkind == tkind.TK_DEF) { d = parsedef(p, exported); - } else { if (p.curkind == TK_TYPE) { + } else { if (p.curkind == tkind.TK_TYPE) { d = parsetypedecl(p, exported); - } else { if (p.curkind == TK_LET) { + } else { if (p.curkind == tkind.TK_LET) { d = parselet(p, exported); - } else { if (p.curkind == TK_CONST) { + } else { if (p.curkind == tkind.TK_CONST) { d = parselet(p, exported); - } else { if (p.curkind == TK_FN) { + } else { if (p.curkind == tkind.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.curkind != TK_SEMI) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { + for (p.curkind != tkind.TK_SEMI) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { let depth: i32 = 0; for (true) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { depth += 1; advance(p); continue; }; - if (p.curkind == TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { depth += 1; advance(p); continue; }; + if (p.curkind == tkind.TK_RBRACE) { depth -= 1; advance(p); if (depth == 0) { break; }; @@ -3301,7 +3304,7 @@ export fn parsefile(p: *parser) *node = { }; advance(p); }; - if (p.curkind == TK_SEMI) { advance(p); }; + if (p.curkind == tkind.TK_SEMI) { advance(p); }; };};};};};}; if (d != nil) { @@ -6639,10 +6642,10 @@ fn cgun(c: *cgen, n: *node) void = { // address / deref. The wasted load before AMP keeps our asm // byte-identical to the C version. cgexpr(c, n.lhs); - if (n.op == TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; - if (n.op == TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; - if (n.op == TK_AMP) { + if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; + if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; + if (n.op == tkind.TK_AMP) { let opnd: *node = n.lhs; if (opnd != nil) { if (opnd.kind == N_IDENT) { @@ -6658,7 +6661,7 @@ fn cgun(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_NOT) { + if (n.op == tkind.TK_NOT) { let t: str = mklabel(c, "tt"); let e: str = mklabel(c, "te"); emitline("\tCMPQ\t$0, AX\n"); @@ -6681,48 +6684,48 @@ fn cgbin(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tAX\n"); cgexpr(c, n.lhs); emitline("\tPOPQ\tBX\n"); - if (n.op == TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; - if (n.op == TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; - if (n.op == TK_SLASH) { + if (n.op == tkind.TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_SLASH) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; return; }; - if (n.op == TK_PERCENT) { + if (n.op == tkind.TK_PERCENT) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; }; - if (n.op == TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; - if (n.op == TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; - if (n.op == TK_LSHIFT) { + if (n.op == tkind.TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_LSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHLQ\tCX, AX\n"); return; }; - if (n.op == TK_RSHIFT) { + if (n.op == tkind.TK_RSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHRQ\tCX, AX\n"); return; }; - if (n.op == TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; // Comparison: emit CMPQ, jump on signed/unsigned variant, // materialise 0/1 in AX. Same shape as the C cgen. let iscmp: bool = false; let jcc: str = ""; - if (n.op == TK_EQ) { iscmp = true; jcc = "JE"; }; - if (n.op == TK_NEQ) { iscmp = true; jcc = "JNE"; }; - if (n.op == TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; - if (n.op == TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; - if (n.op == TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; - if (n.op == TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; + if (n.op == tkind.TK_EQ) { iscmp = true; jcc = "JE"; }; + if (n.op == tkind.TK_NEQ) { iscmp = true; jcc = "JNE"; }; + if (n.op == tkind.TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; + if (n.op == tkind.TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; + if (n.op == tkind.TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; + if (n.op == tkind.TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; if (iscmp) { let t: str = mklabel(c, "ct"); let e: str = mklabel(c, "ce"); @@ -6846,11 +6849,11 @@ fn cgassign(c: *cgen, n: *node) void = { let lhs: *node = n.lhs; // Discard lvalue `_ = expr;` — evaluate rhs for side effects, // write nothing. Detected by lhs being an N_IDENT with empty str - // (planted by parseprimary on the TK_UNDER token). + // (planted by parseprimary on the tkind.TK_UNDER token). if (lhs != nil) { if (lhs.kind == N_IDENT) { if (lhs.str.len == 0) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); return; }; @@ -6864,8 +6867,8 @@ fn cgassign(c: *cgen, n: *node) void = { // `*bool` / `*u8` / `*i32` we narrow via the local's tnode. if (lhs != nil) { if (lhs.kind == N_UN) { - if (lhs.op == TK_STAR) { - if (n.op == TK_ASSIGN) { + if (lhs.op == tkind.TK_STAR) { + if (n.op == tkind.TK_ASSIGN) { let inner: *node = lhs.lhs; let elemstr: bool = false; let storeop: str = "MOVQ"; @@ -6923,7 +6926,7 @@ fn cgassign(c: *cgen, n: *node) void = { // from base.tnode picks MOVB vs MOVQ. if (lhs != nil) { if (lhs.kind == N_INDEX) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { let base: *node = lhs.lhs; let idx: *node = lhs.rhs; let esz: i32 = 8; @@ -7012,7 +7015,7 @@ fn cgassign(c: *cgen, n: *node) void = { for (fi != nil) { let fn_: str = fi.fname; if (streq(fn_, fld)) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // compound: load current value emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -7026,13 +7029,13 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tBX\n"); }; cgexpr(c, n.rhs); - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs (BX is old lhs, // AX is rhs). - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -7041,7 +7044,7 @@ fn cgassign(c: *cgen, n: *node) void = { // (AX=ptr, BX=len). Use CX as the // address scratch so we don't clobber // the len half before storing it. - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -7109,7 +7112,7 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (innerkind == N_TSLICE) { innerstr = true; }; if (innerstr) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // Compound on `(*str|*slice).field`: load // current → push → eval rhs → combine → store. emitline("\tMOVQ\t"); @@ -7123,8 +7126,8 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs. - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -7179,7 +7182,7 @@ fn cgassign(c: *cgen, n: *node) void = { let fi: *fieldinfo = si.fields; for (fi != nil) { if (streq(fi.fname, fld)) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { // str rhs: AX=ptr, BX=len. // Stash both, then load @@ -7236,7 +7239,7 @@ fn cgassign(c: *cgen, n: *node) void = { let lcn: *local = localfindnode(c, nm); if (lcn != nil) { lcstr = isstrtype(c, lcn.tnode); }; cgexpr(c, n.rhs); - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -7247,13 +7250,13 @@ fn cgassign(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_PLUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); return; }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -7263,15 +7266,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\t"); emitoff(off: i64); emitline("(BP), BX\n"); - if (n.op == TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; - if (n.op == TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; - if (n.op == TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; - if (n.op == TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; - if (n.op == TK_LSHIFTEQ) { + if (n.op == tkind.TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; + if (n.op == tkind.TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; + if (n.op == tkind.TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_LSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHLQ\tCX, BX\n"); }; - if (n.op == TK_RSHIFTEQ) { + if (n.op == tkind.TK_RSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHRQ\tCX, BX\n"); }; @@ -7692,7 +7695,7 @@ fn cglet(c: *cgen, n: *node) void = { // For each field in the lit, evaluate its value and store at // the field's offset within the slot. Field-name → offset // from the struct registry. When the literal carries - // op == TK_ELLIPSIS (autofill marker from the parser), the + // op == tkind.TK_ELLIPSIS (autofill marker from the parser), the // entire slot is zero-filled first so unmentioned fields // read as 0. if (rhs.kind == N_STRUCTLIT) { @@ -7705,7 +7708,7 @@ fn cglet(c: *cgen, n: *node) void = { }; let si: *structinfo = structlookup(c, sname); if (si != nil) { - if (rhs.op == TK_ELLIPSIS) { + if (rhs.op == tkind.TK_ELLIPSIS) { let total: i32 = si.totsize; emitline("\tXORQ\tAX, AX\n"); let zi: i32 = 0; @@ -8426,31 +8429,31 @@ fn enumevalmember(prev: *enummember, e: *node, out: *u64) bool = { if (!enumevalmember(prev, e.lhs, &a)) { return false; }; if (!enumevalmember(prev, e.rhs, &b)) { return false; }; let op: i32 = e.op; - if (op == TK_PLUS) { *out = a + b; return true; }; - if (op == TK_MINUS) { *out = a - b; return true; }; - if (op == TK_STAR) { *out = a * b; return true; }; - if (op == TK_SLASH) { + if (op == tkind.TK_PLUS) { *out = a + b; return true; }; + if (op == tkind.TK_MINUS) { *out = a - b; return true; }; + if (op == tkind.TK_STAR) { *out = a * b; return true; }; + if (op == tkind.TK_SLASH) { if (b == 0u64) { return false; }; *out = a / b; return true; }; - if (op == TK_PERCENT) { + if (op == tkind.TK_PERCENT) { if (b == 0u64) { return false; }; *out = a % b; return true; }; - if (op == TK_AMP) { *out = a & b; return true; }; - if (op == TK_PIPE) { *out = a | b; return true; }; - if (op == TK_CARET) { *out = a ^ b; return true; }; - if (op == TK_LSHIFT) { *out = a << b; return true; }; - if (op == TK_RSHIFT) { *out = a >> b; return true; }; + if (op == tkind.TK_AMP) { *out = a & b; return true; }; + if (op == tkind.TK_PIPE) { *out = a | b; return true; }; + if (op == tkind.TK_CARET) { *out = a ^ b; return true; }; + if (op == tkind.TK_LSHIFT) { *out = a << b; return true; }; + if (op == tkind.TK_RSHIFT) { *out = a >> b; return true; }; return false; }; if (k == N_UN) { let v: u64; if (!enumevalmember(prev, e.lhs, &v)) { return false; }; let op: i32 = e.op; - if (op == TK_MINUS) { *out = (-(v: i64)): u64; return true; }; - if (op == TK_TILDE) { *out = ~v; return true; }; - if (op == TK_PLUS) { *out = v; return true; }; + if (op == tkind.TK_MINUS) { *out = (-(v: i64)): u64; return true; }; + if (op == tkind.TK_TILDE) { *out = ~v; return true; }; + if (op == tkind.TK_PLUS) { *out = v; return true; }; return false; }; return false; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 881cde9c..7ba34bf8 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -111,31 +111,31 @@ fn enumevalmember(prev: *enummember, e: *node, out: *u64) bool = { if (!enumevalmember(prev, e.lhs, &a)) { return false; }; if (!enumevalmember(prev, e.rhs, &b)) { return false; }; let op: i32 = e.op; - if (op == TK_PLUS) { *out = a + b; return true; }; - if (op == TK_MINUS) { *out = a - b; return true; }; - if (op == TK_STAR) { *out = a * b; return true; }; - if (op == TK_SLASH) { + if (op == tkind.TK_PLUS) { *out = a + b; return true; }; + if (op == tkind.TK_MINUS) { *out = a - b; return true; }; + if (op == tkind.TK_STAR) { *out = a * b; return true; }; + if (op == tkind.TK_SLASH) { if (b == 0u64) { return false; }; *out = a / b; return true; }; - if (op == TK_PERCENT) { + if (op == tkind.TK_PERCENT) { if (b == 0u64) { return false; }; *out = a % b; return true; }; - if (op == TK_AMP) { *out = a & b; return true; }; - if (op == TK_PIPE) { *out = a | b; return true; }; - if (op == TK_CARET) { *out = a ^ b; return true; }; - if (op == TK_LSHIFT) { *out = a << b; return true; }; - if (op == TK_RSHIFT) { *out = a >> b; return true; }; + if (op == tkind.TK_AMP) { *out = a & b; return true; }; + if (op == tkind.TK_PIPE) { *out = a | b; return true; }; + if (op == tkind.TK_CARET) { *out = a ^ b; return true; }; + if (op == tkind.TK_LSHIFT) { *out = a << b; return true; }; + if (op == tkind.TK_RSHIFT) { *out = a >> b; return true; }; return false; }; if (k == N_UN) { let v: u64; if (!enumevalmember(prev, e.lhs, &v)) { return false; }; let op: i32 = e.op; - if (op == TK_MINUS) { *out = (-(v: i64)): u64; return true; }; - if (op == TK_TILDE) { *out = ~v; return true; }; - if (op == TK_PLUS) { *out = v; return true; }; + if (op == tkind.TK_MINUS) { *out = (-(v: i64)): u64; return true; }; + if (op == tkind.TK_TILDE) { *out = ~v; return true; }; + if (op == tkind.TK_PLUS) { *out = v; return true; }; return false; }; return false; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 7562673e..1423abbb 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -935,10 +935,10 @@ fn cgun(c: *cgen, n: *node) void = { // address / deref. The wasted load before AMP keeps our asm // byte-identical to the C version. cgexpr(c, n.lhs); - if (n.op == TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; - if (n.op == TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; - if (n.op == TK_AMP) { + if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; + if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; + if (n.op == tkind.TK_AMP) { let opnd: *node = n.lhs; if (opnd != nil) { if (opnd.kind == N_IDENT) { @@ -954,7 +954,7 @@ fn cgun(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_NOT) { + if (n.op == tkind.TK_NOT) { let t: str = mklabel(c, "tt"); let e: str = mklabel(c, "te"); emitline("\tCMPQ\t$0, AX\n"); @@ -977,48 +977,48 @@ fn cgbin(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tAX\n"); cgexpr(c, n.lhs); emitline("\tPOPQ\tBX\n"); - if (n.op == TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; - if (n.op == TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; - if (n.op == TK_SLASH) { + if (n.op == tkind.TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_SLASH) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; return; }; - if (n.op == TK_PERCENT) { + if (n.op == tkind.TK_PERCENT) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; }; - if (n.op == TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; - if (n.op == TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; - if (n.op == TK_LSHIFT) { + if (n.op == tkind.TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_LSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHLQ\tCX, AX\n"); return; }; - if (n.op == TK_RSHIFT) { + if (n.op == tkind.TK_RSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHRQ\tCX, AX\n"); return; }; - if (n.op == TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; // Comparison: emit CMPQ, jump on signed/unsigned variant, // materialise 0/1 in AX. Same shape as the C cgen. let iscmp: bool = false; let jcc: str = ""; - if (n.op == TK_EQ) { iscmp = true; jcc = "JE"; }; - if (n.op == TK_NEQ) { iscmp = true; jcc = "JNE"; }; - if (n.op == TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; - if (n.op == TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; - if (n.op == TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; - if (n.op == TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; + if (n.op == tkind.TK_EQ) { iscmp = true; jcc = "JE"; }; + if (n.op == tkind.TK_NEQ) { iscmp = true; jcc = "JNE"; }; + if (n.op == tkind.TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; + if (n.op == tkind.TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; + if (n.op == tkind.TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; + if (n.op == tkind.TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; if (iscmp) { let t: str = mklabel(c, "ct"); let e: str = mklabel(c, "ce"); @@ -1142,11 +1142,11 @@ fn cgassign(c: *cgen, n: *node) void = { let lhs: *node = n.lhs; // Discard lvalue `_ = expr;` — evaluate rhs for side effects, // write nothing. Detected by lhs being an N_IDENT with empty str - // (planted by parseprimary on the TK_UNDER token). + // (planted by parseprimary on the tkind.TK_UNDER token). if (lhs != nil) { if (lhs.kind == N_IDENT) { if (lhs.str.len == 0) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); return; }; @@ -1160,8 +1160,8 @@ fn cgassign(c: *cgen, n: *node) void = { // `*bool` / `*u8` / `*i32` we narrow via the local's tnode. if (lhs != nil) { if (lhs.kind == N_UN) { - if (lhs.op == TK_STAR) { - if (n.op == TK_ASSIGN) { + if (lhs.op == tkind.TK_STAR) { + if (n.op == tkind.TK_ASSIGN) { let inner: *node = lhs.lhs; let elemstr: bool = false; let storeop: str = "MOVQ"; @@ -1219,7 +1219,7 @@ fn cgassign(c: *cgen, n: *node) void = { // from base.tnode picks MOVB vs MOVQ. if (lhs != nil) { if (lhs.kind == N_INDEX) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { let base: *node = lhs.lhs; let idx: *node = lhs.rhs; let esz: i32 = 8; @@ -1308,7 +1308,7 @@ fn cgassign(c: *cgen, n: *node) void = { for (fi != nil) { let fn_: str = fi.fname; if (streq(fn_, fld)) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // compound: load current value emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -1322,13 +1322,13 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tBX\n"); }; cgexpr(c, n.rhs); - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs (BX is old lhs, // AX is rhs). - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -1337,7 +1337,7 @@ fn cgassign(c: *cgen, n: *node) void = { // (AX=ptr, BX=len). Use CX as the // address scratch so we don't clobber // the len half before storing it. - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -1405,7 +1405,7 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (innerkind == N_TSLICE) { innerstr = true; }; if (innerstr) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // Compound on `(*str|*slice).field`: load // current → push → eval rhs → combine → store. emitline("\tMOVQ\t"); @@ -1419,8 +1419,8 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs. - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -1475,7 +1475,7 @@ fn cgassign(c: *cgen, n: *node) void = { let fi: *fieldinfo = si.fields; for (fi != nil) { if (streq(fi.fname, fld)) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { // str rhs: AX=ptr, BX=len. // Stash both, then load @@ -1532,7 +1532,7 @@ fn cgassign(c: *cgen, n: *node) void = { let lcn: *local = localfindnode(c, nm); if (lcn != nil) { lcstr = isstrtype(c, lcn.tnode); }; cgexpr(c, n.rhs); - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -1543,13 +1543,13 @@ fn cgassign(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_PLUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); return; }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -1559,15 +1559,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\t"); emitoff(off: i64); emitline("(BP), BX\n"); - if (n.op == TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; - if (n.op == TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; - if (n.op == TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; - if (n.op == TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; - if (n.op == TK_LSHIFTEQ) { + if (n.op == tkind.TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; + if (n.op == tkind.TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; + if (n.op == tkind.TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_LSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHLQ\tCX, BX\n"); }; - if (n.op == TK_RSHIFTEQ) { + if (n.op == tkind.TK_RSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHRQ\tCX, BX\n"); }; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index a1ede22b..23b842a6 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -403,7 +403,7 @@ fn cglet(c: *cgen, n: *node) void = { // For each field in the lit, evaluate its value and store at // the field's offset within the slot. Field-name → offset // from the struct registry. When the literal carries - // op == TK_ELLIPSIS (autofill marker from the parser), the + // op == tkind.TK_ELLIPSIS (autofill marker from the parser), the // entire slot is zero-filled first so unmentioned fields // read as 0. if (rhs.kind == N_STRUCTLIT) { @@ -416,7 +416,7 @@ fn cglet(c: *cgen, n: *node) void = { }; let si: *structinfo = structlookup(c, sname); if (si != nil) { - if (rhs.op == TK_ELLIPSIS) { + if (rhs.op == tkind.TK_ELLIPSIS) { let total: i32 = si.totsize; emitline("\tXORQ\tAX, AX\n"); let zi: i32 = 0; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index eb8c5b96..7f36b360 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -459,108 +459,110 @@ export fn stou64(s: str) (u64 | invalid | overflow) = { 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 -------------------------------------------------------- // @@ -579,13 +581,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 }; @@ -602,39 +605,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 ---------------------------------------------------------- @@ -643,101 +646,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 ""; }; - 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 ""; }; + 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 ""; }; + if (k == tkind.TK_LAST) { return ""; }; return ""; }; @@ -836,25 +839,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. @@ -1274,7 +1277,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; @@ -1333,9 +1336,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; @@ -1347,7 +1350,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; }; }; @@ -1413,16 +1416,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); }; @@ -1435,7 +1438,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; @@ -1466,7 +1469,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; @@ -1480,7 +1483,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; @@ -1496,7 +1499,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; @@ -1504,7 +1507,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; @@ -1530,7 +1533,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 @@ -1544,7 +1547,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); @@ -1560,97 +1563,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; @@ -2037,42 +2040,42 @@ fn parseprimary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc); n.uval = p.curuval; n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_STR) { + if (p.curkind == tkind.TK_STR) { let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc); n.str = p.curtext; advance(p); return n; }; - if (p.curkind == TK_RUNE) { + if (p.curkind == tkind.TK_RUNE) { let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc); n.uval = p.curuval; advance(p); return n; }; - if (p.curkind == TK_TRUE) { + if (p.curkind == tkind.TK_TRUE) { advance(p); return newnode(p.a, N_TRUE, pf, pl, pc); }; - if (p.curkind == TK_FALSE) { + if (p.curkind == tkind.TK_FALSE) { advance(p); return newnode(p.a, N_FALSE, pf, pl, pc); }; - if (p.curkind == TK_NIL) { + if (p.curkind == tkind.TK_NIL) { advance(p); return newnode(p.a, N_NIL, pf, pl, pc); }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { advance(p); return newnode(p.a, N_VOIDLIT, pf, pl, pc); }; - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { // Bare `_` — valid only as a discard lvalue. Emit an N_IDENT // with empty str; the checker rejects it outside lvalue // positions. @@ -2082,7 +2085,7 @@ fn parseprimary(p: *parser) *node = { n.str = empty; return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { // Array literal `[a, b, c]` or `[v, w...]` (repeat suffix). // The repeat marker is an N_FIELD node with str = "..." // appended to the element list so cgen can detect it. @@ -2090,12 +2093,12 @@ fn parseprimary(p: *parser) *node = { let n: *node = newnode(p.a, N_ARRLIT, pf, pl, pc); let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_RBRACK) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACK) { + if (p.curkind == tkind.TK_EOF) { break; }; let e: *node = parseexpr(p); if (head == nil) { head = e; tail = e; } else { tail.next = e; tail = e; }; - if (accepttok(p, TK_ELLIPSIS)) { + if (accepttok(p, tkind.TK_ELLIPSIS)) { let rep: *node = newnode(p.a, N_FIELD, p.curfile, p.curline, p.curcol); rep.str = "..."; @@ -2103,34 +2106,34 @@ fn parseprimary(p: *parser) *node = { tail = rep; break; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACK, "expected ']' after array literal"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after array literal"); n.list = head; return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let e: *node = parseexpr(p); // Tuple literal: (a, b, ...) - if (accepttok(p, TK_COMMA)) { + if (accepttok(p, tkind.TK_COMMA)) { let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc); t.list = e; let tail: *node = e; for (true) { - if (p.curkind == TK_RPAREN) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; let en: *node = parseexpr(p); tail.next = en; tail = en; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple"); return t; }; - expecttok(p, TK_RPAREN, "expected ')'"); + expecttok(p, tkind.TK_RPAREN, "expected ')'"); return e; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_IDENT, pf, pl, pc); n.str = p.curtext; advance(p); @@ -2140,19 +2143,19 @@ fn parseprimary(p: *parser) *node = { // expressions, never directly from cond contexts that need a // block; in stmt parsing, the for/if drivers consume their // own paren/cond, so this is safe. - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; // Trailing `...` autofill marker. Stash on s.op so // cgen can zero-fill the slot before per-field stores. - if (p.curkind == TK_ELLIPSIS) { + if (p.curkind == tkind.TK_ELLIPSIS) { advance(p); - s.op = TK_ELLIPSIS; + s.op = tkind.TK_ELLIPSIS; break; }; let fpf: str = p.curfile; @@ -2160,53 +2163,53 @@ fn parseprimary(p: *parser) *node = { let fpc: i32 = p.curcol; let id: str; expectident(p, &id); - expecttok(p, TK_ASSIGN, "expected '=' in struct lit field"); + expecttok(p, tkind.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; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct literal"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct literal"); s.list = head; return s; }; return n; }; - if (p.curkind == TK_MATCH) { + if (p.curkind == tkind.TK_MATCH) { // match (e) { case let v: T => stmt; case T => stmt; case => stmt; }; advance(p); - expecttok(p, TK_LPAREN, "expected '(' after match"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after match scrutinee"); + expecttok(p, tkind.TK_LBRACE, "expected '{' to open match body"); let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_CASE) { + for (p.curkind == tkind.TK_CASE) { let cf: str = p.curfile; let cl: i32 = p.curline; let cc: i32 = p.curcol; advance(p); // past `case` let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc); - if (p.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { advance(p); let id: str; expectident(p, &id); mc.str = id; - expecttok(p, TK_COLON, "expected ':' after match binding"); + expecttok(p, tkind.TK_COLON, "expected ':' after match binding"); mc.lhs = parsetype(p); - } else { if (p.curkind != TK_FATARROW) { + } else { if (p.curkind != tkind.TK_FATARROW) { mc.lhs = parsetype(p); };}; - expecttok(p, TK_FATARROW, "expected '=>' in match arm"); + expecttok(p, tkind.TK_FATARROW, "expected '=>' in match arm"); mc.body = parsestmt(p); if (head == nil) { head = mc; tail = mc; } else { tail.next = mc; tail = mc; }; }; - expecttok(p, TK_RBRACE, "expected '}' after match body"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after match body"); m.list = head; return m; }; @@ -2224,7 +2227,7 @@ fn parsearglist(p: *parser, closekind: i32, headout: **node) void = { 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 (!accepttok(p, tkind.TK_COMMA)) { break; }; if (p.curkind == closekind) { break; }; }; *headout = head; @@ -2236,7 +2239,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let n: *node = newnode(p.a, N_CALL, pf, pl, pc); n.lhs = cur; @@ -2251,24 +2254,24 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { n.list = parsetype(p); } else { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); n.list = arghead; }; - expecttok(p, TK_RPAREN, "expected ')' after args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after args"); cur = n; continue; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); // `[ : hi ]` — slice with implicit lo = 0. - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in slice"); cur = n; continue; }; @@ -2278,33 +2281,33 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { p.nocast = 1; let e: *node = parseexpr(p); p.nocast = prev; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { advance(p); let n: *node = newnode(p.a, N_SLICE, pf, pl, pc); n.lhs = cur; n.rhs = e; - if (p.curkind != TK_RBRACK) { + if (p.curkind != tkind.TK_RBRACK) { n.cond = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in slice"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RBRACK, "expected ']' after index"); cur = n; continue; }; - if (p.curkind == TK_DOT) { + if (p.curkind == tkind.TK_DOT) { advance(p); let n: *node = newnode(p.a, N_DOT, pf, pl, pc); n.lhs = cur; // Hare-style tuple field access: `t.0`, `t.1`. The // numeric literal becomes the field name string so the // cgen tuple-positional path matches `cmd/wcc/parse.c`. - if (p.curkind == TK_INT) { + if (p.curkind == tkind.TK_INT) { n.str = p.curtext; advance(p); } else { @@ -2315,7 +2318,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_COLON) { + if (p.curkind == tkind.TK_COLON) { if (p.nocast != 0) { return cur; }; @@ -2330,7 +2333,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { // `e as T` — assert lhs is variant T (abort otherwise) → T // `e is T` — bool: does lhs currently hold variant T? // Same precedence level as the `:` cast. - if (p.curkind == TK_AS) { + if (p.curkind == tkind.TK_AS) { advance(p); let n: *node = newnode(p.a, N_TYPEASSERT, pf, pl, pc); n.lhs = cur; @@ -2338,7 +2341,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { cur = n; continue; }; - if (p.curkind == TK_IS) { + if (p.curkind == tkind.TK_IS) { advance(p); let n: *node = newnode(p.a, N_TYPETEST, pf, pl, pc); n.lhs = cur; @@ -2348,14 +2351,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = { }; // `e?` — propagate error variant up the stack. // `e!` — abort on error variant. - if (p.curkind == TK_QUESTION) { + if (p.curkind == tkind.TK_QUESTION) { advance(p); let n: *node = newnode(p.a, N_TRYPROP, pf, pl, pc); n.lhs = cur; cur = n; continue; }; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_TRYUNW, pf, pl, pc); n.lhs = cur; @@ -2372,40 +2375,40 @@ fn parseunary(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; let k: i32 = p.curkind; - if (k == TK_MINUS) { + if (k == tkind.TK_MINUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_MINUS; n.lhs = parseunary(p); + n.op = tkind.TK_MINUS; n.lhs = parseunary(p); return n; }; - if (k == TK_PLUS) { + if (k == tkind.TK_PLUS) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_PLUS; n.lhs = parseunary(p); + n.op = tkind.TK_PLUS; n.lhs = parseunary(p); return n; }; - if (k == TK_NOT) { + if (k == tkind.TK_NOT) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_NOT; n.lhs = parseunary(p); + n.op = tkind.TK_NOT; n.lhs = parseunary(p); return n; }; - if (k == TK_TILDE) { + if (k == tkind.TK_TILDE) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_TILDE; n.lhs = parseunary(p); + n.op = tkind.TK_TILDE; n.lhs = parseunary(p); return n; }; - if (k == TK_STAR) { + if (k == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_STAR; n.lhs = parseunary(p); + n.op = tkind.TK_STAR; n.lhs = parseunary(p); return n; }; - if (k == TK_AMP) { + if (k == tkind.TK_AMP) { advance(p); let n: *node = newnode(p.a, N_UN, pf, pl, pc); - n.op = TK_AMP; n.lhs = parseunary(p); + n.op = tkind.TK_AMP; n.lhs = parseunary(p); return n; }; return parsepostfix(p, parseprimary(p)); @@ -2464,15 +2467,15 @@ fn parseletlocal(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - // `let` or `const`. Const-bound locals are marked via n.op = TK_CONST. + // `let` or `const`. Const-bound locals are marked via n.op = tkind.TK_CONST. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); // Hare-style tuple destructure: `let (a, b) = expr;`. // Types are optional per binding (matches C parser; Hare itself // doesn't allow types here, but cmd/wcc/parse.c does). - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { advance(p); let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = nil; @@ -2485,21 +2488,21 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); l.str = id; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; if (head == nil) { head = l; } else { tail.next = l; }; tail = l; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in let destructure"); - expecttok(p, TK_ASSIGN, "expected '=' after let destructure"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in let destructure"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let destructure"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; @@ -2508,17 +2511,17 @@ fn parseletlocal(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; // Comma-multi-let: `let n, s = call();` (ww extension over Hare). // Collects (name, type) pairs, then '=' rhs. Each binding gets // its own N_LET; the wrapping N_MLET carries the rhs. - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MLET, pf, pl, pc); let head: *node = n; let tail: *node = n; - for (accepttok(p, TK_COMMA)) { + for (accepttok(p, tkind.TK_COMMA)) { let lpf: str = p.curfile; let lpl: i32 = p.curline; let lpc: i32 = p.curcol; @@ -2526,26 +2529,26 @@ fn parseletlocal(p: *parser) *node = { let id2: str; expectbindname(p, &id2); l.str = id2; - if (accepttok(p, TK_COLON)) { l.lhs = parsetype(p); }; + if (accepttok(p, tkind.TK_COLON)) { l.lhs = parsetype(p); }; tail.next = l; tail = l; }; - expecttok(p, TK_ASSIGN, "expected '=' after let names"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after let names"); m.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); m.list = head; if (is_const != 0) { - m.op = TK_CONST; + m.op = tkind.TK_CONST; let lc: *node = head; - for (lc != nil) { lc.op = TK_CONST; lc = lc.next; }; + for (lc != nil) { lc.op = tkind.TK_CONST; lc = lc.next; }; }; return m; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); - if (is_const != 0) { n.op = TK_CONST; }; + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; @@ -2553,19 +2556,19 @@ fn parseblock(p: *parser) *node = { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; - expecttok(p, TK_LBRACE, "expected '{' to open block"); + expecttok(p, tkind.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.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_RBRACE, "expected '}' to close block"); blk.list = head; return blk; }; @@ -2575,13 +2578,13 @@ fn parseif(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `if` - expecttok(p, TK_LPAREN, "expected '(' after if"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition"); n.body = parseblock(p); - if (accepttok(p, TK_ELSE)) { - if (p.curkind == TK_IF) { + if (accepttok(p, tkind.TK_ELSE)) { + if (p.curkind == tkind.TK_IF) { n.els = parseif(p); } else { n.els = parseblock(p); @@ -2595,7 +2598,7 @@ fn parsefor(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; advance(p); // past `for` - expecttok(p, TK_LPAREN, "expected '(' after for"); + expecttok(p, tkind.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 @@ -2605,15 +2608,15 @@ fn parsefor(p: *parser) *node = { // `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.curkind == TK_LET) { + if (p.curkind == tkind.TK_LET) { n.lhs = parseletlocal(p); // init (consumes its own ';') n.cond = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after for cond"); + expecttok(p, tkind.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)) { + if (accepttok(p, tkind.TK_SEMI)) { // cond ; post n.cond = first; n.rhs = parseexpr(p); @@ -2622,11 +2625,11 @@ fn parsefor(p: *parser) *node = { n.cond = first; }; }; - expecttok(p, TK_RPAREN, "expected ')' after for"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after for"); n.body = parseblock(p); // Optional `else { ... }` — runs at normal cond-false exit; skipped // by break. Hare's "did the loop find it?" idiom. - if (accepttok(p, TK_ELSE)) { + if (accepttok(p, tkind.TK_ELSE)) { n.els = parseblock(p); }; return n; @@ -2639,37 +2642,37 @@ fn parsestmt(p: *parser) *node = { // `static` is allowed on local lets per Hare; we accept and skip // it (it doesn't change the AST shape). - if (p.curkind == TK_STATIC) { advance(p); }; + if (p.curkind == tkind.TK_STATIC) { advance(p); }; - if (p.curkind == TK_LBRACE) { + if (p.curkind == tkind.TK_LBRACE) { let b: *node = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after block"); + expecttok(p, tkind.TK_SEMI, "expected ';' after block"); return b; }; - if (p.curkind == TK_LET) { return parseletlocal(p); }; - if (p.curkind == TK_CONST) { return parseletlocal(p); }; - if (p.curkind == TK_IF) { + if (p.curkind == tkind.TK_LET) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_CONST) { return parseletlocal(p); }; + if (p.curkind == tkind.TK_IF) { let n: *node = parseif(p); - expecttok(p, TK_SEMI, "expected ';' after if"); + expecttok(p, tkind.TK_SEMI, "expected ';' after if"); return n; }; - if (p.curkind == TK_FOR) { + if (p.curkind == tkind.TK_FOR) { let n: *node = parsefor(p); - expecttok(p, TK_SEMI, "expected ';' after for"); + expecttok(p, tkind.TK_SEMI, "expected ';' after for"); return n; }; - if (p.curkind == TK_RETURN) { + if (p.curkind == tkind.TK_RETURN) { advance(p); let n: *node = newnode(p.a, N_RETURN, pf, pl, pc); - if (p.curkind != TK_SEMI) { + if (p.curkind != tkind.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.curkind == TK_COMMA) { + if (p.curkind == tkind.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)) { + for (accepttok(p, tkind.TK_COMMA)) { let e: *node = parseexpr(p); tail.next = e; tail = e; @@ -2679,31 +2682,31 @@ fn parsestmt(p: *parser) *node = { n.lhs = first; }; }; - expecttok(p, TK_SEMI, "expected ';' after return"); + expecttok(p, tkind.TK_SEMI, "expected ';' after return"); return n; }; - if (p.curkind == TK_DEFER) { + if (p.curkind == tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after defer"); return n; }; - if (p.curkind == TK_YIELD) { + if (p.curkind == tkind.TK_YIELD) { advance(p); let n: *node = newnode(p.a, N_YIELD, pf, pl, pc); n.lhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after yield"); + expecttok(p, tkind.TK_SEMI, "expected ';' after yield"); return n; }; - if (p.curkind == TK_BREAK) { + if (p.curkind == tkind.TK_BREAK) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after break"); + expecttok(p, tkind.TK_SEMI, "expected ';' after break"); return newnode(p.a, N_BREAK, pf, pl, pc); }; - if (p.curkind == TK_CONTINUE) { + if (p.curkind == tkind.TK_CONTINUE) { advance(p); - expecttok(p, TK_SEMI, "expected ';' after continue"); + expecttok(p, tkind.TK_SEMI, "expected ';' after continue"); return newnode(p.a, N_CONTINUE, pf, pl, pc); }; // expression statement, or tuple-destructure multi-assign: @@ -2713,25 +2716,25 @@ fn parsestmt(p: *parser) *node = { // through parsebin(parseunary, 1) so the `=` stays for us to // consume — parseexpr would absorb it. let e: *node = parseexpr(p); - if (p.curkind == TK_COMMA) { + if (p.curkind == tkind.TK_COMMA) { let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc); let head: *node = e; let tail: *node = e; - for (p.curkind == TK_COMMA) { + for (p.curkind == tkind.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"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' after multi-assign lvalues"); m.rhs = parseexpr(p); m.list = head; - expecttok(p, TK_SEMI, "expected ';' after multi-assign"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement"); return n; }; @@ -2752,7 +2755,7 @@ fn parseuse(p: *parser) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_SEMI, "expected ';' after use"); + expecttok(p, tkind.TK_SEMI, "expected ';' after use"); return n; }; @@ -2766,11 +2769,11 @@ fn parsedef(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in def"); + expecttok(p, tkind.TK_COLON, "expected ':' in def"); n.lhs = parsetype(p); - expecttok(p, TK_ASSIGN, "expected '=' in def"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in def"); n.rhs = parseexpr(p); - expecttok(p, TK_SEMI, "expected ';' after def"); + expecttok(p, tkind.TK_SEMI, "expected ';' after def"); n.exported = exported; return n; }; @@ -2780,31 +2783,31 @@ fn parselet(p: *parser, exported: i32) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; // Accept `let` or `const`. Const-bound bindings are marked via - // n.op = TK_CONST so the checker can reject reassignment. + // n.op = tkind.TK_CONST so the checker can reject reassignment. let is_const: i32 = 0; - if (p.curkind == TK_CONST) { is_const = 1; }; + if (p.curkind == tkind.TK_CONST) { is_const = 1; }; advance(p); let n: *node = newnode(p.a, N_LET, pf, pl, pc); n.module = p.l.module; let id: str; expectbindname(p, &id); n.str = id; - if (accepttok(p, TK_COLON)) { + if (accepttok(p, tkind.TK_COLON)) { n.lhs = parsetype(p); }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.rhs = parseexpr(p); }; - expecttok(p, TK_SEMI, "expected ';' after let"); + expecttok(p, tkind.TK_SEMI, "expected ';' after let"); n.exported = exported; - if (is_const != 0) { n.op = TK_CONST; }; + if (is_const != 0) { n.op = tkind.TK_CONST; }; return n; }; fn parseattrs(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind == TK_AT) { + for (p.curkind == tkind.TK_AT) { let pf: str = p.curfile; let pl: i32 = p.curline; let pc: i32 = p.curcol; @@ -2815,11 +2818,11 @@ fn parseattrs(p: *parser) *node = { a.str = id; // `@name(args...)` for FFI-style attrs; `@name` for marker- // only attrs like @test (no parens). - if (accepttok(p, TK_LPAREN)) { + if (accepttok(p, tkind.TK_LPAREN)) { let arghead: *node = nil; - parsearglist(p, TK_RPAREN, &arghead); + parsearglist(p, tkind.TK_RPAREN, &arghead); a.list = arghead; - expecttok(p, TK_RPAREN, "expected ')' after attribute args"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after attribute args"); }; if (head == nil) { head = a; tail = a; } else { tail.next = a; tail = a; }; @@ -2828,7 +2831,7 @@ fn parseattrs(p: *parser) *node = { }; fn parseparams(p: *parser) *node = { - if (p.curkind == TK_RPAREN) { return nil; }; + if (p.curkind == tkind.TK_RPAREN) { return nil; }; let head: *node = nil; let tail: *node = nil; for (true) { @@ -2841,12 +2844,12 @@ fn parseparams(p: *parser) *node = { let id: str; expectbindname(p, &id); n.str = id; - expecttok(p, TK_COLON, "expected ':' in parameter"); + expecttok(p, tkind.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.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; return head; }; @@ -2861,20 +2864,20 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_LPAREN, "expected '(' after fn name"); + expecttok(p, tkind.TK_LPAREN, "expected '(' after fn name"); n.list = parseparams(p); - expecttok(p, TK_RPAREN, "expected ')' after params"); - if (p.curkind != TK_ASSIGN) { - if (p.curkind != TK_SEMI) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after params"); + if (p.curkind != tkind.TK_ASSIGN) { + if (p.curkind != tkind.TK_SEMI) { n.lhs = parsetype(p); }; }; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { n.body = parseblock(p); - expecttok(p, TK_SEMI, "expected ';' after fn body"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn body"); } else { // Body-less fn: FFI declaration (`fn name(args) ret;`). - expecttok(p, TK_SEMI, "expected ';' after fn header"); + expecttok(p, tkind.TK_SEMI, "expected ';' after fn header"); }; n.exported = exported; n.attr = attrs; @@ -2891,9 +2894,9 @@ fn parsetypedecl(p: *parser, exported: i32) *node = { let id: str; expectident(p, &id); n.str = id; - expecttok(p, TK_ASSIGN, "expected '=' in type decl"); + expecttok(p, tkind.TK_ASSIGN, "expected '=' in type decl"); n.lhs = parsetype(p); - expecttok(p, TK_SEMI, "expected ';' after type decl"); + expecttok(p, tkind.TK_SEMI, "expected ';' after type decl"); n.exported = exported; return n; }; @@ -2926,7 +2929,7 @@ type parser = struct { // nocast: while inside `[...]` we treat ':' as the slice // separator, not the cast operator. Mirrors parse.c's flag. nocast: i32, - curkind: i32, + curkind: i32, // holds a `tkind` value (relaxation lets it mix) curfile: str, curline: i32, curcol: i32, @@ -2974,10 +2977,10 @@ fn expecttok(p: *parser, k: i32, what: str) bool = { return false; }; -// expectident — consume the current TK_IDENT and return its text. +// expectident — consume the current tkind.TK_IDENT and return its text. // Returns the empty str on error (and advances to make progress). fn expectident(p: *parser, into: *str) bool = { - if (p.curkind != TK_IDENT) { + if (p.curkind != tkind.TK_IDENT) { errmsg(p, "expected identifier"); advance(p); return false; @@ -2991,7 +2994,7 @@ fn expectident(p: *parser, into: *str) bool = { // discard marker. On `_`, returns "" so the checker skips // scope_define for the binding. fn expectbindname(p: *parser, into: *str) bool = { - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { let empty: str; *into = empty; advance(p); @@ -3031,7 +3034,7 @@ fn parsetype(p: *parser) *node = { let pl: i32 = p.curline; let pc: i32 = p.curcol; - if (p.curkind == TK_NOT) { + if (p.curkind == tkind.TK_NOT) { // `!T` — Hare error-flagged type wrapper. advance(p); let n: *node = newnode(p.a, N_TBANG, pf, pl, pc); @@ -3039,16 +3042,16 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_STAR) { + if (p.curkind == tkind.TK_STAR) { advance(p); let n: *node = newnode(p.a, N_TPTR, pf, pl, pc); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_LBRACK) { + if (p.curkind == tkind.TK_LBRACK) { advance(p); - if (p.curkind == TK_RBRACK) { + if (p.curkind == tkind.TK_RBRACK) { advance(p); let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc); n.lhs = parsetype(p); @@ -3058,24 +3061,24 @@ fn parsetype(p: *parser) *node = { // `[_]T` — length inferred from initialiser. n.rhs stays nil // as the sentinel; the cgen path for N_LET fills it from the // array literal's element count. - if (p.curkind == TK_UNDER) { + if (p.curkind == tkind.TK_UNDER) { advance(p); } else { n.rhs = parseexpr(p); }; - expecttok(p, TK_RBRACK, "expected ']' in array type"); + expecttok(p, tkind.TK_RBRACK, "expected ']' in array type"); n.lhs = parsetype(p); return n; }; - if (p.curkind == TK_STRUCT) { + if (p.curkind == tkind.TK_STRUCT) { advance(p); - expecttok(p, TK_LBRACE, "expected '{' after struct"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after struct"); let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc); let fhead: *node = nil; let ftail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let fpf: str = p.curfile; let fpl: i32 = p.curline; let fpc: i32 = p.curcol; @@ -3083,32 +3086,32 @@ fn parsetype(p: *parser) *node = { let fid: str; expectident(p, &fid); f.str = fid; - expecttok(p, TK_COLON, "expected ':' in field"); + expecttok(p, tkind.TK_COLON, "expected ':' in field"); f.lhs = parsetype(p); if (fhead == nil) { fhead = f; ftail = f; } else { ftail.next = f; ftail = f; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after struct fields"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after struct fields"); n.list = fhead; return n; }; - if (p.curkind == TK_ENUM) { + if (p.curkind == tkind.TK_ENUM) { // `enum [storage] { NAME [= expr], ... }` // Storage defaults to i32 (lhs == nil). Each member is an // N_TENUMMEMBER with str=name and lhs = value expr or nil // (auto-increment when omitted). advance(p); let n: *node = newnode(p.a, N_TENUM, pf, pl, pc); - if (p.curkind != TK_LBRACE) { + if (p.curkind != tkind.TK_LBRACE) { n.lhs = parsetype(p); }; - expecttok(p, TK_LBRACE, "expected '{' after enum"); + expecttok(p, tkind.TK_LBRACE, "expected '{' after enum"); let mhead: *node = nil; let mtail: *node = nil; - for (p.curkind != TK_RBRACE) { - if (p.curkind == TK_EOF) { break; }; + for (p.curkind != tkind.TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; let mpf: str = p.curfile; let mpl: i32 = p.curline; let mpc: i32 = p.curcol; @@ -3116,19 +3119,19 @@ fn parsetype(p: *parser) *node = { let mid: str; expectident(p, &mid); m.str = mid; - if (accepttok(p, TK_ASSIGN)) { + if (accepttok(p, tkind.TK_ASSIGN)) { m.lhs = parseexpr(p); }; if (mhead == nil) { mhead = m; mtail = m; } else { mtail.next = m; mtail = m; }; - if (!accepttok(p, TK_COMMA)) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; }; - expecttok(p, TK_RBRACE, "expected '}' after enum members"); + expecttok(p, tkind.TK_RBRACE, "expected '}' after enum members"); n.list = mhead; return n; }; - if (p.curkind == TK_VOID) { + if (p.curkind == tkind.TK_VOID) { // `void` keyword in type-expr context — emit as N_TNAME so // resolution treats it like any other primitive name. let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); @@ -3137,15 +3140,15 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_IDENT) { + if (p.curkind == tkind.TK_IDENT) { let n: *node = newnode(p.a, N_TNAME, pf, pl, pc); let acc: str = p.curtext; advance(p); // Dotted path collapse: pkg.Type → single TNAME with the // joined string. Mirrors C parsetype's loop. - for (p.curkind == TK_DOT) { + for (p.curkind == tkind.TK_DOT) { advance(p); - if (p.curkind != TK_IDENT) { break; }; + if (p.curkind != tkind.TK_IDENT) { break; }; acc = joindotted(p.a, acc, p.curtext); advance(p); }; @@ -3153,11 +3156,11 @@ fn parsetype(p: *parser) *node = { return n; }; - if (p.curkind == TK_LPAREN) { + if (p.curkind == tkind.TK_LPAREN) { // (T) or (T, T, ...) or (T | T | ...) advance(p); let first: *node = parsetype(p); - if (accepttok(p, TK_PIPE)) { + if (accepttok(p, tkind.TK_PIPE)) { let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc); let head: *node = first; let tail: *node = first; @@ -3165,14 +3168,14 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_PIPE)) { break; }; + if (!accepttok(p, tkind.TK_PIPE)) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tagged-union type"); + expecttok(p, tkind.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"); + if (!accepttok(p, tkind.TK_COMMA)) { + expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type"); return first; }; let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc); @@ -3182,23 +3185,23 @@ fn parsetype(p: *parser) *node = { let e: *node = parsetype(p); tail.next = e; tail = e; - if (!accepttok(p, TK_COMMA)) { break; }; - if (p.curkind == TK_RPAREN) { break; }; + if (!accepttok(p, tkind.TK_COMMA)) { break; }; + if (p.curkind == tkind.TK_RPAREN) { break; }; }; - expecttok(p, TK_RPAREN, "expected ')' in tuple type"); + expecttok(p, tkind.TK_RPAREN, "expected ')' in tuple type"); n.list = head; return n; }; - if (p.curkind == TK_FN) { + if (p.curkind == tkind.TK_FN) { advance(p); - expecttok(p, TK_LPAREN, "expected '(' after fn in type"); + expecttok(p, tkind.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"); + expecttok(p, tkind.TK_RPAREN, "expected ')' after fn type params"); n.lhs = parsetype(p); return n; }; @@ -3216,39 +3219,39 @@ fn parsetype(p: *parser) *node = { // 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; }; + if (k == tkind.TK_OR) { return 1; }; + if (k == tkind.TK_AND) { return 2; }; + if (k == tkind.TK_EQ) { return 3; }; + if (k == tkind.TK_NEQ) { return 3; }; + if (k == tkind.TK_LT) { return 4; }; + if (k == tkind.TK_LE) { return 4; }; + if (k == tkind.TK_GT) { return 4; }; + if (k == tkind.TK_GE) { return 4; }; + if (k == tkind.TK_PIPE) { return 5; }; + if (k == tkind.TK_CARET) { return 6; }; + if (k == tkind.TK_AMP) { return 7; }; + if (k == tkind.TK_LSHIFT) { return 8; }; + if (k == tkind.TK_RSHIFT) { return 8; }; + if (k == tkind.TK_PLUS) { return 9; }; + if (k == tkind.TK_MINUS) { return 9; }; + if (k == tkind.TK_STAR) { return 10; }; + if (k == tkind.TK_SLASH) { return 10; }; + if (k == tkind.TK_PERCENT) { return 10; }; return 0; }; fn isassignop(k: 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; }; + if (k == tkind.TK_ASSIGN) { return true; }; + if (k == tkind.TK_PLUSEQ) { return true; }; + if (k == tkind.TK_MINUSEQ) { return true; }; + if (k == tkind.TK_STAREQ) { return true; }; + if (k == tkind.TK_SLASHEQ) { return true; }; + if (k == tkind.TK_PERCENTEQ) { return true; }; + if (k == tkind.TK_AMPEQ) { return true; }; + if (k == tkind.TK_PIPEEQ) { return true; }; + if (k == tkind.TK_CARETEQ) { return true; }; + if (k == tkind.TK_LSHIFTEQ) { return true; }; + if (k == tkind.TK_RSHIFTEQ) { return true; }; return false; }; @@ -3260,36 +3263,36 @@ export fn parsefile(p: *parser) *node = { let head: *node = nil; let tail: *node = nil; - for (p.curkind != TK_EOF) { + for (p.curkind != tkind.TK_EOF) { let attrs: *node = parseattrs(p); let exported: i32 = 0; - if (p.curkind == TK_EXPORT) { exported = 1; advance(p); }; + if (p.curkind == tkind.TK_EXPORT) { exported = 1; advance(p); }; let d: *node = nil; - if (p.curkind == TK_USE) { + if (p.curkind == tkind.TK_USE) { d = parseuse(p); - } else { if (p.curkind == TK_DEF) { + } else { if (p.curkind == tkind.TK_DEF) { d = parsedef(p, exported); - } else { if (p.curkind == TK_TYPE) { + } else { if (p.curkind == tkind.TK_TYPE) { d = parsetypedecl(p, exported); - } else { if (p.curkind == TK_LET) { + } else { if (p.curkind == tkind.TK_LET) { d = parselet(p, exported); - } else { if (p.curkind == TK_CONST) { + } else { if (p.curkind == tkind.TK_CONST) { d = parselet(p, exported); - } else { if (p.curkind == TK_FN) { + } else { if (p.curkind == tkind.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.curkind != TK_SEMI) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { + for (p.curkind != tkind.TK_SEMI) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { let depth: i32 = 0; for (true) { - if (p.curkind == TK_EOF) { break; }; - if (p.curkind == TK_LBRACE) { depth += 1; advance(p); continue; }; - if (p.curkind == TK_RBRACE) { + if (p.curkind == tkind.TK_EOF) { break; }; + if (p.curkind == tkind.TK_LBRACE) { depth += 1; advance(p); continue; }; + if (p.curkind == tkind.TK_RBRACE) { depth -= 1; advance(p); if (depth == 0) { break; }; @@ -3301,7 +3304,7 @@ export fn parsefile(p: *parser) *node = { }; advance(p); }; - if (p.curkind == TK_SEMI) { advance(p); }; + if (p.curkind == tkind.TK_SEMI) { advance(p); }; };};};};};}; if (d != nil) { @@ -6639,10 +6642,10 @@ fn cgun(c: *cgen, n: *node) void = { // address / deref. The wasted load before AMP keeps our asm // byte-identical to the C version. cgexpr(c, n.lhs); - if (n.op == TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; - if (n.op == TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; - if (n.op == TK_AMP) { + if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; }; + if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; }; + if (n.op == tkind.TK_AMP) { let opnd: *node = n.lhs; if (opnd != nil) { if (opnd.kind == N_IDENT) { @@ -6658,7 +6661,7 @@ fn cgun(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_NOT) { + if (n.op == tkind.TK_NOT) { let t: str = mklabel(c, "tt"); let e: str = mklabel(c, "te"); emitline("\tCMPQ\t$0, AX\n"); @@ -6681,48 +6684,48 @@ fn cgbin(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tAX\n"); cgexpr(c, n.lhs); emitline("\tPOPQ\tBX\n"); - if (n.op == TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; - if (n.op == TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; - if (n.op == TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; - if (n.op == TK_SLASH) { + if (n.op == tkind.TK_PLUS) { emitline("\tADDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_MINUS) { emitline("\tSUBQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_STAR) { emitline("\tIMULQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_SLASH) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; return; }; - if (n.op == TK_PERCENT) { + if (n.op == tkind.TK_PERCENT) { emitline("\tMOVQ\t$0, DX\n"); if (unsignd) { emitline("\tDIVQ\tBX\n"); } else { emitline("\tIDIVQ\tBX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; }; - if (n.op == TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; - if (n.op == TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; - if (n.op == TK_LSHIFT) { + if (n.op == tkind.TK_AMP) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_PIPE) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_CARET) { emitline("\tXORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_LSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHLQ\tCX, AX\n"); return; }; - if (n.op == TK_RSHIFT) { + if (n.op == tkind.TK_RSHIFT) { emitline("\tMOVQ\tBX, CX\n"); emitline("\tSHRQ\tCX, AX\n"); return; }; - if (n.op == TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; - if (n.op == TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_AND) { emitline("\tANDQ\tBX, AX\n"); return; }; + if (n.op == tkind.TK_OR) { emitline("\tORQ\tBX, AX\n"); return; }; // Comparison: emit CMPQ, jump on signed/unsigned variant, // materialise 0/1 in AX. Same shape as the C cgen. let iscmp: bool = false; let jcc: str = ""; - if (n.op == TK_EQ) { iscmp = true; jcc = "JE"; }; - if (n.op == TK_NEQ) { iscmp = true; jcc = "JNE"; }; - if (n.op == TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; - if (n.op == TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; - if (n.op == TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; - if (n.op == TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; + if (n.op == tkind.TK_EQ) { iscmp = true; jcc = "JE"; }; + if (n.op == tkind.TK_NEQ) { iscmp = true; jcc = "JNE"; }; + if (n.op == tkind.TK_LT) { iscmp = true; if (unsignd) { jcc = "JB"; } else { jcc = "JL"; }; }; + if (n.op == tkind.TK_LE) { iscmp = true; if (unsignd) { jcc = "JBE"; } else { jcc = "JLE"; }; }; + if (n.op == tkind.TK_GT) { iscmp = true; if (unsignd) { jcc = "JA"; } else { jcc = "JG"; }; }; + if (n.op == tkind.TK_GE) { iscmp = true; if (unsignd) { jcc = "JAE"; } else { jcc = "JGE"; }; }; if (iscmp) { let t: str = mklabel(c, "ct"); let e: str = mklabel(c, "ce"); @@ -6846,11 +6849,11 @@ fn cgassign(c: *cgen, n: *node) void = { let lhs: *node = n.lhs; // Discard lvalue `_ = expr;` — evaluate rhs for side effects, // write nothing. Detected by lhs being an N_IDENT with empty str - // (planted by parseprimary on the TK_UNDER token). + // (planted by parseprimary on the tkind.TK_UNDER token). if (lhs != nil) { if (lhs.kind == N_IDENT) { if (lhs.str.len == 0) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); return; }; @@ -6864,8 +6867,8 @@ fn cgassign(c: *cgen, n: *node) void = { // `*bool` / `*u8` / `*i32` we narrow via the local's tnode. if (lhs != nil) { if (lhs.kind == N_UN) { - if (lhs.op == TK_STAR) { - if (n.op == TK_ASSIGN) { + if (lhs.op == tkind.TK_STAR) { + if (n.op == tkind.TK_ASSIGN) { let inner: *node = lhs.lhs; let elemstr: bool = false; let storeop: str = "MOVQ"; @@ -6923,7 +6926,7 @@ fn cgassign(c: *cgen, n: *node) void = { // from base.tnode picks MOVB vs MOVQ. if (lhs != nil) { if (lhs.kind == N_INDEX) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { let base: *node = lhs.lhs; let idx: *node = lhs.rhs; let esz: i32 = 8; @@ -7012,7 +7015,7 @@ fn cgassign(c: *cgen, n: *node) void = { for (fi != nil) { let fn_: str = fi.fname; if (streq(fn_, fld)) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // compound: load current value emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -7026,13 +7029,13 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPUSHQ\tBX\n"); }; cgexpr(c, n.rhs); - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs (BX is old lhs, // AX is rhs). - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -7041,7 +7044,7 @@ fn cgassign(c: *cgen, n: *node) void = { // (AX=ptr, BX=len). Use CX as the // address scratch so we don't clobber // the len half before storing it. - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); @@ -7109,7 +7112,7 @@ fn cgassign(c: *cgen, n: *node) void = { }; if (innerkind == N_TSLICE) { innerstr = true; }; if (innerstr) { - if (n.op != TK_ASSIGN) { + if (n.op != tkind.TK_ASSIGN) { // Compound on `(*str|*slice).field`: load // current → push → eval rhs → combine → store. emitline("\tMOVQ\t"); @@ -7123,8 +7126,8 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tBX\n"); // PLUSEQ is commutative; MINUSEQ // needs lhs - rhs. - if (n.op == TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); }; + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, BX\n"); emitline("\tMOVQ\tBX, AX\n"); }; @@ -7179,7 +7182,7 @@ fn cgassign(c: *cgen, n: *node) void = { let fi: *fieldinfo = si.fields; for (fi != nil) { if (streq(fi.fname, fld)) { - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { // str rhs: AX=ptr, BX=len. // Stash both, then load @@ -7236,7 +7239,7 @@ fn cgassign(c: *cgen, n: *node) void = { let lcn: *local = localfindnode(c, nm); if (lcn != nil) { lcstr = isstrtype(c, lcn.tnode); }; cgexpr(c, n.rhs); - if (n.op == TK_ASSIGN) { + if (n.op == tkind.TK_ASSIGN) { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -7247,13 +7250,13 @@ fn cgassign(c: *cgen, n: *node) void = { }; return; }; - if (n.op == TK_PLUSEQ) { + if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); return; }; - if (n.op == TK_MINUSEQ) { + if (n.op == tkind.TK_MINUSEQ) { emitline("\tSUBQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); @@ -7263,15 +7266,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\t"); emitoff(off: i64); emitline("(BP), BX\n"); - if (n.op == TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; - if (n.op == TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; - if (n.op == TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; - if (n.op == TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; - if (n.op == TK_LSHIFTEQ) { + if (n.op == tkind.TK_STAREQ) { emitline("\tIMULQ\tAX, BX\n"); }; + if (n.op == tkind.TK_AMPEQ) { emitline("\tANDQ\tAX, BX\n"); }; + if (n.op == tkind.TK_PIPEEQ) { emitline("\tORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_CARETEQ) { emitline("\tXORQ\tAX, BX\n"); }; + if (n.op == tkind.TK_LSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHLQ\tCX, BX\n"); }; - if (n.op == TK_RSHIFTEQ) { + if (n.op == tkind.TK_RSHIFTEQ) { emitline("\tMOVQ\tAX, CX\n"); emitline("\tSHRQ\tCX, BX\n"); }; @@ -7692,7 +7695,7 @@ fn cglet(c: *cgen, n: *node) void = { // For each field in the lit, evaluate its value and store at // the field's offset within the slot. Field-name → offset // from the struct registry. When the literal carries - // op == TK_ELLIPSIS (autofill marker from the parser), the + // op == tkind.TK_ELLIPSIS (autofill marker from the parser), the // entire slot is zero-filled first so unmentioned fields // read as 0. if (rhs.kind == N_STRUCTLIT) { @@ -7705,7 +7708,7 @@ fn cglet(c: *cgen, n: *node) void = { }; let si: *structinfo = structlookup(c, sname); if (si != nil) { - if (rhs.op == TK_ELLIPSIS) { + if (rhs.op == tkind.TK_ELLIPSIS) { let total: i32 = si.totsize; emitline("\tXORQ\tAX, AX\n"); let zi: i32 = 0; @@ -8426,31 +8429,31 @@ fn enumevalmember(prev: *enummember, e: *node, out: *u64) bool = { if (!enumevalmember(prev, e.lhs, &a)) { return false; }; if (!enumevalmember(prev, e.rhs, &b)) { return false; }; let op: i32 = e.op; - if (op == TK_PLUS) { *out = a + b; return true; }; - if (op == TK_MINUS) { *out = a - b; return true; }; - if (op == TK_STAR) { *out = a * b; return true; }; - if (op == TK_SLASH) { + if (op == tkind.TK_PLUS) { *out = a + b; return true; }; + if (op == tkind.TK_MINUS) { *out = a - b; return true; }; + if (op == tkind.TK_STAR) { *out = a * b; return true; }; + if (op == tkind.TK_SLASH) { if (b == 0u64) { return false; }; *out = a / b; return true; }; - if (op == TK_PERCENT) { + if (op == tkind.TK_PERCENT) { if (b == 0u64) { return false; }; *out = a % b; return true; }; - if (op == TK_AMP) { *out = a & b; return true; }; - if (op == TK_PIPE) { *out = a | b; return true; }; - if (op == TK_CARET) { *out = a ^ b; return true; }; - if (op == TK_LSHIFT) { *out = a << b; return true; }; - if (op == TK_RSHIFT) { *out = a >> b; return true; }; + if (op == tkind.TK_AMP) { *out = a & b; return true; }; + if (op == tkind.TK_PIPE) { *out = a | b; return true; }; + if (op == tkind.TK_CARET) { *out = a ^ b; return true; }; + if (op == tkind.TK_LSHIFT) { *out = a << b; return true; }; + if (op == tkind.TK_RSHIFT) { *out = a >> b; return true; }; return false; }; if (k == N_UN) { let v: u64; if (!enumevalmember(prev, e.lhs, &v)) { return false; }; let op: i32 = e.op; - if (op == TK_MINUS) { *out = (-(v: i64)): u64; return true; }; - if (op == TK_TILDE) { *out = ~v; return true; }; - if (op == TK_PLUS) { *out = v; return true; }; + if (op == tkind.TK_MINUS) { *out = (-(v: i64)): u64; return true; }; + if (op == tkind.TK_TILDE) { *out = ~v; return true; }; + if (op == tkind.TK_PLUS) { *out = v; return true; }; return false; }; return false; @@ -9359,8 +9362,8 @@ export fn main(argc: i32, argv: **u8) i32 = { let t: tok; lexnext(&l, &t); tokprint(1i32, &t); - if (t.kind == TK_EOF) { break; }; - if (t.kind == TK_ERR) { break; }; + if (t.kind == tkind.TK_EOF) { break; }; + if (t.kind == tkind.TK_ERR) { break; }; }; } else { if (mode == 97) { // '-a' let ps: parser; diff --git a/selfhost/cmd/wwdump/main.ww b/selfhost/cmd/wwdump/main.ww index 43db3de7..0270fda0 100644 --- a/selfhost/cmd/wwdump/main.ww +++ b/selfhost/cmd/wwdump/main.ww @@ -122,8 +122,8 @@ export fn main(argc: i32, argv: **u8) i32 = { let t: tok; lexnext(&l, &t); tokprint(1i32, &t); - if (t.kind == TK_EOF) { break; }; - if (t.kind == TK_ERR) { break; }; + if (t.kind == tkind.TK_EOF) { break; }; + if (t.kind == tkind.TK_ERR) { break; }; }; } else { if (mode == 97) { // '-a' let ps: parser;