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