ag: even more refactored
[uebersetzerbau-ss10.git] / ag / symtable.c
index 41957203a5bef66afcee1438ef9a5e533c31a4b2..7242f0684ebf51eefb9ba9fc2cda816114f3e73d 100755 (executable)
@@ -28,12 +28,13 @@ struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short
        struct symbol *elm;
        struct symbol *new_elm;
 
-       if(tab_lookup(tab,ident) != SYMNULL) {
+       if(tab_lookup(tab, ident) != SYMNULL) {
                if(check) {
                        fprintf(stderr, "Feld doppelt vorhanden: \"%s\"\n", ident);
                        exit(3);
+               } else {
+                       tab = tab_remove_symbol(tab, ident);
                }
-               tab = tab_remove_symbol(tab, ident);
        }
        
        new_elm = (struct symbol *) malloc(sizeof(struct symbol));
@@ -61,17 +62,13 @@ struct symbol *tab_lookup(struct symbol *tab, char *ident)
        if(tab == SYMNULL) {
                return SYMNULL;
        }
-       
-       if(strcmp(elm->ident, ident) == 0) {
-               return elm;
-       }
-       
-       while(elm->next != SYMNULL) {
-               elm = elm->next;
+
+       do {
                if(strcmp(elm->ident, ident) == 0) {
                        return elm;
                }
-       }
+               elm = elm->next;
+       } while(elm != SYMNULL);
 
        return SYMNULL;
 }
@@ -93,23 +90,17 @@ struct symbol *tab_remove_symbol(struct symbol *tab, char *ident)
 {
        struct symbol *elm = tab;
        struct symbol *previous_elm = SYMNULL;
-       struct symbol *new_elm;
-
-       if(tab == SYMNULL) {
-               return SYMNULL;
-       }
 
        while(elm != SYMNULL) {
                if(strcmp(elm->ident, ident) == 0) {
                        if(previous_elm == SYMNULL) {
-                               new_elm = elm->next;
+                               tab = elm->next;
                        } else {
                                previous_elm->next = elm->next;
-                               new_elm = tab;
                        }
                        (void)free(elm->ident);
                        (void)free(elm);
-                       return new_elm;
+                       break;
                }
                previous_elm = elm;
                elm = elm->next;