about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2025-06-30 02:53:10 +0200
committerMel <mel@rnrd.eu>2025-06-30 02:53:10 +0200
commit1e2b456f08a5d00c8cbe290fb7974818e8f17b46 (patch)
tree5c267daec7df47ca790f96cb951e04dfbaeb6dc5
parentb16cf0fc98940554ef0febfe443ef690618d5083 (diff)
downloadcatskill-1e2b456f08a5d00c8cbe290fb7974818e8f17b46.tar.zst
catskill-1e2b456f08a5d00c8cbe290fb7974818e8f17b46.zip
Re-format FOR_EACH macros as control structures
Signed-off-by: Mel <mel@rnrd.eu>
-rw-r--r--.clang-format3
-rw-r--r--boot/common.c13
-rw-r--r--boot/tree.c29
-rw-r--r--boot/visit.c57
4 files changed, 35 insertions, 67 deletions
diff --git a/.clang-format b/.clang-format
index 577bf60..40bac45 100644
--- a/.clang-format
+++ b/.clang-format
@@ -16,4 +16,7 @@ ContinuationIndentWidth: 4
 IndentCaseLabels: false
 IndentWidth: 4
 PenaltyIndentedWhitespace: 100
+ForEachMacros:
+  - FOR_EACH
+  - STRING_ARRAY_FOR_EACH
 ...
diff --git a/boot/common.c b/boot/common.c
index 8291a51..318edf7 100644
--- a/boot/common.c
+++ b/boot/common.c
@@ -75,10 +75,10 @@ failure(const ascii* message)
 {
     fflush(stdout); // flush stdout to ensure any message is printed before the error.
 
-    const ascii* format =
-        ANSI_RED ANSI_BOLD ";( sorry, a failure has occurred..." ANSI_NO_BOLD "\n"
-        "-> %s!" "\n"
-        ANSI_RESET;
+    const ascii* format = ANSI_RED ANSI_BOLD
+        ";( sorry, a failure has occurred..." ANSI_NO_BOLD "\n"
+        "-> %s!"
+        "\n" ANSI_RESET;
     fprintf(stderr, format, message);
 
     exit(EXIT_FAILURE);
@@ -92,8 +92,7 @@ check(bool condition, const ascii* message)
 }
 
 // for each entry in a linked list.
-#define FOR_EACH(type, cursor, head) \
-    for (type cursor = head; cursor != nil; cursor = cursor->next)
+#define FOR_EACH(type, cursor, head) for (type cursor = head; cursor != nil; cursor = cursor->next)
 
 // the common size of region memory blocks.
 #define REGION_SIZE 65536
@@ -239,7 +238,7 @@ string_array_at(const struct String_Array* array, uint index)
 }
 
 #define STRING_ARRAY_FOR_EACH(cursor, str, array) \
-    struct String str = array.strings[0]; \
+    struct String str = array.strings[0];         \
     for (uint cursor = 0; cursor < array.count; str = array.strings[++cursor])
 
 // single iteration of the CRC32 checksum algorithm
