From 7ea7654db6e827038e38c3589c8d5b314db3fcb2 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Mon, 18 May 2020 20:28:51 -0700 Subject: fix: line accounting is less buggy --- sys/cmd/cc/lex.c | 70 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 23 deletions(-) (limited to 'sys/cmd/cc/lex.c') diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 90f282a..20c6f9c 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -39,10 +39,8 @@ getnsbyte(Lexer *lx) for (;;) { if (b >= RuneSelf || !isspace(b)) return b; - if (b == '\n') { - lx->pos.line++; + if (b == '\n') return b; - } b = getbyte(lx); } return b; @@ -257,9 +255,12 @@ Dispatch: goto TNum; switch (b) { - case ' ': case '\n': case '\r': case '\t': case '\v': case '\f': + case '\n': + lx->pos.line++; + case ' ': case '\r': case '\t': case '\v': case '\f': while (b = getbyte(lx), isspace(b)) - if (b == '\n') lx->pos.line++; + if (b == '\n') + lx->pos.line++; goto Dispatch; case '\'': @@ -363,7 +364,6 @@ Dispatch: else if (b == '/') { while (b != EOF && b != '\n') b = getbyte(lx); - lx->pos.line++; goto Dispatch; } else if (b == '*') { int level = 1; @@ -378,7 +378,8 @@ Dispatch: if (b == '/') level--; } - if (b == '\n') lx->pos.line++; + if (b == '\n') + lx->pos.line++; b = getbyte(lx); } goto Dispatch; @@ -396,7 +397,11 @@ Dispatch: break; case EOF: - panicf("need to implement popio"); + popio(lx); + if (lx->io) + goto GetByte; + tok.kind = Aeof; + return tok; CASE1('(', Alparen) CASE1(')', Arparen) @@ -593,8 +598,8 @@ pushio(Lexer *lx, Io *new) lx->io = new; lx->pos = (Pos){ - .line = 0, - .col = 0, + .line = 1, + .col = 1, .path = new->path, }; } @@ -608,6 +613,11 @@ popio(Lexer *lx) if (!prev) { panicf("no buffer left"); } + /* + * NOTE: should we copy over unget bytes here? + * would modify the buffer... + */ + freeio(lx->io); lx->pos = prev->store; lx->io = prev; @@ -619,18 +629,6 @@ popio(Lexer *lx) #define PTR_HASH(p) (uintptr)(p) #define PTR_EQUAL(p1, p2) ((uintptr)(p1) == (uintptr)(p2)) -Sym* -lookup(SymTab *tab, string ident) -{ - int idx; - MAP_GET(idx, tab, ident, PTR_HASH, PTR_EQUAL); - - if (idx < tab->n_buckets) - return tab->vals[idx]; - - return nil; -} - static int moresymtab(SymTab *tab, int n) @@ -662,6 +660,32 @@ define(SymTab *tab, string name, int kind) return sym; } +Sym* +lookup(SymTab *tab, string ident) +{ + int idx; + MAP_GET(idx, tab, ident, PTR_HASH, PTR_EQUAL); + + if (idx < tab->n_buckets) + return tab->vals[idx]; + + return nil; +} + + +error +forget(SymTab *tab, string ident) +{ + int idx; + MAP_GET(idx, tab, ident, PTR_HASH, PTR_EQUAL); + + if (idx < tab->n_buckets) { + MAP_DEL(tab, idx); + return 0; + } + return 1; +} + // ----------------------------------------------------------------------- // error reporting @@ -671,7 +695,7 @@ errorat(Pos x, byte *fmt, ...) va_list args; va_start(args, fmt); - printf("error %d: ", x.line); + printf("error:%s:%d:%d: ", osĀ·basename(x.path), x.line, x.col); vprintf(fmt, args); printf("\n"); -- cgit v1.2.1