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
|
/*
* 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] = '/' };
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);
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 = 0;
printer->indentation_lst_children = 0;
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_BINARY_OPERATION: {
ast_binary_operation_t binop = ast->data.binary_operation;
ast_pretty_printer_printf(printer, "BinaryOperation operation='%c'\n", operation_kinds[binop.kind]);
ast_pretty_printer_add_indentation(printer);
{
ast_pretty_printer_printf(printer, "left:\n");
ast_pretty_printer_add_indentation(printer);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
ast_pretty_printer_print_ast(printer, binop.left);
ast_pretty_printer_rm_indentation(printer);
}
printer->indentation_lst_children |= 1 << printer->indentation_level;
ast_pretty_printer_printf(printer, "right:\n");
ast_pretty_printer_add_indentation(printer);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
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);
for (size_t i = 0; i < function.body->size; ++i) {
if (i + 1 >= function.body->size) {
printer->indentation_lst_children |= 1 << printer->indentation_level;
}
ast_pretty_printer_print_ast(printer, vector_at(function.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: {
printer->indentation_lst_children |= 1 << printer->indentation_level;
ast_pretty_printer_printf(printer, "Literal type=i32 value='%d'\n", literal.value.integer);
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);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
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);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
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);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
ast_pretty_printer_printf(printer, "expression:\n");
ast_pretty_printer_add_indentation(printer);
{
printer->indentation_lst_children |= 1 << printer->indentation_level;
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 |= 1 << printer->indentation_level;
}
inline static void
ast_pretty_printer_rm_indentation(ast_pretty_printer_t *printer)
{
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) & 1) {
if (i + 1 > printer->indentation_level && (printer->indentation_lst_children & (1 << i))) {
printer->indentation_lst_children ^= (1 << i);
printer->indentation_fmt ^= (1 << i);
fprintf(printer->stream, "└─ ");
} else if (i + 1 > printer->indentation_level) {
fprintf(printer->stream, "├─ ");
} else {
fprintf(printer->stream, "│ ");
}
} else {
fprintf(printer->stream, " ");
}
}
}
|