about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2026-05-03 22:38:34 +0200
committerMel <mel@rnrd.eu>2026-05-03 22:38:34 +0200
commit13f5c18441b34427722e4869cd8362b6c38075de (patch)
treeb338ad3915e35636fdc9e038664958c3747794e6
parent56c0c620078b906a1994e0833c522913928b37aa (diff)
downloadcatskill-13f5c18441b34427722e4869cd8362b6c38075de.tar.zst
catskill-13f5c18441b34427722e4869cd8362b6c38075de.zip
Macro for empty leaf walking for IR visiting, re-format
Signed-off-by: Mel <mel@rnrd.eu>
-rw-r--r--boot/visit/ir.c90
1 files changed, 50 insertions, 40 deletions
diff --git a/boot/visit/ir.c b/boot/visit/ir.c
index 13ea529..b2239d5 100644
--- a/boot/visit/ir.c
+++ b/boot/visit/ir.c
@@ -13,6 +13,9 @@
 
 #include "../catboot.h"
 
+#define WALK_LEAF_FUNCTION(name, type) \
+    void name(struct Visit* visit, type node) {}
+
 struct Visit
 {
     struct Visit_Table* table;
@@ -75,9 +78,7 @@ walk(struct Visit* visit, struct Unit* unit)
 void
 walk_unit(struct Visit* visit, struct Unit* unit)
 {
-    FOR_EACH_ARRAY(struct Type*, type, &unit->types.entries, {
-        VISIT(visit_type, type);
-    })
+    FOR_EACH_ARRAY(struct Type*, type, &unit->types.entries, { VISIT(visit_type, type); })
     FOR_EACH_ARRAY(struct Function*, function, &unit->functions.entries, {
         VISIT(visit_function, function);
     })
@@ -115,8 +116,8 @@ walk_type(struct Visit* visit, struct Type* type)
     }
 }
 
-void walk_type_primitive(struct Visit* visit, struct Type* type) {}
-void walk_type_alias(struct Visit* visit, struct Type* type) {}
+WALK_LEAF_FUNCTION(walk_type_primitive, struct Type*);
+WALK_LEAF_FUNCTION(walk_type_alias, struct Type*);
 
 void
 walk_type_structure(struct Visit* visit, struct Type* type)
@@ -131,7 +132,8 @@ void
 walk_type_variant(struct Visit* visit, struct Type* type)
 {
     for (uint i = 0; i < array_length(&type->value.variant.cases); ++i) {
-        struct Variant_Case* one_case = array_at(struct Variant_Case, &type->value.variant.cases, i);
+        struct Variant_Case* one_case =
+            array_at(struct Variant_Case, &type->value.variant.cases, i);
         if (one_case->has_payload) VISIT(visit_type_ref, &one_case->payload);
     }
 }
@@ -244,10 +246,10 @@ walk_statement_return(struct Visit* visit, struct Statement* stmt)
     VISIT_MAYBE(visit_expression, stmt->value.return_value.value);
 }
 
-void walk_statement_break(struct Visit* visit, struct Statement* stmt) {}
-void walk_statement_continue(struct Visit* visit, struct Statement* stmt) {}
-void walk_statement_label(struct Visit* visit, struct Statement* stmt) {}
-void walk_statement_goto(struct Visit* visit, struct Statement* stmt) {}
+WALK_LEAF_FUNCTION(walk_statement_break, struct Statement*);
+WALK_LEAF_FUNCTION(walk_statement_continue, struct Statement*);
+WALK_LEAF_FUNCTION(walk_statement_label, struct Statement*);
+WALK_LEAF_FUNCTION(walk_statement_goto, struct Statement*);
 
 void
 walk_statement_block(struct Visit* visit, struct Statement* stmt)
@@ -305,11 +307,11 @@ walk_expression(struct Visit* visit, struct Expression* expr)
     }
 }
 
-void walk_expression_integer_literal(struct Visit* visit, struct Expression* expr) {}
-void walk_expression_float_literal(struct Visit* visit, struct Expression* expr) {}
-void walk_expression_string_literal(struct Visit* visit, struct Expression* expr) {}
-void walk_expression_boolean_literal(struct Visit* visit, struct Expression* expr) {}
-void walk_expression_name(struct Visit* visit, struct Expression* expr) {}
+WALK_LEAF_FUNCTION(walk_expression_integer_literal, struct Expression*);
+WALK_LEAF_FUNCTION(walk_expression_float_literal, struct Expression*);
+WALK_LEAF_FUNCTION(walk_expression_string_literal, struct Expression*);
+WALK_LEAF_FUNCTION(walk_expression_boolean_literal, struct Expression*);
+WALK_LEAF_FUNCTION(walk_expression_name, struct Expression*);
 
 void
 walk_expression_unary_operation(struct Visit* visit, struct Expression* expr)
@@ -363,22 +365,18 @@ void
 walk_expression_construct(struct Visit* visit, struct Expression* expr)
 {
     for (uint i = 0; i < array_length(&expr->value.construct.fields); ++i) {
-        struct Construct_Field* field = array_at(struct Construct_Field, &expr->value.construct.fields, i);
+        struct Construct_Field* field =
+            array_at(struct Construct_Field, &expr->value.construct.fields, i);
         VISIT(visit_expression, field->value);
     }
 }
 
