summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2023-05-06 01:49:36 +0200
committerCarlos Maniero <carlos@maniero.me>2023-05-06 12:35:06 -0300
commit6a297e0ef57f2ae6d6134bd44a33c55fa9628cfe (patch)
tree93720e9db194e45822e588cf6b1dc9d45e13d464
parent100d9b6c128e47c76dcc86811e89ae07e9b54dbd (diff)
cli: Fix bitwise handling on --ast-dump
In C, literal integers default to a 32-bit size for arithmetic operations. Unfortunately, this was causing incorrect values to be assigned to our uint64_t variables, leading to unexpected behavior. To resolve this issue, we have updated our code to explicitly set the literal size using the "ULL" suffix (unsigned long long). It's important to note that this implementation has a limitation of 64 levels of indentation. Beyond this point, we may encounter a 64-bit overflow. However, at present, we don't anticipate the need to visualize trees that exceed this depth. If this requirement arises in the future, we can explore solutions like dynamically creating new numbers to accommodate larger tree sizes. Overall, this change ensures that our code is functioning correctly and improves the reliability of our codebase. Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r--src/ast_pretty_printer.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/src/ast_pretty_printer.c b/src/ast_pretty_printer.c
index df17763..ec8d2de 100644
--- a/src/ast_pretty_printer.c
+++ b/src/ast_pretty_printer.c
@@ -36,6 +36,9 @@ 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);
@@ -48,8 +51,8 @@ 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_fmt = 0ULL;
+ printer->indentation_lst_children = 0ULL;
printer->indentation_level = 0;
printer->stream = stream;
}
@@ -70,18 +73,18 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
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_set_lst_children(printer);
ast_pretty_printer_printf(printer, "right:\n");
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, binop.right);
ast_pretty_printer_rm_indentation(printer);
@@ -98,7 +101,7 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
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_set_lst_children(printer);
}
ast_pretty_printer_print_ast(printer, vector_at(function.body, i));
}
@@ -110,7 +113,7 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
switch (literal.kind) {
case AST_LITERAL_INTEGER: {
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "Literal type=i32 value='%d'\n", literal.value.integer);
break;
}
@@ -123,7 +126,7 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, return_stmt.argument);
ast_pretty_printer_rm_indentation(printer);
@@ -136,7 +139,7 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, var_decl.value);
ast_pretty_printer_rm_indentation(printer);
@@ -149,12 +152,12 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast)
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_printf(printer, "expression:\n");
ast_pretty_printer_add_indentation(printer);
{
- printer->indentation_lst_children |= 1 << printer->indentation_level;
+ ast_pretty_printer_set_lst_children(printer);
ast_pretty_printer_print_ast(printer, var_assign.expression);
ast_pretty_printer_rm_indentation(printer);
@@ -194,7 +197,7 @@ inline static void
ast_pretty_printer_add_indentation(ast_pretty_printer_t *printer)
{
printer->indentation_level++;
- printer->indentation_fmt |= 1 << printer->indentation_level;
+ printer->indentation_fmt |= 1ULL << printer->indentation_level;
}
inline static void
@@ -203,14 +206,20 @@ 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) & 1) {
- if (i + 1 > printer->indentation_level && (printer->indentation_lst_children & (1 << i))) {
- printer->indentation_lst_children ^= (1 << i);
- printer->indentation_fmt ^= (1 << 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, "├─ ");