-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmm.y
449 lines (393 loc) · 16.6 KB
/
cmm.y
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "symtab.h"
#include "utils.h"
#include "ast.h"
#include "globals.h"
#include "codegen.h"
#include "cmm.tab.h"
extern char *yytext;
// Parse state information
int mylineno;
int mycolno;
bool foundError = false;
// Typing and scoping
bool funcTypeSet = false;
bool declaredExtern = false;
Type currentFunctionReturnType = VOID_TYPE;
Scope *globalScope;
Scope *scope;
Type baseDeclType;
// Root of the syntax tree
FunctionDeclaration *root;
// Helper functions
Vector *parametersVector(FunctionParameter *parameters);
bool addFunctionDeclarationToScope(Type type, char *identifier, FunctionParameter *parameters);
void resetFunctionType();
void markGlobals(Scope *scope);
%}
%union {
Expression *expression;
Statement *statement;
Type type;
FunctionParameter *parameter;
FunctionDeclaration *functionDeclaration;
VariableDeclaration *variableDeclaration;
char *string;
}
%type<expression> expr optional_expr expr_list int_expr char_expr
%type<statement> stmt assg optional_assign stmt_list
%type<type> type
%type<functionDeclaration> func prog
%type<variableDeclaration> optional_var_decl_list var_decl var_decl_list
%type<parameter> param_types param_types_list non_void_param_type
%type<string> func_header
/* Language Tokens */
%token WHILE FOR
%token INT CHAR VOID
%token IF ELSE
%token EXTERN RETURN
%token COMMA SEMICOLON
/* Brackets */
%token LEFT_PAREN RIGHT_PAREN
%token LEFT_SQUARE_BRACKET RIGHT_SQUARE_BRACKET
%token LEFT_CURLY_BRACKET RIGHT_CURLY_BRACKET
/* Operators */
%token EQ NEQ LTE LT GT GTE NOT
%token AND OR
%token ADD MINUS MUL DIV
%token ASSIGN
/* Text tokens */
%token <expression> STRINGCON
%token <string> ID INTCON CHARCON
/* If/Else Precedence fix */
%nonassoc WITHOUT_ELSE
%nonassoc ELSE
/* Operator Precedence specification */
%left OR
%left AND
%left EQ NEQ equality_op
%left GTE LTE GT LT relop
%left ADD MINUS add_sub
%left MUL DIV mul_div
%right UMINUS NOT
%%
prog : decl prog { $$ = $2; root = $$; }
| func prog { $1->next = $2; $$ = $1; root = $$; }
| epsilon { $$ = NULL; root = $$; }
/**************************************************************************************************
* DECLARATIONS
*************************************************************************************************/
decl : type var_decl_list SEMICOLON
{
markGlobals(scope);
globalScope = flattenScope(scope);
scope = newScope(globalScope); // Clear out the scope the declaration was made in (ew)
resetFunctionType();
}
| type name_args_lists SEMICOLON
{
scope = newScope(globalScope);
resetFunctionType();
}
| void name_args_lists SEMICOLON
{
scope = newScope(globalScope);
resetFunctionType();
}
| extern type name_args_lists SEMICOLON
{
scope = newScope(globalScope);
declaredExtern = false;
resetFunctionType();
}
| extern void name_args_lists SEMICOLON
{
scope = newScope(globalScope);
declaredExtern = false;
resetFunctionType();
}
| error SEMICOLON
;
name_args_lists : ID LEFT_PAREN param_types RIGHT_PAREN
{
debug(E_DEBUG, "Declaring prototype for %s with type %s\n", $1, typeName(currentFunctionReturnType));
Vector *params = parametersVector($3);
declareFunction(globalScope, currentFunctionReturnType, $1, params, declaredExtern, true);
scope = newScope(globalScope);
}
| name_args_lists COMMA ID LEFT_PAREN param_types RIGHT_PAREN
{
debug(E_DEBUG, "Declaring prototype for %s with type %s\n", $3, typeName(currentFunctionReturnType));
Vector *params = parametersVector($5);
declareFunction(globalScope, currentFunctionReturnType, $3, params, declaredExtern, true);
scope = newScope(globalScope);
}
;
/**************************************************************************************************
* FUNCTION DECLARATIONS
*************************************************************************************************/
extern : EXTERN { declaredExtern = true; }
func_header : type ID LEFT_PAREN param_types RIGHT_PAREN
{
addFunctionDeclarationToScope($1, $2, $4);
$$ = $2;
}
| void ID LEFT_PAREN param_types RIGHT_PAREN
{
addFunctionDeclarationToScope(VOID_TYPE, $2, $4);
$$ = $2;
}
func : func_header LEFT_CURLY_BRACKET optional_var_decl_list stmt_list RIGHT_CURLY_BRACKET
{
char *identifier = $1;
ScopeElement *elem = findScopeElement(globalScope, identifier);
if(elem->elementType == SCOPE_FUNC) {
ScopeFunction *func = elem->function;
FunctionDeclaration *decl = newFunction(currentFunctionReturnType, identifier,
func->parameters, $3, $4);
decl->functionScope = scope;
scope = newScope(globalScope);
$$ = decl;
} else {
$$ = NULL;
}
resetFunctionType();
}
;
optional_var_decl_list : type var_decl_list SEMICOLON optional_var_decl_list
{ baseDeclType = $1; $2->next = $4; $$ = $2; }
| epsilon { $$ = NULL; }
/**************************************************************************************************
* STATEMENTS
*************************************************************************************************/
stmt : IF LEFT_PAREN expr RIGHT_PAREN stmt %prec WITHOUT_ELSE { $$ = newIfStatement(scope, $3, $5); }
| IF LEFT_PAREN expr RIGHT_PAREN stmt ELSE stmt { $$ = newIfElseStatement(scope, $3, $5, $7); }
| WHILE LEFT_PAREN expr RIGHT_PAREN stmt { $$ = newWhileStatement(scope, $3, $5); }
| FOR LEFT_PAREN optional_assign SEMICOLON optional_expr SEMICOLON optional_assign RIGHT_PAREN stmt
{ $$ = newForStatement(scope, $3, $5, $7, $9); }
| RETURN optional_expr SEMICOLON { $$ = newReturnStatement(scope, $2); }
| assg SEMICOLON { $$ = $1; }
| ID LEFT_PAREN expr_list RIGHT_PAREN SEMICOLON
{ $$ = newFunctionCallStatement(scope, newFunctionExpression(scope, $1, $3)); }
| LEFT_CURLY_BRACKET stmt_list RIGHT_CURLY_BRACKET { $$ = $2; }
| SEMICOLON { $$ = NULL; }
| error SEMICOLON { $$ = NULL; }
| error RIGHT_CURLY_BRACKET { $$ = NULL; }
;
stmt_list : stmt stmt_list
{
if($1) {
$1->next = $2;
$$ = $1;
} else {
$$ = NULL;
}
}
| epsilon { $$ = NULL; }
;
/**************************************************************************************************
* EXPRESSIONS
*************************************************************************************************/
expr : MINUS expr %prec UMINUS { $$ = newUnaryExpression(NEG_OP, $2); }
| NOT expr %prec UMINUS { $$ = newUnaryExpression(NOT_OP, $2); }
| expr ADD expr %prec add_sub { $$ = newBinaryExpression(ADD_OP, $1, $3); }
| expr MINUS expr %prec add_sub { $$ = newBinaryExpression(SUB_OP, $1, $3); }
| expr AND expr { $$ = newBinaryExpression(AND_OP, $1, $3); }
| expr OR expr { $$ = newBinaryExpression(OR_OP, $1, $3); }
| expr MUL expr %prec mul_div { $$ = newBinaryExpression(MUL_OP, $1, $3); }
| expr DIV expr %prec mul_div { $$ = newBinaryExpression(DIV_OP, $1, $3); }
| expr EQ expr %prec equality_op { $$ = newBinaryExpression(EQ_OP, $1, $3); }
| expr NEQ expr %prec equality_op { $$ = newBinaryExpression(NEQ_OP, $1, $3); }
| expr GTE expr %prec relop { $$ = newBinaryExpression(GTE_OP, $1, $3); }
| expr LTE expr %prec relop { $$ = newBinaryExpression(LTE_OP, $1, $3); }
| expr GT expr %prec relop { $$ = newBinaryExpression(GT_OP, $1, $3); }
| expr LT expr %prec relop { $$ = newBinaryExpression(LT_OP, $1, $3); }
| ID LEFT_PAREN expr_list RIGHT_PAREN { $$ = newFunctionExpression(scope, $1, $3); }
| ID LEFT_SQUARE_BRACKET expr RIGHT_SQUARE_BRACKET { $$ = newVariableExpression(scope, $1, $3); }
| ID { $$ = newVariableExpression(scope, $1, NULL); }
| LEFT_PAREN expr RIGHT_PAREN { $$ = $2; }
| int_expr { $$ = $1; }
| char_expr { $$ = $1; }
| STRINGCON { $$ = newCharArrayConstExpression(strdup(yytext)); }
| error { $$ = NULL; }
;
optional_expr : expr { $$ = $1; }
| epsilon { $$ = NULL; }
;
expr_list : optional_expr { $$ = $1; }
| expr_list COMMA expr { $3->next = $1; $$ = $3; }
int_expr: INTCON { $$ = newIntConstExpression(atoi(strdup(yytext))); }
;
char_expr: CHARCON { $$ = newCharConstExpression(strdup(yytext)); }
;
/**************************************************************************************************
* VARIABLE DECLARATIONS
*************************************************************************************************/
var_decl : ID
{
declareVar(scope, baseDeclType, $1, false);
$$ = newVariable(baseDeclType, $1);
}
| ID LEFT_SQUARE_BRACKET int_expr RIGHT_SQUARE_BRACKET
{
Type arrayType = baseDeclType == INT_TYPE ? INT_ARRAY_TYPE : CHAR_ARRAY_TYPE;
ScopeElement *elem = declareVar(scope, arrayType, $1, false);
$$ = newVariable(arrayType, $1);
elem->variable->size = $3->constantExpression->value->integer_value;
elem->variable->value = NULL;
}
;
var_decl_list : var_decl { $$ = $1; }
| var_decl_list COMMA var_decl { $1->next = $3; $$ = $1; }
| epsilon { $$ = NULL; }
/**************************************************************************************************
* TYPES
*************************************************************************************************/
void : VOID
{
if(!funcTypeSet) {
currentFunctionReturnType = VOID_TYPE;
funcTypeSet = true;
}
}
type : CHAR
{
if(!funcTypeSet) {
currentFunctionReturnType = CHAR_TYPE;
funcTypeSet = true;
}
baseDeclType = CHAR_TYPE; $$ = CHAR_TYPE;
}
| INT
{
if(!funcTypeSet) {
currentFunctionReturnType = INT_TYPE;
funcTypeSet = true;
}
baseDeclType = INT_TYPE; $$ = INT_TYPE;
}
;
/**************************************************************************************************
* FUNCTION PARAMETERS
*************************************************************************************************/
param_types : void { $$ = NULL; }
| non_void_param_type { $$ = $1; }
| param_types_list COMMA non_void_param_type { $3->next = $1; $$ = $3; }
;
non_void_param_type : type ID
{
declareVar(scope, baseDeclType, $2, true);
$$ = newFunctionParameter(baseDeclType, $2);
}
| type ID LEFT_SQUARE_BRACKET RIGHT_SQUARE_BRACKET
{
if(baseDeclType == INT_TYPE) {
declareVar(scope, INT_ARRAY_TYPE, $2, true);
$$ = newFunctionParameter(INT_ARRAY_TYPE, $2);
} else if(baseDeclType == CHAR_TYPE) {
declareVar(scope, CHAR_ARRAY_TYPE, $2, true);
$$ = newFunctionParameter(CHAR_ARRAY_TYPE, $2);
} else {
fprintf(stderr, "Type error, on line %d: Arrays of type %s are not supported.\n",
mylineno, typeName(baseDeclType));
foundError = true;
}
}
;
param_types_list : non_void_param_type { $$ = $1; }
| param_types_list COMMA non_void_param_type
{
$3->next = $1;
$$ = $3;
}
| epsilon { $$ = NULL; }
;
/**************************************************************************************************
* ASSIGNMENTS
*************************************************************************************************/
assg : ID ASSIGN expr
{
$$ = newAssignmentStatement(scope, $1, NULL, $3);
}
| ID LEFT_SQUARE_BRACKET expr RIGHT_SQUARE_BRACKET ASSIGN expr
{
$$ = newAssignmentStatement(scope, $1, $3, $6);
}
;
optional_assign: assg { $$ = $1; }
| error { $$ = NULL; }
| epsilon { $$ = NULL; }
;
epsilon:
;
%%
void markGlobals(Scope *scope) {
Vector *vars = scope->variables;
for(int i = 0; i < vars->size; i++) {
ScopeElement *elem = vectorGet(vars, i);
if(elem->elementType == SCOPE_VAR) {
debug(E_DEBUG, "Making global variable %s\n", elem->identifier);
elem->variable->global = true;
}
}
}
Vector *parametersVector(FunctionParameter *parameters) {
Vector *params = newVector(5);
while(parameters) {
vectorAdd(params, parameters);
parameters = parameters->next;
}
return params;
}
bool addFunctionDeclarationToScope(Type type, char *identifier, FunctionParameter *parameters) {
Vector *params = parametersVector(parameters);
bool success = declareFunction(globalScope, type, identifier, params, declaredExtern, false);
// Mark the function as implemented
if(success) {
ScopeElement *elem = findScopeElement(globalScope, identifier);
ScopeFunction *func = elem->function;
func->implemented = true;
}
return success;
}
void resetFunctionType() {
funcTypeSet = false;
currentFunctionReturnType = VOID_TYPE;
}
int main(int argc, char **argv) {
#ifdef DEBUG
FILE *debugFile = NULL;
debugFile = fopen("debug.log", "w");
setDebugOutputStream(debugFile);
setDebuggingLevel(E_ALL);
#endif
globalScope = newScope(NULL);
scope = newScope(globalScope);
yyparse();
if(foundError) {
return 1;
} else {
// Generate code for the syntax tree
debug(E_DEBUG, "-------------------BEGIN CODE GEN-------------------\n");
generateCode(root);
}
#ifdef DEBUG
fclose(debugFile);
#endif
return 0;
}
int yyerror() {
foundError = true;
if(yytext[0] == 0) {
fprintf(stderr, "Encountered unexpected EOF while parsing \"%s\" starting on line %d.\n",
yytext, mylineno);
} else {
fprintf(stderr, "%d:%d - Encountered error while parsing: \"%s\"\n", mylineno, mycolno,
yytext);
}
return 1;
}