gros merge
This commit is contained in:
231
srcs/parsing/ast/ast.c
Normal file
231
srcs/parsing/ast/ast.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/01 11:13:58 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
// ===================================================================
|
||||
|
||||
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent);
|
||||
|
||||
// ====================================================================
|
||||
|
||||
|
||||
void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh)
|
||||
{
|
||||
t_nodell *nodell;
|
||||
|
||||
nodell = cutll(lst, token, 1);
|
||||
parrent->left = create_ast_n(nodell->node, parrent, msh);
|
||||
parrent->right = create_ast_n(nodell->next->node, parrent, msh);
|
||||
// free_lltab(sublsts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ===================================================================
|
||||
|
||||
|
||||
|
||||
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->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_msh *msh)
|
||||
{
|
||||
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, t_msh *msh);
|
||||
cpy = cpy->next;
|
||||
i++;
|
||||
}
|
||||
cpy = NULL;
|
||||
// free_lltab(sublsts);
|
||||
}
|
||||
|
||||
//======================================================================
|
||||
|
||||
|
||||
|
||||
void remove_parentheses(t_node **lst)
|
||||
{
|
||||
t_node *tmp;
|
||||
t_node *cpy;
|
||||
|
||||
if (!lst || !*lst || !(*lst)->next)
|
||||
return;
|
||||
|
||||
tmp = *lst;
|
||||
*lst = (*lst)->next;
|
||||
free(tmp->val);
|
||||
free(tmp);
|
||||
cpy = *lst;
|
||||
while (cpy->next && cpy->next->next)
|
||||
cpy = cpy->next;
|
||||
if (cpy->next)
|
||||
{
|
||||
free(cpy->next->val);
|
||||
free(cpy->next);
|
||||
cpy->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh)
|
||||
{
|
||||
t_node *cpy;
|
||||
|
||||
cpy = lst;
|
||||
while (cpy)
|
||||
{
|
||||
ft_printf("%s\n", cpy->val);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
remove_parentheses(&lst);
|
||||
ft_printf("\n\n");
|
||||
cpy = lst;
|
||||
while (cpy)
|
||||
{
|
||||
ft_printf("%s\n", cpy->val);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
parent->left = create_ast_n(lst, parent, msh);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//==================================================================
|
||||
|
||||
|
||||
|
||||
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh)
|
||||
{
|
||||
t_ast_n *node;
|
||||
t_node *token;
|
||||
|
||||
node = malloc(sizeof(t_ast_n));
|
||||
token = get_top_token(lst, &node->state);
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->pline = NULL;
|
||||
node->msh = msh;
|
||||
node->parent = parent;
|
||||
if (node->state == _AND || node->state == _OR)
|
||||
create_and_or(node, lst, token, msh);
|
||||
else if (node->state == _SUBSH)
|
||||
create_subsh(node, lst, msh);
|
||||
else if (node->state == _PLINE)
|
||||
create_pline(node, lst, token, msh);
|
||||
else
|
||||
create_cmd(node, lst);
|
||||
return (node);
|
||||
}
|
||||
|
||||
t_ast_n *get_ast(t_msh *msh, t_node *lst)
|
||||
{
|
||||
t_ast_n *head;
|
||||
|
||||
head = create_ast_n(lst, NULL, msh);
|
||||
return (head);
|
||||
}
|
||||
74
srcs/parsing/ast/cutll.c
Normal file
74
srcs/parsing/ast/cutll.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cutll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 09:56:34 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/31 14:05:12 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
static void add_nodell(t_nodell **nodell, t_node *node)
|
||||
{
|
||||
t_nodell *tmp;
|
||||
|
||||
if (!nodell || !(*nodell))
|
||||
{
|
||||
*nodell = malloc(sizeof(t_nodell));
|
||||
(*nodell)->node = node;
|
||||
(*nodell)->next = NULL;
|
||||
return ;
|
||||
}
|
||||
tmp = *nodell;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = malloc(sizeof(t_nodell));
|
||||
tmp->next->node = node;
|
||||
tmp->next->next = NULL;
|
||||
}
|
||||
|
||||
static t_node *get_node(t_node **lst, t_node *expected, int limiter)
|
||||
{
|
||||
t_node *node;
|
||||
|
||||
node = NULL;
|
||||
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;
|
||||
}
|
||||
return (node);
|
||||
}
|
||||
|
||||
t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter)
|
||||
{
|
||||
t_nodell *out;
|
||||
t_node *node;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
out = NULL;
|
||||
while (i <= limiter)
|
||||
{
|
||||
node = get_node(&lst, expected, limiter);
|
||||
if (!node)
|
||||
break;
|
||||
add_nodell(&out, node);
|
||||
t_nodell *tmp = out;
|
||||
while (tmp)
|
||||
tmp = tmp->next;
|
||||
if(lst && lst->next)
|
||||
lst = lst->next;
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
76
srcs/parsing/ast/top_token.c
Normal file
76
srcs/parsing/ast/top_token.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* top_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 17:14:26 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/31 17:14:26 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
static int last_tok_subsh(t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if (lst->next == NULL && !ft_strncmp(lst->val, ")", 1))
|
||||
return (1);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void skip_parentheses(t_node **lst)
|
||||
{
|
||||
if (!ft_strncmp((*lst)->val, "(", 1))
|
||||
{
|
||||
while ((*lst)->next && ft_strncmp((*lst)->next->val, ")", 1))
|
||||
{
|
||||
if (!ft_strncmp((*lst)->val, "(", 1))
|
||||
skip_parentheses(&(*lst)->next);
|
||||
*lst = (*lst)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static t_node *find_token(char *tok, t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
skip_parentheses(&lst);
|
||||
if (!ft_strncmp(lst->val, tok, ft_strlen(tok)))
|
||||
return (lst);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_node *get_top_token(t_node *lst, t_state *state)
|
||||
{
|
||||
*state = _SUBSH;
|
||||
if (!ft_strncmp(lst->val, "(", 1) && last_tok_subsh(lst))
|
||||
return (lst);
|
||||
else if (find_token("&&", lst))
|
||||
{
|
||||
*state = _AND;
|
||||
return (find_token("&&", lst));
|
||||
}
|
||||
else if (find_token("||", lst))
|
||||
{
|
||||
*state = _OR;
|
||||
return (find_token("||", lst));
|
||||
}
|
||||
else if (find_token("|", lst))
|
||||
{
|
||||
*state = _PLINE;
|
||||
return (find_token("|", lst));
|
||||
}
|
||||
else
|
||||
{
|
||||
*state = UNDEF;
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user