-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolTable.c
200 lines (178 loc) · 6.53 KB
/
symbolTable.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "symbolTable.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// This file is for reference only, you are not required to follow the implementation. //
int HASH(char * str) {
int idx=0;
while (*str){
idx = idx << 1;
idx+=*str;
str++;
}
return (idx & (HASH_TABLE_SIZE-1));
}
SymbolTable symbolTable;
SymbolTableEntry* newSymbolTableEntry(char* symbolName, SymbolAttribute* attribute, int nestingLevel)
{
SymbolTableEntry* symbolTableEntry = (SymbolTableEntry*)malloc(sizeof(SymbolTableEntry));
symbolTableEntry->nextInHashChain = NULL;
symbolTableEntry->prevInHashChain = NULL;
symbolTableEntry->nextInSameLevel = NULL;
symbolTableEntry->sameNameInOuterLevel = NULL;
symbolTableEntry->attribute = attribute; // not sure
symbolTableEntry->name = symbolName; // not sure
symbolTableEntry->nestingLevel = nestingLevel;
return symbolTableEntry;
}
void removeFromHashChain(SymbolTableEntry* entry) // delete
{
if(entry->prevInHashChain != NULL) {
entry->prevInHashChain->nextInHashChain = entry->nextInHashChain;
}
else {
int hashVal = HASH(entry->name);
symbolTable.hashTable[hashVal] = entry->nextInHashChain;
}
if(entry->nextInHashChain != NULL) {
entry->nextInHashChain->prevInHashChain = entry->prevInHashChain;
}
entry->prevInHashChain = NULL;
entry->nextInHashChain = NULL;
return;
}
void enterIntoHashChain(SymbolTableEntry* entry) // add
{
int hashVal = HASH(entry->name);
SymbolTableEntry *head = symbolTable.hashTable[hashVal];
entry->nextInHashChain = head;
symbolTable.hashTable[hashVal] = entry;
if(head != NULL) {
head->prevInHashChain = entry;
}
return;
}
void initializeSymbolTable()
{
for(int i = 0; i < HASH_TABLE_SIZE; ++i) {
symbolTable.hashTable[i] = NULL;
}
for(int i = 0; i < MAX_SCOPE_SIZE; ++i) {
symbolTable.scopeDisplay[i] = NULL;
}
symbolTable.currentLevel = 0;
symbolTable.scopeDisplayElementCount = 0;
// int
SymbolAttribute *attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
attribute->attributeKind = TYPE_ATTRIBUTE;
attribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
attribute->attr.typeDescriptor->properties.dataType = INT_TYPE;
enterSymbol(SYMBOL_TABLE_INT_NAME, attribute);
// float
attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
attribute->attributeKind = TYPE_ATTRIBUTE;
attribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
attribute->attr.typeDescriptor->properties.dataType = FLOAT_TYPE;
enterSymbol(SYMBOL_TABLE_FLOAT_NAME, attribute);
// void
attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
attribute->attributeKind = TYPE_ATTRIBUTE;
attribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
attribute->attr.typeDescriptor->properties.dataType = VOID_TYPE;
enterSymbol(SYMBOL_TABLE_VOID_NAME, attribute);
// read
attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
attribute->attributeKind = FUNCTION_SIGNATURE;
attribute->attr.functionSignature->parametersCount = 0;
attribute->attr.functionSignature->parameterList = NULL;
attribute->attr.functionSignature->returnType = INT_TYPE;
enterSymbol(SYMBOL_TABLE_SYS_LIB_READ, attribute);
// fread
attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
attribute->attributeKind = FUNCTION_SIGNATURE;
attribute->attr.functionSignature->parametersCount = 0;
attribute->attr.functionSignature->parameterList = NULL;
attribute->attr.functionSignature->returnType = FLOAT_TYPE;
enterSymbol(SYMBOL_TABLE_SYS_LIB_FREAD, attribute);
// write
attribute = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
attribute->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
attribute->attributeKind = FUNCTION_SIGNATURE;
attribute->attr.functionSignature->parametersCount = 1;
attribute->attr.functionSignature->parameterList = NULL;
attribute->attr.functionSignature->returnType = INT_TYPE;
enterSymbol(SYMBOL_TABLE_SYS_LIB_WRITE, attribute);
return;
}
void symbolTableEnd()
{
// ...
}
SymbolTableEntry* retrieveSymbol(char* symbolName)
{
SymbolTableEntry *sym = symbolTable.hashTable[HASH(symbolName)];
while(sym != NULL) {
if(strcmp(sym->name, symbolName) == 0) {
return sym;
}
sym = sym->nextInHashChain;
}
return NULL;
}
SymbolTableEntry* enterSymbol(char* symbolName, SymbolAttribute* attribute)
{
SymbolTableEntry *oldSym = retrieveSymbol(symbolName);
if(oldSym != NULL && oldSym->nestingLevel == symbolTable.currentLevel) {
return NULL; // duplicate definition
}
SymbolTableEntry *newSym = newSymbolTableEntry(symbolName, attribute, symbolTable.currentLevel);
// add to scope display
newSym->nextInSameLevel = symbolTable.scopeDisplay[symbolTable.currentLevel];
newSym->nestingLevel = symbolTable.currentLevel;
symbolTable.scopeDisplay[symbolTable.currentLevel] = newSym;
// add to hash table
if(oldSym != NULL){
removeFromHashChain(oldSym);
}
enterIntoHashChain(newSym);
newSym->sameNameInOuterLevel = oldSym;
return newSym;
}
//remove the symbol from the current scope
void removeSymbol(char* symbolName)
{
// NONE
}
SymbolTableEntry* declaredLocally(char* symbolName)
{
SymbolTableEntry *oldSym = retrieveSymbol(symbolName);
if(oldSym != NULL && oldSym->nestingLevel == symbolTable.currentLevel) {
return oldSym;
}
return NULL;
}
void openScope()
{
++(symbolTable.currentLevel);
return;
}
void closeScope()
{
SymbolTableEntry *curSym = symbolTable.scopeDisplay[symbolTable.currentLevel];
while(curSym != NULL) {
SymbolTableEntry *prevSym = curSym->sameNameInOuterLevel;
SymbolTableEntry *oldSym = curSym;
curSym = curSym->nextInSameLevel;
removeFromHashChain(oldSym);
if(prevSym != NULL) {
enterIntoHashChain(prevSym);
}
}
--(symbolTable.currentLevel);
return;
}