9c53557159fd9caf515e3167e51007db84bb9ec5
[uebersetzerbau-ss10.git] / ag / symtable.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "symtable.h"
5
6 #define DD
7
8 struct symbol *new_tab(void)
9 {
10         return SYMNULL;
11 }
12
13 struct symbol *clone_tab(struct symbol *tab)
14 {
15         struct symbol *elm;
16         struct symbol *new_tabx;
17 #ifdef DD
18         printf("clone_tab: tab(%08X)\n", tab);
19 #endif
20
21         elm = tab;
22         new_tabx = new_tab();
23         while(elm != SYMNULL) {
24                 new_tabx = tab_add_symbol(new_tabx, elm->ident, elm->type, 0);
25                 elm = elm->next;
26         }
27
28         return new_tabx;
29 }
30
31 struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check)
32 {
33         struct symbol *elm;
34         struct symbol *new_elm;
35 #ifdef DD
36         printf("tab_add_symbol: tab(%08X), ident(%s), type(%i), check(%i)\n", tab, ident, type, check);
37 #endif
38
39         if(tab_lookup(tab, ident, type) != SYMNULL) {
40                 if(check) {
41                         fprintf(stderr, "Identifier doppelt vorhanden: \"%s\"\n", ident);
42                         exit(3);
43                 } else {
44                         tab = tab_remove_symbol(tab, ident, type);
45                 }
46         }
47         
48         new_elm = (struct symbol *) malloc(sizeof(struct symbol));
49         new_elm->next = SYMNULL;
50         new_elm->ident = strdup(ident);
51         new_elm->type = type;
52
53         if(tab == SYMNULL) {
54                 return new_elm;
55         }
56
57         elm = tab;
58         while(elm->next != SYMNULL) {
59                 elm = elm->next;
60         }
61         elm->next = new_elm;
62         
63         return tab;
64 }
65
66 struct symbol *tab_lookup(struct symbol *tab, char *ident, short type)
67 {
68         struct symbol *elm = tab;
69
70         if(tab == SYMNULL) {
71                 return SYMNULL;
72         }
73
74         do {
75                 if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) {
76                         return elm;
77                 }
78                 elm = elm->next;
79         } while(elm != SYMNULL);
80
81         return SYMNULL;
82 }
83
84 struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check)
85 {
86         struct symbol *elm = to_add;
87         struct symbol *new_tab = clone_tab(tab);
88 #ifdef DD
89         printf("tab_merge: tab(%08X), to_add(%08X), check(%i), new_tab(%08X)\n", tab, to_add, check, new_tab);
90 #endif
91         
92         while(elm != SYMNULL) {
93                 new_tab = tab_add_symbol(new_tab, elm->ident, elm->type, check);
94                 elm = elm->next;
95         }
96
97         return new_tab;
98 }
99
100 struct symbol *tab_remove_symbol(struct symbol *tab, char *ident, short type)
101 {
102         struct symbol *elm = tab;
103         struct symbol *previous_elm = SYMNULL;
104
105         while(elm != SYMNULL) {
106                 if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) {
107                         if(previous_elm == SYMNULL) {
108                                 tab = elm->next;
109                         } else {
110                                 previous_elm->next = elm->next;
111                         }
112                         (void)free(elm->ident);
113                         (void)free(elm);
114                         break;
115                 }
116                 previous_elm = elm;
117                 elm = elm->next;
118         }
119
120         return tab;
121 }
122
123 void check(struct symbol *tab, char *ident, short type)
124 {
125         struct symbol *elm;
126 #ifdef DD
127         printf("check_variable: tab(%08X), ident(%s), type(%i), elm(%08X)\n", tab, ident, type, elm);
128 #endif
129
130         if(type == S_VAR) {
131                 elm = tab_lookup(tab, ident, type);
132                 if(elm != SYMNULL) {
133                         return;
134                 }
135         }
136
137         elm = tab_lookup(tab, ident, S_FIELD);
138         if(elm == SYMNULL) {
139                 fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident);
140                 exit(3);
141         }
142 }
143