-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table.c
221 lines (159 loc) · 4.8 KB
/
symbol_table.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#define VARIABLE_E 1
#define FUNCTION 2
#define CONSTANT 3
#define PARAMETER 4
#define TEMPORARY_VARIABLE 5
#define PASS_BY_VALUE 6
#define PASS_BY_REFERENCE 7
#define TYPE_FUNCTION 8
#define TYPE_PROCEDURE 9
struct entity {
char name[30];
int nestingLevel;
struct entity *next;
// Variable (and "offset" used for Parameter):
int type;
int offset;
// Function:
int startQuad;
struct recordArgument *argumentList;
// Constant:
char value[30];
// Parameter / tempParameter:
int parMode;
};
struct scope {
char name[30];
int nestingLevel;
struct entity *entityList;
int framelength;
struct scope *next;
};
struct recordArgument {
int parMode;
int type;
struct recordArgument *next;
};
// List Global Variables:
struct scope *scopeHead = NULL;
struct recordArgument *argumentHead = NULL;
void addScope(char name[30]);
void deleteScope();
void addEntity(char name[30], int type, int mode, char value[30]);
struct entity *lookupEntity(char name[30]);
void printSymbolTable();
// Functions:
void addScope(char name[30]) {
printf("Debug: Inside addScope.\n\n");
// This function pushes a new_scope to the top of the scope stack:
struct scope *new_scope;
new_scope = malloc(sizeof(struct scope));
strcpy(new_scope->name, name);
new_scope->entityList = NULL;
if (scopeHead == NULL) {
scopeHead = new_scope;
new_scope->nestingLevel = 1;
new_scope->framelength = 12;
new_scope->next = NULL;
} else {
new_scope->nestingLevel = scopeHead->nestingLevel + 1;
new_scope->framelength = 12;
new_scope->next = scopeHead;
scopeHead = new_scope;
}
}
void deleteScope() {
printf("Debug: Inside deleteScope.\n\n");
if (scopeHead == NULL) {
printf("The scope stack is empty.\n\n");
exit(1);
} else {
scopeHead = scopeHead->next;
}
}
void addEntity(char name[30], int type, int mode, char value[30]) {
printf("Debug: Inside addEntity.\n\n");
struct entity *new_entity;
struct entity *current;
struct entity *previous;
int last_offset = -1;
new_entity = malloc(sizeof(struct entity));
// Data copy:
strcpy(new_entity->name, name);
new_entity->type = type;
new_entity->parMode = mode;
strcpy(new_entity->value, value);
new_entity->nestingLevel = scopeHead->nestingLevel;
printf("ADDED ENTITY: Name: %s, Type: %d, Mode: %d, Value: %s\n\n", name, type, mode, value);
new_entity->next = NULL;
previous = NULL;
current = scopeHead->entityList;
if(current == NULL) {
scopeHead->entityList = new_entity;
new_entity->offset = 12;
scopeHead->framelength = 16;
} else {
while (current != NULL){
previous = current;
if(current->type != TYPE_FUNCTION || current->type != TYPE_PROCEDURE) {
last_offset = current->offset;
}
current = current->next;
}
previous->next = new_entity;
new_entity->offset = last_offset + 4;
scopeHead->framelength = new_entity->offset + 4;
}
}
struct entity *lookupEntity(char name[30]) {
printf("Debug: Inside lookupEntity.\n\n");
struct entity *currentEntity;
struct scope *currentScope;
currentScope = scopeHead;
while (currentScope != NULL) {
currentEntity = currentScope->entityList;
while (currentEntity != NULL) {
if(strcmp(currentEntity->name, name) == 0 || strcmp(currentScope->name, name) == 0) {
printf("LOOKUP ENTITY: To be returned -> Name: %s\n\n", currentEntity->name);
return currentEntity;
}
currentEntity = currentEntity->next;
}
currentScope=currentScope->next;
}
if (!isdigit(name[0])) {
printf("The requested Entity (%s) was not found.\n\n", name);
}
// Returns NULL if the Entity is not found:
return(NULL);
}
void printSymbolTable() {
struct scope *currentScope;
struct entity *currentEntity;
currentScope = scopeHead;
printf("-- Symbol Table Printout --\n");
while(currentScope != NULL) {
printf("\nNesting Level: #%d\n", currentScope->nestingLevel);
printf("Scope Name: %s\n\n", currentScope->name);
currentEntity = currentScope->entityList;
while (currentEntity != NULL) {
if(currentEntity->type == VARIABLE_E) {
printf("<Variable %s, %d> \n", currentEntity->name, currentEntity->offset);
} else if (currentEntity->type == TYPE_FUNCTION) {
printf("<Function: %s> \n", currentEntity->name);
} else if (currentEntity->type == TYPE_PROCEDURE) {
printf("<Procedure: %s> \n", currentEntity->name);
} else if (currentEntity->type == CONSTANT) {
printf("<Constant: %s, %d, Value:%s> \n", currentEntity->name, currentEntity->offset, currentEntity->value);
} else if (currentEntity->type == PARAMETER) {
printf("<Parameter: %s, %d> \n", currentEntity->name, currentEntity->offset);
} else {
// Case: Temporary Variable
printf("<Temporary Variable: %s, %d> \n", currentEntity->name, currentEntity->offset);
}
currentEntity = currentEntity->next;
}
currentScope = currentScope->next;
}
printf("\n-- End of Symbol Table --\n\n");
}