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
|
/*
* Copyright (C) 2023 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ast_pretty_printer.h"
#include "ast.h"
#include "string_view.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
const char *operation_kinds[AST_BINOP_N] = {
[AST_BINOP_ADITION] = "+", [AST_BINOP_SUBTRACTION] = "-", [AST_BINOP_MULTIPLICATION] = "*",
[AST_BINOP_DIVISION] = "/", [AST_BINOP_EQUAL] = "==", [AST_BINOP_NOT_EQUAL] = "!=",
[AST_BINOP_AND] = "&&", [AST_BINOP_OR] = "||", [AST_BINOP_GT] = ">",
[AST_BINOP_LT] = "<", [AST_BINOP_LT_EQUAL] = "<=", [AST_BINOP_GT_EQUAL] = ">=",
};
void
ast_pretty_printer_init(ast_pretty_printer_t *printer, FILE *stream);
inline static void
ast_pretty_printer_add_indentation(ast_pretty_printer_t *printer);
inline static void
ast_pretty_printer_rm_indentation(ast_pretty_printer_t *printer);
inline static void
ast_pretty_printer_set_lst_children(ast_pretty_printer_t *printer);
static void
ast_pretty_printer_print_indentation(ast_pretty_printer_t *printer);
static int
ast_pretty_printer_printf(ast_pretty_printer_t *printer, const char *fmt, ...);
void
ast_pretty_printer_init(ast_pretty_printer_t *printer, FILE *stream)
{
assert(printer);
assert(stream);
printer->indentation_fmt = 0ULL;
printer->indentation_lst_children = 0ULL;
printer->indentation_level = 0;
printer->stream = stream;
}
void
ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
{
assert(ast);
switch (ast->kind) {
case AST_NAMESPACE:
ast_pretty_printer_printf(printer, "Namespace\n");
ast_pretty_printer_add_indentation(printer);
{
for (size_t i = 0; i < ast->data.ns.nodes->size; ++i) {
if (i + 1 >= ast->data.ns.nodes->size) {
ast_pretty_printer_set_lst_children(printer);
}
ast_pretty_printer_print_ast(printer, vector_at(ast->data.ns.nodes, i));
}
ast_pretty_printer_rm_indentation(printer);
}
break;
case AST_IF_STMT: {
ast_if_stmt_t if_stmt = ast->data.if_stmt;
ast_pretty_printer_printf(printer, "IfStmt\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_printf(printer, "condition:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, if_stmt.condition);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "body:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, if_stmt.body);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_BINARY_OPERATION: {
ast_binary_operation_t binop = ast->data.binary_operation;
ast_pretty_printer_printf(printer, "BinaryOperation operation='%s'\n", operation_kinds[binop.kind]);
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_printf(printer, "left:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, binop.left);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "right:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, binop.right);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_FUNCTION_DECLARATION: {
ast_function_declaration_t function = ast->data.function;
ast_pretty_printer_printf(printer, "FunctionDecl name='" SVFMT "'\n", SVARG(&function.identifier.name));
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "body:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, ast->data.function.body);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_FUNCTION_CALL: {
ast_function_call_t var = ast->data.function_call;
ast_pretty_printer_printf(printer, "FunctionCall name='" SVFMT "'\n", SVARG(&var.identifier->name));
break;
}
case AST_BLOCK: {
ast_pretty_printer_printf(printer, "Block\n");
ast_pretty_printer_add_indentation(printer);
{
for (size_t i = 0; i < ast->data.block.body->size; ++i) {
if (i + 1 >= ast->data.block.body->size) {
ast_pretty_printer_set_lst_children(printer);
}
ast_pretty_printer_print_ast(printer, vector_at(ast->data.block.body, i));
}
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_LITERAL: {
ast_literal_t literal = ast->data.literal;
switch (literal.kind) {
case AST_LITERAL_INTEGER: {
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "Literal type=i32 value='%d'\n", literal.value.integer);
break;
}
case AST_LITERAL_BOOL:
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(
printer, "Literal type=bool value='%s'\n", literal.value.boolean ? "true" : "false");
break;
}
break;
}
case AST_RETURN_STMT: {
ast_return_stmt_t return_stmt = ast->data.return_stmt;
ast_pretty_printer_printf(printer, "ReturnStmt\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, return_stmt.argument);
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_VARIABLE_DECLARATION: {
ast_variable_declaration_t var_decl = ast->data.variable_declaration;
ast_pretty_printer_printf(printer, "VariableDecl name='" SVFMT "'\n", SVARG(&var_decl.identifier.name));
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, var_decl.value);
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_VARIABLE_ASSIGNMENT: {
ast_variable_assignment_t var_assign = ast->data.variable_assignment;
ast_pretty_printer_printf(printer, "VariableAssignment name='" SVFMT "'\n", SVARG(&var_assign.identifier->name));
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "expression:\n");
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, var_assign.expression);
ast_pretty_printer_rm_indentation(printer);
}
ast_pretty_printer_rm_indentation(printer);
}
break;
}
case AST_VARIABLE: {
ast_variable_t var = ast->data.variable;
ast_pretty_printer_printf(printer, "Variable name='" SVFMT "'\n", SVARG(&var.identifier->name));
break;
}
case AST_UNKOWN_NODE: {
fprintf(printer->stream, "AST_UNKOWN_NODE\n");
break;
}
}
}
static int
ast_pretty_printer_printf(ast_pretty_printer_t *printer, const char *fmt, ...)
{
int ret = 0;
va_list args;
va_start(args, fmt);
ast_pretty_printer_print_indentation(printer);
ret = vprintf(fmt, args);
va_end(args);
return ret;
}
inline static void
ast_pretty_printer_add_indentation(ast_pretty_printer_t *printer)
{
printer->indentation_level++;
printer->indentation_fmt |= 1ULL << printer->indentation_level;
}
inline static void
ast_pretty_printer_rm_indentation(ast_pretty_printer_t *printer)
{
printer->indentation_level--;
}
inline static void
ast_pretty_printer_set_lst_children(ast_pretty_printer_t *printer)
{
printer->indentation_lst_children |= 1ULL << printer->indentation_level;
}
static void
ast_pretty_printer_print_indentation(ast_pretty_printer_t *printer)
{
for (size_t i = 0; i <= printer->indentation_level; ++i) {
if ((printer->indentation_fmt >> i) & 1ULL) {
if (i + 1 > printer->indentation_level && (printer->indentation_lst_children & (1ULL << i))) {
printer->indentation_lst_children ^= (1ULL << i);
printer->indentation_fmt ^= (1ULL << i);
fprintf(printer->stream, "└─ ");
} else if (i + 1 > printer->indentation_level) {
fprintf(printer->stream, "├─ ");
} else {
fprintf(printer->stream, "│ ");
}
} else {
fprintf(printer->stream, " ");
}
}
}
|