-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.c
342 lines (275 loc) · 10.1 KB
/
ast.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "typecheck.h"
#include "symtab.h"
#include "utils.h"
#include "globals.h"
#include "errors.h"
#include "vector.h"
static inline Statement *newStatement() {
Statement *statement = malloc(sizeof(Statement));
statement->next = NULL;
statement->code = newVector(25);
return statement;
}
static Expression *newExpression() {
Expression *expr = malloc(sizeof(Expression));
expr->type = CONST;
expr->next = NULL;
expr->inferredType = UNKNOWN;
expr->variableExpression = NULL;
expr->place = NULL;
expr->code = newVector(25);
return expr;
}
Expression *newBinaryExpression(BinaryOperation op, Expression *left, Expression *right) {
BinaryExpression *binaryExpr = malloc(sizeof(BinaryExpression));
binaryExpr->operation = op;
binaryExpr->leftOperand = left;
binaryExpr->rightOperand = right;
Expression *expr = newExpression();
expr->binaryExpression = binaryExpr;
expr->type = BINARY;
debug(E_DEBUG, "Creating binary expression with left operand types %s and %s\n",
expressionTypeName(left), expressionTypeName(right));
typeCheckExpression(expr);
return expr;
}
Expression *newUnaryExpression(UnaryOperation op, Expression *operand) {
UnaryExpression *unaryExpr = malloc(sizeof(UnaryExpression));
unaryExpr->operation = op;
unaryExpr->operand = operand;
Expression *expr = newExpression();
expr->unaryExpression = unaryExpr;
expr->type = UNARY;
debug(E_DEBUG, "Creating unary expression with operand of type %s\n",
expressionTypeName(operand));
typeCheckExpression(expr);
return expr;
}
Expression *newVariableExpression(Scope *scope, char *identifier, Expression *arrayIndex) {
ScopeElement *elem = findScopeElement(scope, identifier);
VariableExpression *variableExpression = malloc(sizeof(VariableExpression));
Expression *expr = newExpression();
if(elem) {
if(elem->elementType == SCOPE_VAR) {
// Create the variable expression
ScopeVariable *var = elem->variable;
Type varType = var->type;
variableExpression->type = varType;
variableExpression->identifier = identifier;
variableExpression->arrayIndex = arrayIndex;
// Wrap it
expr->variableExpression = variableExpression;
expr->type = VARIABLE;
debug(E_DEBUG, "Creating variable expression for ID %s\n", identifier);
} else {
error(VAR_AS_FUNCTION, identifier);
}
} else {
error(UNDECLARED_INDENTIFIER, identifier);
foundError = true;
}
typeCheckExpression(expr);
return expr;
}
Expression *newFunctionExpression(Scope *scope, char *identifier, Expression *arguments) {
ScopeElement *elem = findScopeElement(scope, identifier);
Expression *expr = NULL;
if(elem) {
if(elem->elementType == SCOPE_FUNC) {
FunctionExpression *functionExpression = malloc(sizeof(FunctionExpression));
expr = newExpression();
// Create the function expression
ScopeFunction *func = elem->function;
functionExpression->identifier = identifier;
functionExpression->returnType = func->returnType;
functionExpression->arguments = arguments;
// Wrap it
expr->functionExpression = functionExpression;
expr->type = FUNCTION;
debug(E_DEBUG, "Creating function expression for ID %s\n", identifier);
} else {
error(VAR_AS_FUNCTION, identifier);
}
} else {
error(CALL_UNDEF_FUNCTION, identifier);
}
typeCheckExpression(expr);
return expr;
}
Expression *newConstExpression(Type type, Value *value) {
ConstExpression *constExpr = malloc(sizeof(ConstExpression));
constExpr->type = type;
constExpr->value = value;
Expression *expr = newExpression();
expr->constantExpression = constExpr;
expr->type = CONST;
expr->inferredType = type;
return expr;
}
Expression *newIntConstExpression(int val) {
Value *value = malloc(sizeof(Value));
value->integer_value = val;
debug(E_DEBUG, "Creating int const with value %d\n", val);
return newConstExpression(INT_TYPE, value);
}
Expression *newCharConstExpression(char *val) {
Value *value = malloc(sizeof(Value));
if(val[1] == '\\') {
if(val[2] == 0) {
value->char_value = '\0';
} else if(val[2] == 'n') {
value->char_value = '\n';
}
} else {
value->char_value = val[1];
}
debug(E_DEBUG, "Creating char const with value %c\n", val);
return newConstExpression(CHAR_TYPE, value);
}
Expression *newCharArrayConstExpression(char val[]) {
Value *value = malloc(sizeof(Value));
value->char_array_value = val;
debug(E_DEBUG, "Creating char array const with value %s\n", val);
return newConstExpression(CHAR_ARRAY_TYPE, value);
}
Expression *newIntArrayConstExpression(int val[]) {
Value *value = malloc(sizeof(Value));
value->int_array_value = val;
debug(E_DEBUG, "Creating int array const with value %d\n", val);
return newConstExpression(INT_ARRAY_TYPE, value);
}
/* Constructor functions for Statements */
Statement *newForStatement(Scope *scope, Statement *initial, Expression *condition,
Statement *change, Statement *body) {
ForStatement *forStatement = malloc(sizeof(ForStatement));
forStatement->initial = initial;
forStatement->condition = condition;
forStatement->change = change;
forStatement->body = body;
Statement *stmt = newStatement();
stmt->stmt_for = forStatement;
stmt->type = ST_FOR;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newWhileStatement(Scope *scope, Expression *condition, Statement *body) {
WhileStatement *whileStatement = malloc(sizeof(WhileStatement));
whileStatement->condition = condition;
whileStatement->body = body;
Statement *stmt = newStatement();
stmt->stmt_while = whileStatement;
stmt->type = ST_WHILE;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newFunctionCallStatement(Scope *scope, Expression *functionCall) {
FunctionCallStatement *callStatement = malloc(sizeof(FunctionCallStatement));
callStatement->functionCall = functionCall;
Statement *stmt = newStatement();
stmt->stmt_func = callStatement;
stmt->type = ST_FUNC;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newIfStatement(Scope *scope, Expression *condition, Statement *body) {
IfStatement *ifStatement = malloc(sizeof(IfStatement));
ifStatement->condition = condition;
ifStatement->satisfied = body;
Statement *stmt = newStatement();
stmt->stmt_if = ifStatement;
stmt->type = ST_IF;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newIfElseStatement(Scope *scope, Expression *condition, Statement *satisfied, Statement *unsatisfied) {
IfElseStatement *ifElseStatement = malloc(sizeof(IfElseStatement));
ifElseStatement->condition = condition;
ifElseStatement->satisfied = satisfied;
ifElseStatement->unsatisfied = unsatisfied;
Statement *stmt = newStatement();
stmt->stmt_if_else = ifElseStatement;
stmt->type = ST_IF_ELSE;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newReturnStatement(Scope *scope, Expression *returnValue) {
ReturnStatement *returnStatement = malloc(sizeof(ReturnStatement));
returnStatement->returnValue = returnValue;
Statement *stmt = newStatement();
stmt->stmt_return = returnStatement;
stmt->type = ST_RETURN;
typeCheckStatement(scope, stmt);
return stmt;
}
Statement *newAssignmentStatement(Scope *scope, char *identifier, Expression *arrayIndex, Expression *expression) {
AssignmentStatement *assign = malloc(sizeof(AssignmentStatement));
assign->identifier = identifier;
assign->arrayIndex = arrayIndex;
assign->expression = expression;
Statement *stmt = newStatement();
stmt->stmt_assign = assign;
stmt->type = ST_ASSIGN;
typeCheckStatement(scope, stmt);
return stmt;
}
/* Constructor functions for Declarations */
VariableDeclaration *newVariable(Type type, char *identifier) {
VariableDeclaration *varDecl = malloc(sizeof(VariableDeclaration));
varDecl->type = type;
varDecl->identifier = identifier;
varDecl->next = NULL;
return varDecl;
}
FunctionParameter *newFunctionParameter(Type type, char *identifier) {
FunctionParameter *param = malloc(sizeof(FunctionParameter));
if(param) {
param->type = type;
param->identifier = identifier;
param->next = NULL;
}
return param;
}
FunctionDeclaration *newFunction(Type returnType, char *functionName, Vector *parameters,
VariableDeclaration *declarations, Statement *body) {
FunctionDeclaration *funcDecl = malloc(sizeof(FunctionDeclaration));
funcDecl->returnType = returnType;
funcDecl->functionName = functionName;
funcDecl->parameters = parameters;
funcDecl->declarations = declarations;
funcDecl->body = body;
funcDecl->next = NULL;
funcDecl->functionScope = NULL;
funcDecl->code = newVector(25);
return funcDecl;
}
/* Utility Functions */
char *expressionTypeName(Expression *expression) {
char *name = "ERROR";
if(expression) {
ExpressionType type = expression->type;
switch(type) {
case CONST: name = "Constant"; break;
case VARIABLE: name = "Variable"; break;
case FUNCTION: name = "Function"; break;
case UNARY: name = "Unary"; break;
case BINARY: name = "Binary"; break;
}
}
return name;
}
char *typeName(Type type) {
char *name = "ERROR";
switch(type) {
case INT_TYPE: name = "INT"; break;
case CHAR_TYPE: name = "CHAR"; break;
case CHAR_ARRAY_TYPE: name = "CHAR_ARRAY"; break;
case INT_ARRAY_TYPE: name = "INT_ARRAY"; break;
case VOID_TYPE: name = "VOID"; break;
case BOOL_TYPE: name = "BOOL"; break;
default: break;
}
return name;
}