-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.c
335 lines (290 loc) · 7.89 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
/**
* @file symtable.c
* @author Tomáš Hobza (xhobza03@vutbr.cz)
* @brief Implementation of the table of symbols functions.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "symtable.h"
unsigned long gen_id_idx_cnt = 0;
const uint32_t FNV_PRIME = 16777619;
const uint32_t FNV_OFFSET_BASIS = 2166136261;
uint32_t hash(char *input)
{
// Check for NULL input
if (input == NULL)
{
return -1; // Return an error value for null input
}
// Calculate the length of the input
int len = strlen(input);
// Check for empty string
if (len <= 0)
{
return -1; // Return an error value for empty string
}
uint32_t hash = FNV_OFFSET_BASIS;
for (int i = 0; i < len; i++)
{
hash ^= (uint32_t)input[i];
hash *= FNV_PRIME;
}
return hash % SYMTABLE_MAX_ITEMS;
}
DEFINE_STACK_FUNCTIONS(symtable);
void symtable_stack_free_all(symtable_stack *stack)
{
while (stack->size > 0)
{
symtable_free(symtable_stack_pop(stack));
}
free(stack);
}
symtable symtable_init()
{
symtable st = malloc(sizeof(symtable *));
st->symtable = malloc(sizeof(symtable_item *) * SYMTABLE_MAX_ITEMS);
if (st == NULL)
{
return NULL;
}
for (int i = 0; i < SYMTABLE_MAX_ITEMS; i++)
{
st->symtable[i] = NULL;
}
st->all_children_return = true;
st->found_else = false;
st->found_return = false;
return st;
}
symtable_item *symtable_add(symtable_item *item, symtable table)
{
// get hash
const uint32_t item_hash = hash(item->id);
if (item_hash == (uint32_t)-1)
{
return NULL;
}
if (table->symtable[item_hash] == NULL) // there is no item with this hash
{
table->symtable[item_hash] = item; // just add it
}
else // there is an item with this hash
{
// add it to the end of the one-way list
symtable_item *last_item = table->symtable[item_hash];
while (last_item->next != NULL)
{
last_item = last_item->next;
}
last_item->next = item;
}
// for a variable, generate a globally unique index
if (item->type == VARIABLE)
{
item->data.var_data->gen_id_idx = gen_id_idx_cnt;
gen_id_idx_cnt++;
}
return item;
}
symtable_item *symtable_find(char *name, symtable table, bool is_func)
{
// get the search hash
const uint32_t item_hash = hash(name);
if (item_hash == (uint32_t)-1)
{
return NULL;
}
// go through the one-way list and find the item
symtable_item *item = table->symtable[item_hash];
while (item != NULL)
{
if (item->id == 0)
{
item = item->next;
continue;
}
if (strcmp(item->id, name) == 0 && item->type == is_func)
{
return item;
}
else if (strcmp(item->id, name) == 0 && item->type == VARIABLE)
{
return item;
}
item = item->next;
}
return NULL; // not found -> return NULL
}
symtable_item *symtable_find_in_stack(char *name, symtable_stack *stack, bool is_func)
{
int cnt = 0;
symtable_node *node = stack->top;
// for every table of symbols in the stack (from top/current to bottom/global)
while (node != NULL)
{
// find the item in the table
symtable_item *item = symtable_find(name, node->data, is_func);
if (item != NULL)
{
DEBUG_SEMANTIC_CODE(printf("Found %s in %d. symtable\n", name, cnt););
item->scope = (int)sym_st->size - cnt - 1;
return item;
}
// item not found, go to the next table in the stack
node = node->next;
cnt = cnt + 1;
}
return NULL;
}
void add_param(FunctionData *func)
{
if (func->params == NULL)
{
// First param
func->params = init_param_data(1);
if (func->params == NULL)
{
// Handle memory allocation error
return;
}
func->capacity = 1;
func->params_count = 0;
}
else if (func->params_count == func->capacity)
{
// Need to resize
int new_capacity = func->capacity * 2; // Or choose another resizing strategy
ParamData *new_params = realloc(func->params, new_capacity * sizeof(ParamData));
if (new_params == NULL)
{
// Handle memory allocation error
return;
}
func->params = new_params;
func->capacity = new_capacity;
}
func->params_count++;
}
VariableData *init_var_data()
{
VariableData *var_data = malloc(sizeof(VariableData));
if (var_data == NULL)
{
return NULL;
}
var_data->gen_id_idx = 0;
var_data->is_const = false;
var_data->is_initialized = false;
var_data->type = TYPE_EMPTY;
return var_data;
}
ParamData *init_param_data(int count)
{
ParamData *param_data = malloc(count * sizeof(ParamData));
if (param_data == NULL)
{
return NULL;
}
param_data->id = NULL;
param_data->name = NULL;
param_data->type = TYPE_EMPTY;
return param_data;
}
FunctionData *init_func_data()
{
FunctionData *func_data = malloc(sizeof(FunctionData));
if (func_data == NULL)
{
return NULL;
}
func_data->params_count = 0;
func_data->capacity = 0;
func_data->return_type = TYPE_EMPTY;
return func_data;
}
symtable_item *init_symtable_item(bool is_func)
{
symtable_item *new_sti = malloc(sizeof(symtable_item));
if (new_sti == NULL)
{
return NULL;
}
if (is_func == false) // is variable
{
new_sti->type = VARIABLE;
new_sti->data.var_data = init_var_data();
}
else if (is_func == true) // is function
{
new_sti->id = NULL;
new_sti->type = FUNCTION;
new_sti->data.func_data = init_func_data();
new_sti->data.func_data->params = NULL;
new_sti->data.func_data->params_count = 0;
new_sti->data.func_data->capacity = 0;
new_sti->data.func_data->return_type = TYPE_EMPTY;
new_sti->data.func_data->found_return = false;
}
new_sti->next = NULL;
return new_sti;
}
void symtable_print(symtable table)
{
// for each item in the table
for (int i = 0; i < SYMTABLE_MAX_ITEMS; i++)
{
// print all the items in the one-way list
printf("%d: ", i);
if (table->symtable[i] == NULL)
{
printf("NULL\n");
}
else
{
symtable_item *item = table->symtable[i];
while (item != NULL)
{
if (item->type == VARIABLE)
{
printf("id: %s, type: %d, is_const: %d, is_init: %d, is_param: %d", item->id, item->data.var_data->type, item->data.var_data->is_const, item->data.var_data->is_initialized, item->data.var_data->is_param);
}
else if (item->type == FUNCTION)
{
printf("id: %s, return_type: %d, ", item->id, item->data.func_data->return_type);
for (int i = 0; i < item->data.func_data->params_count; i++)
{
printf("param %d: %s, %s, %d, ", i, item->data.func_data->params[i].name, item->data.func_data->params[i].id, item->data.func_data->params[i].type);
}
}
item = item->next;
}
printf("\n");
}
}
}
void free_synonyms(symtable_item *item)
{
// free all chained items
if (item->next != NULL)
{
free_synonyms(item->next);
}
free(item);
}
void symtable_free(symtable table)
{
// free all items in the table
for (int i = 0; i < SYMTABLE_MAX_ITEMS; i++)
{
if (table->symtable[i] != NULL)
{
free_synonyms(table->symtable[i]); // free all chained items
}
}
free(table);
}