From 5f2965bcaca608f0d9af84373565539ae384918d Mon Sep 17 00:00:00 2001 From: Teddy Date: Wed, 26 Mar 2014 19:22:20 +0800 Subject: semantics: symbol table now works --- semantics.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) (limited to 'semantics.c') diff --git a/semantics.c b/semantics.c index 9178316..f491b2c 100644 --- a/semantics.c +++ b/semantics.c @@ -1,14 +1,26 @@ #include +#include #include +#include #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; @@ -41,10 +53,16 @@ void ctable_clip(CTable_t ct, unsigned int hv, int max_lvl) { CScope_t cscope_create() { CScope_t p = NEW(CScope); - p->lvl = 0; + 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; } @@ -88,6 +106,45 @@ void cscope_exit(CScope_t cs) { cs->lvl--; } +void ctable_debug_print(CTable_t ct) { + int i; + fprintf(stderr, "*** CTable ***\n"); + for (i = 0; i < MAX_TABLE_SIZE; i++) + if (ct->head[i]) + { + CTNode *p; + fprintf(stderr, "[%04d]", i); + for (p = ct->head[i]; p; p = p->next) + fprintf(stderr, "->[%s:%d]", ct->pfunc(p), p->lvl); + fprintf(stderr, "\n"); + } + fprintf(stderr, "*** CTable ***\n"); +} + +void cscope_debug_print(CScope_t cs) { + int lvl = cs->lvl; + CSNode *p; + fprintf(stderr, "*** CScope ***\n"); + for (p = cs->top; p; p = p->next) + { + CVar *vp; + CType *tp; + fprintf(stderr, "Level %d:\n", lvl--); + fprintf(stderr, "Vars: "); + for (vp = p->vhead; vp; vp = vp->next) + fprintf(stderr, "%s ", vp->name); + fprintf(stderr, "\nTypes: "); + for (tp = p->thead; tp; tp = tp->next) + fprintf(stderr, "%s ", tp->name); + fprintf(stderr, "\n\n"); + } + fprintf(stderr, "Var Table:\n"); + ctable_debug_print(cs->tvar); + fprintf(stderr, "Type Table:\n"); + ctable_debug_print(cs->ttype); + fprintf(stderr, "*** CScope ***\n"); +} + CVar *cscope_lookup_var(CScope_t cs, const char *name) { return ctable_lookup(cs->tvar, name); } @@ -101,5 +158,31 @@ unsigned int bkdr_hash(const char *str) { unsigned int hv = 0; while (*str) hv = hv * seed + (unsigned)(*str++); - return hv; + return hv % 5; +} + +const char *cvar_print(void *var) { + static char buff[MAX_DEBUG_PRINT_BUFF]; + sprintf(buff, "%s", ((CVar *)var)->name); + return buff; +} + +const char *ctype_print(void *type) { + static char buff[MAX_DEBUG_PRINT_BUFF]; + sprintf(buff, "%s", ((CType *)type)->name); + return buff; +} + +CVar_t cvar_create(const char *name, CType *type) { + CVar *cv = NEW(CVar); + cv->name = name; + cv->type = type; + return cv; +} + +CType_t ctype_create(const char *name, int type) { + CType *ct = NEW(CType); + ct->name = name; + ct->type = type; + return ct; } -- cgit v1.2.3