From 73c04db73163d1d2719bb97a6b8c133065df75c3 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Mon, 18 May 2020 18:22:42 -0700 Subject: feat: macro expansion and constant evaluation prototype --- sys/cmd/cc/lex.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'sys/cmd/cc/lex.c') diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 6d3da59..90f282a 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -26,23 +26,32 @@ puttok(Token tok) // simple wrappers byte -getbyte(Lexer *l) +getbyte(Lexer *lx) { - return bufio·getbyte(&l->io->buf); + return bufio·getbyte(&lx->io->buf); } byte -getnsbyte(Lexer *l) +getnsbyte(Lexer *lx) { - byte b; - while (b = bufio·getbyte(&l->io->buf), isspace(b)); + int b; + b = getbyte(lx); + for (;;) { + if (b >= RuneSelf || !isspace(b)) + return b; + if (b == '\n') { + lx->pos.line++; + return b; + } + b = getbyte(lx); + } return b; } rune -getrune(Lexer *l) +getrune(Lexer *lx) { - return bufio·getrune(&l->io->buf); + return bufio·getrune(&lx->io->buf); } byte @@ -242,7 +251,7 @@ GetByte: Dispatch: tok.pos.beg = lx->pos; - if (b >= RuneSelf || isalpha(b)) + if (b >= RuneSelf || isalpha(b) || b == '_') goto TAlpha; if (isdigit(b)) goto TNum; @@ -434,6 +443,7 @@ Dispatch: r = b; n = 10; s = lx->buf; + ungetbyte(lx); if (*s == '0') { b = *++s; switch (b) { @@ -510,6 +520,7 @@ Dispatch: } *lx->b = '\0'; d = strtod(lx->buf, nil); + ungetbyte(lx); tok.kind = Alit | Vfloat; tok.val.f = d; @@ -539,6 +550,8 @@ Dispatch: u = getbyte(lx); } *s = '\0'; + ungetbyte(lx); + tok.kind = Aident; tok.val.s = lx->buf; @@ -547,7 +560,7 @@ Dispatch: tok.kind = Akeywd; } - sym = lookup(lx->sym, tok.val.s); + sym = lookup(&lx->sym, tok.val.s); if (sym) { io = makeio(); io->buf.end += expandmacro(lx, sym, io->b); @@ -606,11 +619,6 @@ popio(Lexer *lx) #define PTR_HASH(p) (uintptr)(p) #define PTR_EQUAL(p1, p2) ((uintptr)(p1) == (uintptr)(p2)) -struct SymTab -{ - MAP_STRUCT_BODY(string, Sym*); -}; - Sym* lookup(SymTab *tab, string ident) { @@ -640,14 +648,16 @@ putsym(SymTab *tab, Sym *sym, error *err) Sym* define(SymTab *tab, string name, int kind) { - Sym *sym; + int i; + Sym *sym; error err; sym = mem·arenaalloc(C.heap, 1, sizeof(*sym)); sym->name = name; sym->kind = kind; - putsym(tab, sym, &err); + i = putsym(tab, sym, &err); + tab->vals[i] = sym; return sym; } -- cgit v1.2.1