-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymtable.c
More file actions
100 lines (93 loc) · 2.47 KB
/
symtable.c
File metadata and controls
100 lines (93 loc) · 2.47 KB
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
/**
* School project to subject IFJ (Formal Languages and Compilers)
* Compiler implementation of imperative language IFJ18
*
* Module for hash table used as symbol table
*
* Author: Julius Marko
* Login: xmarko17
*/
#include "symtable.h"
#include "error.h"
#include <stdio.h>
int hash_code(t_key key) {
int retval = 1;
size_t idlen = strlen(key);
for (size_t i = 0; i < idlen; i++)
retval += key[i];
return (retval % SYMTABLE_SIZE);
}
void st_init(st *st_ptr) {
for (int i = 0; i < SYMTABLE_SIZE; i++) {
(*st_ptr)[i] = NULL;
}
}
st_elem *st_search(st *st_ptr, t_key key) {
if (st_ptr == NULL || key == NULL)
return NULL;
int index = hash_code(key);
st_elem *tmp = (*st_ptr)[index];
while (tmp) {
if (strcmp(tmp->key, key) == 0) {
return tmp;
}
tmp = tmp->ptrnext;
}
return NULL;
}
int st_insert(st *st_ptr, t_key key, st_elem_types elem_type, elem_data *data) {
if (st_ptr == NULL || key == NULL) {
return ERR_INTERNAL;
}
st_elem *tmp = st_search(st_ptr, key);
if (tmp) {
if (tmp->data != NULL) st_clear_elem_data(tmp);
tmp->data = data;
} else { // item with id doesnt exist
tmp = malloc(sizeof(struct st_elem));
if (tmp == NULL) {
return ERR_INTERNAL;
}
tmp->key = malloc(sizeof(char) * (strlen(key) + 1));
if (tmp->key == NULL) {
return ERR_INTERNAL;
}
strcpy(tmp->key, key);
tmp->elem_type = elem_type;
tmp->data = data;
tmp->ptrnext = NULL;
int index = hash_code(key);
if ((*st_ptr)[index]) { // insert before first item
tmp->ptrnext = (*st_ptr)[index];
}
(*st_ptr)[index] = tmp;
}
return 0;
}
void st_clear_elem_data(st_elem *elem) {
free(elem->data->id);
if (elem->elem_type == FUNCTION && !elem->data->is_builtin) {
for (size_t j = 0; j < elem->data->params_count; j++) {
free(elem->data->params[j]);
}
free(elem->data->params);
}
}
void st_clear_all(st *st_ptr) {
if (st_ptr == NULL) {
return;
}
st_elem *tmp, *next;
for (size_t i = 0; i < SYMTABLE_SIZE; ++i) {
next = (*st_ptr)[i];
while (next) {
tmp = next;
next = tmp->ptrnext;
free(tmp->key);
st_clear_elem_data(tmp);
free(tmp->data);
free(tmp);
}
(*st_ptr)[i] = NULL;
}
}