diff --git a/boot/tree.c b/boot/tree.c
index 1fcaa62..2dcfdd2 100644
--- a/boot/tree.c
+++ b/boot/tree.c
@@ -595,8 +595,7 @@ type_node_print(const struct Type_Node* type_node)
         break;
     case TYPE_NODE_TUPLE: {
         printf("tuple");
-        FOR_EACH(struct Type_Node*, current, type_node->value.tuple.head)
-        {
+        FOR_EACH (struct Type_Node*, current, type_node->value.tuple.head) {
             printf(" ");
             type_node_print(current);
         }
@@ -615,8 +614,7 @@ type_node_print(const struct Type_Node* type_node)
     }
     case TYPE_NODE_STRUCTURE: {
         printf("structure");
-        FOR_EACH(struct Type_Node*, current, type_node->value.structure.fields)
-        {
+        FOR_EACH (struct Type_Node*, current, type_node->value.structure.fields) {
             printf(" (field %s) ", current->value_name.data);
             type_node_print(current);
         }
@@ -624,8 +622,7 @@ type_node_print(const struct Type_Node* type_node)
     }
     case TYPE_NODE_VARIANT: {
         printf("variant");
-        FOR_EACH(struct Type_Node*, current, type_node->value.variant.variants)
-        {
+        FOR_EACH (struct Type_Node*, current, type_node->value.variant.variants) {
             if (type_node_is_none(current)) {
                 printf(" (variant %s)", current->value_name.data);
             } else {
@@ -638,8 +635,7 @@ type_node_print(const struct Type_Node* type_node)
     }
     case TYPE_NODE_CLASS:
         printf("class");
-        FOR_EACH(struct Type_Node*, current, type_node->value.class.methods)
-        {
+        FOR_EACH (struct Type_Node*, current, type_node->value.class.methods) {
             check(current->type == TYPE_NODE_FUNCTION,
                   "expected class method type node to be a function type");
             printf(" (method %s ", current->value_name.data);
@@ -811,8 +807,7 @@ expression_new(
 void
 function_header_node_print(const struct Function_Header_Node* header)
 {
-    FOR_EACH(struct Type_Node*, current, header->parameters_type_and_name)
-    {
+    FOR_EACH (struct Type_Node*, current, header->parameters_type_and_name) {
         if (current != header->parameters_type_and_name) printf(" ");
 
         if (!string_is_empty(current->value_name))
@@ -876,8 +871,7 @@ expression_print(const struct Expression* expression)
         printf("(call/construct ");
         expression_print(coc->subject);
         uint i = 0;
-        FOR_EACH(struct Expression*, argument, coc->arguments)
-        {
+        FOR_EACH (struct Expression*, argument, coc->arguments) {
             struct String name = string_array_at(&coc->argument_names, i++);
             if (!string_is_empty(name)) {
                 printf(" (named arg '%s' ", name.data);
@@ -1062,8 +1056,7 @@ void
 block_node_print(const struct Block_Node* block)
 {
     printf("(block \n");
-    FOR_EACH(struct Statement*, statement, block->statements)
-    {
+    FOR_EACH (struct Statement*, statement, block->statements) {
         printf("\t");
         statement_print(statement);
         printf("\n");
@@ -1075,10 +1068,7 @@ void
 bare_declaration_node_print(const struct Bare_Declaration_Node* declaration)
 {
     printf("(declaration ");
-    STRING_ARRAY_FOR_EACH(i, name, declaration->names)
-    {
-        printf("%s ", name.data);
-    }
+    STRING_ARRAY_FOR_EACH (i, name, declaration->names) { printf("%s ", name.data); }
     if (!type_node_is_none(declaration->type)) {
         type_node_print(declaration->type);
         printf(" ");
@@ -1230,8 +1220,7 @@ struct Tree
 void
 tree_print(const struct Tree* tree)
 {
-    FOR_EACH(struct Statement*, statement, tree->top_level_statements)
-    {
+    FOR_EACH (struct Statement*, statement, tree->top_level_statements) {
         statement_print(statement);
         printf("\n");
     }
diff --git a/boot/visit.c b/boot/visit.c
index 396f8b8..c685cf6 100644
--- a/boot/visit.c
+++ b/boot/visit.c
@@ -81,8 +81,7 @@ walk(struct Visit* visit, struct Tree* tree)
 void
 walk_tree(struct Visit* visit, struct Tree* tree)
 {
-    FOR_EACH(struct Statement*, statement, tree->top_level_statements)
-    {
+    FOR_EACH (struct Statement*, statement, tree->top_level_statements) {
         VISIT(visit_statement, statement);
     }
 }
@@ -253,10 +252,7 @@ walk_expression_call_or_construct(struct Visit* visit, struct Expression* expres
 {
     struct Expression_Call_Or_Construct* coc = &expression->value.call_or_construct;
     VISIT(visit_expression, coc->subject);
-    FOR_EACH(struct Expression*, argument, coc->arguments)
-    {
-        VISIT(visit_expression, argument);
-    }
+    FOR_EACH (struct Expression*, argument, coc->arguments) { VISIT(visit_expression, argument); }
 }
 
 void
@@ -349,8 +345,7 @@ walk_type_node_maybe(struct Visit* visit, struct Type_Node* node)
 void
 walk_type_node_tuple(struct Visit* visit, struct Type_Node* node)
 {
-    FOR_EACH(struct Type_Node*, current, node->value.tuple.head)
-    {
+    FOR_EACH (struct Type_Node*, current, node->value.tuple.head) {
         VISIT(visit_type_node, current);
     }
 }
@@ -371,8 +366,7 @@ walk_type_node_function(struct Visit* visit, struct Type_Node* node)
 void
 walk_type_node_structure(struct Visit* visit, struct Type_Node* node)
 {
-    FOR_EACH(struct Type_Node*, field, node->value.structure.fields)
-    {
+    FOR_EACH (struct Type_Node*, field, node->value.structure.fields) {
         VISIT(visit_type_node, field);
     }
 }
@@ -380,8 +374,7 @@ walk_type_node_structure(struct Visit* visit, struct Type_Node* node)
 void
 walk_type_node_variant(struct Visit* visit, struct Type_Node* node)
 {
-    FOR_EACH(struct Type_Node*, method, node->value.variant.variants)
-    {
+    FOR_EACH (struct Type_Node*, method, node->value.variant.variants) {
         VISIT(visit_type_node, method);
     }
 }
@@ -389,8 +382,7 @@ walk_type_node_variant(struct Visit* visit, struct Type_Node* node)
 void
 walk_type_node_class(struct Visit* visit, struct Type_Node* node)
 {
-    FOR_EACH(struct Type_Node*, method, node->value.class.methods)
-    {
+    FOR_EACH (struct Type_Node*, method, node->value.class.methods) {
         VISIT(visit_type_node, method);
     }
 }
@@ -398,10 +390,7 @@ walk_type_node_class(struct Visit* visit, struct Type_Node* node)
 void
 walk_block_node(struct Visit* visit, struct Block_Node* node)
 {
-    FOR_EACH(struct Statement*, statement, node->statements)
-    {
-        VISIT(visit_statement, statement);
-    }
+    FOR_EACH (struct Statement*, statement, node->statements) { VISIT(visit_statement, statement); }
 }
 
 void
@@ -414,8 +403,7 @@ walk_bare_declaration_node(struct Visit* visit, struct Bare_Declaration_Node* no
 void
 walk_function_header_node(struct Visit* visit, struct Function_Header_Node* header)
 {
-    FOR_EACH(struct Type_Node*, param_type, header->parameters_type_and_name)
-    {
+    FOR_EACH (struct Type_Node*, param_type, header->parameters_type_and_name) {
         VISIT(visit_type_node, param_type);
     }
     VISIT_MAYBE(visit_type_node, header->return_type);
@@ -505,8 +493,7 @@ void
 printer_visit_tree(struct Visit* visit, struct Tree* tree)
 {
     DATA_FOR_VISIT(struct Tree_Printer, printer);
-    FOR_EACH(struct Statement*, statement, tree->top_level_statements)
-    {
+    FOR_EACH (struct Statement*, statement, tree->top_level_statements) {
         VISIT(visit_statement, statement);
         fprintf(printer->output, "\n");
     }
@@ -730,8 +717,7 @@ printer_visit_expression_call_or_construct(struct Visit* visit, struct Expressio
     VISIT(visit_expression, coc->subject);
 
     uint i = 0;
-    FOR_EACH(struct Expression*, argument, coc->arguments)
-    {
+    FOR_EACH (struct Expression*, argument, coc->arguments) {
         struct String name = string_array_at(&coc->argument_names, i++);
         if (name.data && name.data[0] != '\0') {
             fprintf(printer->output, " (named arg '%s' ", name.data);
@@ -842,8 +828,7 @@ printer_visit_type_node_tuple(struct Visit* visit, struct Type_Node* node)
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
     fprintf(printer->output, "tuple");
-    FOR_EACH(struct Type_Node*, current, node->value.tuple.head)
-    {
+    FOR_EACH (struct Type_Node*, current, node->value.tuple.head) {
         fprintf(printer->output, " ");
         VISIT(visit_type_node, current);
     }
@@ -875,8 +860,7 @@ printer_visit_type_node_structure(struct Visit* visit, struct Type_Node* node)
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
     fprintf(printer->output, "structure");
-    FOR_EACH(struct Type_Node*, current, node->value.structure.fields)
-    {
+    FOR_EACH (struct Type_Node*, current, node->value.structure.fields) {
         fprintf(printer->output, " (field %s) ", current->value_name.data);
         VISIT(visit_type_node, current);
     }
@@ -888,8 +872,7 @@ printer_visit_type_node_variant(struct Visit* visit, struct Type_Node* node)
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
     fprintf(printer->output, "variant");
-    FOR_EACH(struct Type_Node*, current, node->value.variant.variants)
-    {
+    FOR_EACH (struct Type_Node*, current, node->value.variant.variants) {
         if (current->type == TYPE_NODE_NONE) {
             fprintf(printer->output, " (variant %s)", current->value_name.data);
         } else {
@@ -906,8 +889,7 @@ printer_visit_type_node_class(struct Visit* visit, struct Type_Node* node)
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
     fprintf(printer->output, "class");
-    FOR_EACH(struct Type_Node*, current, node->value.class.methods)
-    {
+    FOR_EACH (struct Type_Node*, current, node->value.class.methods) {
         fprintf(printer->output, " (method %s ", current->value_name.data);
         VISIT(visit_function_header_node, &current->value.function.header);
         fprintf(printer->output, ")");
@@ -921,8 +903,7 @@ printer_visit_block_node(struct Visit* visit, struct Block_Node* node)
 
     fprintf(printer->output, "(block \n");
     printer->indentation_level++;
-    FOR_EACH(struct Statement*, statement, node->statements)
-    {
+    FOR_EACH (struct Statement*, statement, node->statements) {
         VISIT(visit_statement, statement);
         fprintf(printer->output, "\n");
     }
@@ -937,10 +918,7 @@ printer_visit_bare_declaration_node(struct Visit* visit, struct Bare_Declaration
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
     fprintf(printer->output, "(declaration ");
-    STRING_ARRAY_FOR_EACH(i, name, node->names)
-    {
-        fprintf(printer->output, "%s ", name.data);
-    }
+    STRING_ARRAY_FOR_EACH (i, name, node->names) { fprintf(printer->output, "%s ", name.data); }
 
     if (node->type && node->type->type != TYPE_NODE_NONE) {
         VISIT(visit_type_node, node->type);
@@ -960,8 +938,7 @@ printer_visit_function_header(struct Visit* visit, struct Function_Header_Node*
 {
     DATA_FOR_VISIT(struct Tree_Printer, printer);
 
-    FOR_EACH(struct Type_Node*, current, header->parameters_type_and_name)
-    {
+    FOR_EACH (struct Type_Node*, current, header->parameters_type_and_name) {
         if (current != header->parameters_type_and_name) fprintf(printer->output, " ");
 
         if (current->value_name.data && current->value_name.data[0] != '\0')