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);
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);
}
}

View File

@@ -35,6 +35,7 @@ typedef struct s_elems
// internal
char *replace_ampercent(char *src);
char *replace_left_red(char *src);
t_dio_node get_cmd_txt(t_ast_n *node);
int print_ast(t_ast_n *node, t_elems *e, int fd);

View File

@@ -36,11 +36,11 @@ const char *translate_redir(t_redir redir)
const char *out;
if (redir == _RED_L)
out = "redir : < &#10;";
out = "redir : RED_L&#10;";
else if (redir == _RED_R)
out = "redir : > &#10;";
out = "redir : RED_R&#10;";
else if (redir == _RED_DR)
out = "redir : >> &#10;";
out = "redir : _RED_DR &#10;";
else
out = "Not redirected &#10;";
return (out);
@@ -49,12 +49,16 @@ const char *translate_redir(t_redir redir)
t_dio_node get_cmd_txt(t_ast_n *node)
{
t_dio_node txt;
char *args;
txt.st = translate_state(node->state);
if (node->state == _CMD)
{
txt.cmd = ft_sprintf("%s%s%s", NL, node->cmd, NL);
txt.args = ft_sprintf(ft_tabstr(node->args), NL);
txt.cmd = ft_sprintf("%s%s", NL, node->cmd);
txt.cmd = replace_left_red(txt.cmd);
args = ft_tabstr(node->args);
txt.args = ft_sprintf("%s%s%s", NL, args, NL);
free(args);
txt.redir = translate_redir(node->redir);
txt.inf = ft_sprintf("Infile : %s%s", node->infile, NL);
txt.outf = ft_sprintf("Outfile : %s", node->outfile);

View File

@@ -39,6 +39,7 @@ void gen_dio_linked_list(t_node *head, int fd)
{
rect.text = ft_sprintf("%s", current->val);
rect.text = replace_ampercent(rect.text);
rect.text = replace_left_red(rect.text);
arrow.id_dst = drawio_create_elem(fd, &rect);
drawio_create_elem(fd, &arrow);
arrow.id_src = arrow.id_dst;

View File

@@ -12,7 +12,7 @@
#include "drawio.h"
static int get_amp_count(char *str)
static int get_char_count(char *str, char c)
{
int i;
int count;
@@ -21,7 +21,7 @@ static int get_amp_count(char *str)
count = 0;
while (str[i])
{
if (str[i] == '&')
if (str[i] == c)
count++;
i++;
}
@@ -37,7 +37,7 @@ char *replace_ampercent(char *src)
i = -1;
j = 0;
amp_count = get_amp_count(src);
amp_count = get_char_count(src, '&');
out = malloc(ft_strlen(src) + amp_count * 4 + 1);
while (src[++i])
{
@@ -54,5 +54,39 @@ char *replace_ampercent(char *src)
out[j++] = src[i];
}
out[j] = 0;
free(src);
return (out);
}
char *replace_left_red(char *src)
{
int i;
int j;
int redl_count;
char *out;
i = -1;
j = 0;
redl_count = get_char_count(src, '<');
out = malloc(ft_strlen(src) + redl_count * 7 + 1);
while (src[++i])
{
if (src[i] == '<')
{
out[j] = '&';
out[j + 1] = 'a';
out[j + 2] = 'm';
out[j + 3] = 'p';
out[j + 4] = ';';
out[j + 5] = 'l';
out[j + 6] = 't';
out[j + 7] = ';';
j += 8;
}
else
out[j++] = src[i];
}
out[j] = 0;
free(src);
return (out);
}

View File

@@ -39,7 +39,7 @@ void truncate_comment(char *str)
int main(int ac, char **av, char **envp)
{
t_node *lst;
// t_ast_n *ast;
t_ast_n *ast;
int dio;
(void)envp;
@@ -57,21 +57,13 @@ int main(int ac, char **av, char **envp)
dio = drawio_init(av[1]);
gen_dio_linked_list(lst, dio);
}
// ast = get_ast(envp, lst);
// if (!ast)
// return (1);
ast = get_ast(envp, lst);
if (!ast)
return (1);
// tmp
t_nodell *lls = cutll(lst, "||", -1);
while (lls)
{
gen_dio_linked_list(lls->node, dio);
lls = lls->next;
}
if (DEBUG)
{
// gen_dio_ast(ast, dio);
gen_dio_ast(ast, dio);
drawio_end_file(dio);
ft_debug(" draw.io file generated !\n");
}