-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol_table.c
executable file
·235 lines (191 loc) · 4.75 KB
/
symbol_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "symbol_table.h"
// adds the default functions to the global table
void insert_default_functions(table *to_insert) {
symbol *put_char = insert_element(to_insert, "putchar", "Int", NULL);
insert_type("Int", put_char);
symbol *get_char = insert_element(to_insert, "getchar", "Int", NULL);
insert_type("Void", get_char);
}
table *create_table(char *name, int is_func_definition) {
// creates the table and respective elements
table *new_table = (table *)malloc(sizeof(table));
new_table->print = 0;
new_table->name = strdup(name);
new_table->symbol = NULL;
new_table->is_func_definition = is_func_definition;
new_table->next = NULL;
// go to end of tables list
table *aux = tables;
if(tables != NULL) {
while(aux->next != NULL) {
// check if table already exists
if(strcmp(name, aux->name) == 0) {
return NULL;
}
aux = aux->next;
}
// add to end of list
aux->next = new_table;
} else {
tables = new_table;
}
return new_table;
}
// search table by its name
table *get_table(char *name) {
table *search_aux = tables;
while(search_aux != NULL) {
if(strcmp(name, search_aux->name) == 0) {
return search_aux;
}
search_aux = search_aux->next;
}
return NULL;
}
// insert new symbol at table end
symbol *insert_element(table *to_insert, char *name, char *type, param_type *params_types) {
// create the symbol
symbol *new_symbol=(symbol *) malloc(sizeof(symbol));
new_symbol->has_error = 0;
new_symbol->to_print = 1;
new_symbol->is_param = 0;
new_symbol->name = NULL;
if (name != NULL) {
new_symbol->name = strdup(name);
}
new_symbol->type = strdup(type);
new_symbol->param = params_types;
new_symbol->next = NULL;
// create pointer to the first symbol in table
symbol *aux = to_insert->symbol;
// iterate symbols of table
if (aux != NULL) {
while(aux->next != NULL) {
aux = aux->next;
}
aux->next = new_symbol;
} else {
to_insert->symbol = new_symbol;
}
return new_symbol;
}
//return table symbol with the same name
symbol *get_element(table *table, char *name) {
symbol *aux = table->symbol;
while(aux != NULL) {
if (strcmp(name, aux->name) == 0) {
return aux;
}
aux = aux->next;
}
return NULL;
}
void insert_type(char *name, symbol *to_insert_type) {
param_type *type_aux = to_insert_type->param;
param_type *type = (param_type *) malloc(sizeof(param_type));
type->name = strdup(name);
type->next = NULL;
if (type_aux != NULL) {
while(type_aux->next != NULL) {
type_aux = type_aux->next;
}
type_aux->next = type;
} else {
to_insert_type->param = type;
}
}
// aux function to show function param types
void show_func_param_types(param_type *param) {
param_type *aux = param;
aux->name[0] = tolower(aux->name[0]);
printf("%s", aux->name);
aux = aux->next;
while(aux != NULL) {
printf(",");
aux->name[0] = tolower(aux->name[0]);
printf("%s", aux->name);
aux = aux->next;
}
}
void show_symbol(symbol *symbol) {
symbol->type[0] = tolower(symbol->type[0]);
printf("%s\t%s", symbol->name, symbol->type);
if (symbol->is_param == 1) {
printf("\tparam\n");
}
else if (symbol->param != NULL) {
printf("(");
show_func_param_types(symbol->param);
printf(")\n");
} else {
printf("\n");
}
}
void show_table(table *table) {
symbol *aux = table->symbol;
// check if it's global function or not
if(strcmp("Global", table->name) != 0) {
printf("===== Function %s Symbol Table =====\n", table->name);
} else {
printf("===== %s Symbol Table =====\n", table->name);
}
while(aux != NULL) {
if (aux->to_print == 1) {
show_symbol(aux);
}
aux = aux->next;
}
}
void show_tables() {
table *aux = tables;
while(aux != NULL) {
if(aux->print) {
show_table(aux);
printf("\n");
}
aux = aux->next;
}
}
void destroy_param_types(param_type *current) {
if (current == NULL) {
return;
}
if (current->name != NULL) {
free(current->name);
}
destroy_param_types(current->next);
free(current);
}
void destroy_symbols(symbol *current) {
if (current == NULL) {
return;
}
if (current->name != NULL) {
free(current->name);
}
if (current->type != NULL) {
free(current->type);
}
if (current->param != NULL) {
destroy_param_types(current->param);
}
destroy_symbols(current->next);
free(current);
}
void destroy_tables(table *current) {
if (current == NULL) {
return;
}
if (current->name != NULL) {
free(current->name);
}
if(current->symbol != NULL) {
destroy_symbols(current->symbol);
}
destroy_tables(current->next);
free(current);
}