From: Dominik Kaiser Date: Mon, 8 Jul 2024 14:35:30 +0000 (+0200) Subject: Add most of the parser X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=3b2efe45ce23f51e97a54b820b41f230b99bbab2;p=42%2Fminishell.git Add most of the parser --- diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..5bf82f6 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next = ft_lstnew(parse_statement(current_tokens)); + current = current->next; + } + if (current == NULL) + { + // Free: ft_lstclear(&result, free_node); + return (NULL); + } + current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); + } + return (result); +} + +static t_node *parse_statement(t_token *tokens) +{ + t_token *left_side_tokens; + + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); + if (tokens != NULL) + { + return (new_pipe_node(parse_cmd(left_side_tokens), + parse_statement(tokens))); + } + else + { + return (parse_cmd(left_side_tokens)); + } +} + +static t_node *parse_cmd(t_token *tokens) +{ + char **args; + t_assign **assigns; + t_redirection **redirs; + + redirs = collect_redirs(&tokens); + assigns = collect_assigns(&tokens); + args = collect_args(&tokens); + return (new_cmd_node(args, assigns, redirs)); +} + +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; + 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); +}