scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 2630b2abf0a6fb9c78352f7bea6d19f4bcfaa57c
parent 8b6cc849d8e198cfa639b4de3ad84a22f2d36e07
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 Jun 2012 20:15:08 +0200

Install symbols for integer constant

This patch install a symbol for each constant that there is in the
code. This symbol will be used later in the parser like attribute of the
node into the parser tree.

Diffstat:
Mlex.c | 4++++
Msymbol.c | 8++++----
Msymbol.h | 2+-
3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/lex.c b/lex.c @@ -24,6 +24,7 @@ static char number(void) { register char *bp; register char ch; + register struct symbol *sym; for (bp = yytext; bp < yytext + TOKSIZ_MAX; *bp++ = ch) { if (!isdigit(ch = getc(yyin))) @@ -33,6 +34,9 @@ static char number(void) error("identifier too long %s", yytext); *bp = '\0'; ungetc(ch, yyin); + yyval.sym = sym = install(NULL, 0); + sym->val = atoi(yytext); + sym->type = T_INT; return CONSTANT; } diff --git a/symbol.c b/symbol.c @@ -58,8 +58,8 @@ struct symbol *install(const char *s, unsigned char key) if (s) { sym->str = xstrdup(s); - - head = &iden_hash.buf[key], next = head->h_next; + head = &iden_hash.buf[key & NR_SYM_HASH-1]; + next = head->h_next; sym->h_next = next; sym->h_prev = next->h_prev; head->h_next = sym; @@ -75,8 +75,8 @@ struct symbol *lookup(char *s, unsigned char key) { register struct symbol *bp, *head; - head = &iden_hash.buf[key]; - for (bp = head->h_next; bp != head; bp = bp->next) { + head = &iden_hash.buf[key & NR_SYM_HASH-1]; + for (bp = head->h_next; bp != head; bp = bp->h_next) { if (!strcmp(bp->str, s)) return bp; } diff --git a/symbol.h b/symbol.h @@ -13,6 +13,7 @@ struct symbol { unsigned char level; }; unsigned char tok; /* used in keywords */ + short val; }; struct symbol *next; struct symbol *h_next, *h_prev; @@ -23,7 +24,6 @@ struct symctx { struct symctx *next; }; - extern void new_ctx(struct symctx *ctx); extern void del_ctx(void); extern struct symbol *install(const char *s, unsigned char key);