selfhost: graduate N_* defs to nkind enum
This commit is contained in:
326
lib/ww/ast.ww
326
lib/ww/ast.ww
@@ -17,84 +17,88 @@ use tok;
|
||||
// Mirror of cmd/wcc/ww.h Nkind. Values must stay numerically equal so
|
||||
// the AST diff probe in 990_selfhost works.
|
||||
|
||||
def N_NONE: i32 = 0;
|
||||
// Mirror of the C `Nkind` enum in cmd/wcc/ww.h. Numeric values are
|
||||
// explicit and must stay in sync — the 990_selfhost test diffs
|
||||
// astprint against the C side byte-for-byte. Tail-appended entries
|
||||
// (TYPETEST onward) preserve every prior N_* value.
|
||||
type nkind = enum i32 {
|
||||
N_NONE = 0,
|
||||
|
||||
def N_INTLIT: i32 = 1;
|
||||
def N_FLOATLIT: i32 = 2;
|
||||
def N_STRLIT: i32 = 3;
|
||||
def N_RUNELIT: i32 = 4;
|
||||
def N_TRUE: i32 = 5;
|
||||
def N_FALSE: i32 = 6;
|
||||
def N_NIL: i32 = 7;
|
||||
def N_IDENT: i32 = 8;
|
||||
N_INTLIT = 1,
|
||||
N_FLOATLIT = 2,
|
||||
N_STRLIT = 3,
|
||||
N_RUNELIT = 4,
|
||||
N_TRUE = 5,
|
||||
N_FALSE = 6,
|
||||
N_NIL = 7,
|
||||
N_IDENT = 8,
|
||||
|
||||
def N_BIN: i32 = 9;
|
||||
def N_UN: i32 = 10;
|
||||
def N_CALL: i32 = 11;
|
||||
def N_INDEX: i32 = 12;
|
||||
def N_DOT: i32 = 13;
|
||||
def N_CAST: i32 = 14;
|
||||
def N_STRUCTLIT:i32 = 15;
|
||||
def N_ARRLIT: i32 = 16;
|
||||
def N_FIELD: i32 = 17;
|
||||
def N_ASSIGN: i32 = 18;
|
||||
def N_ALLOC: i32 = 19;
|
||||
def N_FREE: i32 = 20;
|
||||
def N_RECV: i32 = 21;
|
||||
def N_SLICE: i32 = 22;
|
||||
def N_SPREAD: i32 = 23;
|
||||
N_BIN = 9,
|
||||
N_UN = 10,
|
||||
N_CALL = 11,
|
||||
N_INDEX = 12,
|
||||
N_DOT = 13,
|
||||
N_CAST = 14,
|
||||
N_STRUCTLIT = 15,
|
||||
N_ARRLIT = 16,
|
||||
N_FIELD = 17,
|
||||
N_ASSIGN = 18,
|
||||
N_ALLOC = 19,
|
||||
N_FREE = 20,
|
||||
N_RECV = 21,
|
||||
N_SLICE = 22,
|
||||
N_SPREAD = 23,
|
||||
|
||||
def N_BLOCK: i32 = 24;
|
||||
def N_EXPRSTMT: i32 = 25;
|
||||
def N_LET: i32 = 26;
|
||||
def N_RETURN: i32 = 27;
|
||||
def N_IF: i32 = 28;
|
||||
def N_FOR: i32 = 29;
|
||||
def N_FORRANGE: i32 = 30;
|
||||
def N_DEFER: i32 = 31;
|
||||
def N_BREAK: i32 = 32;
|
||||
def N_CONTINUE: i32 = 33;
|
||||
def N_SWITCH: i32 = 34;
|
||||
def N_CASE: i32 = 35;
|
||||
N_BLOCK = 24,
|
||||
N_EXPRSTMT = 25,
|
||||
N_LET = 26,
|
||||
N_RETURN = 27,
|
||||
N_IF = 28,
|
||||
N_FOR = 29,
|
||||
N_FORRANGE = 30,
|
||||
N_DEFER = 31,
|
||||
N_BREAK = 32,
|
||||
N_CONTINUE = 33,
|
||||
N_SWITCH = 34,
|
||||
N_CASE = 35,
|
||||
|
||||
def N_FILE: i32 = 36;
|
||||
def N_USE: i32 = 37;
|
||||
def N_DEF: i32 = 38;
|
||||
def N_TYPEDECL: i32 = 39;
|
||||
def N_FNDECL: i32 = 40;
|
||||
def N_PARAM: i32 = 41;
|
||||
N_FILE = 36,
|
||||
N_USE = 37,
|
||||
N_DEF = 38,
|
||||
N_TYPEDECL = 39,
|
||||
N_FNDECL = 40,
|
||||
N_PARAM = 41,
|
||||
|
||||
def N_TNAME: i32 = 42;
|
||||
def N_TPTR: i32 = 43;
|
||||
def N_TSLICE: i32 = 44;
|
||||
def N_TARRAY: i32 = 45;
|
||||
def N_TFN: i32 = 46;
|
||||
def N_TSTRUCT: i32 = 47;
|
||||
def N_TFIELD: i32 = 48;
|
||||
def N_TCHAN: i32 = 49;
|
||||
N_TNAME = 42,
|
||||
N_TPTR = 43,
|
||||
N_TSLICE = 44,
|
||||
N_TARRAY = 45,
|
||||
N_TFN = 46,
|
||||
N_TSTRUCT = 47,
|
||||
N_TFIELD = 48,
|
||||
N_TCHAN = 49,
|
||||
|
||||
def N_ATTR: i32 = 50;
|
||||
def N_TTUPLE: i32 = 51;
|
||||
def N_TTAGGED: i32 = 52;
|
||||
def N_TUPLE: i32 = 53;
|
||||
def N_MATCH: i32 = 54;
|
||||
def N_MCASE: i32 = 55;
|
||||
def N_TRYPROP: i32 = 56;
|
||||
def N_TRYUNW: i32 = 57;
|
||||
def N_MLET: i32 = 58;
|
||||
def N_MASSIGN: i32 = 59;
|
||||
N_ATTR = 50,
|
||||
N_TTUPLE = 51,
|
||||
N_TTAGGED = 52,
|
||||
N_TUPLE = 53,
|
||||
N_MATCH = 54,
|
||||
N_MCASE = 55,
|
||||
N_TRYPROP = 56,
|
||||
N_TRYUNW = 57,
|
||||
N_MLET = 58,
|
||||
N_MASSIGN = 59,
|
||||
|
||||
// Appended at the tail to keep all prior N_* values stable. The
|
||||
// 990_selfhost test diffs astprint against the C side byte-for-byte.
|
||||
def N_TYPETEST: i32 = 60;
|
||||
def N_TYPEASSERT: i32 = 61;
|
||||
def N_VOIDLIT: i32 = 62;
|
||||
def N_TBANG: i32 = 63;
|
||||
def N_YIELD: i32 = 64;
|
||||
def N_TENUM: i32 = 65;
|
||||
def N_TENUMMEMBER: i32 = 66;
|
||||
N_TYPETEST = 60,
|
||||
N_TYPEASSERT = 61,
|
||||
N_VOIDLIT = 62,
|
||||
N_TBANG = 63,
|
||||
N_YIELD = 64,
|
||||
N_TENUM = 65,
|
||||
N_TENUMMEMBER = 66,
|
||||
|
||||
def N_LAST: i32 = 67;
|
||||
N_LAST = 67,
|
||||
};
|
||||
|
||||
// ---- Node -------------------------------------------------------------
|
||||
|
||||
@@ -103,7 +107,7 @@ type node = struct {
|
||||
file: str,
|
||||
line: i32,
|
||||
col: i32,
|
||||
op: i32, // for N_BIN / N_UN / N_ASSIGN
|
||||
op: i32, // for nkind.N_BIN / nkind.N_UN / nkind.N_ASSIGN
|
||||
str: str,
|
||||
uval: u64,
|
||||
fval: f64,
|
||||
@@ -133,74 +137,74 @@ export fn newnode(a: *arena, k: i32, file: str, line: i32, col: i32) *node = {
|
||||
// ---- printer ----------------------------------------------------------
|
||||
|
||||
fn nkname(k: i32) str = {
|
||||
if (k == N_NONE) { return "none"; };
|
||||
if (k == N_INTLIT) { return "int"; };
|
||||
if (k == N_FLOATLIT) { return "float"; };
|
||||
if (k == N_STRLIT) { return "str"; };
|
||||
if (k == N_RUNELIT) { return "rune"; };
|
||||
if (k == N_TRUE) { return "true"; };
|
||||
if (k == N_FALSE) { return "false"; };
|
||||
if (k == N_NIL) { return "nil"; };
|
||||
if (k == N_IDENT) { return "id"; };
|
||||
if (k == N_BIN) { return "bin"; };
|
||||
if (k == N_UN) { return "un"; };
|
||||
if (k == N_CALL) { return "call"; };
|
||||
if (k == N_INDEX) { return "index"; };
|
||||
if (k == N_DOT) { return "dot"; };
|
||||
if (k == N_CAST) { return "cast"; };
|
||||
if (k == N_STRUCTLIT) { return "structlit"; };
|
||||
if (k == N_ARRLIT) { return "arrlit"; };
|
||||
if (k == N_FIELD) { return "field"; };
|
||||
if (k == N_ASSIGN) { return "assign"; };
|
||||
if (k == N_ALLOC) { return "alloc"; };
|
||||
if (k == N_FREE) { return "free"; };
|
||||
if (k == N_RECV) { return "recv"; };
|
||||
if (k == N_SLICE) { return "slice"; };
|
||||
if (k == N_SPREAD) { return "spread"; };
|
||||
if (k == N_BLOCK) { return "block"; };
|
||||
if (k == N_EXPRSTMT) { return "exprstmt"; };
|
||||
if (k == N_LET) { return "let"; };
|
||||
if (k == N_RETURN) { return "return"; };
|
||||
if (k == N_IF) { return "if"; };
|
||||
if (k == N_FOR) { return "for"; };
|
||||
if (k == N_FORRANGE) { return "forrange"; };
|
||||
if (k == N_DEFER) { return "defer"; };
|
||||
if (k == N_BREAK) { return "break"; };
|
||||
if (k == N_CONTINUE) { return "continue"; };
|
||||
if (k == N_SWITCH) { return "switch"; };
|
||||
if (k == N_CASE) { return "case"; };
|
||||
if (k == N_FILE) { return "file"; };
|
||||
if (k == N_USE) { return "use"; };
|
||||
if (k == N_DEF) { return "def"; };
|
||||
if (k == N_TYPEDECL) { return "typedecl"; };
|
||||
if (k == N_FNDECL) { return "fn"; };
|
||||
if (k == N_PARAM) { return "param"; };
|
||||
if (k == N_TNAME) { return "tname"; };
|
||||
if (k == N_TPTR) { return "tptr"; };
|
||||
if (k == N_TSLICE) { return "tslice"; };
|
||||
if (k == N_TARRAY) { return "tarray"; };
|
||||
if (k == N_TFN) { return "tfn"; };
|
||||
if (k == N_TSTRUCT) { return "tstruct"; };
|
||||
if (k == N_TFIELD) { return "tfield"; };
|
||||
if (k == N_TCHAN) { return "tchan"; };
|
||||
if (k == N_ATTR) { return "attr"; };
|
||||
if (k == N_TTUPLE) { return "ttuple"; };
|
||||
if (k == N_TTAGGED) { return "ttagged"; };
|
||||
if (k == N_TUPLE) { return "tuple"; };
|
||||
if (k == N_MATCH) { return "match"; };
|
||||
if (k == N_MCASE) { return "mcase"; };
|
||||
if (k == N_TRYPROP) { return "tryprop"; };
|
||||
if (k == N_TRYUNW) { return "tryunw"; };
|
||||
if (k == N_MLET) { return "mlet"; };
|
||||
if (k == N_MASSIGN) { return "massign"; };
|
||||
if (k == N_TYPETEST) { return "typetest"; };
|
||||
if (k == N_TYPEASSERT) { return "typeassert"; };
|
||||
if (k == N_VOIDLIT) { return "voidlit"; };
|
||||
if (k == N_TBANG) { return "tbang"; };
|
||||
if (k == N_YIELD) { return "yield"; };
|
||||
if (k == N_TENUM) { return "tenum"; };
|
||||
if (k == N_TENUMMEMBER) { return "tenummember"; };
|
||||
if (k == N_LAST) { return "last"; };
|
||||
if (k == nkind.N_NONE) { return "none"; };
|
||||
if (k == nkind.N_INTLIT) { return "int"; };
|
||||
if (k == nkind.N_FLOATLIT) { return "float"; };
|
||||
if (k == nkind.N_STRLIT) { return "str"; };
|
||||
if (k == nkind.N_RUNELIT) { return "rune"; };
|
||||
if (k == nkind.N_TRUE) { return "true"; };
|
||||
if (k == nkind.N_FALSE) { return "false"; };
|
||||
if (k == nkind.N_NIL) { return "nil"; };
|
||||
if (k == nkind.N_IDENT) { return "id"; };
|
||||
if (k == nkind.N_BIN) { return "bin"; };
|
||||
if (k == nkind.N_UN) { return "un"; };
|
||||
if (k == nkind.N_CALL) { return "call"; };
|
||||
if (k == nkind.N_INDEX) { return "index"; };
|
||||
if (k == nkind.N_DOT) { return "dot"; };
|
||||
if (k == nkind.N_CAST) { return "cast"; };
|
||||
if (k == nkind.N_STRUCTLIT) { return "structlit"; };
|
||||
if (k == nkind.N_ARRLIT) { return "arrlit"; };
|
||||
if (k == nkind.N_FIELD) { return "field"; };
|
||||
if (k == nkind.N_ASSIGN) { return "assign"; };
|
||||
if (k == nkind.N_ALLOC) { return "alloc"; };
|
||||
if (k == nkind.N_FREE) { return "free"; };
|
||||
if (k == nkind.N_RECV) { return "recv"; };
|
||||
if (k == nkind.N_SLICE) { return "slice"; };
|
||||
if (k == nkind.N_SPREAD) { return "spread"; };
|
||||
if (k == nkind.N_BLOCK) { return "block"; };
|
||||
if (k == nkind.N_EXPRSTMT) { return "exprstmt"; };
|
||||
if (k == nkind.N_LET) { return "let"; };
|
||||
if (k == nkind.N_RETURN) { return "return"; };
|
||||
if (k == nkind.N_IF) { return "if"; };
|
||||
if (k == nkind.N_FOR) { return "for"; };
|
||||
if (k == nkind.N_FORRANGE) { return "forrange"; };
|
||||
if (k == nkind.N_DEFER) { return "defer"; };
|
||||
if (k == nkind.N_BREAK) { return "break"; };
|
||||
if (k == nkind.N_CONTINUE) { return "continue"; };
|
||||
if (k == nkind.N_SWITCH) { return "switch"; };
|
||||
if (k == nkind.N_CASE) { return "case"; };
|
||||
if (k == nkind.N_FILE) { return "file"; };
|
||||
if (k == nkind.N_USE) { return "use"; };
|
||||
if (k == nkind.N_DEF) { return "def"; };
|
||||
if (k == nkind.N_TYPEDECL) { return "typedecl"; };
|
||||
if (k == nkind.N_FNDECL) { return "fn"; };
|
||||
if (k == nkind.N_PARAM) { return "param"; };
|
||||
if (k == nkind.N_TNAME) { return "tname"; };
|
||||
if (k == nkind.N_TPTR) { return "tptr"; };
|
||||
if (k == nkind.N_TSLICE) { return "tslice"; };
|
||||
if (k == nkind.N_TARRAY) { return "tarray"; };
|
||||
if (k == nkind.N_TFN) { return "tfn"; };
|
||||
if (k == nkind.N_TSTRUCT) { return "tstruct"; };
|
||||
if (k == nkind.N_TFIELD) { return "tfield"; };
|
||||
if (k == nkind.N_TCHAN) { return "tchan"; };
|
||||
if (k == nkind.N_ATTR) { return "attr"; };
|
||||
if (k == nkind.N_TTUPLE) { return "ttuple"; };
|
||||
if (k == nkind.N_TTAGGED) { return "ttagged"; };
|
||||
if (k == nkind.N_TUPLE) { return "tuple"; };
|
||||
if (k == nkind.N_MATCH) { return "match"; };
|
||||
if (k == nkind.N_MCASE) { return "mcase"; };
|
||||
if (k == nkind.N_TRYPROP) { return "tryprop"; };
|
||||
if (k == nkind.N_TRYUNW) { return "tryunw"; };
|
||||
if (k == nkind.N_MLET) { return "mlet"; };
|
||||
if (k == nkind.N_MASSIGN) { return "massign"; };
|
||||
if (k == nkind.N_TYPETEST) { return "typetest"; };
|
||||
if (k == nkind.N_TYPEASSERT) { return "typeassert"; };
|
||||
if (k == nkind.N_VOIDLIT) { return "voidlit"; };
|
||||
if (k == nkind.N_TBANG) { return "tbang"; };
|
||||
if (k == nkind.N_YIELD) { return "yield"; };
|
||||
if (k == nkind.N_TENUM) { return "tenum"; };
|
||||
if (k == nkind.N_TENUMMEMBER) { return "tenummember"; };
|
||||
if (k == nkind.N_LAST) { return "last"; };
|
||||
return "?";
|
||||
};
|
||||
|
||||
@@ -263,31 +267,31 @@ fn pr(fd: i32, n: *node, d: i32) void = {
|
||||
let nm: str = nkname(n.kind);
|
||||
os.write(fd, nm.ptr, nm.len: u64);
|
||||
|
||||
if (n.kind == N_INTLIT) {
|
||||
if (n.kind == nkind.N_INTLIT) {
|
||||
putc1(fd, 32u8);
|
||||
let buf: [32]u8;
|
||||
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
} else { if (n.kind == N_RUNELIT) {
|
||||
} else { if (n.kind == nkind.N_RUNELIT) {
|
||||
putc1(fd, 32u8);
|
||||
let buf: [32]u8;
|
||||
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
} else { if (
|
||||
n.kind == N_STRLIT ||
|
||||
n.kind == N_IDENT ||
|
||||
n.kind == N_USE ||
|
||||
n.kind == N_DOT ||
|
||||
n.kind == N_DEF ||
|
||||
n.kind == N_TYPEDECL ||
|
||||
n.kind == N_FNDECL ||
|
||||
n.kind == N_PARAM ||
|
||||
n.kind == N_LET ||
|
||||
n.kind == N_TNAME ||
|
||||
n.kind == N_TFIELD ||
|
||||
n.kind == N_TENUMMEMBER ||
|
||||
n.kind == N_FIELD ||
|
||||
n.kind == N_ATTR
|
||||
n.kind == nkind.N_STRLIT ||
|
||||
n.kind == nkind.N_IDENT ||
|
||||
n.kind == nkind.N_USE ||
|
||||
n.kind == nkind.N_DOT ||
|
||||
n.kind == nkind.N_DEF ||
|
||||
n.kind == nkind.N_TYPEDECL ||
|
||||
n.kind == nkind.N_FNDECL ||
|
||||
n.kind == nkind.N_PARAM ||
|
||||
n.kind == nkind.N_LET ||
|
||||
n.kind == nkind.N_TNAME ||
|
||||
n.kind == nkind.N_TFIELD ||
|
||||
n.kind == nkind.N_TENUMMEMBER ||
|
||||
n.kind == nkind.N_FIELD ||
|
||||
n.kind == nkind.N_ATTR
|
||||
) {
|
||||
// Match C ast.c: print the str field whenever it's non-nil,
|
||||
// even if its length is zero (e.g. an empty STRLIT prints
|
||||
@@ -298,22 +302,22 @@ fn pr(fd: i32, n: *node, d: i32) void = {
|
||||
putq(fd, s);
|
||||
};
|
||||
} else { if (
|
||||
n.kind == N_BIN ||
|
||||
n.kind == N_UN ||
|
||||
n.kind == N_ASSIGN
|
||||
n.kind == nkind.N_BIN ||
|
||||
n.kind == nkind.N_UN ||
|
||||
n.kind == nkind.N_ASSIGN
|
||||
) {
|
||||
putc1(fd, 32u8);
|
||||
let on: str = tokname(n.op);
|
||||
os.write(fd, on.ptr, on.len: u64);
|
||||
};};};};
|
||||
|
||||
if (n.kind == N_FNDECL) {
|
||||
if (n.kind == nkind.N_FNDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == N_DEF) {
|
||||
if (n.kind == nkind.N_DEF) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
if (n.kind == N_TYPEDECL) {
|
||||
if (n.kind == nkind.N_TYPEDECL) {
|
||||
if (n.exported != 0) { os.write(fd, " export".ptr, 7u64); };
|
||||
};
|
||||
putc1(fd, 10u8); // '\n'
|
||||
|
||||
@@ -9,7 +9,7 @@ fn parseuse(p: *parser) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `use`
|
||||
let n: *node = newnode(p.a, N_USE, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_USE, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
n.str = id;
|
||||
@@ -22,7 +22,7 @@ fn parsedef(p: *parser, exported: i32) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `def`
|
||||
let n: *node = newnode(p.a, N_DEF, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_DEF, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
@@ -45,7 +45,7 @@ fn parselet(p: *parser, exported: i32) *node = {
|
||||
let is_const: i32 = 0;
|
||||
if (p.curkind == tkind.TK_CONST) { is_const = 1; };
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_LET, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
@@ -70,7 +70,7 @@ fn parseattrs(p: *parser) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p);
|
||||
let a: *node = newnode(p.a, N_ATTR, pf, pl, pc);
|
||||
let a: *node = newnode(p.a, nkind.N_ATTR, pf, pl, pc);
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
a.str = id;
|
||||
@@ -96,7 +96,7 @@ fn parseparams(p: *parser) *node = {
|
||||
let pf: str = p.curfile;
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
let n: *node = newnode(p.a, N_PARAM, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_PARAM, pf, pl, pc);
|
||||
// Param form: (IDENT|'_') ':' type. Anonymous-type-only params
|
||||
// (used in fn type expressions) aren't yet wired here.
|
||||
let id: str;
|
||||
@@ -117,7 +117,7 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `fn`
|
||||
let n: *node = newnode(p.a, N_FNDECL, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_FNDECL, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
@@ -147,7 +147,7 @@ fn parsetypedecl(p: *parser, exported: i32) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `type`
|
||||
let n: *node = newnode(p.a, N_TYPEDECL, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TYPEDECL, pf, pl, pc);
|
||||
n.module = p.l.module;
|
||||
let id: str;
|
||||
expectident(p, &id);
|
||||
|
||||
@@ -22,56 +22,56 @@ fn parseprimary(p: *parser) *node = {
|
||||
let pc: i32 = p.curcol;
|
||||
|
||||
if (p.curkind == tkind.TK_INT) {
|
||||
let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_INTLIT, pf, pl, pc);
|
||||
n.uval = p.curuval;
|
||||
n.str = p.curtext;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_STR) {
|
||||
let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_STRLIT, pf, pl, pc);
|
||||
n.str = p.curtext;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_RUNE) {
|
||||
let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_RUNELIT, pf, pl, pc);
|
||||
n.uval = p.curuval;
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_TRUE) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_TRUE, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_TRUE, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_FALSE) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_FALSE, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_FALSE, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_NIL) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_NIL, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_NIL, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_VOID) {
|
||||
advance(p);
|
||||
return newnode(p.a, N_VOIDLIT, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_VOIDLIT, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_UNDER) {
|
||||
// Bare `_` — valid only as a discard lvalue. Emit an N_IDENT
|
||||
// Bare `_` — valid only as a discard lvalue. Emit an nkind.N_IDENT
|
||||
// with empty str; the checker rejects it outside lvalue
|
||||
// positions.
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_IDENT, pf, pl, pc);
|
||||
let empty: str;
|
||||
n.str = empty;
|
||||
return n;
|
||||
};
|
||||
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 = "..."
|
||||
// The repeat marker is an nkind.N_FIELD node with str = "..."
|
||||
// appended to the element list so cgen can detect it.
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_ARRLIT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_ARRLIT, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACK) {
|
||||
@@ -80,7 +80,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
if (head == nil) { head = e; tail = e; }
|
||||
else { tail.next = e; tail = e; };
|
||||
if (accepttok(p, tkind.TK_ELLIPSIS)) {
|
||||
let rep: *node = newnode(p.a, N_FIELD,
|
||||
let rep: *node = newnode(p.a, nkind.N_FIELD,
|
||||
p.curfile, p.curline, p.curcol);
|
||||
rep.str = "...";
|
||||
tail.next = rep;
|
||||
@@ -98,7 +98,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
let e: *node = parseexpr(p);
|
||||
// Tuple literal: (a, b, ...)
|
||||
if (accepttok(p, tkind.TK_COMMA)) {
|
||||
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
||||
let t: *node = newnode(p.a, nkind.N_TUPLE, pf, pl, pc);
|
||||
t.list = e;
|
||||
let tail: *node = e;
|
||||
for (true) {
|
||||
@@ -115,7 +115,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
return e;
|
||||
};
|
||||
if (p.curkind == tkind.TK_IDENT) {
|
||||
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_IDENT, pf, pl, pc);
|
||||
n.str = p.curtext;
|
||||
advance(p);
|
||||
// `IDENT {` — struct literal. Disambiguate: only consume as a
|
||||
@@ -126,7 +126,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
// own paren/cond, so this is safe.
|
||||
if (p.curkind == tkind.TK_LBRACE) {
|
||||
advance(p);
|
||||
let s: *node = newnode(p.a, N_STRUCTLIT, pf, pl, pc);
|
||||
let s: *node = newnode(p.a, nkind.N_STRUCTLIT, pf, pl, pc);
|
||||
s.lhs = n;
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
@@ -146,7 +146,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
expectident(p, &id);
|
||||
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);
|
||||
let f: *node = newnode(p.a, nkind.N_FIELD, fpf, fpl, fpc);
|
||||
f.str = id;
|
||||
f.lhs = v;
|
||||
if (head == nil) { head = f; tail = f; }
|
||||
@@ -163,7 +163,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after match");
|
||||
let m: *node = newnode(p.a, N_MATCH, pf, pl, pc);
|
||||
let m: *node = newnode(p.a, nkind.N_MATCH, pf, pl, pc);
|
||||
m.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after match scrutinee");
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' to open match body");
|
||||
@@ -174,7 +174,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
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);
|
||||
let mc: *node = newnode(p.a, nkind.N_MCASE, cf, cl, cc);
|
||||
if (p.curkind == tkind.TK_LET) {
|
||||
advance(p);
|
||||
let id: str;
|
||||
@@ -196,7 +196,7 @@ fn parseprimary(p: *parser) *node = {
|
||||
};
|
||||
errmsg(p, "expected expression");
|
||||
advance(p);
|
||||
return newnode(p.a, N_NONE, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_NONE, pf, pl, pc);
|
||||
};
|
||||
|
||||
fn parsearglist(p: *parser, closekind: i32, headout: **node) void = {
|
||||
@@ -222,12 +222,12 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
let pc: i32 = p.curcol;
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_CALL, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_CALL, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
// size(T)/align(T): the single arg is a type expression,
|
||||
// not a regular expression. Special-case at the parser.
|
||||
let is_typeop: i32 = 0;
|
||||
if (cur.kind == N_IDENT) {
|
||||
if (cur.kind == nkind.N_IDENT) {
|
||||
if (streqlocal(cur.str, "size")) { is_typeop = 1; };
|
||||
if (streqlocal(cur.str, "align")) { is_typeop = 1; };
|
||||
};
|
||||
@@ -247,7 +247,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
// `[ : hi ]` — slice with implicit lo = 0.
|
||||
if (p.curkind == tkind.TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
if (p.curkind != tkind.TK_RBRACK) {
|
||||
n.cond = parseexpr(p);
|
||||
@@ -264,7 +264,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
p.nocast = prev;
|
||||
if (p.curkind == tkind.TK_COLON) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_SLICE, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
if (p.curkind != tkind.TK_RBRACK) {
|
||||
@@ -274,7 +274,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_INDEX, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_INDEX, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = e;
|
||||
expecttok(p, tkind.TK_RBRACK, "expected ']' after index");
|
||||
@@ -283,7 +283,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
};
|
||||
if (p.curkind == tkind.TK_DOT) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_DOT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_DOT, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
// Hare-style tuple field access: `t.0`, `t.1`. The
|
||||
// numeric literal becomes the field name string so the
|
||||
@@ -304,7 +304,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
return cur;
|
||||
};
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_CAST, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_CAST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
@@ -316,7 +316,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
// Same precedence level as the `:` cast.
|
||||
if (p.curkind == tkind.TK_AS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TYPEASSERT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TYPEASSERT, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
@@ -324,7 +324,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
};
|
||||
if (p.curkind == tkind.TK_IS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TYPETEST, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TYPETEST, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
n.rhs = parsetype(p);
|
||||
cur = n;
|
||||
@@ -334,14 +334,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||
// `e!` — abort on error variant.
|
||||
if (p.curkind == tkind.TK_QUESTION) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TRYPROP, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TRYPROP, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
cur = n;
|
||||
continue;
|
||||
};
|
||||
if (p.curkind == tkind.TK_NOT) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TRYUNW, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TRYUNW, pf, pl, pc);
|
||||
n.lhs = cur;
|
||||
cur = n;
|
||||
continue;
|
||||
@@ -358,37 +358,37 @@ fn parseunary(p: *parser) *node = {
|
||||
let k: i32 = p.curkind;
|
||||
if (k == tkind.TK_MINUS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_MINUS; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == tkind.TK_PLUS) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_PLUS; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == tkind.TK_NOT) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_NOT; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == tkind.TK_TILDE) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_TILDE; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == tkind.TK_STAR) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_STAR; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
if (k == tkind.TK_AMP) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_UN, pf, pl, pc);
|
||||
n.op = tkind.TK_AMP; n.lhs = parseunary(p);
|
||||
return n;
|
||||
};
|
||||
@@ -412,7 +412,7 @@ fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
|
||||
if (np <= pr) { break; };
|
||||
rhs = parsebin(p, rhs, np);
|
||||
};
|
||||
let n: *node = newnode(p.a, N_BIN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_BIN, pf, pl, pc);
|
||||
n.op = op; n.lhs = cur; n.rhs = rhs;
|
||||
cur = n;
|
||||
};
|
||||
@@ -427,7 +427,7 @@ fn parseexpr(p: *parser) *node = {
|
||||
let pc: i32 = p.curcol;
|
||||
let op: i32 = p.curkind;
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_ASSIGN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_ASSIGN, pf, pl, pc);
|
||||
n.op = op;
|
||||
n.lhs = e;
|
||||
n.rhs = parseexpr(p); // right-associative
|
||||
|
||||
@@ -132,14 +132,14 @@ fn parsetype(p: *parser) *node = {
|
||||
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);
|
||||
let n: *node = newnode(p.a, nkind.N_TBANG, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_STAR) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TPTR, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
@@ -148,13 +148,13 @@ fn parsetype(p: *parser) *node = {
|
||||
advance(p);
|
||||
if (p.curkind == tkind.TK_RBRACK) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TSLICE, pf, pl, pc);
|
||||
n.lhs = parsetype(p);
|
||||
return n;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_TARRAY, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TARRAY, pf, pl, pc);
|
||||
// `[_]T` — length inferred from initialiser. n.rhs stays nil
|
||||
// as the sentinel; the cgen path for N_LET fills it from the
|
||||
// as the sentinel; the cgen path for nkind.N_LET fills it from the
|
||||
// array literal's element count.
|
||||
if (p.curkind == tkind.TK_UNDER) {
|
||||
advance(p);
|
||||
@@ -169,7 +169,7 @@ fn parsetype(p: *parser) *node = {
|
||||
if (p.curkind == tkind.TK_STRUCT) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' after struct");
|
||||
let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TSTRUCT, pf, pl, pc);
|
||||
let fhead: *node = nil;
|
||||
let ftail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
@@ -177,7 +177,7 @@ fn parsetype(p: *parser) *node = {
|
||||
let fpf: str = p.curfile;
|
||||
let fpl: i32 = p.curline;
|
||||
let fpc: i32 = p.curcol;
|
||||
let f: *node = newnode(p.a, N_TFIELD, fpf, fpl, fpc);
|
||||
let f: *node = newnode(p.a, nkind.N_TFIELD, fpf, fpl, fpc);
|
||||
let fid: str;
|
||||
expectident(p, &fid);
|
||||
f.str = fid;
|
||||
@@ -195,10 +195,10 @@ fn parsetype(p: *parser) *node = {
|
||||
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
|
||||
// nkind.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);
|
||||
let n: *node = newnode(p.a, nkind.N_TENUM, pf, pl, pc);
|
||||
if (p.curkind != tkind.TK_LBRACE) {
|
||||
n.lhs = parsetype(p);
|
||||
};
|
||||
@@ -210,7 +210,7 @@ fn parsetype(p: *parser) *node = {
|
||||
let mpf: str = p.curfile;
|
||||
let mpl: i32 = p.curline;
|
||||
let mpc: i32 = p.curcol;
|
||||
let m: *node = newnode(p.a, N_TENUMMEMBER, mpf, mpl, mpc);
|
||||
let m: *node = newnode(p.a, nkind.N_TENUMMEMBER, mpf, mpl, mpc);
|
||||
let mid: str;
|
||||
expectident(p, &mid);
|
||||
m.str = mid;
|
||||
@@ -227,16 +227,16 @@ fn parsetype(p: *parser) *node = {
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_VOID) {
|
||||
// `void` keyword in type-expr context — emit as N_TNAME so
|
||||
// `void` keyword in type-expr context — emit as nkind.N_TNAME so
|
||||
// resolution treats it like any other primitive name.
|
||||
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TNAME, pf, pl, pc);
|
||||
n.str = "void";
|
||||
advance(p);
|
||||
return n;
|
||||
};
|
||||
|
||||
if (p.curkind == tkind.TK_IDENT) {
|
||||
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TNAME, pf, pl, pc);
|
||||
let acc: str = p.curtext;
|
||||
advance(p);
|
||||
// Dotted path collapse: pkg.Type → single TNAME with the
|
||||
@@ -256,7 +256,7 @@ fn parsetype(p: *parser) *node = {
|
||||
advance(p);
|
||||
let first: *node = parsetype(p);
|
||||
if (accepttok(p, tkind.TK_PIPE)) {
|
||||
let n: *node = newnode(p.a, N_TTAGGED, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TTAGGED, pf, pl, pc);
|
||||
let head: *node = first;
|
||||
let tail: *node = first;
|
||||
for (true) {
|
||||
@@ -273,7 +273,7 @@ fn parsetype(p: *parser) *node = {
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after parenthesised type");
|
||||
return first;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_TTUPLE, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TTUPLE, pf, pl, pc);
|
||||
let head: *node = first;
|
||||
let tail: *node = first;
|
||||
for (true) {
|
||||
@@ -291,7 +291,7 @@ fn parsetype(p: *parser) *node = {
|
||||
if (p.curkind == tkind.TK_FN) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after fn in type");
|
||||
let n: *node = newnode(p.a, N_TFN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_TFN, pf, pl, pc);
|
||||
// Anonymous-or-named params: parseparams handles named only;
|
||||
// for fn-type expressions the C parser allows IDENT-less
|
||||
// (anonymous) params. Stub: only named params for now.
|
||||
@@ -303,7 +303,7 @@ fn parsetype(p: *parser) *node = {
|
||||
|
||||
errmsg(p, "expected type");
|
||||
advance(p);
|
||||
return newnode(p.a, N_TNAME, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_TNAME, pf, pl, pc);
|
||||
};
|
||||
|
||||
// ---- expressions (Pratt) ---------------------------------------------
|
||||
@@ -354,7 +354,7 @@ fn isassignop(k: i32) bool = {
|
||||
// are resolved by the two-pass checker — no body-less prototypes needed.
|
||||
|
||||
export fn parsefile(p: *parser) *node = {
|
||||
let f: *node = newnode(p.a, N_FILE, p.curfile, p.curline, p.curcol);
|
||||
let f: *node = newnode(p.a, nkind.N_FILE, p.curfile, p.curline, p.curcol);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
|
||||
|
||||
@@ -18,14 +18,14 @@ fn parseletlocal(p: *parser) *node = {
|
||||
// doesn't allow types here, but cmd/wcc/parse.c does).
|
||||
if (p.curkind == tkind.TK_LPAREN) {
|
||||
advance(p);
|
||||
let m: *node = newnode(p.a, N_MLET, pf, pl, pc);
|
||||
let m: *node = newnode(p.a, nkind.N_MLET, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (true) {
|
||||
let lpf: str = p.curfile;
|
||||
let lpl: i32 = p.curline;
|
||||
let lpc: i32 = p.curcol;
|
||||
let l: *node = newnode(p.a, N_LET, lpf, lpl, lpc);
|
||||
let l: *node = newnode(p.a, nkind.N_LET, lpf, lpl, lpc);
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
l.str = id;
|
||||
@@ -48,7 +48,7 @@ fn parseletlocal(p: *parser) *node = {
|
||||
return m;
|
||||
};
|
||||
|
||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_LET, pf, pl, pc);
|
||||
let id: str;
|
||||
expectbindname(p, &id);
|
||||
n.str = id;
|
||||
@@ -57,16 +57,16 @@ fn parseletlocal(p: *parser) *node = {
|
||||
};
|
||||
// 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.
|
||||
// its own nkind.N_LET; the wrapping nkind.N_MLET carries the rhs.
|
||||
if (p.curkind == tkind.TK_COMMA) {
|
||||
let m: *node = newnode(p.a, N_MLET, pf, pl, pc);
|
||||
let m: *node = newnode(p.a, nkind.N_MLET, pf, pl, pc);
|
||||
let head: *node = n;
|
||||
let tail: *node = n;
|
||||
for (accepttok(p, tkind.TK_COMMA)) {
|
||||
let lpf: str = p.curfile;
|
||||
let lpl: i32 = p.curline;
|
||||
let lpc: i32 = p.curcol;
|
||||
let l: *node = newnode(p.a, N_LET, lpf, lpl, lpc);
|
||||
let l: *node = newnode(p.a, nkind.N_LET, lpf, lpl, lpc);
|
||||
let id2: str;
|
||||
expectbindname(p, &id2);
|
||||
l.str = id2;
|
||||
@@ -98,7 +98,7 @@ fn parseblock(p: *parser) *node = {
|
||||
let pl: i32 = p.curline;
|
||||
let pc: i32 = p.curcol;
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' to open block");
|
||||
let blk: *node = newnode(p.a, N_BLOCK, pf, pl, pc);
|
||||
let blk: *node = newnode(p.a, nkind.N_BLOCK, pf, pl, pc);
|
||||
let head: *node = nil;
|
||||
let tail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
@@ -120,7 +120,7 @@ fn parseif(p: *parser) *node = {
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `if`
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after if");
|
||||
let n: *node = newnode(p.a, N_IF, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_IF, pf, pl, pc);
|
||||
n.cond = parseexpr(p);
|
||||
expecttok(p, tkind.TK_RPAREN, "expected ')' after if condition");
|
||||
n.body = parseblock(p);
|
||||
@@ -140,11 +140,11 @@ fn parsefor(p: *parser) *node = {
|
||||
let pc: i32 = p.curcol;
|
||||
advance(p); // past `for`
|
||||
expecttok(p, tkind.TK_LPAREN, "expected '(' after for");
|
||||
let n: *node = newnode(p.a, N_FOR, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_FOR, pf, pl, pc);
|
||||
// Three forms (matching C parser):
|
||||
// for (cond) — only cond
|
||||
// for (init; cond; post) — full
|
||||
// for (true) — infinite (cond is N_TRUE)
|
||||
// for (true) — infinite (cond is nkind.N_TRUE)
|
||||
// Distinguish by counting ';'. Look at first chunk: if it's a
|
||||
// `let` stmt that's the init. Otherwise, parse expr; if next is
|
||||
// ';' it was cond. If we see two ';' total after init, post is
|
||||
@@ -204,13 +204,13 @@ fn parsestmt(p: *parser) *node = {
|
||||
};
|
||||
if (p.curkind == tkind.TK_RETURN) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_RETURN, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_RETURN, pf, pl, pc);
|
||||
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 == tkind.TK_COMMA) {
|
||||
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
||||
let t: *node = newnode(p.a, nkind.N_TUPLE, pf, pl, pc);
|
||||
t.list = first;
|
||||
let tail: *node = first;
|
||||
for (accepttok(p, tkind.TK_COMMA)) {
|
||||
@@ -228,14 +228,14 @@ fn parsestmt(p: *parser) *node = {
|
||||
};
|
||||
if (p.curkind == tkind.TK_DEFER) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_DEFER, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_DEFER, pf, pl, pc);
|
||||
n.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after defer");
|
||||
return n;
|
||||
};
|
||||
if (p.curkind == tkind.TK_YIELD) {
|
||||
advance(p);
|
||||
let n: *node = newnode(p.a, N_YIELD, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_YIELD, pf, pl, pc);
|
||||
n.lhs = parseexpr(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after yield");
|
||||
return n;
|
||||
@@ -243,12 +243,12 @@ fn parsestmt(p: *parser) *node = {
|
||||
if (p.curkind == tkind.TK_BREAK) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after break");
|
||||
return newnode(p.a, N_BREAK, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_BREAK, pf, pl, pc);
|
||||
};
|
||||
if (p.curkind == tkind.TK_CONTINUE) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after continue");
|
||||
return newnode(p.a, N_CONTINUE, pf, pl, pc);
|
||||
return newnode(p.a, nkind.N_CONTINUE, pf, pl, pc);
|
||||
};
|
||||
// expression statement, or tuple-destructure multi-assign:
|
||||
// a, b = expr;
|
||||
@@ -258,7 +258,7 @@ fn parsestmt(p: *parser) *node = {
|
||||
// consume — parseexpr would absorb it.
|
||||
let e: *node = parseexpr(p);
|
||||
if (p.curkind == tkind.TK_COMMA) {
|
||||
let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc);
|
||||
let m: *node = newnode(p.a, nkind.N_MASSIGN, pf, pl, pc);
|
||||
let head: *node = e;
|
||||
let tail: *node = e;
|
||||
for (p.curkind == tkind.TK_COMMA) {
|
||||
@@ -273,7 +273,7 @@ fn parsestmt(p: *parser) *node = {
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after multi-assign");
|
||||
return m;
|
||||
};
|
||||
let n: *node = newnode(p.a, N_EXPRSTMT, pf, pl, pc);
|
||||
let n: *node = newnode(p.a, nkind.N_EXPRSTMT, pf, pl, pc);
|
||||
n.lhs = e;
|
||||
expecttok(p, tkind.TK_SEMI, "expected ';' after expression statement");
|
||||
return n;
|
||||
|
||||
Reference in New Issue
Block a user