This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.c
357 lines (290 loc) · 6.7 KB
/
generator.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "ast.h"
#include "generator.h"
#include "type.h"
static char* create_tabs(int tabs)
{
char* output = malloc(sizeof(char) * (tabs + 1));
assert(output != NULL);
for (int i = 0; i < tabs; i++)
{
output[i] = '\t';
}
output[tabs] = '\0';
return output;
}
static void expression(FILE* file, ast_t* ast);
static void compound_statement(FILE* file, ast_t* ast, int tabs);
static void dispatcher(FILE* file, ast_t* ast, int tabs);
static void integer(FILE* file, ast_t* ast)
{
fprintf(file, "%li", ast->integer);
}
static void boolean(FILE* file, ast_t* ast)
{
fprintf(file, ast->boolean ? "true" : "false");
}
static void variable(FILE* file, ast_t* ast)
{
fprintf(file, ast->var.name);
}
static void type(FILE* file, type_e type)
{
switch (type)
{
case TYPE_INTEGER: fprintf(file, "long"); break;
case TYPE_BOOLEAN: fprintf(file, "bool"); break;
case TYPE_VOID : fprintf(file, "void"); break;
default:
// TODO: Throws an error.
break;
}
}
static void type_variable(FILE* file, ast_t* ast)
{
type(file, ast->var.type);
fprintf(file, " ");
variable(file, ast);
}
static void fn_call(FILE* file, ast_t* ast)
{
fprintf(file, ast->call.name);
fprintf(file, "(");
ast_list_t* arg = ast->call.args;
while (arg != NULL)
{
expression(file, arg->value);
arg = arg->next;
if (arg != NULL)
{
fprintf(file, ", ");
}
}
fprintf(file, ")");
}
static void unary(FILE* file, ast_t* ast)
{
switch (ast->unary.op)
{
case AST_LEFT_INCREMENT:
fprintf(file, "++");
expression(file, ast->unary.operand);
break;
case AST_RIGHT_INCREMENT:
expression(file, ast->unary.operand);
fprintf(file, "++");
break;
case AST_LEFT_DECREMENT:
fprintf(file, "--");
expression(file, ast->unary.operand);
break;
case AST_RIGHT_DECREMENT:
expression(file, ast->unary.operand);
fprintf(file, "--");
break;
case AST_NEGATE:
fprintf(file, "!");
expression(file, ast->unary.operand);
break;
case AST_PARENTHESIS:
fprintf(file, "(");
expression(file, ast->unary.operand);
fprintf(file, ")");
break;
default:
// Throws an error...
break;
}
}
static void binary(FILE* file, ast_t* ast)
{
expression(file, ast->binary.left);
switch (ast->binary.op)
{
case AST_MULTIPLICATION: fprintf(file, " * " ); break;
case AST_DIVISION : fprintf(file, " / " ); break;
case AST_ADDITION : fprintf(file, " + " ); break;
case AST_SUBSTRACTION : fprintf(file, " - " ); break;
case AST_LESS : fprintf(file, " < " ); break;
case AST_LESS_EQUAL : fprintf(file, " <= "); break;
case AST_EQUAL : fprintf(file, " == "); break;
case AST_NOT_EQUAL : fprintf(file, " != "); break;
case AST_GREATER_EQUAL : fprintf(file, " >= "); break;
case AST_GREATER : fprintf(file, " > " ); break;
case AST_AND : fprintf(file, " && "); break;
case AST_OR : fprintf(file, " || "); break;
default:
// Throws an error...
break;
}
expression(file, ast->binary.right);
}
static void ret(FILE* file, ast_t* ast)
{
fprintf(file, "return ");
expression(file, ast->ret.expression);
fprintf(file, ";\n");
}
static void assignment(FILE* file, ast_t* ast)
{
variable(file, ast->assigment.lvalue);
fprintf(file, " = ");
expression(file, ast->assigment.rvalue);
fprintf(file, ";\n");
}
static void declaration(FILE* file, ast_t* ast)
{
type_variable(file, ast->declaration.lvalue);
if (ast->declaration.rvalue != NULL)
{
fprintf(file, " = ");
expression(file, ast->declaration.rvalue);
}
fprintf(file, ";\n");
}
static void expression(FILE* file, ast_t* ast)
{
switch (ast->type)
{
case AST_INTEGER:
integer(file, ast);
break;
case AST_BOOLEAN:
boolean(file, ast);
break;
case AST_VARIABLE:
variable(file, ast);
break;
case AST_FNCALL:
fn_call(file, ast);
break;
case AST_UNARY:
unary(file, ast);
break;
case AST_BINARY:
binary(file, ast);
break;
default:
// Throws an error...
break;
}
}
static void loop(FILE* file, ast_t* ast, int tabs)
{
fprintf(file, "while (");
expression(file, ast->loop.condition);
fprintf(file, ")\n");
if (ast->loop.statement->type != AST_COMPOUND_STATEMENT)
{
tabs += 1;
}
dispatcher(file, ast->loop.statement, tabs);
}
static void branch(FILE* file, ast_t* ast, int tabs)
{
fprintf(file, "if (");
expression(file, ast->branch.condition);
fprintf(file, ")\n");
dispatcher(file, ast->branch.valid, ast->branch.valid->type == AST_COMPOUND_STATEMENT ? tabs : tabs + 1);
if (ast->branch.invalid != NULL)
{
char* strtabs = create_tabs(tabs);
fprintf(file, "%selse", strtabs);
free(strtabs);
if (ast->branch.invalid->type == AST_CONDITION)
{
fprintf(file, " ");
branch(file, ast->branch.invalid, tabs);
}
else
{
fprintf(file, "\n");
dispatcher(file, ast->branch.invalid, ast->branch.invalid->type == AST_COMPOUND_STATEMENT ? tabs : tabs + 1);
}
}
}
static void dispatcher(FILE* file, ast_t* ast, int tabs)
{
char* s = create_tabs(tabs);
fprintf(file, s);
free(s);
switch (ast->type)
{
case AST_FNCALL:
fn_call(file, ast);
fprintf(file, ";\n");
break;
case AST_UNARY:
unary(file, ast);
fprintf(file, ";\n");
break;
case AST_ASSIGNMENT:
assignment(file, ast);
break;
case AST_DECLARATION:
declaration(file, ast);
break;
case AST_RETURN:
ret(file, ast);
break;
case AST_LOOP:
loop(file, ast, tabs);
break;
case AST_CONDITION:
branch(file, ast, tabs);
break;
case AST_COMPOUND_STATEMENT:
compound_statement(file, ast, tabs);
break;
default:
// Throws an error
break;
}
}
static void compound_statement(FILE* file, ast_t* ast, int tabs)
{
char* local_tabs = create_tabs(tabs);
char* inside_tabs = create_tabs(tabs + 1);
fprintf(file, "{\n");
ast_list_t* statement = ast->compound_statements.statements;
while (statement != NULL)
{
dispatcher(file, statement->value, tabs + 1);
statement = statement->next;
}
fprintf(file, "%s}\n", local_tabs);
free(local_tabs);
free(inside_tabs);
}
static void function(FILE* file, ast_t* ast)
{
type(file, ast->function.return_type);
fprintf(file, " %s(", ast->function.name);
ast_list_t* param = ast->function.params;
while (param != NULL)
{
type_variable(file, param->value);
param = param->next;
if (param != NULL)
{
fprintf(file, ", ");
}
}
fprintf(file, ")\n");
ast_t* statements = ast_new_comp_stmt(ast->function.statments);
compound_statement(file, statements, 0);
free(statements);
}
void generator(FILE* file, ast_list_t* ast)
{
fprintf(file, "#include <stdbool.h>\n");
ast_list_t* func = ast;
while (func != NULL)
{
fprintf(file, "\n");
function(file, func->value);
func = func->next;
}
}