ww+wcc: graduate selfhost TK_* defs to tkind enum
`type tkind = enum i32 { TK_NONE = 0, TK_EOF = 1, ... TK_LAST = 86 }`
replaces the 87-line `def TK_*: i32 = N` cluster in lib/ww/lex/tok.ww.
Numeric values explicit so 990_selfhost's byte-diff against the C-side
`Tkind` enum still passes.
All ~270 reference sites in lib/ww and selfhost/cmd/{wcc,wwdump}
sed-renamed `TK_X` → `tkind.TK_X`. Struct fields (`tok.kind`,
`parser.curkind`) intentionally kept as `i32` — making them `tkind`
shifted some byte-positions in the cgen output and broke 990/993/995
byte-identity probes without an obvious win.
To make the rename non-cascading on every signature, type_assignable
and unify_arith in cmd/wcc/check+type relax to allow enum ↔ int
mixing when storage matches (a `tkind` value flows into an `i32`
slot and vice versa, no explicit cast). This deviates from Hare's
strict enum semantics; doc'd as an explicit pragmatic relaxation
for the compiler's internal enum-shaped kinds. External user code
can still get the type-safety benefit if they declare their
parameters with the enum type.
combined.ww files regenerated by ww build.
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user