-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolTable.c
204 lines (191 loc) · 6.48 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
201
202
203
204
#include "symbolDef.h"
#include "symbolTable.h"
#define INT_WIDTH 2
#define REAL_WIDTH 4
funcEntry ftable[F_TABLE_SIZE];
int hashFunc(char* key)
{
unsigned long hash = 5381;
int c;
while (c = *key++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return (int)(hash % F_TABLE_SIZE);
}
int hashInd(char* key)
{
unsigned long hash = 5381;
int c;
while (c = *key++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return (int)(hash % IND_TABLE_SIZE);
}
IndTable initializeIndTable(){
IndTable indtable = (IndTable) malloc(sizeof(indEntry) * IND_TABLE_SIZE);
for(int i = 0; i < IND_TABLE_SIZE; i++){
indtable[i].isvalid = 0;
indtable[i].width = 0;
indtable[i].offset = 0;
indtable[i].next = NULL;
//indtable[i].tlist = NULL;
}
return indtable;
}
RecTable initializeRecTable(){
RecTable rectable = (RecTable) malloc(sizeof(indEntry) * REC_TABLE_SIZE);
for(int i = 0; i < REC_TABLE_SIZE; i++){
rectable[i].isvalid = 0;
rectable[i].width = 0;
rectable[i].tlist=NULL;
rectable[i].next = NULL;
}
return rectable;
}
void initializeFuncTable(){
//FuncTable ftable = (FuncTable) malloc(sizeof(funcEntry) * F_TABLE_SIZE);
for(int i = 0; i < F_TABLE_SIZE; i++){
ftable[i].isvalid = 0;
ftable[i].input_par = NULL;
ftable[i].output_par = NULL;
ftable[i].next = NULL;
ftable[i].idtable = NULL;
}
}
void addVariable(char *funcName, AstNode varNode, int offs, int wid)
{
int findex = hashFunc(funcName);
funcEntry* temp = ftable + findex;
/* while(strcmp(funcName, temp->name) != 0){
temp = temp->next;
}
if(temp->idtable==NULL)
{
temp->idtable = initializeIndTable();
} */
int findvar = hashInd(varNode->value);
indEntry *tempind = temp->idtable + findvar;
if(tempind->isvalid==0)
{
tempind->isvalid = 1;
tempind->vtype = varNode->tlist->vtype;
strncpy(tempind->name, varNode->value, MAX_LEXEME_SIZE+1);
tempind->width = wid;
tempind->offset = offs;
}
else
{
while(tempind->next!=NULL && strcmp(tempind->name,varNode->value)!=0)
{
tempind = tempind->next;
}
if(tempind->next!=NULL)
{
printf("Line %d: Redeclaration of variable %s\n",varNode->line_number, varNode->value);
//errflag=1
}
else
{
tempind->next = (struct indEntry*)malloc(sizeof(indEntry));
tempind->next->isvalid=1;
tempind->next->vtype = varNode->tlist->vtype;
strncpy(tempind->next->name, varNode->value, MAX_LEXEME_SIZE+1);
tempind->next->width = wid;
tempind->next->offset = offs;
}
}
return;
}
void addFunction(AstNode funcNode)
{
int index = hash(funcNode -> value);
int offset = 0, wid;
AstNode fieldnode;
if(ftable[index].isvalid == 0){
strncpy(ftable[index].name, funcNode->value, sizeof(funcNode->value));
ftable[index].idtable = initializeIndTable();
//funid - inputpar - output par - stmts - typedefinitions - declarations - individual declaration
AstNode tnode = funcNode->child->right->right->child->right->child;
//IndTable tempInd = ftable[index].idtable;
while(tnode!=NULL){
switch(tnode->tlist->vtype){
case intType:
addVariable(funcNode->value, tnode, offset, INT_WIDTH);
offset += INT_WIDTH;
break;
case realType:
addVariable(funcNode->value, tnode, offset, REAL_WIDTH);
offset += REAL_WIDTH;
break;
case recordType:
fieldnode = tnode -> child;
wid = 0;
while(fieldnode!=NULL)
{
if(fieldnode->tlist->vtype == intType)
{
wid+=INT_WIDTH;
}
else
{
wid+=REAL_WIDTH;
}
}
addVariable(funcNode->value, tnode, offset, wid);
offset += wid;
break;
}
tnode = tnode->right;
}
ftable[index].input_par = funcNode->child->right;
ftable[index].output_par = funcNode->child->right->right;
ftable[index].next = NULL;
ftable[index].isvalid = 1;
}
else{
FuncTable temp = (struct funcEntry*) &ftable[index];
while(temp->next != NULL){
if(strcmp(funcNode->child->value, temp->name) == 0){
printf("Line %d: Redeclaration of variable %s\n", funcNode->child->line_number, funcNode->child->value);
}
temp = temp->next;
}
struct funcEntry f;
strncpy(f.name, funcNode -> value, sizeof(funcNode -> value));
f.isvalid = 1;
f.idtable = initializeIndTable();
AstNode tnode = funcNode->child->right->right->child->right->child;
// IndTable tempInd = ftable[index].idtable;
while(tnode!=NULL){
switch(tnode->tlist->vtype){
case intType:
addVariable(funcNode->value, tnode, offset, INT_WIDTH);
offset += INT_WIDTH;
break;
case realType:
addVariable(funcNode->value, tnode, offset, REAL_WIDTH);
offset += REAL_WIDTH;
break;
case recordType:
fieldnode = tnode -> child;
wid = 0;
while(fieldnode!=NULL)
{
if(fieldnode->tlist->vtype == intType)
{
wid+=INT_WIDTH;
}
else
{
wid+=REAL_WIDTH;
}
}
addVariable(funcNode->value, tnode, offset, wid);
offset += wid;
break;
}
}
f.input_par = funcNode->child->right;
f.output_par = funcNode->child->right->right;
f.next = NULL;
temp->next = (struct funcEntry*)&f;
}
}