This commit is contained in:
Nathan Lebrun
2025-01-31 18:57:58 +01:00
parent 3fd862f171
commit 1c643b7119
9 changed files with 224 additions and 104 deletions

View File

@@ -12,96 +12,106 @@
#include "ast.h"
void setup_cmd(t_ast_n *node, char *cmd, char *args)
// ===================================================================
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)
{
node->cmd = ft_strdup(cmd);
node->args = ft_split(args, " ");
t_nodell *nodell;
nodell = cutll(lst, token->val, 1);
parrent->left = create_ast_n(nodell->node, parrent);
parrent->right = create_ast_n(nodell->next->node, parrent);
// free_lltab(sublsts);
}
int last_tok_subsh(t_node *lst)
// ===================================================================
t_redir get_red()
void create_cmd(t_ast_n *self, t_node *lst)
{
while (lst)
{
if (lst->next == NULL && !ft_strncmp(lst->val, ")", 1))
return (1);
lst = lst->next;
}
return (0);
self->state = _CMD;
self->cmd = ft_strdup(lst->val);
self->args = ft_split("test random val", " ");
}
void skip_parentheses(t_node **lst)
//=====================================================================
// void create_pline(t_ast_n *parent, t_node *lst, t_node *token)
// {
//
// }
//======================================================================
void remove_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;
}
}
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;
}
}
t_node *find_token(char *tok, t_node *lst)
void create_subsh(t_ast_n *parent, t_node *lst)
{
while (lst)
t_node *cpy;
cpy = lst;
while (cpy)
{
skip_parentheses(&lst);
if (!ft_strncmp(lst->val, tok, ft_strlen(tok)))
return (lst);
lst = lst->next;
ft_printf("%s\n", cpy->val);
cpy = cpy->next;
}
return (NULL);
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);
}
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);
}
}
/*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_node **sublsts;*/
/**/
/*sublsts = cutll(lst, token);*/
/* parrent->left = create_ast_n(sublsts[0], parrent);*/
/* parrent->left = create_ast_n(sublsts[1], parrent);*/
/* free_lltab(sublsts);*/
/* free(token);*/
/*}*/
/*void create_cmd(t_ast_n *parent, t_node *lst, t_node *token)*/
/*{*/
/*}*/
//==================================================================
/*void create_pline(t_ast_n *parent, t_node *lst, t_node *token)*/
/*{*/
/*}*/
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent)
{
@@ -117,15 +127,16 @@ t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent)
node->left = NULL;
node->right = NULL;
node->pline = NULL;
// node->parent = parent; pas encore tester
/*if (node->state == _PLINE)*/
/*create_pline(node, lst, token);*/
/*else if (node->state == _CMD)*/
/*create_cmd(node, lst, token);*/
/*else if (node->state == _SUBSH)*/
/*create_subsh(node, lst, token);*/
/*else*/
/*create_and_or(node, lst, token);*/
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
create_cmd(node, lst);
return (node);
}

View File

@@ -64,6 +64,7 @@ typedef struct s_nodell
t_ast_n *get_ast(char **envp, t_node *lst);
t_nodell *cutll(t_node *lst, char *expected, size_t limiter);
t_node *get_top_token(t_node *lst, t_state *state);
// env TMP
char **init_env(char **envp);

View File

@@ -12,7 +12,7 @@
#include "ast.h"
void add_nodell(t_nodell **nodell, t_node *node)
static void add_nodell(t_nodell **nodell, t_node *node)
{
t_nodell *tmp;
@@ -31,7 +31,7 @@ void add_nodell(t_nodell **nodell, t_node *node)
tmp->next->next = NULL;
}
t_node *get_node(t_node **lst, char *expected)
static t_node *get_node(t_node **lst, char *expected)
{
t_node *node;

76
test/ast/top_token.c Normal file
View 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 "ast.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);
}
}