-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.c
517 lines (450 loc) · 14.6 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file symtable.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Symbol table implementation using hash table
*/
#include "symtable.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include "token.h"
#define AVG_LEN_MAX 2.0
#define INIT_SIZE 8
size_t htab_hash_function(const char* str) {
uint32_t h = 0; // musí mít 32 bitů
const unsigned char* p;
for (p = (const unsigned char*)str; *p != '\0'; p++)
h = 65599 * h + *p;
return h;
}
void htab_resize(htab_t* t, size_t newn) {
if (t == NULL) {
return;
}
// Allocate new array
struct htab_item** new_arr = malloc(newn * sizeof(struct htab_item*));
if (new_arr == NULL) {
error_exit(ERR_INTERNAL);
}
// Initialize new array
for (size_t i = 0; i < newn; i++) {
new_arr[i] = NULL;
}
// Rehash old items into new array
for (size_t i = 0; i < t->arr_size; i++) {
struct htab_item* item = t->arr_ptr[i];
while (item != NULL) {
// Get reference to next item
struct htab_item* next = item->next;
// Reset item's next pointer
item->next = NULL;
// Find new bucket
size_t index = htab_hash_function(item->pair.key) % newn;
struct htab_item* current = new_arr[index];
// Add to head
if (current == NULL) {
new_arr[index] = item;
} else {
// Add to tail
while (current->next != NULL) {
current = current->next;
}
current->next = item;
}
// Update pointer to next item
item = next;
}
}
// Free old array
free(t->arr_ptr);
// Update array pointer and size inside table
t->arr_ptr = new_arr;
t->arr_size = newn;
}
size_t htab_bucket_count(const htab_t* t) {
if (t == NULL) {
return 0;
}
return t->arr_size;
}
void htab_clear(htab_t* t) {
if (t == NULL) {
return;
}
// Iterate over all buckets
for (size_t i = 0; i < t->arr_size; i++) {
struct htab_item* item = t->arr_ptr[i];
// Item exists
while (item != NULL) {
struct htab_item* next = item->next;
// Free the item
free((char*)item->pair.key);
if (item->pair.value.type == HTAB_FUNCTION) {
for (int j = 0; j < item->pair.value.function.param_count; j++) {
str_free(&item->pair.value.function.params[j].name);
}
free(item->pair.value.function.params);
}
free(item);
// Update pointer
item = next;
}
// Clear bucket pointer
t->arr_ptr[i] = NULL;
}
// Clear size
t->size = 0;
}
htab_pair_t* htab_find(htab_t* t, htab_key_t key) {
if (t == NULL || key == NULL) {
return NULL;
}
// Find item bucket
size_t index = htab_hash_function(key) % t->arr_size;
struct htab_item* item = t->arr_ptr[index];
// Find item
while (item != NULL) {
// Found the item
if (strcmp(item->pair.key, key) == 0) {
return &(item->pair);
}
// Update pointer
item = item->next;
}
// Item not found
return NULL;
}
void htab_free(htab_t* t) {
if (t == NULL) {
return;
}
// Clear the table
htab_clear(t);
// Free the array
free(t->arr_ptr);
// Free the table
free(t);
}
htab_t* htab_new() {
// Allocate memory for the table
htab_t* t = malloc(sizeof(htab_t));
if (t == NULL) {
error_exit(ERR_INTERNAL);
}
// Allocate memory for the array
t->arr_ptr = malloc(INIT_SIZE * sizeof(struct htab_item*));
if (t->arr_ptr == NULL) {
free(t);
error_exit(ERR_INTERNAL);
}
// Initialize everything
for (size_t i = 0; i < INIT_SIZE; i++) {
t->arr_ptr[i] = NULL;
}
t->arr_size = INIT_SIZE;
t->size = 0;
return t;
}
htab_pair_t* htab_add(htab_t* t, htab_key_t key, htab_value_t value) {
if (t == NULL || key == NULL) {
return NULL;
}
// Look if pair already exists
htab_pair_t* pair = htab_find(t, key);
// This shouldn't happen
if (pair != NULL) {
fprintf(stderr, "Error: Key already exists in the table\n");
error_exit(ERR_INTERNAL);
}
// Create new item
struct htab_item* item = malloc(sizeof(struct htab_item));
if (item == NULL) {
error_exit(ERR_INTERNAL);
}
// Initialize item
item->next = NULL;
item->pair.key = malloc(strlen(key) + 1);
if (item->pair.key == NULL) {
free(item);
error_exit(ERR_INTERNAL);
}
strcpy((char*)item->pair.key, key);
item->pair.value = value;
// Add to list
size_t index = htab_hash_function(key) % t->arr_size;
struct htab_item* target = t->arr_ptr[index];
// Add to head
if (target == NULL) {
t->arr_ptr[index] = item;
} else {
// Add to tail
while (target->next != NULL) {
target = target->next;
}
target->next = item;
}
// Update size
t->size++;
// Update hash table size
if (htab_size(t) / (double)htab_bucket_count(t) > AVG_LEN_MAX) {
htab_resize(t, htab_bucket_count(t) * 2);
}
// Return new item
return &(item->pair);
}
htab_pair_t* htab_add_function(htab_t* t, token_t* token, bool definition) {
// Check if function is already defined
htab_pair_t* pair = htab_find(t, token->attr.val_s.val);
// When we are defining new function and it already exists
if (definition && pair != NULL && pair->value.function.defined) {
error_exit(ERR_SEM_FUN);
}
// If we have partial definition (from calling), return it
if (pair != NULL) {
if (definition) {
pair->value.function.defined = true;
}
return pair;
}
// Add function to symbol table
return htab_add(
t, token->attr.val_s.val,
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 0, .param_count_guess = -1, .defined = definition}});
}
void htab_function_add_param(htab_pair_t* fun, token_t* token) {
// Increase parameter count
fun->value.function.param_count++;
// Resize parameter array
htab_param_t* params =
realloc(fun->value.function.params, fun->value.function.param_count * sizeof(htab_param_t));
if (params == NULL) {
error_exit(ERR_INTERNAL);
}
// Copy attributes from token
fun->value.function.params = params;
fun->value.function.params[fun->value.function.param_count - 1].type = token->type;
fun->value.function.params[fun->value.function.param_count - 1].required = !token->attr.val_b;
}
void htab_function_add_param_name(htab_pair_t* fun, token_t* token) {
// Check if we are not redefining parameter
for (int i = 0; i < fun->value.function.param_count - 1; i++) {
if (strcmp(fun->value.function.params[i].name.val, token->attr.val_s.val) == 0) {
error_exit(ERR_SEM_CALL);
}
}
// Copy parameter name
fun->value.function.params[fun->value.function.param_count - 1].name =
str_new_from_str(&token->attr.val_s);
}
void htab_function_add_return_type(htab_pair_t* fun, token_t* token) {
// Set return type
fun->value.function.returns.type = token->type;
fun->value.function.returns.required = !token->attr.val_b;
}
void htab_function_check_all_defined(htab_t* t) {
for (size_t i = 0; i < t->arr_size; i++) {
struct htab_item* item = t->arr_ptr[i];
// Item exists
while (item != NULL) {
if (item->pair.value.type == HTAB_FUNCTION) {
// Check if function is defined
if (!item->pair.value.function.defined) {
error_exit(ERR_SEM_FUN);
}
}
item = item->next;
}
}
}
void htab_function_check_params(htab_pair_t* fun, int param_count, bool definition) {
// Function has variable number of parameters
if (fun->value.function.param_count == -1) {
return;
}
if (definition) {
// Check if guessed parameter count is correct
if (fun->value.function.param_count_guess != -1 &&
fun->value.function.param_count_guess != fun->value.function.param_count) {
error_exit(ERR_SEM_CALL);
}
} else {
// If function is already defined check the actual count
if (fun->value.function.defined) {
if (fun->value.function.param_count != param_count) {
error_exit(ERR_SEM_CALL);
}
} else {
// If function is not defined, guess the parameter count
if (fun->value.function.param_count_guess == -1) {
fun->value.function.param_count_guess = param_count;
} else if (fun->value.function.param_count_guess != param_count) {
error_exit(ERR_SEM_CALL);
}
}
}
}
bool htab_add_variable(htab_t* t, token_t* token) {
// Check if variable is already defined
if (htab_find(t, token->attr.val_s.val) != NULL) {
// We can redefine variables
return false;
}
// Add variable to symbol table
htab_add(t, token->attr.val_s.val,
(htab_value_t){
.type = HTAB_VARIABLE,
});
return true;
}
size_t htab_size(const htab_t* t) {
if (t == NULL) {
return 0;
}
return t->size;
}
void htab_define_buildin(htab_t* t) {
htab_param_t* params;
// write
htab_add(t, "write",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = -1, .defined = true, .returns = {.type = TOK_VOID}}});
// readi
htab_add(t, "readi",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 0,
.defined = true,
.returns = {.type = TOK_INT, .required = false}},
});
// readf
htab_add(t, "readf",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 0,
.defined = true,
.returns = {.type = TOK_FLOAT, .required = false}},
});
// reads
htab_add(t, "reads",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 0,
.defined = true,
.returns = {.type = TOK_STRING, .required = false}},
});
// strlen
params = malloc(sizeof(htab_param_t));
if (params == NULL) {
error_exit(ERR_INTERNAL);
}
params[0].type = TOK_STRING;
params[0].name = str_new();
params[0].required = true;
htab_add(t, "strlen",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_INT, .required = true}},
});
// chr
params = malloc(sizeof(htab_param_t));
if (params == NULL) {
error_exit(ERR_INTERNAL);
}
params[0].type = TOK_INT;
params[0].name = str_new();
params[0].required = true;
htab_add(t, "chr",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_STRING, .required = true}},
});
// ord
params = malloc(sizeof(htab_param_t));
if (params == NULL) {
error_exit(ERR_INTERNAL);
}
params[0].type = TOK_STRING;
params[0].name = str_new();
params[0].required = true;
htab_add(t, "ord",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_INT, .required = true}},
});
// substring
params = malloc(3 * sizeof(htab_param_t));
if (params == NULL) {
error_exit(ERR_INTERNAL);
}
params[0].type = TOK_STRING;
params[0].name = str_new();
params[0].required = true;
params[1].type = TOK_INT;
params[1].name = str_new();
params[1].required = true;
params[2].type = TOK_INT;
params[2].name = str_new();
params[2].required = true;
htab_add(t, "substring",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 3,
.defined = true,
.params = params,
.returns = {.type = TOK_STRING, .required = true}},
});
// floatval
params = malloc(sizeof(htab_param_t));
params[0].required = true;
params[0].name = str_new();
htab_add(t, "floatval",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_FLOAT, .required = true}},
});
// intval
params = malloc(sizeof(htab_param_t));
params[0].required = true;
params[0].name = str_new();
htab_add(t, "intval",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_INT, .required = true}},
});
// strval
params = malloc(sizeof(htab_param_t));
params[0].required = true;
params[0].name = str_new();
htab_add(t, "strval",
(htab_value_t){
.type = HTAB_FUNCTION,
.function = {.param_count = 1,
.defined = true,
.params = params,
.returns = {.type = TOK_STRING, .required = true}},
});
}