diff --git a/lib/libft/srcs/drawio/srcs/create_elem.c b/lib/libft/srcs/drawio/srcs/create_elem.c index a0b7611..c777863 100644 --- a/lib/libft/srcs/drawio/srcs/create_elem.c +++ b/lib/libft/srcs/drawio/srcs/create_elem.c @@ -31,7 +31,8 @@ int draw_arrow(int fd, int id, t_dio_elem *elem) if (!elem->id_src || !elem->id_dst) return (0); ft_fprintf(fd, "\n", elem->id_src, elem->id_dst); ft_fprintf(fd, "\n\n"); return (id); diff --git a/test/ast/ast.c b/test/ast/ast.c index 56cb958..fe85e0d 100644 --- a/test/ast/ast.c +++ b/test/ast/ast.c @@ -6,7 +6,7 @@ /* By: nalebrun +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */ -/* Updated: 2025/01/24 08:22:16 by nalebrun ### ########.fr */ +/* Updated: 2025/02/01 11:13:58 by nalebrun ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token) { t_nodell *nodell; - nodell = cutll(lst, token->val, 1); + nodell = cutll(lst, token, 1); parrent->left = create_ast_n(nodell->node, parrent); parrent->right = create_ast_n(nodell->next->node, parrent); // free_lltab(sublsts); @@ -35,27 +35,116 @@ void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token) -t_redir get_red() +t_redir is_redir(t_node *node) +{ + if (!node) + return (_NR); + if (!ft_strncmp(node->val, ">", 1)) + return (_RED_R); + if (!ft_strncmp(node->val, ">>", 1)) + return (_RED_DR); + if (!ft_strncmp(node->val, "<", 1)) + return (_RED_L); + return (_NR); +} + +char **lltotab(t_node *lst, t_node *limiter) +{ + char **out; + int count; + t_node *cpy; + + count = 0; + cpy = lst; + while (cpy && cpy != limiter) + { + count++; + cpy = cpy->next; + } + out = ft_calloc(count + 1, sizeof(char *)); + if (!out) + return (NULL); + cpy = lst; + count = 0; + while (cpy && cpy != limiter) + { + out[count] = ft_strdup(cpy->val); + count++; + cpy = cpy->next; + } + out[count] = NULL; + return (out); +} + +t_node *get_redir_node(t_node *head) +{ + t_node *cpy; + + cpy = head; + while (cpy && !is_redir(cpy)) + cpy = cpy->next; + return (cpy); +} void create_cmd(t_ast_n *self, t_node *lst) { + t_node *sec_redir_node; + self->state = _CMD; - self->cmd = ft_strdup(lst->val); - self->args = ft_split("test random val", " "); + self->infile = NULL; + self->outfile = NULL; + if (is_redir(lst) != _NR) + { + self->cmd = ft_strdup(lst->next->next->val); + self->args = lltotab(lst->next->next, NULL); + self->infile = ft_strdup(lst->next->val); + } + else + { + self->cmd = ft_strdup(lst->val); + self->args = lltotab(lst, get_redir_node(lst->next)); + } + sec_redir_node = get_redir_node(lst->next); + if (lst->next && sec_redir_node) + { + self->outfile = ft_strdup(sec_redir_node->next->val); + } + self->redir = is_redir(get_redir_node(lst)); } - - //===================================================================== +void create_pline(t_ast_n *self, t_node *lst, t_node *token) +{ + t_nodell *nodell; + t_nodell *cpy; + int count; + int i; + + nodell = cutll(lst, token, -1); + cpy = nodell; + count = 0; + while (cpy) + { + count++; + cpy = cpy->next; + } + self->pline = ft_calloc(count + 1, sizeof(t_ast_n *)); + cpy = nodell; + i = 0; + while (cpy) + { + self->pline[i] = create_ast_n(cpy->node, self); + cpy = cpy->next; + i++; + } + cpy = NULL; + // free_lltab(sublsts); +} -// void create_pline(t_ast_n *parent, t_node *lst, t_node *token) -// { -// -// } @@ -90,6 +179,7 @@ void remove_parentheses(t_node **lst) void create_subsh(t_ast_n *parent, t_node *lst) { t_node *cpy; + cpy = lst; while (cpy) { @@ -121,20 +211,26 @@ t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent) node = malloc(sizeof(t_ast_n)); token = get_top_token(lst, &node->state); - (void)token; - (void)parent; + ft_debug("================\n"); + ft_debug("NEW NODE CREATED\n"); + if (token && token->val) + ft_debug("token val: %s\n", token->val); + else + ft_debug("token val : NULL\n"); + ft_debug("node state : %d\n", node->state); + ft_debug("================\n\n"); node->left = NULL; node->right = NULL; node->pline = NULL; - // node->parent = parent; pas encore tester + node->parent = parent; if (node->state == _AND || node->state == _OR) create_and_or(node, lst, token); else if (node->state == _SUBSH) create_subsh(node, lst); - // if (node->state == _PLINE) - // create_pline(node, lst, token); + else if (node->state == _PLINE) + create_pline(node, lst, token); else create_cmd(node, lst); diff --git a/test/ast/ast.h b/test/ast/ast.h index 991253c..8ca622e 100644 --- a/test/ast/ast.h +++ b/test/ast/ast.h @@ -63,7 +63,7 @@ typedef struct s_nodell } t_nodell; t_ast_n *get_ast(char **envp, t_node *lst); -t_nodell *cutll(t_node *lst, char *expected, size_t limiter); +t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter); t_node *get_top_token(t_node *lst, t_state *state); // env TMP diff --git a/test/ast/cutll.c b/test/ast/cutll.c index 9cf0edc..aa98125 100644 --- a/test/ast/cutll.c +++ b/test/ast/cutll.c @@ -31,12 +31,17 @@ static void add_nodell(t_nodell **nodell, t_node *node) tmp->next->next = NULL; } -static t_node *get_node(t_node **lst, char *expected) +static t_node *get_node(t_node **lst, t_node *expected, int limiter) { t_node *node; node = NULL; - while ((*lst) && ft_strncmp((*lst)->val, expected, ft_strlen(expected))) + while (limiter != -1 && (*lst) && (*lst) != expected) + { + add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision); + (*lst) = (*lst)->next; + } + while (limiter == -1 && (*lst) && ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val))) { add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision); (*lst) = (*lst)->next; @@ -44,7 +49,7 @@ static t_node *get_node(t_node **lst, char *expected) return (node); } -t_nodell *cutll(t_node *lst, char *expected, size_t limiter) +t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter) { t_nodell *out; t_node *node; @@ -54,7 +59,7 @@ t_nodell *cutll(t_node *lst, char *expected, size_t limiter) out = NULL; while (i <= limiter) { - node = get_node(&lst, expected); + node = get_node(&lst, expected, limiter); if (!node) break; add_nodell(&out, node);