-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.c
More file actions
129 lines (116 loc) · 3.52 KB
/
symtab.c
File metadata and controls
129 lines (116 loc) · 3.52 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
SymTable* init_hash_table(){
SymTable* symtable = calloc(sizeof(SymTable), 1);
if (!symtable) return NULL;
symtable->hash_table = calloc(SYMTABLE_SIZE, sizeof(list_t*));
symtable->cur_scope = 0;
return symtable;
}
unsigned int hash(char *key){
unsigned int hashval = 0;
for(;*key!='\0';key++) hashval += *key;
hashval += key[0] % 11 + (key[0] << 3) - key[0];
return hashval % SYMTABLE_SIZE;
}
void Insert(SymTable* symtable, Node* node, int type){
unsigned int hashval = hash(node->name);
list_t *l = symtable->hash_table[hashval];
l = (list_t*) calloc(sizeof(list_t), 1);
l->st_type = type;
l->node = node;
l->scope = symtable->cur_scope;
/* add to hashtable */
l->next = symtable->hash_table[hashval];
symtable->hash_table[hashval] = l;
}
list_t *Lookup(SymTable* symtable, char *name){
/* return symbol if found or NULL if not found */
unsigned int hashval = hash(name);
list_t *l = symtable->hash_table[hashval];
while ((l != NULL) && (strcmp(name,l->node->name) != 0)) l = l->next;
return l; // NULL is not found
}
list_t *LookupScope(SymTable* symtable, char *name){
/* return symbol if found or NULL if not found */
unsigned int hashval = hash(name);
list_t *l = symtable->hash_table[hashval];
while ((l != NULL) &&
((strcmp(name,l->node->name) != 0) ||
l->scope != symtable->cur_scope)) {
l = l->next;
}
return l; // NULL if not found
}
void DecrScope(SymTable* symtable){ /* hide the current scope */
if (symtable->cur_scope > 0) symtable->cur_scope--;
}
void IncrScope(SymTable* symtable){ /* go to next scope */
symtable->cur_scope++;
}
void ClearSymTable(SymTable* symtable) {
/* Delete scope >= this current scope */
list_t** table = symtable->hash_table;
for (int i=0; i < SYMTABLE_SIZE; ++i){
if (table[i] == NULL) continue;
list_t* head = table[i];
while (head != NULL && head->scope >= symtable->cur_scope) {
list_t* temp = head;
head = head->next;
free(temp);
}
table[i] = head;
/*
It should never be the case that once we find a lower scope, we find the current scope again.
list_t* temp = head;
list_t* prev;
while (temp != NULL) {
while (temp != NULL && temp->scope < symtable->cur_scope) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) continue;
prev->next = temp->next;
free(temp);
temp = prev->next;
}
*/
}
}
void DeleteSymTable(SymTable* symtable) {
list_t** table = symtable->hash_table;
for (int i=0; i < SYMTABLE_SIZE; ++i){
if (table[i] == NULL) continue;
list_t* head = table[i];
while (head != NULL) {
list_t* temp = head;
head = head->next;
free(temp);
}
}
free(table);
free(symtable);
}
/* print to stdout by default */
void symtab_dump(SymTable* symtable, FILE * of){
int i;
printf("------------ ----- ------ ------------\n");
printf("Name SCOPE Type Line Numbers\n");
printf("------------ ----- ------ -------------\n");
for (i=0; i < SYMTABLE_SIZE; ++i){
if (symtable->hash_table[i] != NULL){
list_t *l = symtable->hash_table[i];
while (l != NULL){
printf("%-12s ",l->node->name);
printf("%6d ", l->scope);
if (l->st_type == LONG_TYPE) printf("%-7s","long");
else if (l->st_type == FUNCTION_TYPE) printf("%-7s", "function");
else printf("%-7s","undef"); // if UNDEF or 0
printf("\n");
l = l->next;
}
}
}
}