- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast.cpp
More file actions
Latest commit
executable file
·279 lines (260 loc) · 8.34 KB
/
Copy pathast.cpp
File metadata and controls
executable file
·279 lines (260 loc) · 8.34 KB
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
#include"ast.h"
#include"main.h"
// Return a string representing the struct type passed in
constchar *ast_type_str(ast_type type)
{
switch (type.type)
{
caseVAR_BOOL:
return"boolean";
caseVAR_INT:
return"int";
caseVAR_INT_ARRAY:
return"int[]";
caseVAR_CLASS:
return type.classname;
caseVAR_STRING_ARRAY:
return"String[]";
caseVAR_VOID:
return"void";
default:
return"-- UNKNOWN TYPE --";
}
}
// Map the id of the parent class to `this` in Method(this, arg1, arg2,..)
voidinitialize_var_declaration_this(ast_methoddecl *class_methods, char* classname) {
for (ast_methoddecl *method=class_methods; method!=NULL; method = method->next) {
for (ast_vardecl *param = method->params; param!=NULL; param = param->next) {
if (param->id == "this") {
param->type.classname = classname;
}
}
}
}
// Write to the file, the appropriate indent for the indent level
voidprint_indent(FILE *file, int indent_level)
{
int i;
for(i = 0; i < indent_level; ++i)
fputs("", file);
}
// Print the `ast_expr`
voidast_expr_print(FILE *file, int indent_level, ast_expr *expr)
{
if(expr == NULL)
return; // If ExpList is empty, print nothing
print_indent(file, indent_level);
char *op_str;
switch(expr->type)
{
caseINT_CONST:
fprintf(file, "INT_CONST(%d)\n", expr->int_const);
break;
caseBOOL_CONST:
fprintf(file, "BOOL_CONST(%s)\n", expr->bool_const ? "true" : "false");
break;
caseVARNAME:
fprintf(file, "VARNAME(%s)\n", expr->id);
break;
caseTHIS_PTR:
fprintf(file, "THIS_PTR\n");
break;
caseNOT_EXPR:
fprintf(file, "NOT_EXPR:\n");
ast_expr_print(file, indent_level + 1, expr->expr);
break;
caseNEW_CLASS:
fprintf(file, "NEW_CLASS(%s)\n", expr->id);
break;
caseNEW_INT_ARRAY:
fprintf(file, "NEW_INT_ARRAY[\n");
ast_expr_print(file, indent_level + 1, expr->expr);
print_indent(file, indent_level);
fprintf(file, "]\n");
break;
caseARRAY_LENGTH:
fprintf(file, "ARRAY_LENGTH:\n");
ast_expr_print(file, indent_level + 1, expr->expr);
break;
caseARRAY_INDEX:
fprintf(file, "ARRAY_INDEX:\n");
ast_expr_print(file, indent_level + 1, expr->array);
print_indent(file, indent_level+1);
fprintf(file, "[\n");
ast_expr_print(file, indent_level + 1, expr->array_index);
print_indent(file, indent_level+1);
fprintf(file, "]\n");
break;
caseMETHOD_CALL:
fprintf(file, "METHOD_CALL(%s):\n", expr->method);
print_indent(file, indent_level + 1);
fprintf(file, "OBJECT:\n");
ast_expr_print(file, indent_level + 2, expr->object);
print_indent(file, indent_level + 1);
fprintf(file, "EXP_LIST:\n");
ast_expr_print(file, indent_level + 2, expr->exp_list);
break;
caseBINOP:
switch(expr->oper) {
caseAND:
op_str = "AND";
break;
caseLESS:
op_str = "LESS";
break;
casePLUS:
op_str = "PLUS";
break;
caseMINUS:
op_str = "MINUS";
break;
caseMULT:
op_str = "MULT";
break;
default:
op_str = "--- UNKNOWN OPERAND ---";
break;
}
fprintf(file, "BINOP(%s):\n", op_str);
print_indent(file, indent_level + 1);
fprintf(file, "LHS:\n");
ast_expr_print(file, indent_level + 2, expr->lhs);
print_indent(file, indent_level + 1);
fprintf(file, "RHS:\n");
ast_expr_print(file, indent_level + 2, expr->rhs);
break;
default:
fprintf(file, "--- UNKNOWN EXPRESSION ---\n");
break;
}
if(expr->next != NULL) // ExpList
ast_expr_print(file, indent_level, expr->next);
}
// Print the ast struct `ast_stmt`.
// This prints all statements list by looping through the linked list `->next` reference
voidast_stmt_print(FILE *file, int indent_level, ast_stmt *stmt)
{
if(stmt == NULL)
return; // If StmtList is empty, print nothing.
print_indent(file, indent_level);
switch(stmt->type)
{
caseBLOCK:
fputs("BLOCK:\n", file);
ast_stmt_print(file, indent_level+1, stmt->stmt_list);
break;
caseIF_ELSE:
fprintf(file, "IF_ELSE:\n");
print_indent(file, indent_level + 1);
fprintf(file, "COND:\n");
ast_expr_print(file, indent_level+2, stmt->cond);
print_indent(file, indent_level + 1);
fprintf(file, "TRUE:\n");
ast_stmt_print(file, indent_level+2, stmt->true_branch);
print_indent(file, indent_level + 1);
fprintf(file, "FALSE:\n");
ast_stmt_print(file, indent_level+2, stmt->false_branch);
break;
caseWHILE_STMT:
fprintf(file, "WHILE_STMT:\n");
print_indent(file, indent_level + 1);
fprintf(file, "COND:\n");
ast_expr_print(file, indent_level+2, stmt->cond);
print_indent(file, indent_level + 1);
fprintf(file, "WHILE_TRUE:\n");
ast_stmt_print(file, indent_level+2, stmt->while_branch);
break;
caseSYS_OUT:
fprintf(file, "SYS_OUT:\n");
ast_expr_print(file, indent_level + 1, stmt->expr);
break;
caseVAR_ASSIGN:
fprintf(file, "VAR_ASSIGN(%s):\n", stmt->id);
print_indent(file, indent_level + 1);
fprintf(file, "EXPRESSION:\n");
ast_expr_print(file, indent_level + 2, stmt->assign_expr);
break;
caseARRAY_ASSIGN:
fprintf(file, "ARRAY_ASSIGN(%s):\n", stmt->id);
print_indent(file, indent_level + 1);
fprintf(file, "INDEX:\n");
ast_expr_print(file, indent_level + 2, stmt->array_index);
print_indent(file, indent_level + 1);
fprintf(file, "EXPRESSION:\n");
ast_expr_print(file, indent_level + 2, stmt->assign_expr);
break;
default:
fprintf(file, "--- UNKNOWN STATEMENT ---\n");
break;
}
if(stmt->next != NULL) // StmtList
ast_stmt_print(file, indent_level, stmt->next);
}
// Print the ast struct `ast_vardecl`.
// This prints all variable declarations by looping through the linked list `->next` reference
voidast_vardecl_print(FILE *file, int indent_level, ast_vardecl *decl)
{
if(decl == NULL)
return; // If VarList is empty, print nothing.
print_indent(file, indent_level);
fprintf(file, "%s : %s\n", decl->id, ast_type_str(decl->type));
if(decl->next != NULL)
ast_vardecl_print(file, indent_level, decl->next);
}
// Print the ast struct `ast_methoddecl`.
// This prints all method declarations by looping through the linked list `->next` reference
voidast_method_print(FILE *file, int indent_level, ast_methoddecl *method)
{
if(method == NULL)
return; // If MethodList is empty, print nothing.
print_indent(file, indent_level);
fprintf(file, "MethodDeclaration(%s : %s):\n", method->id, ast_type_str(method->type));
print_indent(file, indent_level + 1);
fprintf(file, "PARAMETERS:\n");
ast_vardecl_print(file, indent_level + 2, method->params);
print_indent(file, indent_level + 1);
fprintf(file, "VarDeclaration:\n");
ast_vardecl_print(file, indent_level + 2, method->var_decl);
print_indent(file, indent_level + 1);
fprintf(file, "BODY:\n");
ast_stmt_print(file, indent_level + 2, method->body);
print_indent(file, indent_level + 1);
fprintf(file, "RETURN:\n");
ast_expr_print(file, indent_level + 2, method->return_expr);
if(method->next != NULL) // StmtList
ast_method_print(file, indent_level, method->next);
}
// Print the ast struct `ast_classdecl`.
// This prints all class declarations by looping through the linked list `->next` reference
voidast_class_print(FILE *file, int indent_level, ast_classdecl *class_declaration)
{
if(class_declaration == NULL)
return; // If MethodList is empty, print nothing.
print_indent(file, indent_level);
fprintf(file, "ClassDeclaration(%s):\n", class_declaration->id);
print_indent(file, indent_level + 1);
fprintf(file, "FIELDS:\n");
ast_vardecl_print(file, indent_level + 2, class_declaration->fields);
print_indent(file, indent_level + 1);
fprintf(file, "METHODS:\n");
ast_method_print(file, indent_level + 2, class_declaration->methods);
if(class_declaration->next != NULL) // StmtList
ast_class_print(file, indent_level, class_declaration->next);
}
// Print the ast struct `ast_mainclass`
voidast_main_print(FILE *file, int indent_level, ast_mainclass *main_class)
{
print_indent(file, indent_level);
fprintf(file, "MainClass(%s):\n", main_class->id);
ast_method_print(file, indent_level + 1, main_class->method);
}
// Print our AST
voidast_program_print(FILE *file, int indent_level, ast_program *program)
{
print_indent(file, indent_level);
fprintf(file, "PROGRAM:\n");
ast_main_print(file, indent_level + 1, &program->main_class);
print_indent(file, indent_level + 1);
fprintf(file, "CLASSES:\n");
ast_class_print(file, indent_level + 2, program->class_list);
}