Upodated stuff

This commit is contained in:
Maieul BOYER 2024-09-05 19:04:57 +00:00
parent c5bc77f467
commit 7401038bf7
5 changed files with 397 additions and 399 deletions

View file

@ -46,6 +46,7 @@ stack/stack_manipulate3 \
stack/stack_node \
stack/stack_summary \
stack/stack_version \
subtree/subtree_balance \
subtree/subtree_funcs \
subtree/subtree_helper \
subtree/subtree_new \

View file

@ -1,5 +1,4 @@
#include "../static/char_set/charset_inline.h"
#include "parser/api.h"
#include "gmr/symbols.h"
bool ts_lex(t_lexer *lexer, t_state_id state)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,81 @@
#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)
{
t_u32 initial_stack_size = stack->len;
t_subtree tree = self;
TSSymbol symbol = tree->symbol;
for (t_u32 i = 0; i < count; i++)
{
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;
ts_subtree_children(tree)[0] = (grandchild);
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;
}
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]);
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)
{
// return ;
t_vec_subtree tree_stack;
t_subtree tree;
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);
if (repeat_delta > 0)
{
t_u32 n = (t_u32)repeat_delta;
for (t_u32 i = n / 2; i > 0; i /= 2)
{
ts_subtree__compress(tree, i, language, &tree_stack);
n -= i;
}
}
}
for (t_u32 i = 0; i < tree->child_count; i++)
{
t_subtree child = ts_subtree_children(tree)[i];
if (ts_subtree_child_count(child) > 0 && child->ref_count == 1)
vec_subtree_push(&tree_stack, (child));
}
}
vec_subtree_free(tree_stack);
}

View file

@ -10,9 +10,13 @@
/* */
/* ************************************************************************** */
#include "fcntl.h"
#include "me/fs/fs.h"
#include "me/os/os.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/printf/printf.h"
#include "unistd.h"
static t_error _pipe_get_fd_slot(struct s_file_slot **read,
struct s_file_slot **write)
@ -28,6 +32,25 @@ static t_error _pipe_get_fd_slot(struct s_file_slot **read,
return (NO_ERROR);
}
static void _pipe_set_close_exec(t_fd *fd)
{
t_string s;
int new_fd;
int fd_perm;
s = string_new(16);
me_printf_str(&s, "/proc/self/fd/%i", fd->fd);
fd_perm = O_RDONLY;
if (fd->perms == FD_WRITE)
fd_perm = O_WRONLY;
new_fd = open(s.buf, fd_perm | O_CLOEXEC);
string_free(s);
if (new_fd == -1)
me_abort("pipe close_on_exec failed");
close(fd->fd);
fd->fd = new_fd;
}
t_error create_pipe(t_pipe *out)
{
int fd[2];
@ -52,5 +75,6 @@ t_error create_pipe(t_pipe *out)
write->slot.fd.name = str_clone("<pipe[write]>");
out->read = &read->slot.fd;
out->write = &write->slot.fd;
(_pipe_set_close_exec(out->read), _pipe_set_close_exec(out->write));
return (NO_ERROR);
}