update: misc stuff

This commit is contained in:
maix0 2024-09-14 12:55:30 +00:00
parent 392fd77eca
commit 5669430b17
2 changed files with 64 additions and 37 deletions

View file

@ -6,14 +6,15 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:33:11 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 "parser/reduce_action.h"
#include "me/vec/vec_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_reduce_action action;
t_u32 i; t_u32 i;

View file

@ -1,38 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* subtree_balance.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "me/vec/vec_subtree.h"
#include "parser/inner/ptypes.h" #include "parser/inner/ptypes.h"
#include "parser/subtree.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; initial_stack_size = stack->len;
TSSymbol symbol = tree->symbol; tree = self;
for (t_u32 i = 0; i < count; i++) symbol = tree->symbol;
i = 0;
while (i < count)
{ {
if (tree->ref_count > 1 || tree->child_count < 2) if (tree->ref_count > 1 || tree->child_count < 2)
break ; break ;
child = (ts_subtree_children(tree)[0]);
t_subtree child = (ts_subtree_children(tree)[0]); if (child->child_count < 2 || child->ref_count > 1
if (child->child_count < 2 || child->ref_count > 1 || child->symbol != symbol) || child->symbol != symbol)
break ; break ;
grandchild = (ts_subtree_children(child)[0]);
t_subtree grandchild = (ts_subtree_children(child)[0]); if (grandchild->child_count < 2 || grandchild->ref_count > 1
if (grandchild->child_count < 2 || grandchild->ref_count > 1 || grandchild->symbol != symbol) || grandchild->symbol != symbol)
break ; break ;
ts_subtree_children(tree)[0] = (grandchild); 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); ts_subtree_children(grandchild)[grandchild->child_count - 1] = (child);
vec_subtree_push(stack, tree); vec_subtree_push(stack, tree);
tree = grandchild; tree = grandchild;
i++;
} }
while (stack->len > initial_stack_size) while (stack->len > initial_stack_size)
{ {
vec_subtree_pop(stack, &tree); vec_subtree_pop(stack, &tree);
t_subtree child = (ts_subtree_children(tree)[0]); child = (ts_subtree_children(tree)[0]);
t_subtree grandchild = (ts_subtree_children(child)[child->child_count - 1]); grandchild = (ts_subtree_children(child)[child->child_count - 1]);
ts_subtree_summarize_children(grandchild, language); ts_subtree_summarize_children(grandchild, language);
ts_subtree_summarize_children(child, language); ts_subtree_summarize_children(child, language);
ts_subtree_summarize_children(tree, language); ts_subtree_summarize_children(tree, language);
@ -41,40 +61,46 @@ void ts_subtree__compress(t_subtree self, t_u32 count, const t_language *languag
void ts_subtree_balance(t_subtree self, const t_language *language) void ts_subtree_balance(t_subtree self, const t_language *language)
{ {
// return ; t_i64 repeat_delta;
t_vec_subtree tree_stack; t_subtree child1;
t_subtree child2;
t_subtree child;
t_subtree tree; t_subtree tree;
t_u32 i;
t_u32 n;
t_vec_subtree tree_stack;
tree_stack = vec_subtree_new(16, NULL); tree_stack = vec_subtree_new(16, NULL);
if (ts_subtree_child_count(self) > 0 && self->ref_count == 1) if (ts_subtree_child_count(self) > 0 && self->ref_count == 1)
vec_subtree_push(&tree_stack, (self)); vec_subtree_push(&tree_stack, (self));
while (tree_stack.len > 0) while (tree_stack.len > 0)
{ {
vec_subtree_pop(&tree_stack, &tree); vec_subtree_pop(&tree_stack, &tree);
if (tree->repeat_depth > 0) if (tree->repeat_depth > 0)
{ {
t_subtree child1 = ts_subtree_children(tree)[0]; child1 = ts_subtree_children(tree)[0];
t_subtree child2 = ts_subtree_children(tree)[tree->child_count - 1]; 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); repeat_delta = (t_i64)ts_subtree_repeat_depth(child1)
- (t_i64)ts_subtree_repeat_depth(child2);
if (repeat_delta > 0) if (repeat_delta > 0)
{ {
t_u32 n = (t_u32)repeat_delta; n = (t_u32)repeat_delta;
for (t_u32 i = n / 2; i > 0; i /= 2) i = n / 2;
while (i > 0)
{ {
ts_subtree__compress(tree, i, language, &tree_stack); ts_subtree__compress(tree, i, language, &tree_stack);
n -= i; n -= i;
i /= 2;
} }
} }
} }
i = 0;
for (t_u32 i = 0; i < tree->child_count; i++) 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) if (ts_subtree_child_count(child) > 0 && child->ref_count == 1)
vec_subtree_push(&tree_stack, (child)); vec_subtree_push(&tree_stack, (child));
i++;
} }
} }
vec_subtree_free(tree_stack); vec_subtree_free(tree_stack);