More print improvements.

This commit is contained in:
2025-01-15 07:15:31 +01:00
parent 41d1dd406d
commit a72a9ff50d
2 changed files with 46 additions and 16 deletions

52
AST.jai
View File

@@ -105,6 +105,7 @@ pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *Strin
}
pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
indent(builder, indentation);
print_to_builder(builder, tprint("(:= %", node.name));
if node.kind != .Unnamed_Field && node.token.ident_value.count > 0 {
@@ -127,7 +128,7 @@ pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Bu
if !node.array_field && node.children.count > 0 {
append(builder, " ");
pretty_print_children(node, indentation, builder);
pretty_print_node(node.children[0], indentation, builder);
}
append(builder, ")");
@@ -139,6 +140,15 @@ Children_Print_Flags :: enum_flags {
Space :: 1 << 2;
}
pretty_print_block :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
for child : node.children {
pretty_print_node(child, indentation, builder);
if it_index != node.children.count - 1 {
append(builder, "\n");
}
}
}
pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *String_Builder, flags : Children_Print_Flags = .Separator) {
if !parent {
return;
@@ -152,7 +162,8 @@ pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *Stri
if !child continue;
pretty_print_node(child, indentation, builder);
ind := ifx it_index > 0 indentation else 0;
pretty_print_node(child, ind, builder);
if it_index != children.count - 1 {
if flags & .Separator {
@@ -208,7 +219,9 @@ pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_B
print_to_builder(builder, op_to_string(op));
append(builder, " ");
pretty_print_children(node, 0, builder, flags = 0);
pretty_print_node(node.children[0], 0, builder);
append(builder, " ");
pretty_print_node(node.children[1], 0, builder);
append(builder, ")");
}
@@ -217,14 +230,14 @@ pretty_print_unary :: (node : *AST_Node, indentation : int, builder : *String_Bu
op := node.token;
print_to_builder(builder, op_to_string(op));
pretty_print_children(node, 0, builder, flags = 0);
pretty_print_node(node.children[0], 0, builder);
}
print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
indent(builder, indentation);
append(builder, "(return ");
pretty_print_children(node, 0, builder);
pretty_print_children(node, indentation, builder);
append(builder, ")");
}
@@ -238,11 +251,11 @@ pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Build
append(builder, "\n");
body := node.children[1];
pretty_print_node(body, indentation + 1, builder);
pretty_print_node(body, indentation, builder);
if node.children.count == 3 {
append(builder, "\n");
pretty_print_node(node.children[2], indentation + 1, builder);
pretty_print_node(node.children[2], indentation, builder);
}
append(builder, ")");
@@ -252,7 +265,7 @@ print_expression_statement :: (node : *AST_Node, indentation : int, builder : *S
indent(builder, indentation);
if node.children[0] {
pretty_print_node(node.children[0], indentation, builder);
pretty_print_node(node.children[0], 0, builder);
}
}
@@ -262,7 +275,7 @@ pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Bui
print_return_node(node, indentation, builder);
}
case .If; {
pretty_print_if(node, indentation + 2, builder);
pretty_print_if(node, indentation + 4, builder);
}
case .Struct;
case .ArgList; {
@@ -278,7 +291,7 @@ pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Bui
pretty_print_field(node, indentation, builder);
}
case .Block; {
pretty_print_children(node, indentation + 2, builder, flags = .NewLine);
pretty_print_block(node, indentation, builder);
}
case .Binary; {
pretty_print_binary(node, indentation, builder);
@@ -369,12 +382,27 @@ pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder
print_to_builder(builder, " (@%)", hint.string_value);
}
}
}
if declaration.children.count > 0 {
print_to_builder(builder, "\n");
pretty_print_children(declaration, indentation + 1, builder, flags = .NewLine);
if declaration.kind == .Function {
field_list := declaration.children[0];
pretty_print_fieldlist(field_list, indentation + 1, builder);
append(builder, "\n");
if declaration.children.count > 1 {
body := declaration.children[1];
pretty_print_node(body, indentation + 1, builder);
}
} else if declaration.kind == .Struct {
pretty_print_node(declaration.children[0], indentation + 1, builder);
} else {
pretty_print_node(declaration.children[0], indentation + 1, builder);
}
// print_to_builder(builder, "\n");
// pretty_print_children(declaration, indentation + 1, builder, flags = .NewLine);
}
append(builder, ")");