-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol_table.c
55 lines (44 loc) · 1.12 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
#include <stdlib.h>
#include <stdio.h>
#include "hashmap.h"
#include "symbol_table.h"
static struct hashmap hash;
void symtab_init(void){
hm_initialize(8, 0.5, sizeof(struct symtab_item), &hash);
}
struct hm_item *symtab_insert(const char* symbol, int code){
struct symtab_item item;
item.code = code;
item.data_type = 0;
item.id_type = 0;
item.decl = NULL;
hm_put(&hash, symbol, &item);
return hm_getref(&hash, symbol);
}
int symtab_get(const char* symbol, struct symtab_item* dummy){
return hm_get(&hash, symbol, (void*) dummy);
}
void symtab_print(void){
hm_fprint(stdout, &hash, 0);
}
static const char *data_type_to_string[] = {
"", "double", "", "float", "", "long", "", "",
"", "", "", "", "", "", "", "short",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "byte",
"boolean"
};
static const char *id_type_to_string[] = {
"",
"var",
"vec",
"fun"
};
void symtab_fprint_item(FILE *stream, struct symtab_item *item) {
fprintf(stream, "data_t: %s, id_t: %s",
data_type_to_string[item->data_type],
id_type_to_string[item->id_type]);
}
void symtab_destroy(void){
hm_terminate(&hash);
}