-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymtable.c
395 lines (333 loc) · 10.4 KB
/
symtable.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/******************************************************************************
* IFJ21
* symtable.c
*
* Authors: Vojtěch Dvořák (xdvora3o), Tomáš Dvořák (xdvora3r)
* Purpose: Implementation of symbol table used in compiler
*
* Based on our solutions of second IAL project
*
* Last change: 7. 12. 2021
*****************************************************************************/
/**
* @file symtable.c
* @brief Implementation of symbol table used in compiler
*
* @authors Vojtěch Dvořák (xdvora3o), Tomáš Dvořák (xdvora3r)
*/
#include "symtable.h"
DSTACK(tree_node_t*, ts,) /**< Operations with tree nodes stack (used in destroy tab function) */
DSTACK(symtab_t, symtabs, fprintf(stderr," %s", s->data[i].t->key)) /**< Operations with stack of symtabs */
/**
* @brief Initializes symbol table
* @param symbol table to be initialized
*/
void init_tab(symtab_t *tab) {
tab->t = NULL;
tab->parent_ind = UNSET;
}
/**
* @brief Searches for symbol table with specific key
* @param tab symbol table in which should be searching executed
* @param key key of element that should be found
* @return Pointer to found symbol or NULL
*/
tree_node_t *search(symtab_t *tab, const char *key) {
tree_node_t * cur_node = tab->t;
while(cur_node) {
int comparison_result = str_cmp(cur_node->key, key);
if(comparison_result == 0) {
break;
}
else if(comparison_result > 0) {
cur_node = cur_node->l_ptr;
}
else {
cur_node = cur_node->r_ptr;
}
}
return cur_node;
}
/**
* @brief Inserts a new element into existing symbol table
* @param tab destination table
* @param key key of new element
*/
void insert_sym(symtab_t *tab, const char *key, sym_data_t newdata) {
tree_node_t **cur_node = &(tab->t);
bool was_inserted = false;
while(*cur_node && !was_inserted) { //Find place for new node
int comparison_result = str_cmp((*cur_node)->key, key);
if(comparison_result == 0) {
data_dtor(&(*cur_node)->data);
(*cur_node)->data = newdata;
was_inserted = true;
}
else if(comparison_result > 0) {
cur_node = &(*cur_node)->l_ptr;
}
else {
cur_node = &(*cur_node)->r_ptr;
}
}
if(!was_inserted) { //Create new node and allocate memory for it
*cur_node = (tree_node_t *)malloc(sizeof(tree_node_t));
if(*cur_node == NULL) {
return;
}
int ret = str_cpy(&(*cur_node)->key, key, strlen(key));
if(ret == STR_FAILURE) {
*cur_node = NULL;
return;
}
(*cur_node)->data = newdata;
(*cur_node)->l_ptr = NULL;
(*cur_node)->r_ptr = NULL;
}
}
/**
* @brief Replaces deleted element with two children by rightmost element
* @param tab destination table
* @param target replaced element
*/
void replace_by_rightmost(tree_node_t *target, tree_node_t **tab) {
if(*tab == NULL) {
return;
}
while((*tab)->r_ptr) {
tab = &(*tab)->r_ptr;
}
tree_node_t *temp = *tab;
target->data = (*tab)->data;
free(target->key);
target->key = (*tab)->key;
if((*tab)->l_ptr != NULL) {
*tab = (*tab)->l_ptr;
}
else {
*tab = NULL;
}
free(temp);
}
/**
* @brief Deletes element with specific key and frees all its resources
* @param tab destination table
* @param key key of element to be deleted
*/
void delete_sym(symtab_t *tab, const char *key) {
tree_node_t **cur_node = &(tab->t);
while(*cur_node) {
int comparison_result = str_cmp((*cur_node)->key, key);
if(comparison_result == 0) { //Node was found
tree_node_t *to_be_deleted = *cur_node;
if(to_be_deleted->l_ptr && to_be_deleted->r_ptr) {
replace_by_rightmost(to_be_deleted, &(to_be_deleted)->l_ptr);
}
else {
if(to_be_deleted->l_ptr) {
*cur_node = to_be_deleted->l_ptr;
}
else if(to_be_deleted->r_ptr) {
*cur_node = to_be_deleted->r_ptr;
}
else {
*cur_node = NULL;
}
data_dtor(&to_be_deleted->data);
free(to_be_deleted->key);
free(to_be_deleted);
}
}
else if(comparison_result > 0) {
cur_node = &(*cur_node)->l_ptr;
}
else {
cur_node = &(*cur_node)->r_ptr;
}
}
}
/**
* @brief Deletetes the entire symbol table and correctly frees its resources
* @param tab symbol table to be deleted
*/
void destroy_tab(symtab_t *tab) {
ts_stack_t stack;
ts_stack_init(&stack);
tree_node_t *curr_node = tab->t;
do {
if(curr_node == NULL) {
if(!ts_is_empty(&stack)) {
curr_node = ts_pop(&stack);
}
}
else {
tree_node_t *tmp = curr_node;
if(curr_node->r_ptr != NULL) {
ts_push(&stack, curr_node->r_ptr);
}
curr_node = curr_node->l_ptr;
data_dtor(&tmp->data);
free(tmp->key);
free(tmp);
}
} while(curr_node != NULL || !ts_is_empty(&stack));
ts_stack_dtor(&stack);
tab->t = NULL;
}
/**
* @brief Converts character used in operand type grammar in get_rule() to sym_dtype enum
*/
sym_dtype_t char_to_dtype(char type_c) {
sym_dtype_t type;
switch (type_c)
{
case 'n':
type = NUM;
break;
case 'i':
type = INT;
break;
case 's':
type = STR;
break;
case 'z':
type = NIL;
break;
case 'b':
type = BOOL;
break;
default:
type = UNDEFINED;
}
return type;
}
/**
* @brief Converts enum type used in symtable to correspoding character symbol
*/
char dtype_to_char(sym_dtype_t type) {
char type_c;
switch (type)
{
case NUM:
type_c = 'n';
break;
case INT:
type_c = 'i';
break;
case STR:
type_c = 's';
break;
case NIL:
type_c = 'z';
break;
case BOOL:
type_c = 'b';
break;
default:
type_c = ' ';
}
return type_c;
}
/**
* @brief Initializes strings data structure of symbol
* @return If there were error during initialization returns EXIT_FAILURE, otherwise EXIT_SUCCESS
*/
int init_data(sym_data_t *new_data) {
if(str_init(&new_data->name) != STR_SUCCESS) {
return EXIT_FAILURE;
}
if(str_init(&new_data->params) != STR_SUCCESS) {
return EXIT_FAILURE;
}
if(str_init(&new_data->ret_types) != STR_SUCCESS) {
return EXIT_FAILURE;
}
new_data->was_used = false;
return EXIT_SUCCESS;
}
/**
* @brief Frees all resources that data holds and sets it to the state before initialization
*/
void data_dtor(sym_data_t *data) {
str_dtor(&data->name);
str_dtor(&data->params);
str_dtor(&data->ret_types);
}
/**
* @brief Contains static array with builtin functions and its attributes (parameter, return types)
*/
sym_data_t* builtin_functions(unsigned int index) {
if (index >= BUILTIN_TABLE_SIZE) {
return NULL;
}
static sym_data_t builtin_functions[BUILTIN_TABLE_SIZE] = {
//Name Return types Parameters
{{0, 0, "chr"}, FUNC, {0, 0, "s"}, {0, 0, "i"}, UNSET, DEFINED, false},
{{0, 0, "ord"}, FUNC, {0, 0, "i"}, {0, 0, "si"}, UNSET, DEFINED, false},
{{0, 0, "readi"}, FUNC, {0, 0, "i"}, {0, 0, ""}, UNSET, DEFINED, false},
{{0, 0, "readn"}, FUNC, {0, 0, "n"}, {0, 0, ""}, UNSET, DEFINED, false},
{{0, 0, "reads"}, FUNC, {0, 0, "s"}, {0, 0, ""}, UNSET, DEFINED, false},
{{0, 0, "substr"}, FUNC, {0, 0, "s"}, {0, 0, "snn"}, UNSET, DEFINED, false},
{{0, 0, "tointeger"}, FUNC, {0, 0, "i"}, {0, 0, "n"}, UNSET, DEFINED, false},
{{0, 0, "write"}, FUNC, {0, 0, ""}, {0, 0, "%"}, UNSET, DEFINED, false}};
return &builtin_functions[index];
}
/**
* @brief Inserts all builtin functions into given symbol table
* @note Inseted functions will have same name as key in symbol table
*/
void load_builtin_f(symtab_t *dst) {
for(int i = 0; builtin_functions(i); i++) {
char * f_name = to_str(&builtin_functions(i)->name);
insert_sym(dst, f_name, *builtin_functions(i));
}
}
/**
* @brief Tries to find function by name in table of builtin functions
* @return Pointer to function data in static table if function is found, other wise NULL
*/
sym_data_t* search_builtin(const char *f_name) {
//If function name start with 'z' (for example) -> search from the end
int i = f_name[0] < 'z' - 'a' / 2 ? 0 : BUILTIN_TABLE_SIZE - 1;
int inc = f_name[0] < 'z' - 'a' / 2 ? 1 : -1;
for(; builtin_functions(i) != NULL; i += inc) {
if(str_cmp(to_str(&builtin_functions(i)->name), f_name) == 0) {
return builtin_functions(i);
}
}
return NULL;
}
/**
* @brief Checks if key identifies any of builtin functions, if yes puts it into given symtable
* @return True if symbol was found in builtin functions table otherwise false
*/
bool check_builtin(char *key, symtab_t *dst) {
sym_data_t *bfunc_data_ptr = search_builtin(key);
if(bfunc_data_ptr) {
insert_sym(dst, to_str(&bfunc_data_ptr->name), *bfunc_data_ptr);
return true;
}
else {
return false;
}
}
/**
* @brief Performs searching in stack of symtabs
* @return If nothing is found returns NULL otherwise returns pointer to first occurence
*/
tree_node_t * deep_search(symtabs_stack_t *sym_stack,
symtab_t *start_symtab,
char *key) {
symtab_t *curr_tab = start_symtab;
while(curr_tab != NULL) {
tree_node_t * result_of_searching = search(curr_tab, key);
if(result_of_searching) { //If something is found return pointer
return result_of_searching;
}
else { //If not, try to search it in 'parent' symbol table
curr_tab = symtabs_get_ptr(sym_stack, curr_tab->parent_ind);
}
}
return NULL;
}
/*** End of symtable.c ***/