From 775f25a837e749abce279908ebd75a3a1b484b8c Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Mon, 27 Jan 2025 14:14:02 +0100 Subject: [PATCH] hardcode ast --- srcs/ast/ast.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ srcs/ast/ast.h | 58 +++++++++++++++++++++++++++++++++++++++++++ tests/ast/ast.c | 57 ++++++++++++++++++++++++++++++++++++++----- tests/ast/ast.h | 43 ++++++++++++++++++++++++++++++-- tests/parsing.h | 4 +++ tests/test.c | 10 +++----- tests/tmp_env.c | 44 +++++++++++++++++++++++++++++++++ 7 files changed, 266 insertions(+), 15 deletions(-) create mode 100644 srcs/ast/ast.c create mode 100644 srcs/ast/ast.h create mode 100644 tests/tmp_env.c diff --git a/srcs/ast/ast.c b/srcs/ast/ast.c new file mode 100644 index 0000000..ba58118 --- /dev/null +++ b/srcs/ast/ast.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 08:22:16 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ast.h" + +t_ast_n *created_ast_n(t_state st, t_ast_n *prt, t_ast_n *he) +{ + t_ast_n *node; + + node = malloc(sizeof(t_ast_n)); + if (!node) + return (NULL); + node->state = st; + node->parent = prt; + node->head = he; + node->ex_code = 0; + node->cmd = NULL; + node->args = NULL; + node->_stdout = 1; + node->_stdin = 0; + node->redir = _NR; + node->infile = NULL; + node->outfile = NULL; + if (prt) + node->shlvl = prt->shlvl; + else + node->shlvl = 0; + node->env = NULL; + return (node); +} + +void setup_cmd(t_ast_n *node, char *cmd, char *args) +{ + node->cmd = ft_strdup(cmd); + node->args = ft_split(args, " "); +} + +t_ast_n *return_hardcode_ast(char **envp) +{ + t_ast_n *head; + + head = created_ast_n(_AND, NULL, NULL); + head->env = init_env(envp); + head->left = created_ast_n(_CMD, head, head); + setup_cmd(head->left, "echo", "echo coucou"); + head->right = created_ast_n(_PLINE, head, head); + head->right->pline = malloc(sizeof(t_ast_n *) * 4); + head->right->pline[0] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[0], "ls", "ls"); + head->right->pline[1] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[1], "cat", "cat -e"); + head->right->pline[2] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[2], "wc", "wc -l"); + head->right->pline[3] = NULL; + return (head); +} diff --git a/srcs/ast/ast.h b/srcs/ast/ast.h new file mode 100644 index 0000000..a099805 --- /dev/null +++ b/srcs/ast/ast.h @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ast.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 08:23:27 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 08:23:27 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AST_H +# define AST_H + +# include "../../includes/env.h" + +typedef enum e_state +{ + UNDEF, + _AND, + _OR, + _CMD, + _PLINE +} t_state; + +typedef enum e_redir +{ + _NR, + _RED_L, + _RED_R, + _RED_DR +} t_redir; + +typedef struct s_ast_n +{ + t_state state; + struct s_ast_n *parent; + struct s_ast_n *left; + struct s_ast_n *right; + struct s_ast_n **pline; + int ex_code; + struct s_ast_n *head; + char *cmd; + char **args; + int fds[2]; + int _stdout; + int _stdin; + t_redir redir; + char *infile; + char *outfile; + int shlvl; + char **env; +} t_ast_n; + +t_ast_n *return_hardcode_ast(char **envp); + +#endif diff --git a/tests/ast/ast.c b/tests/ast/ast.c index f7084ba..ba58118 100644 --- a/tests/ast/ast.c +++ b/tests/ast/ast.c @@ -12,9 +12,54 @@ #include "ast.h" -/*void ast(t_node *token_lst)*/ -/*{*/ -/* t_node *head = token_lst;*/ -/**/ -/**/ -/*}*/ +t_ast_n *created_ast_n(t_state st, t_ast_n *prt, t_ast_n *he) +{ + t_ast_n *node; + + node = malloc(sizeof(t_ast_n)); + if (!node) + return (NULL); + node->state = st; + node->parent = prt; + node->head = he; + node->ex_code = 0; + node->cmd = NULL; + node->args = NULL; + node->_stdout = 1; + node->_stdin = 0; + node->redir = _NR; + node->infile = NULL; + node->outfile = NULL; + if (prt) + node->shlvl = prt->shlvl; + else + node->shlvl = 0; + node->env = NULL; + return (node); +} + +void setup_cmd(t_ast_n *node, char *cmd, char *args) +{ + node->cmd = ft_strdup(cmd); + node->args = ft_split(args, " "); +} + +t_ast_n *return_hardcode_ast(char **envp) +{ + t_ast_n *head; + + head = created_ast_n(_AND, NULL, NULL); + head->env = init_env(envp); + head->left = created_ast_n(_CMD, head, head); + setup_cmd(head->left, "echo", "echo coucou"); + head->right = created_ast_n(_PLINE, head, head); + head->right->pline = malloc(sizeof(t_ast_n *) * 4); + head->right->pline[0] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[0], "ls", "ls"); + head->right->pline[1] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[1], "cat", "cat -e"); + head->right->pline[2] = created_ast_n(_CMD, head->right, head); + setup_cmd(head->right->pline[2], "wc", "wc -l"); + head->right->pline[3] = NULL; + return (head); +} diff --git a/tests/ast/ast.h b/tests/ast/ast.h index dfedc78..a099805 100644 --- a/tests/ast/ast.h +++ b/tests/ast/ast.h @@ -13,7 +13,46 @@ #ifndef AST_H # define AST_H -#include "../../includes/minishell.h" -#include "../tokenizer/tokenizer.h" +# include "../../includes/env.h" + +typedef enum e_state +{ + UNDEF, + _AND, + _OR, + _CMD, + _PLINE +} t_state; + +typedef enum e_redir +{ + _NR, + _RED_L, + _RED_R, + _RED_DR +} t_redir; + +typedef struct s_ast_n +{ + t_state state; + struct s_ast_n *parent; + struct s_ast_n *left; + struct s_ast_n *right; + struct s_ast_n **pline; + int ex_code; + struct s_ast_n *head; + char *cmd; + char **args; + int fds[2]; + int _stdout; + int _stdin; + t_redir redir; + char *infile; + char *outfile; + int shlvl; + char **env; +} t_ast_n; + +t_ast_n *return_hardcode_ast(char **envp); #endif diff --git a/tests/parsing.h b/tests/parsing.h index f6c207d..a8bcb06 100644 --- a/tests/parsing.h +++ b/tests/parsing.h @@ -14,8 +14,12 @@ # define PARSING_H # include "tokenizer/tokenizer.h" +# include "ast/ast.h" // drawio void gen_dio_linked_list(t_node *head, char *fp); +// tmp_env +char **init_env(char **envp); + #endif diff --git a/tests/test.c b/tests/test.c index ad33661..b964f30 100644 --- a/tests/test.c +++ b/tests/test.c @@ -49,24 +49,20 @@ void truncate_comment(char *str) int main(int ac, char **av, char **envp) { t_node *lst; - - (void)ac; - (void)envp; + /*t_data data;*/ if (ac != 3) { ft_error("./test drawio_file command_str\n"); return (1); } - - /*t_data data;*/ - /*data = init_data(envp);*/ truncate_comment(av[1]); lst = tokenize(av[2]); if (!lst) return (1); gen_dio_linked_list(lst, av[1]); - debug_linked_list(lst, "ff"); + /*debug_linked_list(lst, "ff");*/ + return_hardcode_ast(envp); free_linked_list(lst); } diff --git a/tests/tmp_env.c b/tests/tmp_env.c new file mode 100644 index 0000000..93fc40e --- /dev/null +++ b/tests/tmp_env.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tmp_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/27 12:35:29 by nalebrun #+# #+# */ +/* Updated: 2025/01/27 12:35:29 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "../lib/libft/libft.h" + +static int count_var(char **envp) +{ + int i; + + i = 0; + if (!envp) + return (-1); + while (envp[i]) + i++; + return (i); +} + +char **init_env(char **envp) +{ + char **env; + int i; + + i = 0; + env = malloc(sizeof(char *) * (count_var(envp) + 1)); + if (!env) + return (NULL); + env[count_var(envp)] = NULL; + while (envp[i]) + { + env[i] = ft_strdup(envp[i]); + i++; + } + return (env); +}