diff options
Diffstat (limited to 'semantics.c')
-rw-r--r-- | semantics.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/semantics.c b/semantics.c index 865ae2a..9178316 100644 --- a/semantics.c +++ b/semantics.c @@ -3,24 +3,23 @@ #include "semantics.h" #define NEW(type) ((type *)malloc(sizeof(type))) -CTable_t ctable_create(Cmp_t cmp, Hashfunc_t hfunc) { +CTable_t ctable_create(Hashfunc_t hfunc) { CTable_t ct = NEW(CTable); memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE); - ct->cmp = cmp; ct->hfunc = hfunc; return ct; } -void *ctable_lookup(CTable_t ct, char *key) { +void *ctable_lookup(CTable_t ct, const char *key) { unsigned int hv = (ct->hfunc(key)) % MAX_TABLE_SIZE; CTNode *p = ct->head[hv]; for (; p; p = p->next) - if (ct->cmp(p->key, key)) + if (strcmp(p->key, key)) return p->val; return NULL; /* not found */ } -void ctable_insert(CTable_t ct, char *key, void *val, int lvl) { +void ctable_insert(CTable_t ct, const char *key, void *val, int lvl) { unsigned int hv = (ct->hfunc(key)) % MAX_TABLE_SIZE; CTNode *np = NEW(CTNode); np->key = key; @@ -44,17 +43,24 @@ CScope_t cscope_create() { CScope_t p = NEW(CScope); p->lvl = 0; p->top = NULL; - p->tvar = ctable_create(var_cmp, var_hfunc); - p->ttype = ctable_create(type_cmp, type_hfunc); + p->tvar = ctable_create(bkdr_hash); + p->ttype = ctable_create(bkdr_hash); + return p; } void cscope_push_var(CScope_t cs, CVar *var) { +#ifdef CIBIC_DEBUG + assert(cs->top); +#endif var->next = cs->top->vhead; cs->top->vhead = var; ctable_insert(cs->tvar, var->name, var, cs->lvl); } void cscope_push_type(CScope_t cs, CType *type) { +#ifdef CIBIC_DEBUG + assert(cs->top); +#endif type->next = cs->top->thead; cs->top->thead = type; ctable_insert(cs->ttype, type->name, type, cs->lvl); @@ -63,6 +69,8 @@ void cscope_push_type(CScope_t cs, CType *type) { void cscope_enter(CScope_t cs) { CSNode *np = NEW(CSNode); np->next = cs->top; + np->vhead = NULL; + np->thead = NULL; cs->top = np; cs->lvl++; } @@ -72,18 +80,26 @@ void cscope_exit(CScope_t cs) { CVar *vp; CType *tp; for (vp = cs->top->vhead; vp; vp = vp->next) - ctable_clip(cs->tvar, var_hfunc(vp->name), cs->lvl); + ctable_clip(cs->tvar, bkdr_hash(vp->name), cs->lvl); for (tp = cs->top->thead; tp; tp = tp->next) - ctable_clip(cs->ttype, type_hfunc(tp->name), cs->lvl); + ctable_clip(cs->ttype, bkdr_hash(tp->name), cs->lvl); free(cs->top); cs->top = lower; cs->lvl--; } -CVar *cscope_lookup_var(CScope_t cs, char *name) { +CVar *cscope_lookup_var(CScope_t cs, const char *name) { return ctable_lookup(cs->tvar, name); } -CType *cscope_lookup_type(CScope_t cs, char *name) { +CType *cscope_lookup_type(CScope_t cs, const char *name) { return ctable_lookup(cs->ttype, name); } + +unsigned int bkdr_hash(const char *str) { + unsigned int seed = 131; + unsigned int hv = 0; + while (*str) + hv = hv * seed + (unsigned)(*str++); + return hv; +} |