diff options
| author | Christopher Uhlig | 2024-10-17 15:32:43 +0200 |
|---|---|---|
| committer | Christopher Uhlig | 2024-10-17 15:32:43 +0200 |
| commit | c1ac406e52fe273a0e14b0f079b522e58c04acd3 (patch) | |
| tree | 05d2a6c6c838ade625baf9a13e3a6ff0a3663739 /src/parser.c | |
| parent | 9298e72de61af5311678c656c12fc177589671a7 (diff) | |
| parent | 92bdbf67adaf09d1c831db3cee9456e9c447063f (diff) | |
| download | minishell-c1ac406e52fe273a0e14b0f079b522e58c04acd3.tar.gz minishell-c1ac406e52fe273a0e14b0f079b522e58c04acd3.zip | |
Merge branch 'env' of https://github.com/dpu-kaiser/minishell into env
Diffstat (limited to 'src/parser.c')
| -rw-r--r-- | src/parser.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..06c1df7 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ +/* Updated: 2024/09/17 19:03:48 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "minishell.h" +#include "token.h" + +static t_token *find_token_by_type(t_token *tokens, int type); +t_token *split_at_first(t_token **tokens, int type); +static t_node *parse_statement(t_token *tokens); + +t_list *parse(t_token *tokens) +{ + t_node *result; + + if ((*tokens).type == PIPE_TOKEN) + result = NULL; + else + result = parse_statement(tokens); + if (result == NULL) + printf("Parsing error.\n"); + return (ft_lstnew(result)); +} + +static t_node *parse_statement(t_token *tokens) +{ + t_token *left_side_tokens; + + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); + if (left_side_tokens == NULL) + { + free_tokens(tokens); + return (NULL); + } + else if (tokens != NULL) + { + return (new_pipe_node(parse_cmd(left_side_tokens), + parse_statement(tokens))); + } + else + { + return (parse_cmd(left_side_tokens)); + } +} + +t_token *split_at_first(t_token **tokens, int type) +{ + t_token *split; + t_token *result; + + split = find_token_by_type(*tokens, type); + if (split == NULL) + { + result = *tokens; + *tokens = NULL; + return (result); + } + result = *tokens; + *tokens = split->next; + if (result == split) + result = NULL; + free_token(split); + return (result); +} + +static t_token *find_token_by_type(t_token *tokens, int type) +{ + while (tokens != NULL) + { + if (tokens->type == type) + return (tokens); + tokens = tokens->next; + } + return (NULL); +} |
