/
symbols.c
68 lines (53 loc) · 1.58 KB
/
symbols.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
#include "symbols.h"
/* Create new symbol... */
static
ASymbol *create_symbol(const char *name) {
ASymbol *newsym = malloc(sizeof(ASymbol));
newsym->name = malloc(strlen(name)+1);
strcpy(newsym->name, name);
return newsym;
}
/* Looks up a symbol in the given symbol table, or adds it
* if it doesn't already exist. */
ASymbol *get_symbol(ASymbolTable *t, const char *name) {
ASymbolMapping *m = NULL;
HASH_FIND_STR( *t, name, m );
if (m != NULL) {
return m->sym;
} else {
ASymbol *newsym = create_symbol(name);
ASymbolMapping *mapping = malloc(sizeof(ASymbolMapping));
mapping->name = malloc(strlen(name)+1);
strcpy(mapping->name, name);
mapping->sym = newsym;
HASH_ADD_KEYPTR( hh, *t, mapping->name, strlen(mapping->name), mapping );
return mapping->sym;
}
}
/* Print a symbol. */
void print_symbol(ASymbol *s) {
printf("%s", s->name);
}
/* Print a symbol. */
void fprint_symbol(FILE *out, ASymbol *s) {
fprintf(out, "%s", s->name);
}
/* Free a symbol. (Should only be called at the end.) */
void free_symbol(ASymbol *to_free) {
free(to_free->name);
free(to_free);
}
/* Free a symbol mapping. (Likewise!) */
void free_symbol_mapping(ASymbolMapping *to_free) {
free(to_free->name);
free_symbol(to_free->sym);
free(to_free);
}
/* Free the symbol table at the end of the program. */
void free_symbol_table(ASymbolTable *t) {
ASymbolMapping *current, *tmp;
HASH_ITER(hh, *t, current, tmp) {
HASH_DEL(*t, current);
free_symbol_mapping(current);
}
}