update: misc stuff
This commit is contained in:
parent
392fd77eca
commit
5669430b17
2 changed files with 64 additions and 37 deletions
|
|
@ -6,14 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
|
|
|||
|
|
@ -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 "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)
|
||||
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)
|
||||
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);
|
||||
|
|
@ -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)
|
||||
{
|
||||
// return ;
|
||||
t_vec_subtree tree_stack;
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue