/
symbol.c
65 lines (55 loc) · 1.73 KB
/
symbol.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
// General stuff
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symbol.h"
symbol_table_t* symbol_init() {
symbol_table_t* symbol_table = malloc(sizeof(symbol_table_t));
symbol_table->symbols = list_init();
symbol_table->parent = NULL;
return symbol_table;
}
void symbol_free(symbol_t* symbol) {
free(symbol->name);
free(symbol);
}
void symbol_table_free(symbol_table_t* symbol_table) {
printf("Freeing table: %p -> %p\n", symbol_table, symbol_table->symbols);
list_visit(symbol_table->symbols, (void(*)(void*))symbol_free);
list_free(symbol_table->symbols);
free(symbol_table);
}
symbol_table_t* symbol_create_scope(symbol_table_t* parent) {
symbol_table_t* scope = symbol_init();
scope->parent = parent;
return scope;
}
symbol_t* symbol_get(symbol_table_t* symbol_table, char* name) {
if (!symbol_table) return NULL;
symbol_t* symbol = symbol_get_in_scope(symbol_table, name);
if (symbol)
return symbol;
return symbol_get(symbol_table->parent, name);
}
symbol_t* symbol_get_in_scope(symbol_table_t* symbol_table, char* name) {
if (!symbol_table) return NULL;
list_item_t* iter = list_iter_init(symbol_table->symbols);
for (; iter; iter = list_iter(iter)) {
symbol_t* candidate = iter->val;
if (strcmp(candidate->name, name) == 0) {
return candidate;
}
}
return NULL;
}
symbol_t* symbol_set(symbol_table_t* symbol_table, char* name, type_t* type, bool is_param) {
symbol_t* new_symbol = (symbol_t*)malloc(sizeof(symbol_t));
new_symbol->name = name;
new_symbol->type = type;
new_symbol->is_param = is_param;
new_symbol->ret_type = NULL;
new_symbol->value = NULL;
new_symbol->num_params = 0;
list_push(symbol_table->symbols, new_symbol);
return new_symbol;
}