#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "semantics.h"
#define NEW(type) ((type *)malloc(sizeof(type)))
#ifdef CIBIC_DEBUG
CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc) {
CTable_t ct = NEW(CTable);
memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
ct->hfunc = hfunc;
ct->pfunc = pfunc;
return ct;
}
#else
CTable_t ctable_create(Hashfunc_t hfunc) {
CTable_t ct = NEW(CTable);
memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
ct->hfunc = hfunc;
return ct;
}
#endif
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 (!strcmp(p->key, key))
return p->val;
return NULL; /* not found */
}
int ctable_insert(CTable_t ct, const char *key, void *val, int lvl) {
unsigned int hv = ct->hfunc(key) % MAX_TABLE_SIZE;
CTNode *p = ct->head[hv];
CTNode *np;
for (; p && p->lvl == lvl; p = p->next)
if (!strcmp(p->key, key))
return 0; /* conflict */
np = NEW(CTNode);
np->key = key;
np->val = val;
np->lvl = lvl;
np->next = ct->head[hv];
ct->head[hv] = np;
return 1;
}
void ctable_clip(CTable_t ct, const char *key, int max_lvl) {
unsigned int hv = ct->hfunc(key) % MAX_TABLE_SIZE;
CTNode *p = ct->head[hv], *np;
for (; p && p->lvl > max_lvl; p = np)
{
np = p->next;
free(p);
}
ct->head[hv] = p;
}
CScope_t cscope_create() {
CScope_t p = NEW(CScope);
p->lvl = -1;
p->top = NULL;
#ifdef CIBIC_DEBUG
p->tvar = ctable_create(bkdr_hash, cvar_print);
p->ttype = ctable_create(bkdr_hash, ctype_print);
#else
p->tvar = ctable_create(bkdr_hash);
p->ttype = ctable_create(bkdr_hash);
#endif
cscope_enter(p);
return p;
}
int cscope_push_var(CScope_t cs, CVar *var) {
#ifdef CIBIC_DEBUG
assert(cs->top);
#endif
if (ctable_insert(cs->tvar, var->name, var, cs->lvl))
{
var->next = cs->top->vhead;
cs->top->vhead = var;
return 1;
}
else return 0; /* naming conflict */
}
int cscope_push_type(CScope_t cs, CType *type) {
#ifdef CIBIC_DEBUG
assert(cs->top);
#endif
if (ctable_insert(cs->ttype, type->name, type, cs->lvl))
{
type->next = cs->top->thead;
cs->top->thead = type;
return 1;
}
else return 0; /* naming conflict */
}
void cscope_enter(CScope_t cs) {
CSNode *np = NEW(CSNode);
np->next = cs->top;
np->vhead = NULL;