-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
293 lines (271 loc) · 7.99 KB
/
symbol.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Justin Canton, 1000017910
Samprit Raihan, 998138830
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "symbol.h"
symbol *head = NULL;
symbol *currentScope = NULL;
symbol *previousScope = NULL;
int ScopeCount = 0;
// Predefined variables
char* predefined[13] = {
"gl_FragColor",
"gl_FragDepth",
"gl_FragCoord",
"gl_TexCoord",
"gl_Color",
"gl_Secondary",
"gl_FogFragCoord",
"gl_Light_Half",
"gl_Light_Ambient",
"gl_Material_Shininess",
"env1",
"env2",
"env3"
};
// Initailize the global symbol table with predefined variables
void init_PredefinedVars()
{
//printf("initPreDefVars\n");
int i;
struct symbol *s = NULL;
for(i = 0; i < 13; i++)
{
s = (struct symbol*)malloc(sizeof(struct symbol));
// only gl_FragDepth is bool
if(strcmp(predefined[i], "gl_FragDepth") == 0)
{
s->type_int = 2;
s->type = (char *)"bool";
}
// rest is vec4
else
{
s->type_int = 33;
s->type = (char *)"vec4";
}
s->name = (char*) predefined[i];
s->constant = -1;
s->next = NULL;
symbol *tmp = currentScope->symtable;
if(tmp == NULL)
{
currentScope->symtable = s;
}
else
{
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = s;
}
}
//debug_printSymbolTable();
}
/*Add the variable to the symbol table*/
void pushVar(char *name, int type, int constant) {
struct symbol *s = NULL;
s = (struct symbol*)malloc(sizeof(struct symbol));
s->name = name;
s->type_int = type;
s->constant = constant;
//Get the type in a string format
switch(type){
case 1:
s->type = (char *)"int";
break;
case 2:
s->type = (char *)"bool";
break;
case 3:
s->type = (char *)"float";
break;
case 11:
s->type = (char *)"ivec2";
break;
case 12:
s->type = (char *)"ivec3";
break;
case 13:
s->type = (char *)"ivec4";
break;
case 21:
s->type = (char *)"bvec2";
break;
case 22:
s->type = (char *)"bvec3";
break;
case 23:
s->type = (char *)"bvec4";
break;
case 31:
s->type = (char *)"vec2";
break;
case 32:
s->type = (char *)"vec3";
break;
case 33:
s->type = (char *)"vec4";
break;
}
//Assign the scope of the variable
if(ScopeCount != 1){
s->var.scope = currentScope;
//printf("identified a variable \"%s\" in the scope of function \"%d\"\n",s->name,s->var.scope->scope);
}
else
{ // Do nothing
//printf("identified a global variable \"%s\"\n",s->name);
}
//Look for the variable in the symbol table
if(ScopeCount == 1)
if(!exists_Sym_glob(name)){
//printf("appending symbol \"%s\" to global symboltable \n",name);
//printf("-----------------------------------------------------------------\n");
s->next = NULL;
symbol *tmp = currentScope->symtable;
if(tmp == NULL){
currentScope->symtable = s;
}else{
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = s;
}
}else return;
else if(!exists_Sym_loc(name)){
//printf("appending symbol \"%s\" to local table \"%d\" \n",name, currentScope->scope);
//printf("-----------------------------------------------------------------\n");
s->next = NULL;
symbol *tmp = currentScope->symtable;
if(tmp == NULL){
currentScope->symtable = s;
}else{
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = s;
}
}
}
/*Moves the scope back 1 when the nested scope is finished*/
struct symbol* resetScope(){
//debug_printSymbolTable();
symbol *s = head;
symbol *t = NULL;
//Find the scope that is right before the previous one and then assign it to current scope
if(currentScope != head){
while(s->next != currentScope)
s = s->next;
}
currentScope = s;
t = currentScope->next;
currentScope->next = NULL;
ScopeCount--;
//printf("reset scope to %d\n", currentScope->scope);
//printf("-----------------------------------------------------------------\n");
//Return the scope for the ast to use
return t;
}
/*Search the global symbol table for the variable to see if it was previously declared*/
int exists_Sym_glob(char const *name){
symbol *s = NULL;
//printf("searching for symbol \"%s\" in global table\n",name);
if(currentScope->symtable == NULL){
//fprintf(errorFile, "ERROR -- global symboltable is empty \n");
//errorOccurred = 1;
return 0;
}
for(s = currentScope->symtable; s != NULL; s = s->next){
if (!strcmp(name, s->name)){
fprintf(errorFile, "ERROR -- multiple declaration of variable \"%s\"\n", name);
errorOccurred = 1;
return 1;
}
}
//printf("cannot find a symbol with the same declaration\n");
return 0;
}
/*Search the local symbol table for the variable to see if it was previously declared*/
int exists_Sym_loc(char const *name){
symbol *s = NULL;
//printf("searching for symbol \"%s\" in table \"%d\"\n",name,currentScope->scope);
if(currentScope->symtable == NULL){
//printf("local table \"%d\" is empty\n", currentScope->scope);
return 0;
}
if(currentScope->symtable != NULL){
for(s = currentScope->symtable; s != NULL; s = s->next){
if (!strcmp(name, s->name)){
fprintf(errorFile, "ERROR -- multiple declaration of variable \"%s\n", name);
errorOccurred = 1;
return 1;
}
}
}
//printf("cannot find a symbol with the same declaration\n");
return 0;
}
/*Search the symbol table for a variable*/
struct symbol* find_Sym(char const *name){
symbol *s = NULL;
for(s = currentScope->symtable; s != NULL; s = s->next){
if (!strcmp(name, s->name)){
//printf("\nfound symbol %s\n",name);
return s;
}
}
return NULL;
}
/*Search the symbol table for a variable*/
struct symbol* find_Sym_External(char const *name){
symbol *s = NULL;
for(s = currentScope->symtable; s != NULL; s = s->next){
if (!strcmp(name, s->name)){
//printf("\nfound symbol %s\n",name);
return s;
}
}
return NULL;
}
/*Print out the symbol table, but not used since this isn't required*/
void debug_printSymbolTable(){
symbol *s = NULL;
symbol *t = NULL;
printf("\n\n\t\t - DEBUG ___ SYMBOL _ TABLE ___ DEBUG - \n\n");
printf("-----------------------------------------------------------------\n");
printf(" global \t\t| local\n");
printf("-----------------------------------------------------------------\n");
for(t = head; t != NULL; t = t->next){
for(s = t->symtable; s != NULL; s = s->next){
if(t->next != NULL){
printf("|type:%s name:%s| \n",s->type,s->name);
}else if(t->scope == 1){
printf("|type:%s name:%s| \n",s->type,s->name);
}else{
printf("\t\t\t|type:%s name:%s| \n",s->type,s->name);
}
}
}
}
/*Make a new scope for each nested scope*/
void newScope(){
ScopeCount = ScopeCount + 1;
struct symbol *s = NULL;
s = (struct symbol*)malloc(sizeof(struct symbol));
s->scope = ScopeCount;
s->symtable = NULL;
//printf("making new scope %d\n", s->scope);
//printf("-----------------------------------------------------------------\n");
if(head == NULL)
head = s;
if(currentScope == NULL){
currentScope = s;
}else{
currentScope->next = s;
currentScope = currentScope->next;
}
}