Proper pretty printing and error handling on non-cool if condition.
This commit is contained in:
178
AST.jai
178
AST.jai
@@ -72,22 +72,25 @@ AST_Node :: struct {
|
||||
pixel_entry_point : bool;
|
||||
}
|
||||
|
||||
|
||||
// ===========================================================
|
||||
// Pretty printing
|
||||
pretty_print_call :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_call :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "(");
|
||||
append(builder, node.name);
|
||||
if node.children.count > 0 {
|
||||
append(builder, " ");
|
||||
pretty_print_children(node.children[0], indentation, builder, flags = 0);
|
||||
pretty_print_children(node.children[0], indentation, builder, flags = 0, skip_indent = true);
|
||||
}
|
||||
append(builder, ")");
|
||||
}
|
||||
|
||||
pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "[");
|
||||
|
||||
pretty_print_children(node, indentation + 1, builder, flags = .NewLine);
|
||||
@@ -95,8 +98,10 @@ pretty_print_arglist :: (node : *AST_Node, indentation : int, builder : *String_
|
||||
append(builder, "]");
|
||||
}
|
||||
|
||||
pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
|
||||
append(builder, "[");
|
||||
pretty_print_children(node, indentation + 1, builder, flags = .NewLine);
|
||||
@@ -104,14 +109,16 @@ pretty_print_fieldlist :: (node : *AST_Node, indentation : int, builder : *Strin
|
||||
append(builder, "]");
|
||||
}
|
||||
|
||||
pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
print_to_builder(builder, tprint("(:= %", node.name));
|
||||
|
||||
if node.kind != .Unnamed_Field && node.token.ident_value.count > 0 {
|
||||
if node.array_field {
|
||||
append(builder, " [");
|
||||
pretty_print_node(node.children[0], 0, builder);
|
||||
pretty_print_node(node.children[0], indentation, builder, true);
|
||||
append(builder, "].");
|
||||
print_to_builder(builder, "%", node.token.ident_value);
|
||||
} else {
|
||||
@@ -128,28 +135,24 @@ pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Bu
|
||||
|
||||
if !node.array_field && node.children.count > 0 {
|
||||
append(builder, " ");
|
||||
pretty_print_node(node.children[0], indentation, builder);
|
||||
pretty_print_node(node.children[0], indentation, builder, true);
|
||||
}
|
||||
|
||||
append(builder, ")");
|
||||
}
|
||||
|
||||
Children_Print_Flags :: enum_flags {
|
||||
NewLine :: 1 << 0;
|
||||
Separator :: 1 << 1;
|
||||
Space :: 1 << 2;
|
||||
NewLine :: 1 << 0;
|
||||
Separator :: 1 << 1;
|
||||
Space :: 1 << 2;
|
||||
Dont_Skip_Indent_On_First :: 1 << 3;
|
||||
}
|
||||
|
||||
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_block :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
pretty_print_children(node, indentation, builder, flags = .NewLine | .Dont_Skip_Indent_On_First);
|
||||
}
|
||||
|
||||
pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *String_Builder, flags : Children_Print_Flags = .Separator) {
|
||||
pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *String_Builder, flags : Children_Print_Flags = .Separator, skip_indent := false) {
|
||||
if !parent {
|
||||
return;
|
||||
}
|
||||
@@ -162,7 +165,20 @@ pretty_print_children :: (parent : *AST_Node, indentation : int, builder : *Stri
|
||||
|
||||
if !child continue;
|
||||
|
||||
ind := ifx it_index > 0 indentation else 0;
|
||||
|
||||
ind := indentation;
|
||||
if flags & .Dont_Skip_Indent_On_First {
|
||||
ind = indentation;
|
||||
} else {
|
||||
if it_index == 0 {
|
||||
ind = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if skip_indent{
|
||||
ind = 0;
|
||||
}
|
||||
// skip := ifx it_index > 0 then false else true;
|
||||
pretty_print_node(child, ind, builder);
|
||||
|
||||
if it_index != children.count - 1 {
|
||||
@@ -211,8 +227,10 @@ op_to_string :: (oper : Token) -> string {
|
||||
return "";
|
||||
}
|
||||
|
||||
pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "(");
|
||||
op := node.token;
|
||||
|
||||
@@ -225,16 +243,20 @@ pretty_print_binary :: (node : *AST_Node, indentation : int, builder : *String_B
|
||||
append(builder, ")");
|
||||
}
|
||||
|
||||
pretty_print_unary :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_unary :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
op := node.token;
|
||||
|
||||
print_to_builder(builder, op_to_string(op));
|
||||
pretty_print_node(node.children[0], 0, builder);
|
||||
}
|
||||
|
||||
print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "(return ");
|
||||
|
||||
pretty_print_children(node, indentation, builder);
|
||||
@@ -242,8 +264,10 @@ print_return_node :: (node : *AST_Node, indentation : int, builder : *String_Bui
|
||||
append(builder, ")");
|
||||
}
|
||||
|
||||
pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "(if ");
|
||||
|
||||
condition := node.children[0];
|
||||
@@ -251,83 +275,87 @@ pretty_print_if :: (node : *AST_Node, indentation : int, builder : *String_Build
|
||||
append(builder, "\n");
|
||||
|
||||
body := node.children[1];
|
||||
pretty_print_node(body, indentation, builder);
|
||||
pretty_print_node(body, indentation + 4, builder);
|
||||
|
||||
if node.children.count == 3 {
|
||||
append(builder, "\n");
|
||||
pretty_print_node(node.children[2], indentation, builder);
|
||||
pretty_print_node(node.children[2], indentation + 4, builder);
|
||||
}
|
||||
|
||||
append(builder, ")");
|
||||
}
|
||||
|
||||
print_expression_statement :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
print_expression_statement :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
|
||||
if node.children[0] {
|
||||
pretty_print_node(node.children[0], 0, builder);
|
||||
}
|
||||
}
|
||||
|
||||
pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if node.kind == {
|
||||
case .Return; {
|
||||
print_return_node(node, indentation, builder);
|
||||
print_return_node(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .If; {
|
||||
pretty_print_if(node, indentation + 4, builder);
|
||||
pretty_print_if(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Struct;
|
||||
case .ArgList; {
|
||||
pretty_print_arglist(node, indentation + 2, builder);
|
||||
pretty_print_arglist(node, indentation + 2, builder, skip_indent);
|
||||
}
|
||||
case .FieldList; {
|
||||
pretty_print_fieldlist(node, indentation + 2, builder);
|
||||
pretty_print_fieldlist(node, indentation + 2, builder, skip_indent);
|
||||
}
|
||||
case .Field; {
|
||||
pretty_print_field(node, indentation, builder);
|
||||
pretty_print_field(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Unnamed_Field; {
|
||||
pretty_print_field(node, indentation, builder);
|
||||
pretty_print_field(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Block; {
|
||||
pretty_print_block(node, indentation, builder);
|
||||
pretty_print_block(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Binary; {
|
||||
pretty_print_binary(node, indentation, builder);
|
||||
pretty_print_binary(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Unary; {
|
||||
pretty_print_unary(node, indentation, builder);
|
||||
pretty_print_unary(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Variable; {
|
||||
pretty_print_variable(node, indentation, builder);
|
||||
pretty_print_variable(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Expression_Statement; {
|
||||
print_expression_statement(node, indentation, builder);
|
||||
print_expression_statement(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Integer; {
|
||||
print_to_builder(builder, "%", node.integer_value);
|
||||
print_to_builder(builder, "%", node.integer_value, skip_indent);
|
||||
}
|
||||
case .Float; {
|
||||
print_to_builder(builder, "%", node.float_value);
|
||||
print_to_builder(builder, "%", node.float_value, skip_indent);
|
||||
}
|
||||
case .Call; {
|
||||
pretty_print_call(node, indentation, builder);
|
||||
pretty_print_call(node, indentation, builder, skip_indent);
|
||||
}
|
||||
case .Error; {
|
||||
print_to_builder(builder, "(error \"%\")", node.name);
|
||||
print_to_builder(builder, "(error \"%\")", node.name, skip_indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
print_to_builder(builder, "%", node.name);
|
||||
|
||||
for child : node.children {
|
||||
if child.kind == .Variable {
|
||||
append(builder, ".");
|
||||
pretty_print_variable(child, indentation, builder);
|
||||
pretty_print_variable(child, indentation, builder, skip_indent = true);
|
||||
} else if child.kind == .Unary {
|
||||
append(builder, "[");
|
||||
pretty_print_node(child.children[0], 0, builder);
|
||||
@@ -336,8 +364,10 @@ pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String
|
||||
}
|
||||
}
|
||||
|
||||
pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder : *String_Builder) {
|
||||
indent(builder, indentation);
|
||||
pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder : *String_Builder, skip_indent := false) {
|
||||
if !skip_indent {
|
||||
indent(builder, indentation);
|
||||
}
|
||||
append(builder, "(");
|
||||
|
||||
if declaration.foreign_declaration {
|
||||
@@ -385,24 +415,24 @@ pretty_print_declaration :: (declaration : *AST_Node, indentation : int, builder
|
||||
}
|
||||
|
||||
if declaration.children.count > 0 {
|
||||
print_to_builder(builder, "\n");
|
||||
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);
|
||||
// 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, true);
|
||||
// }
|
||||
// } 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, ")");
|
||||
|
||||
Reference in New Issue
Block a user