From e918139f88317a72d9320137f633ff967f7ea3ae Mon Sep 17 00:00:00 2001 From: servostar Date: Fri, 26 Apr 2024 15:37:17 +0200 Subject: [PATCH] added recurse --- src/ast/ast.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ast/ast.c b/src/ast/ast.c index 3a1a8fc..db600ee 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -57,3 +57,20 @@ struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, size_t idx) { void AST_delete_node(struct AST_Node_t *_) { #warning "FIXME: not implemented" } + +static void __AST_visit_nodes_recurse2(struct AST_Node_t *root, + void (*for_each)(struct AST_Node_t *node, + size_t depth), + size_t depth) { + (for_each)(root, 0); + + for (size_t i = 0; i < root->child_count; i++) { + __AST_visit_nodes_recurse2(root->children[i], for_each, depth + 1); + } +} + +void AST_visit_nodes_recurse(struct AST_Node_t *root, + void (*for_each)(struct AST_Node_t *node, + size_t depth)) { + __AST_visit_nodes_recurse2(root, for_each, 0); +}