#include #include #include #include "symtable.h" #if 0 #define DD #endif struct symbol *tab_new(void) { return SYMNULL; } struct symbol *tab_clone(struct symbol *tab) { struct symbol *elm = tab; struct symbol *ntab = tab_new(); #ifdef DD fprintf(stderr, "tab_clone: tab(%08X)\n", tab); #endif while(elm != SYMNULL) { ntab = tab_add_symbol(ntab, elm->ident, elm->type, 0, elm->param_index, elm->soffset); elm = elm->next; } return ntab; } struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check, int param_index, int soffset) { struct symbol *elm = tab; struct symbol *new_elm; #ifdef DD fprintf(stderr, "tab_add_symbol: tab(%08X), ident(%s), type(%i), check(%i), param_index(%i), soffset(%i)\n", tab, ident, type, check, param_index, soffset); #endif #if 0 /* kann statt return -3, -4 verursachen... (z.b. codea_ag_snafu_01.3) */ if(param_index >= 6) { fprintf(stderr, "eine methode hat zu viele parameter (max. 6 inkl. this erlaubt)\n"); exit(4); } #endif if(tab_lookup(tab, ident, type) != SYMNULL || (type == S_VAR && tab_lookup(tab, ident, S_PARM) != SYMNULL)) { if(check) { fprintf(stderr, "Identifier doppelt vorhanden: \"%s\"\n", ident); exit(3); } else { tab = tab_remove_symbol(tab, ident, type); } } new_elm = (struct symbol *) malloc(sizeof(struct symbol)); new_elm->next = SYMNULL; new_elm->ident = strdup(ident); new_elm->type = type; new_elm->param_index = param_index; new_elm->soffset = soffset; if(tab == SYMNULL) return new_elm; while(elm->next != SYMNULL) { elm = elm->next; } elm->next = new_elm; return tab; } struct symbol *tab_lookup(struct symbol *tab, char *ident, short type) { struct symbol *elm = tab; if(tab == SYMNULL) return SYMNULL; do { if((elm->type & type) && (strcmp(elm->ident, ident) == 0)) { return elm; } elm = elm->next; } while(elm != SYMNULL); return SYMNULL; } struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check) { struct symbol *elm = to_add; struct symbol *ntab = tab_clone(tab); #ifdef DD fprintf(stderr, "tab_merge: tab(%08X), to_add(%08X), check(%i), ntab(%08X)\n", tab, to_add, check, ntab); #endif while(elm != SYMNULL) { ntab = tab_add_symbol(ntab, elm->ident, elm->type, check, elm->param_index, elm->soffset); elm = elm->next; } return ntab; } struct symbol *tab_remove_symbol(struct symbol *tab, char *ident, short type) { struct symbol *elm = tab; struct symbol *previous_elm = SYMNULL; while(elm != SYMNULL) { if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) { if(previous_elm == SYMNULL) { tab = elm->next; } else { previous_elm->next = elm->next; } (void)free(elm->ident); (void)free(elm); break; } previous_elm = elm; elm = elm->next; } return tab; } void check(struct symbol *tab, char *ident, short type) { struct symbol *elm; #ifdef DD fprintf(stderr, "check: tab(%08X), ident(%s), type(%i), elm(%08X)\n", tab, ident, type, elm); #endif if(type & (S_VAR | S_PARM)) { elm = tab_lookup(tab, ident, type); if(elm != SYMNULL) { return; } } /* keine passende variable gefunden? * => vllt gibt es ja ein passenden feldnamen */ elm = tab_lookup(tab, ident, S_FIELD); if(elm == SYMNULL) { fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident); exit(3); } }