ag: refactor fun
[uebersetzerbau-ss10.git] / ag / symtable.c
index 41957203a5bef66afcee1438ef9a5e533c31a4b2..850c1b08388b9252a5dac9f54ee674c0f256bf11 100755 (executable)
@@ -3,37 +3,43 @@
 #include <stdio.h>
 #include "symtable.h"
 
-struct symbol *new_tab(void)
+#define DD
+
+struct symbol *tab_new(void)
 {
        return SYMNULL;
 }
 
-struct symbol *clone_tab(struct symbol *tab)
+struct symbol *tab_clone(struct symbol *tab)
 {
-       struct symbol *elm;
-       struct symbol *new_tabx;
+       struct symbol *elm = tab;
+       struct symbol *ntab = tab_new();
+#ifdef DD
+       printf("tab_clone: tab(%08X)\n", tab);
+#endif
 
-       elm = tab;
-       new_tabx = new_tab();
        while(elm != SYMNULL) {
-               new_tabx = tab_add_symbol(new_tabx, elm->ident, elm->type, 0);
+               ntab = tab_add_symbol(ntab, elm->ident, elm->type, 0);
                elm = elm->next;
        }
-
-       return new_tabx;
+       return ntab;
 }
 
 struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check)
 {
-       struct symbol *elm;
+       struct symbol *elm = tab;
        struct symbol *new_elm;
+#ifdef DD
+       printf("tab_add_symbol: tab(%08X), ident(%s), type(%i), check(%i)\n", tab, ident, type, check);
+#endif
 
-       if(tab_lookup(tab,ident) != SYMNULL) {
+       if(tab_lookup(tab, ident, type) != SYMNULL) {
                if(check) {
-                       fprintf(stderr, "Feld doppelt vorhanden: \"%s\"\n", ident);
+                       fprintf(stderr, "Identifier doppelt vorhanden: \"%s\"\n", ident);
                        exit(3);
+               } else {
+                       tab = tab_remove_symbol(tab, ident, type);
                }
-               tab = tab_remove_symbol(tab, ident);
        }
        
        new_elm = (struct symbol *) malloc(sizeof(struct symbol));
@@ -45,7 +51,6 @@ struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short
                return new_elm;
        }
 
-       elm = tab;
        while(elm->next != SYMNULL) {
                elm = elm->next;
        }
@@ -54,24 +59,20 @@ struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short
        return tab;
 }
 
-struct symbol *tab_lookup(struct symbol *tab, char *ident)
+struct symbol *tab_lookup(struct symbol *tab, char *ident, short type)
 {
        struct symbol *elm = tab;
 
        if(tab == SYMNULL) {
                return SYMNULL;
        }
-       
-       if(strcmp(elm->ident, ident) == 0) {
-               return elm;
-       }
-       
-       while(elm->next != SYMNULL) {
-               elm = elm->next;
-               if(strcmp(elm->ident, ident) == 0) {
+
+       do {
+               if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) {
                        return elm;
                }
-       }
+               elm = elm->next;
+       } while(elm != SYMNULL);
 
        return SYMNULL;
 }
@@ -79,37 +80,34 @@ struct symbol *tab_lookup(struct symbol *tab, char *ident)
 struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check)
 {
        struct symbol *elm = to_add;
-       struct symbol *new_tab = clone_tab(tab);
+       struct symbol *ntab = tab_clone(tab);
+#ifdef DD
+       printf("tab_merge: tab(%08X), to_add(%08X), check(%i), ntab(%08X)\n", tab, to_add, check, ntab);
+#endif
        
        while(elm != SYMNULL) {
-               new_tab = tab_add_symbol(new_tab, elm->ident, elm->type, check);
+               ntab = tab_add_symbol(ntab, elm->ident, elm->type, check);
                elm = elm->next;
        }
 
-       return new_tab;
+       return ntab;
 }
 
-struct symbol *tab_remove_symbol(struct symbol *tab, char *ident)
+struct symbol *tab_remove_symbol(struct symbol *tab, char *ident, short type)
 {
        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((elm->type == type) && (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;
@@ -118,29 +116,24 @@ struct symbol *tab_remove_symbol(struct symbol *tab, char *ident)
        return tab;
 }
 
-void check_variable(struct symbol *tab, char *ident)
+void check(struct symbol *tab, char *ident, short type)
 {
-       struct symbol *elm = tab_lookup(tab, ident);
-       if(elm != SYMNULL) {
-               if(elm->type != S_VAR) {
-                       fprintf(stderr, "Identifier ist keine Variable: \"%s\"\n", ident);
-                       exit(3);
+       struct symbol *elm;
+#ifdef DD
+       printf("check: tab(%08X), ident(%s), type(%i), elm(%08X)\n", tab, ident, type, elm);
+#endif
+
+       if(type == S_VAR) {
+               elm = tab_lookup(tab, ident, type);
+               if(elm != SYMNULL) {
+                       return;
                }
-       } else {
-               fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident);
-               exit(3);
        }
-}
 
-void check_field(struct symbol *tab, char *ident)
-{
-       struct symbol *elm = tab_lookup(tab, ident);
-       if(elm != SYMNULL) {
-               if(elm->type != S_FIELD) {
-                       fprintf(stderr, "Identifier ist kein Feld: \"%s\"\n", ident);
-                       exit(3);
-               }
-       } else {
+       /* 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);
        }