From 5669430b1733a078fa11354f79910694957ced31 Mon Sep 17 00:00:00 2001 From: maix0 Date: Sat, 14 Sep 2024 12:55:30 +0000 Subject: [PATCH] update: misc stuff --- parser/src/misc/reduce_action.c | 5 +- parser/src/subtree/subtree_balance.c | 96 ++++++++++++++++++---------- 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/parser/src/misc/reduce_action.c b/parser/src/misc/reduce_action.c index 18c98860..a0e6b892 100644 --- a/parser/src/misc/reduce_action.c +++ b/parser/src/misc/reduce_action.c @@ -6,14 +6,15 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/31 17:33:11 by maiboyer #+# #+# */ -/* Updated: 2024/09/06 17:11:17 by maiboyer ### ########.fr */ +/* Updated: 2024/09/14 12:55:11 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser/reduce_action.h" #include "me/vec/vec_reduce_action.h" -void ts_reduce_action_set_add(ReduceActionSet *self, t_reduce_action new_action) +void ts_reduce_action_set_add(\ + ReduceActionSet *self, t_reduce_action new_action) { t_reduce_action action; t_u32 i; diff --git a/parser/src/subtree/subtree_balance.c b/parser/src/subtree/subtree_balance.c index 7e348c85..be790be1 100644 --- a/parser/src/subtree/subtree_balance.c +++ b/parser/src/subtree/subtree_balance.c @@ -1,81 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* subtree_balance.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/14 12:53:20 by maiboyer #+# #+# */ +/* Updated: 2024/09/14 12:53:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "me/vec/vec_subtree.h" #include "parser/inner/ptypes.h" #include "parser/subtree.h" -void ts_subtree__compress(t_subtree self, t_u32 count, const t_language *language, t_vec_subtree *stack) +void ts_subtree__compress(t_subtree self, t_u32 count, + const t_language *language, t_vec_subtree *stack) { - t_u32 initial_stack_size = stack->len; + t_u32 initial_stack_size; + t_subtree tree; + TSSymbol symbol; + t_u32 i; + t_subtree child; + t_subtree grandchild; - t_subtree tree = self; - TSSymbol symbol = tree->symbol; - for (t_u32 i = 0; i < count; i++) + initial_stack_size = stack->len; + tree = self; + symbol = tree->symbol; + i = 0; + while (i < count) { if (tree->ref_count > 1 || tree->child_count < 2) - break; - - t_subtree child = (ts_subtree_children(tree)[0]); - if (child->child_count < 2 || child->ref_count > 1 || child->symbol != symbol) - break; - - t_subtree grandchild = (ts_subtree_children(child)[0]); - if (grandchild->child_count < 2 || grandchild->ref_count > 1 || grandchild->symbol != symbol) - break; - + break ; + child = (ts_subtree_children(tree)[0]); + if (child->child_count < 2 || child->ref_count > 1 + || child->symbol != symbol) + break ; + grandchild = (ts_subtree_children(child)[0]); + if (grandchild->child_count < 2 || grandchild->ref_count > 1 + || grandchild->symbol != symbol) + break ; ts_subtree_children(tree)[0] = (grandchild); - ts_subtree_children(child)[0] = ts_subtree_children(grandchild)[grandchild->child_count - 1]; + ts_subtree_children(child)[0] = ts_subtree_children(grandchild)[\ + grandchild->child_count - 1]; ts_subtree_children(grandchild)[grandchild->child_count - 1] = (child); vec_subtree_push(stack, tree); tree = grandchild; + i++; } - while (stack->len > initial_stack_size) { vec_subtree_pop(stack, &tree); - t_subtree child = (ts_subtree_children(tree)[0]); - t_subtree grandchild = (ts_subtree_children(child)[child->child_count - 1]); + child = (ts_subtree_children(tree)[0]); + grandchild = (ts_subtree_children(child)[child->child_count - 1]); ts_subtree_summarize_children(grandchild, language); ts_subtree_summarize_children(child, language); ts_subtree_summarize_children(tree, language); } } -void ts_subtree_balance(t_subtree self, const t_language *language) +void ts_subtree_balance(t_subtree self, const t_language *language) { - // return ; - t_vec_subtree tree_stack; - t_subtree tree; + t_i64 repeat_delta; + t_subtree child1; + t_subtree child2; + t_subtree child; + t_subtree tree; + t_u32 i; + t_u32 n; + t_vec_subtree tree_stack; tree_stack = vec_subtree_new(16, NULL); - if (ts_subtree_child_count(self) > 0 && self->ref_count == 1) vec_subtree_push(&tree_stack, (self)); - while (tree_stack.len > 0) { vec_subtree_pop(&tree_stack, &tree); - if (tree->repeat_depth > 0) { - t_subtree child1 = ts_subtree_children(tree)[0]; - t_subtree child2 = ts_subtree_children(tree)[tree->child_count - 1]; - t_i64 repeat_delta = (t_i64)ts_subtree_repeat_depth(child1) - (t_i64)ts_subtree_repeat_depth(child2); + child1 = ts_subtree_children(tree)[0]; + child2 = ts_subtree_children(tree)[tree->child_count - 1]; + repeat_delta = (t_i64)ts_subtree_repeat_depth(child1) + - (t_i64)ts_subtree_repeat_depth(child2); if (repeat_delta > 0) { - t_u32 n = (t_u32)repeat_delta; - for (t_u32 i = n / 2; i > 0; i /= 2) + n = (t_u32)repeat_delta; + i = n / 2; + while (i > 0) { ts_subtree__compress(tree, i, language, &tree_stack); n -= i; + i /= 2; } } } - - for (t_u32 i = 0; i < tree->child_count; i++) + i = 0; + while (i < tree->child_count) { - t_subtree child = ts_subtree_children(tree)[i]; + child = ts_subtree_children(tree)[i]; if (ts_subtree_child_count(child) > 0 && child->ref_count == 1) vec_subtree_push(&tree_stack, (child)); + i++; } } vec_subtree_free(tree_stack); -} \ No newline at end of file +}