ww: group lex.ww + tok.ww into lib/ww/lex/ submodule
This commit is contained in:
687
lib/ww/lex/lex.ww
Normal file
687
lib/ww/lex/lex.ww
Normal file
@@ -0,0 +1,687 @@
|
||||
// lib/ww/lex/lex.ww — port of cmd/wcc/lex.c.
|
||||
//
|
||||
// The DFA, the helpers, and the order of decisions all mirror the C
|
||||
// version exactly. The 990_selfhost test diffs the resulting token
|
||||
// stream against the C-side wwdump byte-for-byte; any divergence is
|
||||
// a port bug.
|
||||
//
|
||||
// Calling-convention note: w6c can't yet pass or return structs >16
|
||||
// bytes by value, so `tok` and `pos` are passed by pointer (out
|
||||
// params). The C version passes `Tok` by value; we differ here only
|
||||
// in shape, not in observable behaviour. Token kind values stay
|
||||
// numerically identical.
|
||||
|
||||
use os;
|
||||
use ascii;
|
||||
use mem;
|
||||
use tok;
|
||||
|
||||
type lex = struct {
|
||||
file: str,
|
||||
src: *u8, // raw bytes; not necessarily NUL-terminated
|
||||
srclen: u64,
|
||||
lpos: u64,
|
||||
line: i32,
|
||||
col: i32,
|
||||
a: *arena,
|
||||
errs: i32,
|
||||
module: str, // current module from `// MODULE: foo` directive; "" if none
|
||||
};
|
||||
|
||||
export fn lexinit(l: *lex, a: *arena, file: str, src: *u8, len: u64) void = {
|
||||
l.file = file;
|
||||
l.src = src;
|
||||
l.srclen = len;
|
||||
l.lpos = 0u64;
|
||||
l.line = 1;
|
||||
l.col = 1;
|
||||
l.a = a;
|
||||
l.errs = 0;
|
||||
let empty: str;
|
||||
empty.ptr = nil;
|
||||
empty.len = 0;
|
||||
l.module = empty;
|
||||
};
|
||||
|
||||
// srcb — byte at offset; helper that lifts the cast out of indexing.
|
||||
fn srcb(l: *lex, off: u64) i32 = {
|
||||
let i: i32 = off: i32;
|
||||
let b: u8 = l.src[i];
|
||||
return b: i32;
|
||||
};
|
||||
|
||||
fn lpeek(l: *lex, ahead: u64) i32 = {
|
||||
let p: u64 = l.lpos + ahead;
|
||||
if (p >= l.srclen) { return -1; };
|
||||
return srcb(l, p);
|
||||
};
|
||||
|
||||
fn lget(l: *lex) i32 = {
|
||||
if (l.lpos >= l.srclen) { return -1; };
|
||||
let c: i32 = srcb(l, l.lpos);
|
||||
l.lpos += 1u64;
|
||||
if (c == 10) { // '\n'
|
||||
l.line += 1;
|
||||
l.col = 1;
|
||||
} else {
|
||||
l.col += 1;
|
||||
};
|
||||
return c;
|
||||
};
|
||||
|
||||
fn curpos(l: *lex, out: *pos) void = {
|
||||
out.file = l.file;
|
||||
out.line = l.line;
|
||||
out.col = l.col;
|
||||
};
|
||||
|
||||
// putuint — write `v` (signed, but always non-negative here) to fd 2
|
||||
// in decimal. Standalone so errat doesn't drag in fmt and create a
|
||||
// dependency cycle with strconv.
|
||||
fn putuint(fd: i32, v: i32) void = {
|
||||
let tmp: [16]u8;
|
||||
let i: i32 = 0;
|
||||
let n: i32 = v;
|
||||
for (n > 0) {
|
||||
tmp[i] = ((n % 10) + 48): u8;
|
||||
n = n / 10;
|
||||
i += 1;
|
||||
};
|
||||
if (i == 0) { tmp[0] = 48u8; i = 1; };
|
||||
let buf: [16]u8;
|
||||
let m: i32 = 0;
|
||||
for (i > 0) { i -= 1; buf[m] = tmp[i]; m += 1; };
|
||||
os.write(fd, buf.ptr, m: u64);
|
||||
};
|
||||
|
||||
fn errat(l: *lex, p: *pos, msg: str) void = {
|
||||
let pf: str = p.file;
|
||||
os.write(2, pf.ptr, pf.len: u64);
|
||||
os.write(2, ":".ptr, 1u64);
|
||||
putuint(2, p.line);
|
||||
os.write(2, ":".ptr, 1u64);
|
||||
putuint(2, p.col);
|
||||
os.write(2, ": error: ".ptr, 9u64);
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
l.errs += 1;
|
||||
};
|
||||
|
||||
fn skipws(l: *lex) bool = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { return false; };
|
||||
if (c == 32) { lget(l); continue; };
|
||||
if (c == 9) { lget(l); continue; };
|
||||
if (c == 13) { lget(l); continue; };
|
||||
if (c == 10) { lget(l); continue; };
|
||||
if (c == 47) { // '/'
|
||||
let c2: i32 = lpeek(l, 1u64);
|
||||
if (c2 == 47) {
|
||||
lget(l); lget(l); // consume '//'
|
||||
// Driver injects `// MODULE: foo` before each
|
||||
// source file's contents; capture so cgen can
|
||||
// mangle private symbols by module.
|
||||
if (lpeek(l, 0u64) == 32) { // ' '
|
||||
if (lpeek(l, 1u64) == 77) { // 'M'
|
||||
if (lpeek(l, 2u64) == 79) { // 'O'
|
||||
if (lpeek(l, 3u64) == 68) { // 'D'
|
||||
if (lpeek(l, 4u64) == 85) { // 'U'
|
||||
if (lpeek(l, 5u64) == 76) { // 'L'
|
||||
if (lpeek(l, 6u64) == 69) { // 'E'
|
||||
if (lpeek(l, 7u64) == 58) { // ':'
|
||||
if (lpeek(l, 8u64) == 32) { // ' '
|
||||
let i: i32 = 0;
|
||||
for (i < 9) { lget(l); i += 1; };
|
||||
let start: u64 = l.lpos;
|
||||
for (true) {
|
||||
let cx: i32 = lpeek(l, 0u64);
|
||||
if (cx < 0) { break; };
|
||||
if (cx == 10) { break; };
|
||||
if (cx == 13) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let n: u64 = l.lpos - start;
|
||||
l.module = astrndup(l.a, l.src + start, n);
|
||||
};};};};};};};};};
|
||||
for (true) {
|
||||
let cx: i32 = lpeek(l, 0u64);
|
||||
if (cx < 0) { return false; };
|
||||
if (cx == 10) { break; };
|
||||
lget(l);
|
||||
};
|
||||
continue;
|
||||
};
|
||||
if (c2 == 42) { // '*'
|
||||
lget(l); lget(l);
|
||||
let prev: i32 = -1;
|
||||
for (true) {
|
||||
let x: i32 = lget(l);
|
||||
if (x < 0) {
|
||||
let cp: pos;
|
||||
curpos(l, &cp);
|
||||
errat(l, &cp, "unterminated /* comment");
|
||||
return false;
|
||||
};
|
||||
if (prev == 42) {
|
||||
if (x == 47) { break; };
|
||||
};
|
||||
prev = x;
|
||||
};
|
||||
continue;
|
||||
};
|
||||
};
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
fn parseint(p: *u8, n: u64, base: i32, ok: *bool) u64 = {
|
||||
let v: u64 = 0u64;
|
||||
let got: bool = false;
|
||||
let i: u64 = 0u64;
|
||||
for (i < n) {
|
||||
let ix: i32 = i: i32;
|
||||
let c: u8 = p[ix];
|
||||
if (c == 95u8) { // '_'
|
||||
i += 1u64;
|
||||
continue;
|
||||
};
|
||||
let d: i32 = -1;
|
||||
if (c >= 48u8) {
|
||||
if (c <= 57u8) { d = (c - 48u8): i32; };
|
||||
};
|
||||
if (d < 0) {
|
||||
if (c >= 97u8) {
|
||||
if (c <= 102u8) { d = ((c - 97u8) + 10u8): i32; };
|
||||
};
|
||||
};
|
||||
if (d < 0) {
|
||||
if (c >= 65u8) {
|
||||
if (c <= 70u8) { d = ((c - 65u8) + 10u8): i32; };
|
||||
};
|
||||
};
|
||||
if (d < 0) { *ok = false; return 0u64; };
|
||||
if (d >= base) { *ok = false; return 0u64; };
|
||||
v = v * (base: u64) + (d: u64);
|
||||
got = true;
|
||||
i += 1u64;
|
||||
};
|
||||
*ok = got;
|
||||
return v;
|
||||
};
|
||||
|
||||
fn escape(l: *lex, out: *i32) bool = {
|
||||
let c: i32 = lget(l);
|
||||
if (c < 0) { return false; };
|
||||
if (c == 110) { *out = 10; return true; };
|
||||
if (c == 116) { *out = 9; return true; };
|
||||
if (c == 114) { *out = 13; return true; };
|
||||
if (c == 92) { *out = 92; return true; };
|
||||
if (c == 39) { *out = 39; return true; };
|
||||
if (c == 34) { *out = 34; return true; };
|
||||
if (c == 48) { *out = 0; return true; };
|
||||
if (c == 97) { *out = 7; return true; };
|
||||
if (c == 98) { *out = 8; return true; };
|
||||
if (c == 102) { *out = 12; return true; };
|
||||
if (c == 118) { *out = 11; return true; };
|
||||
if (c == 120) {
|
||||
let hi: i32 = lget(l);
|
||||
let lo: i32 = lget(l);
|
||||
if (hi < 0) { return false; };
|
||||
if (lo < 0) { return false; };
|
||||
if (!ascii.ishex(hi: u8)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
if (!ascii.ishex(lo: u8)) {
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad \\x escape");
|
||||
return false;
|
||||
};
|
||||
let h: i32 = ascii.digitval(hi: u8);
|
||||
let lv: i32 = ascii.digitval(lo: u8);
|
||||
*out = (h << 4) | lv;
|
||||
return true;
|
||||
};
|
||||
let cp: pos; curpos(l, &cp);
|
||||
errat(l, &cp, "bad escape");
|
||||
return false;
|
||||
};
|
||||
|
||||
// scandecimalrun — consume a run of decimal digits and underscores.
|
||||
fn scandecimalrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isdigit(c: u8)) {
|
||||
if (c != 95) { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanhexrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.ishex(c: u8)) {
|
||||
if (c != 95) { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn scanbinrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c == 48) { lget(l); continue; };
|
||||
if (c == 49) { lget(l); continue; };
|
||||
if (c == 95) { lget(l); continue; };
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
fn scanoctrun(l: *lex) void = {
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 48) { break; };
|
||||
if (c > 55) {
|
||||
if (c != 95) { break; };
|
||||
};
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
// scanexp — consume the [eE][+-]?[0-9]+ tail of a float, if present.
|
||||
fn scanexp(l: *lex) void = {
|
||||
let e: i32 = lpeek(l, 0u64);
|
||||
if (e != 101) { if (e != 69) { return; }; }; // 'e' or 'E'
|
||||
lget(l);
|
||||
let s: i32 = lpeek(l, 0u64);
|
||||
if (s == 43) { lget(l); }
|
||||
else { if (s == 45) { lget(l); }; };
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isdigit(c: u8)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
};
|
||||
|
||||
fn lexnum(l: *lex, start: *pos, out: *tok) void = {
|
||||
out.kind = TK_INT;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let begin: u64 = l.lpos;
|
||||
let base: i32 = 10;
|
||||
let isfloat: bool = false;
|
||||
|
||||
let c0: i32 = lpeek(l, 0u64);
|
||||
let c1: i32 = lpeek(l, 1u64);
|
||||
|
||||
if (c0 == 48) { // '0'
|
||||
if (c1 == 120) { // 'x'
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 88) { // 'X'
|
||||
lget(l); lget(l); base = 16; scanhexrun(l);
|
||||
} else { if (c1 == 98) { // 'b'
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 66) { // 'B'
|
||||
lget(l); lget(l); base = 2; scanbinrun(l);
|
||||
} else { if (c1 == 111) { // 'o'
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else { if (c1 == 79) { // 'O'
|
||||
lget(l); lget(l); base = 8; scanoctrun(l);
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= 48) {
|
||||
if (after <= 57) {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};};};};};};
|
||||
} else {
|
||||
scandecimalrun(l);
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
let after: i32 = lpeek(l, 1u64);
|
||||
if (after >= 48) {
|
||||
if (after <= 57) {
|
||||
isfloat = true;
|
||||
lget(l);
|
||||
scandecimalrun(l);
|
||||
scanexp(l);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
let n: u64 = l.lpos - begin;
|
||||
out.text = astrndup(l.a, l.src + begin, n);
|
||||
|
||||
if (isfloat) {
|
||||
// out.fval is already 0 from the top-of-lexnext clear.
|
||||
// We don't strtod the literal yet — the diff fixtures we
|
||||
// care about are float-free; any TK_FLOAT seen in source
|
||||
// gets a placeholder value until we wire a real parser.
|
||||
out.kind = TK_FLOAT;
|
||||
} else {
|
||||
let digs: *u8 = l.src + begin;
|
||||
let dn: u64 = n;
|
||||
if (base != 10) {
|
||||
digs = digs + 2u64;
|
||||
dn -= 2u64;
|
||||
};
|
||||
let ok: bool = false;
|
||||
out.uval = parseint(digs, dn, base, &ok);
|
||||
if (!ok) {
|
||||
errat(l, start, "bad integer literal");
|
||||
out.kind = TK_ERR;
|
||||
};
|
||||
};
|
||||
|
||||
let pc: i32 = lpeek(l, 0u64);
|
||||
if (pc >= 0) {
|
||||
if (ascii.isidstart(pc: u8)) {
|
||||
let sb: u64 = l.lpos;
|
||||
for (true) {
|
||||
let cc: i32 = lpeek(l, 0u64);
|
||||
if (cc < 0) { break; };
|
||||
if (!ascii.isidpart(cc: u8)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let sl: u64 = l.lpos - sb;
|
||||
let p: *u8 = l.src + sb;
|
||||
let isok: bool = false;
|
||||
if (sl == 2u64) {
|
||||
if (p[0] == 105u8) {
|
||||
if (p[1] == 56u8) { isok = true; }; // i8
|
||||
};
|
||||
if (p[0] == 117u8) {
|
||||
if (p[1] == 56u8) { isok = true; }; // u8
|
||||
};
|
||||
};
|
||||
if (sl == 3u64) {
|
||||
if (p[0] == 105u8) {
|
||||
if (p[1] == 49u8) { if (p[2] == 54u8) { isok = true; }; }; // i16
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; }; // i32
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; }; // i64
|
||||
};
|
||||
if (p[0] == 117u8) {
|
||||
if (p[1] == 49u8) { if (p[2] == 54u8) { isok = true; }; };
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; };
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; };
|
||||
};
|
||||
if (p[0] == 102u8) {
|
||||
if (p[1] == 51u8) { if (p[2] == 50u8) { isok = true; }; }; // f32
|
||||
if (p[1] == 54u8) { if (p[2] == 52u8) { isok = true; }; }; // f64
|
||||
};
|
||||
};
|
||||
if (isok) {
|
||||
out.tsuffix = astrndup(l.a, p, sl);
|
||||
} else {
|
||||
l.lpos = sb;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
fn lexident(l: *lex, start: *pos, out: *tok) void = {
|
||||
let begin: u64 = l.lpos;
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) { break; };
|
||||
if (!ascii.isidpart(c: u8)) { break; };
|
||||
lget(l);
|
||||
};
|
||||
let n: u64 = l.lpos - begin;
|
||||
let p: *u8 = l.src + begin;
|
||||
let k: i32 = kwlookup(p, n: i32);
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
if (k != TK_NONE) {
|
||||
out.kind = k;
|
||||
} else {
|
||||
out.kind = TK_IDENT;
|
||||
};
|
||||
out.text = astrndup(l.a, p, n);
|
||||
};
|
||||
|
||||
fn lexstr(l: *lex, start: *pos, out: *tok) void = {
|
||||
let cap: u64 = 32u64;
|
||||
let nb: u64 = 0u64;
|
||||
let buf: *u8 = amalloc(l.a, cap): *u8;
|
||||
for (true) {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) {
|
||||
errat(l, start, "unterminated string");
|
||||
out.kind = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
if (c == 34) { lget(l); break; };
|
||||
let ch: i32 = 0;
|
||||
if (c == 92) {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (nb + 1u64 >= cap) {
|
||||
let ncap: u64 = cap * 2u64;
|
||||
let nb2: *u8 = amalloc(l.a, ncap): *u8;
|
||||
let i: u64 = 0u64;
|
||||
for (i < nb) {
|
||||
let ix: i32 = i: i32;
|
||||
nb2[ix] = buf[ix];
|
||||
i += 1u64;
|
||||
};
|
||||
buf = nb2;
|
||||
cap = ncap;
|
||||
};
|
||||
let nbi: i32 = nb: i32;
|
||||
buf[nbi] = ch: u8;
|
||||
nb += 1u64;
|
||||
};
|
||||
out.kind = TK_STR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
let s: str;
|
||||
s.ptr = buf;
|
||||
s.len = nb: i32;
|
||||
out.text = s;
|
||||
};
|
||||
|
||||
fn lexrune(l: *lex, start: *pos, out: *tok) void = {
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
if (c < 0) {
|
||||
errat(l, start, "unterminated rune");
|
||||
out.kind = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
let ch: i32 = 0;
|
||||
if (c == 92) {
|
||||
lget(l);
|
||||
if (!escape(l, &ch)) { ch = 0; };
|
||||
} else {
|
||||
ch = lget(l);
|
||||
};
|
||||
if (lpeek(l, 0u64) != 39) {
|
||||
errat(l, start, "rune literal missing closing '");
|
||||
out.kind = TK_ERR;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.text = astrndup(l.a, "".ptr, 0u64);
|
||||
return;
|
||||
};
|
||||
lget(l);
|
||||
out.kind = TK_RUNE;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
out.uval = ch: u64;
|
||||
};
|
||||
|
||||
fn emitsimple(start: *pos, k: i32, out: *tok) void = {
|
||||
out.kind = k;
|
||||
out.file = start.file;
|
||||
out.line = start.line;
|
||||
out.col = start.col;
|
||||
};
|
||||
|
||||
// setposfrom — copy file/line/col from a *pos into a tok. Used by
|
||||
// the err-token path where we already have a pos.
|
||||
fn setposfrom(out: *tok, p: *pos) void = {
|
||||
out.file = p.file;
|
||||
out.line = p.line;
|
||||
out.col = p.col;
|
||||
};
|
||||
|
||||
export fn lexnext(l: *lex, out: *tok) void = {
|
||||
// Reset the out token so callers can rely on stale fields being
|
||||
// cleared (they only inspect kind, pos, text, uval, fval, tsuffix
|
||||
// per kind).
|
||||
out.kind = TK_NONE;
|
||||
out.uval = 0u64;
|
||||
// out.fval starts cleared by the caller's stack-local init (lex.ww
|
||||
// allocates the tok with `let t: tok;` which zeroes). We avoid
|
||||
// writing a 0.0 literal here so this file itself stays float-free
|
||||
// and the C/ww wwdump diff over it is byte-identical.
|
||||
let empty: str;
|
||||
empty.ptr = nil;
|
||||
empty.len = 0;
|
||||
out.text = empty;
|
||||
out.tsuffix = empty;
|
||||
|
||||
if (!skipws(l)) {
|
||||
let p: pos; curpos(l, &p);
|
||||
emitsimple(&p, TK_EOF, out);
|
||||
return;
|
||||
};
|
||||
let start: pos; curpos(l, &start);
|
||||
let c: i32 = lpeek(l, 0u64);
|
||||
|
||||
if (c >= 0) {
|
||||
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
|
||||
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
|
||||
};
|
||||
|
||||
if (c == 34) { lget(l); lexstr(l, &start, out); return; };
|
||||
if (c == 39) { lget(l); lexrune(l, &start, out); return; };
|
||||
|
||||
lget(l);
|
||||
|
||||
if (c == 40) { emitsimple(&start, TK_LPAREN, out); return; };
|
||||
if (c == 41) { emitsimple(&start, TK_RPAREN, out); return; };
|
||||
if (c == 123) { emitsimple(&start, TK_LBRACE, out); return; };
|
||||
if (c == 125) { emitsimple(&start, TK_RBRACE, out); return; };
|
||||
if (c == 91) { emitsimple(&start, TK_LBRACK, out); return; };
|
||||
if (c == 93) { emitsimple(&start, TK_RBRACK, out); return; };
|
||||
if (c == 44) { emitsimple(&start, TK_COMMA, out); return; };
|
||||
if (c == 59) { emitsimple(&start, TK_SEMI, out); return; };
|
||||
if (c == 58) { emitsimple(&start, TK_COLON, out); return; };
|
||||
if (c == 64) { emitsimple(&start, TK_AT, out); return; };
|
||||
if (c == 63) { emitsimple(&start, TK_QUESTION, out); return; };
|
||||
if (c == 126) { emitsimple(&start, TK_TILDE, out); return; };
|
||||
|
||||
if (c == 46) { // '.'
|
||||
if (lpeek(l, 0u64) == 46) {
|
||||
if (lpeek(l, 1u64) == 46) {
|
||||
lget(l); lget(l);
|
||||
emitsimple(&start, TK_ELLIPSIS, out); return;
|
||||
};
|
||||
lget(l);
|
||||
emitsimple(&start, TK_DOTDOT, out); return;
|
||||
};
|
||||
emitsimple(&start, TK_DOT, out); return;
|
||||
};
|
||||
|
||||
if (c == 43) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PLUSEQ, out); return; };
|
||||
emitsimple(&start, TK_PLUS, out); return;
|
||||
};
|
||||
if (c == 45) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_MINUSEQ, out); return; };
|
||||
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, TK_ARROW, out); return; };
|
||||
emitsimple(&start, TK_MINUS, out); return;
|
||||
};
|
||||
if (c == 42) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_STAREQ, out); return; };
|
||||
emitsimple(&start, TK_STAR, out); return;
|
||||
};
|
||||
if (c == 47) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_SLASHEQ, out); return; };
|
||||
emitsimple(&start, TK_SLASH, out); return;
|
||||
};
|
||||
if (c == 37) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PERCENTEQ, out); return; };
|
||||
emitsimple(&start, TK_PERCENT, out); return;
|
||||
};
|
||||
if (c == 38) {
|
||||
if (lpeek(l, 0u64) == 38) { lget(l); emitsimple(&start, TK_AND, out); return; };
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_AMPEQ, out); return; };
|
||||
emitsimple(&start, TK_AMP, out); return;
|
||||
};
|
||||
if (c == 124) {
|
||||
if (lpeek(l, 0u64) == 124) { lget(l); emitsimple(&start, TK_OR, out); return; };
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_PIPEEQ, out); return; };
|
||||
emitsimple(&start, TK_PIPE, out); return;
|
||||
};
|
||||
if (c == 94) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_CARETEQ, out); return; };
|
||||
emitsimple(&start, TK_CARET, out); return;
|
||||
};
|
||||
if (c == 61) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_EQ, out); return; };
|
||||
if (lpeek(l, 0u64) == 62) { lget(l); emitsimple(&start, TK_FATARROW, out); return; };
|
||||
emitsimple(&start, TK_ASSIGN, out); return;
|
||||
};
|
||||
if (c == 33) {
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_NEQ, out); return; };
|
||||
emitsimple(&start, TK_NOT, out); return;
|
||||
};
|
||||
if (c == 60) {
|
||||
if (lpeek(l, 0u64) == 60) {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_LSHIFTEQ, out); return; };
|
||||
emitsimple(&start, TK_LSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_LE, out); return; };
|
||||
if (lpeek(l, 0u64) == 45) { lget(l); emitsimple(&start, TK_LARROW, out); return; };
|
||||
emitsimple(&start, TK_LT, out); return;
|
||||
};
|
||||
if (c == 62) {
|
||||
if (lpeek(l, 0u64) == 62) {
|
||||
lget(l);
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_RSHIFTEQ, out); return; };
|
||||
emitsimple(&start, TK_RSHIFT, out); return;
|
||||
};
|
||||
if (lpeek(l, 0u64) == 61) { lget(l); emitsimple(&start, TK_GE, out); return; };
|
||||
emitsimple(&start, TK_GT, out); return;
|
||||
};
|
||||
|
||||
errat(l, &start, "unexpected character");
|
||||
out.kind = TK_ERR;
|
||||
setposfrom(out, &start);
|
||||
let one: [1]u8;
|
||||
one[0] = c: u8;
|
||||
out.text = astrndup(l.a, one.ptr, 1u64);
|
||||
};
|
||||
394
lib/ww/lex/tok.ww
Normal file
394
lib/ww/lex/tok.ww
Normal file
@@ -0,0 +1,394 @@
|
||||
// lib/ww/lex/tok.ww — port of cmd/wcc/tok.c plus the Tkind /
|
||||
// Tok / Pos shapes from cmd/wcc/ww.h.
|
||||
//
|
||||
// Token kind values must stay numerically equal to the C side: the
|
||||
// 990_selfhost test diffs ww-side wwdump output against C-side
|
||||
// wwdump output, byte-for-byte. Reordering this list shifts the
|
||||
// integers and breaks the diff.
|
||||
//
|
||||
// Bottom of file: tokprint, which emits one token per line in a
|
||||
// format identical to cmd/wcc/tok.c:tokprint().
|
||||
|
||||
use os;
|
||||
use strconv;
|
||||
|
||||
// ---- Tkind ------------------------------------------------------------
|
||||
// Mirror of the C enum in cmd/wcc/ww.h. Don't reorder.
|
||||
|
||||
def TK_NONE: i32 = 0;
|
||||
def TK_EOF: i32 = 1;
|
||||
def TK_ERR: i32 = 2;
|
||||
def TK_IDENT: i32 = 3;
|
||||
def TK_INT: i32 = 4;
|
||||
def TK_FLOAT: i32 = 5;
|
||||
def TK_RUNE: i32 = 6;
|
||||
def TK_STR: i32 = 7;
|
||||
|
||||
def TK_FN: i32 = 8;
|
||||
def TK_LET: i32 = 9;
|
||||
def TK_DEF: i32 = 10;
|
||||
def TK_IF: i32 = 11;
|
||||
def TK_ELSE: i32 = 12;
|
||||
def TK_FOR: i32 = 13;
|
||||
def TK_SWITCH: i32 = 14;
|
||||
def TK_CASE: i32 = 15;
|
||||
def TK_RETURN: i32 = 16;
|
||||
def TK_USE: i32 = 17;
|
||||
def TK_TYPE: i32 = 18;
|
||||
def TK_STRUCT: i32 = 19;
|
||||
def TK_DEFER: i32 = 20;
|
||||
def TK_BREAK: i32 = 21;
|
||||
def TK_CONTINUE: i32 = 22;
|
||||
def TK_EXPORT: i32 = 23;
|
||||
def TK_PROC: i32 = 24;
|
||||
def TK_CHAN: i32 = 25;
|
||||
def TK_NIL: i32 = 26;
|
||||
def TK_TRUE: i32 = 27;
|
||||
def TK_FALSE: i32 = 28;
|
||||
def TK_AS: i32 = 29;
|
||||
def TK_STATIC: i32 = 30;
|
||||
def TK_MATCH: i32 = 31;
|
||||
|
||||
def TK_LPAREN: i32 = 32;
|
||||
def TK_RPAREN: i32 = 33;
|
||||
def TK_LBRACE: i32 = 34;
|
||||
def TK_RBRACE: i32 = 35;
|
||||
def TK_LBRACK: i32 = 36;
|
||||
def TK_RBRACK: i32 = 37;
|
||||
def TK_COMMA: i32 = 38;
|
||||
def TK_SEMI: i32 = 39;
|
||||
def TK_COLON: i32 = 40;
|
||||
def TK_DOT: i32 = 41;
|
||||
def TK_ELLIPSIS: i32 = 42;
|
||||
def TK_DOTDOT: i32 = 43;
|
||||
def TK_AT: i32 = 44;
|
||||
def TK_QUESTION: i32 = 45;
|
||||
|
||||
def TK_ASSIGN: i32 = 46;
|
||||
def TK_PLUSEQ: i32 = 47;
|
||||
def TK_MINUSEQ: i32 = 48;
|
||||
def TK_STAREQ: i32 = 49;
|
||||
def TK_SLASHEQ: i32 = 50;
|
||||
def TK_PERCENTEQ: i32 = 51;
|
||||
def TK_AMPEQ: i32 = 52;
|
||||
def TK_PIPEEQ: i32 = 53;
|
||||
def TK_CARETEQ: i32 = 54;
|
||||
def TK_LSHIFTEQ: i32 = 55;
|
||||
def TK_RSHIFTEQ: i32 = 56;
|
||||
|
||||
def TK_PLUS: i32 = 57;
|
||||
def TK_MINUS: i32 = 58;
|
||||
def TK_STAR: i32 = 59;
|
||||
def TK_SLASH: i32 = 60;
|
||||
def TK_PERCENT: i32 = 61;
|
||||
def TK_AMP: i32 = 62;
|
||||
def TK_PIPE: i32 = 63;
|
||||
def TK_CARET: i32 = 64;
|
||||
def TK_TILDE: i32 = 65;
|
||||
def TK_LSHIFT: i32 = 66;
|
||||
def TK_RSHIFT: i32 = 67;
|
||||
|
||||
def TK_EQ: i32 = 68;
|
||||
def TK_NEQ: i32 = 69;
|
||||
def TK_LT: i32 = 70;
|
||||
def TK_LE: i32 = 71;
|
||||
def TK_GT: i32 = 72;
|
||||
def TK_GE: i32 = 73;
|
||||
|
||||
def TK_AND: i32 = 74;
|
||||
def TK_OR: i32 = 75;
|
||||
def TK_NOT: i32 = 76;
|
||||
|
||||
def TK_LARROW: i32 = 77;
|
||||
def TK_ARROW: i32 = 78;
|
||||
def TK_FATARROW: i32 = 79;
|
||||
|
||||
def TK_LAST: i32 = 80;
|
||||
|
||||
// ---- Pos / Tok --------------------------------------------------------
|
||||
//
|
||||
// `pos` is used at error-reporting boundaries; we always pass it via
|
||||
// *pos so the value never gets struct-copied (w6c can't yet copy a
|
||||
// 24-byte struct).
|
||||
//
|
||||
// `tok` is flat — file/line/col live directly on the token rather than
|
||||
// nested inside a `pos` field. Same reason: nested struct field
|
||||
// assignment isn't supported, and flat primitives are.
|
||||
|
||||
type pos = struct {
|
||||
file: str,
|
||||
line: i32,
|
||||
col: i32,
|
||||
};
|
||||
|
||||
type tok = struct {
|
||||
kind: i32,
|
||||
file: str, // path of the source the token came from
|
||||
line: i32,
|
||||
col: i32,
|
||||
text: str, // arena-owned token text (TK_IDENT, TK_STR, TK_ERR)
|
||||
uval: u64, // TK_INT, TK_RUNE
|
||||
fval: f64, // TK_FLOAT
|
||||
tsuffix: str, // typed numeric literal suffix or empty
|
||||
};
|
||||
|
||||
// ---- keyword lookup ---------------------------------------------------
|
||||
|
||||
fn streqn(a: *u8, b: str, n: i32) bool = {
|
||||
if (b.len != n) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
// kwlookup — returns the matching TK_* keyword kind for a byte run,
|
||||
// or TK_NONE if it's an ordinary identifier. Linear search over a
|
||||
// small alphabetised list, matching cmd/wcc/tok.c.
|
||||
export fn kwlookup(p: *u8, n: i32) i32 = {
|
||||
if (streqn(p, "as", n)) { return TK_AS; };
|
||||
if (streqn(p, "break", n)) { return TK_BREAK; };
|
||||
if (streqn(p, "case", n)) { return TK_CASE; };
|
||||
if (streqn(p, "chan", n)) { return TK_CHAN; };
|
||||
if (streqn(p, "continue", n)) { return TK_CONTINUE; };
|
||||
if (streqn(p, "def", n)) { return TK_DEF; };
|
||||
if (streqn(p, "defer", n)) { return TK_DEFER; };
|
||||
if (streqn(p, "else", n)) { return TK_ELSE; };
|
||||
if (streqn(p, "export", n)) { return TK_EXPORT; };
|
||||
if (streqn(p, "false", n)) { return TK_FALSE; };
|
||||
if (streqn(p, "fn", n)) { return TK_FN; };
|
||||
if (streqn(p, "for", n)) { return TK_FOR; };
|
||||
if (streqn(p, "if", n)) { return TK_IF; };
|
||||
if (streqn(p, "let", n)) { return TK_LET; };
|
||||
if (streqn(p, "match", n)) { return TK_MATCH; };
|
||||
if (streqn(p, "nil", n)) { return TK_NIL; };
|
||||
if (streqn(p, "proc", n)) { return TK_PROC; };
|
||||
if (streqn(p, "return", n)) { return TK_RETURN; };
|
||||
if (streqn(p, "static", n)) { return TK_STATIC; };
|
||||
if (streqn(p, "struct", n)) { return TK_STRUCT; };
|
||||
if (streqn(p, "switch", n)) { return TK_SWITCH; };
|
||||
if (streqn(p, "true", n)) { return TK_TRUE; };
|
||||
if (streqn(p, "type", n)) { return TK_TYPE; };
|
||||
if (streqn(p, "use", n)) { return TK_USE; };
|
||||
return TK_NONE;
|
||||
};
|
||||
|
||||
// ---- tokname ----------------------------------------------------------
|
||||
//
|
||||
// Returns the canonical printable spelling for a token kind. Matches
|
||||
// the C tokname()'s output exactly so wwdump output diffs cleanly.
|
||||
|
||||
export fn tokname(k: i32) str = {
|
||||
if (k == TK_NONE) { return "<none>"; };
|
||||
if (k == TK_EOF) { return "EOF"; };
|
||||
if (k == TK_ERR) { return "ERR"; };
|
||||
if (k == TK_IDENT) { return "IDENT"; };
|
||||
if (k == TK_INT) { return "INT"; };
|
||||
if (k == TK_FLOAT) { return "FLOAT"; };
|
||||
if (k == TK_RUNE) { return "RUNE"; };
|
||||
if (k == TK_STR) { return "STR"; };
|
||||
|
||||
if (k == TK_FN) { return "fn"; };
|
||||
if (k == TK_LET) { return "let"; };
|
||||
if (k == TK_DEF) { return "def"; };
|
||||
if (k == TK_IF) { return "if"; };
|
||||
if (k == TK_ELSE) { return "else"; };
|
||||
if (k == TK_FOR) { return "for"; };
|
||||
if (k == TK_SWITCH) { return "switch"; };
|
||||
if (k == TK_CASE) { return "case"; };
|
||||
if (k == TK_RETURN) { return "return"; };
|
||||
if (k == TK_USE) { return "use"; };
|
||||
if (k == TK_TYPE) { return "type"; };
|
||||
if (k == TK_STRUCT) { return "struct"; };
|
||||
if (k == TK_DEFER) { return "defer"; };
|
||||
if (k == TK_BREAK) { return "break"; };
|
||||
if (k == TK_CONTINUE) { return "continue"; };
|
||||
if (k == TK_EXPORT) { return "export"; };
|
||||
if (k == TK_PROC) { return "proc"; };
|
||||
if (k == TK_CHAN) { return "chan"; };
|
||||
if (k == TK_NIL) { return "nil"; };
|
||||
if (k == TK_TRUE) { return "true"; };
|
||||
if (k == TK_FALSE) { return "false"; };
|
||||
if (k == TK_AS) { return "as"; };
|
||||
if (k == TK_STATIC) { return "static"; };
|
||||
if (k == TK_MATCH) { return "match"; };
|
||||
|
||||
if (k == TK_LPAREN) { return "("; };
|
||||
if (k == TK_RPAREN) { return ")"; };
|
||||
if (k == TK_LBRACE) { return "{"; };
|
||||
if (k == TK_RBRACE) { return "}"; };
|
||||
if (k == TK_LBRACK) { return "["; };
|
||||
if (k == TK_RBRACK) { return "]"; };
|
||||
if (k == TK_COMMA) { return ","; };
|
||||
if (k == TK_SEMI) { return ";"; };
|
||||
if (k == TK_COLON) { return ":"; };
|
||||
if (k == TK_DOT) { return "."; };
|
||||
if (k == TK_ELLIPSIS) { return "..."; };
|
||||
if (k == TK_DOTDOT) { return ".."; };
|
||||
if (k == TK_AT) { return "@"; };
|
||||
if (k == TK_QUESTION) { return "?"; };
|
||||
|
||||
if (k == TK_ASSIGN) { return "="; };
|
||||
if (k == TK_PLUSEQ) { return "+="; };
|
||||
if (k == TK_MINUSEQ) { return "-="; };
|
||||
if (k == TK_STAREQ) { return "*="; };
|
||||
if (k == TK_SLASHEQ) { return "/="; };
|
||||
if (k == TK_PERCENTEQ) { return "%="; };
|
||||
if (k == TK_AMPEQ) { return "&="; };
|
||||
if (k == TK_PIPEEQ) { return "|="; };
|
||||
if (k == TK_CARETEQ) { return "^="; };
|
||||
if (k == TK_LSHIFTEQ) { return "<<="; };
|
||||
if (k == TK_RSHIFTEQ) { return ">>="; };
|
||||
|
||||
if (k == TK_PLUS) { return "+"; };
|
||||
if (k == TK_MINUS) { return "-"; };
|
||||
if (k == TK_STAR) { return "*"; };
|
||||
if (k == TK_SLASH) { return "/"; };
|
||||
if (k == TK_PERCENT) { return "%"; };
|
||||
if (k == TK_AMP) { return "&"; };
|
||||
if (k == TK_PIPE) { return "|"; };
|
||||
if (k == TK_CARET) { return "^"; };
|
||||
if (k == TK_TILDE) { return "~"; };
|
||||
if (k == TK_LSHIFT) { return "<<"; };
|
||||
if (k == TK_RSHIFT) { return ">>"; };
|
||||
|
||||
if (k == TK_EQ) { return "=="; };
|
||||
if (k == TK_NEQ) { return "!="; };
|
||||
if (k == TK_LT) { return "<"; };
|
||||
if (k == TK_LE) { return "<="; };
|
||||
if (k == TK_GT) { return ">"; };
|
||||
if (k == TK_GE) { return ">="; };
|
||||
|
||||
if (k == TK_AND) { return "&&"; };
|
||||
if (k == TK_OR) { return "||"; };
|
||||
if (k == TK_NOT) { return "!"; };
|
||||
|
||||
if (k == TK_LARROW) { return "<-"; };
|
||||
if (k == TK_ARROW) { return "->"; };
|
||||
if (k == TK_FATARROW) { return "=>"; };
|
||||
|
||||
if (k == TK_LAST) { return "<last>"; };
|
||||
return "<?>";
|
||||
};
|
||||
|
||||
// ---- writer for tokprint ----------------------------------------------
|
||||
//
|
||||
// fputq mirrors cmd/wcc/tok.c:fputq — quote the string with C-style
|
||||
// escapes for \, ", \n, \t, \r and \xNN for other non-printables.
|
||||
|
||||
fn fputcbyte(fd: i32, b: u8) void = {
|
||||
let buf: [1]u8;
|
||||
buf[0] = b;
|
||||
os.write(fd, buf.ptr, 1u64);
|
||||
};
|
||||
|
||||
fn fputsstr(fd: i32, s: str) void = {
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
fn hexchar(n: u8) u8 = {
|
||||
if (n < 10u8) { return n + 48u8; }; // '0'..'9'
|
||||
return (n - 10u8) + 97u8; // 'a'..'f'
|
||||
};
|
||||
|
||||
fn fputhex2(fd: i32, b: u8) void = {
|
||||
let out: [4]u8;
|
||||
out[0] = 92u8; // '\\'
|
||||
out[1] = 120u8; // 'x'
|
||||
out[2] = hexchar(b >> 4u8);
|
||||
out[3] = hexchar(b & 15u8);
|
||||
os.write(fd, out.ptr, 4u64);
|
||||
};
|
||||
|
||||
fn fputq(fd: i32, p: *u8, n: i32) void = {
|
||||
fputcbyte(fd, 34u8); // '"'
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
let c: u8 = p[i];
|
||||
if (c == 92u8) { // '\\'
|
||||
fputsstr(fd, "\\\\");
|
||||
} else {
|
||||
if (c == 34u8) { // '"'
|
||||
fputsstr(fd, "\\\"");
|
||||
} else {
|
||||
if (c == 10u8) { // '\n'
|
||||
fputsstr(fd, "\\n");
|
||||
} else {
|
||||
if (c == 9u8) { // '\t'
|
||||
fputsstr(fd, "\\t");
|
||||
} else {
|
||||
if (c == 13u8) { // '\r'
|
||||
fputsstr(fd, "\\r");
|
||||
} else {
|
||||
if (c < 32u8) {
|
||||
fputhex2(fd, c);
|
||||
} else {
|
||||
if (c == 127u8) {
|
||||
fputhex2(fd, c);
|
||||
} else {
|
||||
fputcbyte(fd, c);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
fputcbyte(fd, 34u8);
|
||||
};
|
||||
|
||||
// tokprint — write one token line to fd. Format must match
|
||||
// cmd/wcc/tok.c:tokprint() byte-for-byte: that's the diff anchor.
|
||||
// "<file>:<line>:<col> <kindname>[ <value>]\n"
|
||||
//
|
||||
// Takes `t` by pointer because w6c can't yet pass a >16-byte struct
|
||||
// by value; the C version takes Tok by value.
|
||||
export fn tokprint(fd: i32, t: *tok) void = {
|
||||
// Chained-dot field reads (`t.x.y`) on str sub-fields aren't yet
|
||||
// reduced by w6c — `t.x.y` returns the whole str. Lift the str
|
||||
// fields into locals so we can use the str pseudo-field path.
|
||||
let tfile: str = t.file;
|
||||
let ttext: str = t.text;
|
||||
if (tfile.len > 0) {
|
||||
fputsstr(fd, tfile);
|
||||
} else {
|
||||
fputsstr(fd, "<none>");
|
||||
};
|
||||
fputcbyte(fd, 58u8); // ':'
|
||||
let buf: [32]u8;
|
||||
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
fputcbyte(fd, 58u8);
|
||||
n = strconv.i64toa(buf[0:32], t.col: i64);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
fputcbyte(fd, 32u8); // ' '
|
||||
fputsstr(fd, tokname(t.kind));
|
||||
|
||||
if (t.kind == TK_IDENT) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_STR) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_ERR) {
|
||||
fputcbyte(fd, 32u8);
|
||||
fputq(fd, ttext.ptr, ttext.len);
|
||||
} else { if (t.kind == TK_INT) {
|
||||
fputcbyte(fd, 32u8);
|
||||
n = strconv.u64toa(buf[0:32], t.uval);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
} else { if (t.kind == TK_RUNE) {
|
||||
fputcbyte(fd, 32u8);
|
||||
n = strconv.u64toa(buf[0:32], t.uval);
|
||||
os.write(fd, buf.ptr, n: u64);
|
||||
};};};};};
|
||||
// TK_FLOAT is intentionally not handled here — %g formatting
|
||||
// won't byte-match across implementations. Diff fixtures must
|
||||
// be float-free until we implement a stable float formatter.
|
||||
|
||||
fputcbyte(fd, 10u8); // '\n'
|
||||
};
|
||||
Reference in New Issue
Block a user