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