-void
-walk_block(struct Visit* visit, struct Block* block)
-{
-    FOR_EACH_ARRAY(struct Statement*, statement, &block->statements, {
-        VISIT(visit_statement, statement);
-    })
-}
+void walk_block(struct Visit* visit, struct Block* block){ FOR_EACH_ARRAY(
+    struct Statement*, statement, &block -> statements, { VISIT(visit_statement, statement); }) }
 
-void walk_type_ref(struct Visit* visit, struct Type_Ref* ref) {}
-void walk_import(struct Visit* visit, struct Import* import) {}
-void walk_diagnostic(struct Visit* visit, struct Diagnostic* diagnostic) {}
+WALK_LEAF_FUNCTION(walk_type_ref, struct Type_Ref*);
+WALK_LEAF_FUNCTION(walk_import, struct Import*);
+WALK_LEAF_FUNCTION(walk_diagnostic, struct Diagnostic*);
 
 struct Visit_Table walk_functions = {
     .visit_unit = walk_unit,
@@ -447,8 +445,8 @@ struct Printer
     struct Unit* unit;
 };
 
-#define PRINTER_PREAMBLE                  \
-    DATA_FOR_VISIT(struct Printer, p);    \
+#define PRINTER_PREAMBLE               \
+    DATA_FOR_VISIT(struct Printer, p); \
     p->did_print_last_visit = false;
 
 void
@@ -562,8 +560,10 @@ printer_visit_type_ref(struct Visit* visit, struct Type_Ref* ref)
         if (mod == TYPE_MOD_REFERENCE) PRINT(" &");
     }
     struct Type* target = printer_type_at(p, ref->type_id);
-    if (target) PRINT(" %s", string_c_str(target->name));
-    else PRINT(" #%lu", ref->type_id);
+    if (target)
+        PRINT(" %s", string_c_str(target->name));
+    else
+        PRINT(" #%lu", ref->type_id);
     PRINT(")");
 }
 
@@ -592,8 +592,10 @@ printer_visit_type_alias(struct Visit* visit, struct Type* type)
 
     PRINT(" alias");
     struct Type* target = printer_type_at(p, type->value.alias.target_id);
-    if (target) PRINT(" %s", string_c_str(target->name));
-    else PRINT(" #%lu", type->value.alias.target_id);
+    if (target)
+        PRINT(" %s", string_c_str(target->name));
+    else
+        PRINT(" #%lu", type->value.alias.target_id);
 }
 
 void
@@ -617,7 +619,8 @@ printer_visit_type_variant(struct Visit* visit, struct Type* type)
 
     PRINT(" variant");
     for (uint i = 0; i < array_length(&type->value.variant.cases); ++i) {
-        struct Variant_Case* one_case = array_at(struct Variant_Case, &type->value.variant.cases, i);
+        struct Variant_Case* one_case =
+            array_at(struct Variant_Case, &type->value.variant.cases, i);
         PRINT(" (case %s tag=%u", string_c_str(one_case->name), one_case->tag);
         if (one_case->has_payload) {
             PRINT(" ");
@@ -920,12 +923,17 @@ printer_visit_expression_construct(struct Visit* visit, struct Expression* expr)
 
     PRINT("(construct");
     struct Type* type = printer_type_at(p, expr->value.construct.type_id);
-    if (type) PRINT(" %s", string_c_str(type->name));
-    else PRINT(" #%lu", expr->value.construct.type_id);
+    if (type)
+        PRINT(" %s", string_c_str(type->name));
+    else
+        PRINT(" #%lu", expr->value.construct.type_id);
     for (uint i = 0; i < array_length(&expr->value.construct.fields); ++i) {
-        struct Construct_Field* field = array_at(struct Construct_Field, &expr->value.construct.fields, i);
-        if (field->name.length > 0) PRINT(" (field %s ", string_c_str(field->name));
-        else PRINT(" (positional ");
+        struct Construct_Field* field =
+            array_at(struct Construct_Field, &expr->value.construct.fields, i);
+        if (field->name.length > 0)
+            PRINT(" (field %s ", string_c_str(field->name));
+        else
+            PRINT(" (positional ");
         VISIT(visit_expression, field->value);
         PRINT(")");
     }
@@ -963,8 +971,10 @@ printer_visit_diagnostic(struct Visit* visit, struct Diagnostic* diagnostic)
 {
     PRINTER_PREAMBLE
     const ascii* sev = "note";
-    if (diagnostic->severity == DIAGNOSTIC_WARNING) sev = "warning";
-    else if (diagnostic->severity == DIAGNOSTIC_ERROR) sev = "error";
+    if (diagnostic->severity == DIAGNOSTIC_WARNING)
+        sev = "warning";
+    else if (diagnostic->severity == DIAGNOSTIC_ERROR)
+        sev = "error";
     PRINT("(%s \"%s\")", sev, string_c_str(diagnostic->message));
 }