scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 1145b68f8535abf344d36cf9cd4d4f1283f42aa6
parent 99642057a0e6a038dcd4a92500d1a016b137244e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  9 Feb 2012 21:29:35 +0100

Moving hashfun to lex.c

This function will be used only in the lexical analisys so, the best
place for it is in the lex.c file. This avoid disturb the space name.

Diffstat:
lex.c | 14++++++++++++--
symbol.c | 20++++----------------
symbol.h | 2--
3 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/lex.c b/lex.c @@ -10,7 +10,6 @@ #define TOKSIZ_MAX 21 #define NR_KWD_HASH 32 -/* TODO: move hashfun here */ static struct keyword { char *str; @@ -70,6 +69,17 @@ union yyval { +static unsigned char hashfun(register const char *s) +{ + register unsigned char h, ch; + + for (h = 0; ch = *s++; h += ch) + /* nothing */; + return h; +} + + + void init_lex(void) { register struct keyword *bp; @@ -77,7 +87,7 @@ void init_lex(void) for (bp = keywords; bp->str; bp++) { register struct keyword *aux, *ant; - h = hashfun(bp->str); + h = hashfun(bp->str) % (NR_KWD_HASH - 1); if (!(aux = khash[h]) || strcmp(bp->str, aux->str) < 0) { khash[h] = bp; bp->next = aux; diff --git a/symbol.c b/symbol.c @@ -27,16 +27,6 @@ struct symhash siden, sgoto, sstruct; -unsigned char hashfun(register const char *s) -{ - register unsigned char h, ch; - - for (h = 0; ch = *s++; h += ch) - /* nothing */; - return h & NR_SYM_HASH - 1; -} - - void new_ctx(struct symctx *ctx) @@ -78,11 +68,11 @@ void del_ctx(void) -struct symbol *pushsym(struct symhash *h, struct symbol *sym) +struct symbol *pushsym(struct symhash *h, struct symbol *sym, unsigned char hash) { static unsigned char key; - key = hashfun(sym->str); + key = hash % NR_SYM_HASH; h->top = sym; sym->next = h->buf[key]; return h->buf[key] = sym; @@ -91,13 +81,11 @@ struct symbol *pushsym(struct symhash *h, struct symbol *sym) -struct symbol *findsym(struct symhash *h, char *s) +struct symbol *findsym(struct symhash *h, char *s, unsigned char hash) { register struct symbol *bp; - static unsigned char key; - key = hashfun(s); - for (bp = h->buf[key]; bp; bp = bp->next) { + for (bp = h->buf[hash % NR_SYM_HASH]; bp; bp = bp->next) { if (!strcmp(bp->str, s)) return bp; } diff --git a/symbol.h b/symbol.h @@ -16,7 +16,5 @@ struct symbol { struct symhash; extern struct symhash siden, sgoto, sstruct; -extern unsigned char hashfun(register const char *s); - #endif