-
Notifications
You must be signed in to change notification settings - Fork 1
/
symtab.c
224 lines (187 loc) · 7.63 KB
/
symtab.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
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "vector.h"
#include "symtab.h"
#include "globals.h"
#include "ast.h"
#include "utils.h"
#include "typecheck.h"
#include "errors.h"
static int variableId = 0;
static inline ScopeElement *inLocalScope(Scope *scope, char *identifier) {
if(scope) {
Vector *variables = scope->variables;
for(int i = 0; i < variables->size; i++) {
ScopeElement *element = vectorGet(variables, i);
if(element && strcmp(element->identifier, identifier) == 0) {
return element;
}
}
}
return NULL;
}
Scope *newScope(Scope *enclosingScope) {
debug(E_DEBUG, "Creating new scope\n");
Scope *scope = malloc(sizeof(Scope));
scope->enclosingScope = enclosingScope;
scope->variables = newVector(25);
return scope;
}
Scope *flattenScope(Scope *scope) {
Scope *destinationScope = newScope(NULL);
// Traverse every level of scope
while(scope) {
Vector *vars = scope->variables;
for(int i = 0; i < vars->size; i++) {
ScopeElement *elem = vectorGet(vars, i);
char *identifier = elem->identifier;
// If it hasn't already been declared in the flattened scope, "declare it"
if(inLocalScope(destinationScope, identifier)) {
error(REDECL_GLOBAL_VAR, identifier);
} else {
vectorAdd(destinationScope->variables, elem);
}
}
scope = scope->enclosingScope;
}
return destinationScope;
}
Scope *stripScope(Scope *scope) {
if(scope) {
return scope->enclosingScope;
} else {
return NULL;
}
}
ScopeElement *findScopeElement(Scope *scope, char *identifier) {
ScopeElement *elem = NULL;
while(scope) {
elem = inLocalScope(scope, identifier);
if(elem) {
return elem;
} else {
scope = scope->enclosingScope;
}
}
return elem;
}
ScopeElement* declareVar(Scope *scope, Type type, char *identifier, bool parameter) {
ScopeElement *foundVar = inLocalScope(scope, identifier);
if(foundVar) {
error(VAR_ALREADY_DECLARED, identifier);
} else {
debug(E_DEBUG, "Declaring undeclared variable \"%s\" with type %s\n", identifier, typeName(type));
Value *empty = malloc(sizeof(Value));
empty->integer_value = 0;
ScopeVariable *scopeVariable = malloc(sizeof(ScopeVariable));
scopeVariable->type = type;
scopeVariable->value = empty;
scopeVariable->size = -1;
scopeVariable->offset = 0;
scopeVariable->global = false;
scopeVariable->parameter = parameter;
int newIdentifierLength = strlen(identifier) + 20;
char *protectedIdentifier = malloc(newIdentifierLength);
snprintf(protectedIdentifier, newIdentifierLength, "_%s%d", identifier, variableId);
variableId += 1;
ScopeElement *elem = malloc(sizeof(ScopeElement));
elem->identifier = identifier;
elem->protectedIdentifier = protectedIdentifier;
elem->elementType = SCOPE_VAR;
elem->variable = scopeVariable;
vectorAdd(scope->variables, elem);
return elem;
}
return NULL;
}
bool declareFunction(Scope *scope, Type returnType, char *identifier, Vector *parameters,
bool declaredExtern, bool isPrototype) {
ScopeElement *foundVar = findScopeElement(scope, identifier);
bool validDeclaration = true;
// Determine if something with that name already exists
if(foundVar) {
// If that thing is a function, check some properties
if(foundVar->elementType == SCOPE_FUNC) {
ScopeFunction *func = foundVar->function;
// Determine whether or not a function prototype has been duplicated
if(isPrototype) {
error(REDEF_PROTOTYPE, identifier);
validDeclaration = false;
}
// Check if the function's return type has been changed
if(returnType != func->returnType) {
error(CHANGE_RET_TYPE, identifier, typeName(func->returnType), typeName(returnType));
validDeclaration = false;
}
// Determine if the function has been implemented or declared as extern
if(func->implemented) {
// An already implemented function cannot be reimplemented
error(REDEF_FUNCTION, identifier);
validDeclaration = false;
} else if(func->declaredExtern) {
// An extern function cannot be declared in the same file
error(REDEF_EXTERN, identifier);
validDeclaration = false;
} else {
// Ensure that the prototype and declaration match
Vector *expectedParams = func->parameters;
int declared = parameters->size;
int expected = expectedParams->size;
int min = declared < expected ? declared : expected;
// Check the types of each declared and expected parameter
for(int i = 0; i < min; i++) {
FunctionParameter *declaredParam = vectorGet(parameters, i);
FunctionParameter *expectedParam = vectorGet(expectedParams, i);
Type declaredType = declaredParam->type;
Type expectedType = expectedParam->type;
if(! typesCompatible(declaredType, expectedType)) {
error(ARG_TYPE_CHANGE, i, identifier, typeName(declaredType),
typeName(expectedType));
validDeclaration = false;
}
}
if(declared == 0 && expected != 0) {
error(REDEF_WITHOUT_ARGS, identifier);
validDeclaration = false;
} else if(declared != 0 && expected == 0) {
error(REDEF_WITH_ARGS, identifier);
validDeclaration = false;
} else if(declared != expected) {
error(ARG_NUM_CHANGE, identifier, expected, declared);
validDeclaration = false;
}
func->implemented = true;
}
} else {
// If it's a variable, then a variable can't be defined as a scope
error(REDEF_VAR_AS_FUNC, identifier);
validDeclaration = false;
}
} else {
// Add the function to scope
debug(E_DEBUG, "Declaring function %s of type %s on line %d.\n", identifier,
typeName(returnType), mylineno);
ScopeFunction *scopeFunction = malloc(sizeof(ScopeFunction));
scopeFunction->returnType = returnType;
scopeFunction->parameters = parameters;
scopeFunction->implemented = false;
scopeFunction->declaredExtern = declaredExtern;
ScopeElement *elem = malloc(sizeof(ScopeElement));
elem->identifier = identifier;
// Don't override the function name "main" since it is necessary for execution entry
if(strcmp(identifier, "main") == 0) {
elem->protectedIdentifier = identifier;
} else {
int newIdentifierLength = strlen(identifier) + 2;
char *protectedIdentifier = malloc(newIdentifierLength);
snprintf(protectedIdentifier, newIdentifierLength, "_%s", identifier);
elem->protectedIdentifier = protectedIdentifier;
}
elem->elementType = SCOPE_FUNC;
elem->function = scopeFunction;
vectorAdd(scope->variables, elem);
return true;
}
return validDeclaration;
}