From 42ece06fd235b133b090bc90fe07ce4aaa0ed72c Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 21 Jun 2024 17:02:05 +0200 Subject: [PATCH] Change the file structure of the project Because the parsing was implemented inside libft the parsingh was not needed here. The informative contents were moved to about_grammar. The variable expansion and tokenization were given their own files. --- Makefile | 4 +- inc/parsing.h => about_grammar | 80 +++------------------------------- inc/minishell.h | 13 +++++- src/execution.c | 4 +- src/{tree.c => expansion.c} | 18 +++----- src/helpers.c | 20 +++++++++ src/input_handling.c | 42 +++--------------- src/parsing.c | 16 +++++-- src/tokenization.c | 35 +++++++++++++++ 9 files changed, 104 insertions(+), 128 deletions(-) rename inc/parsing.h => about_grammar (55%) rename src/{tree.c => expansion.c} (61%) create mode 100644 src/helpers.c create mode 100644 src/tokenization.c diff --git a/Makefile b/Makefile index 0430610..43bd564 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,11 @@ SRCDIR := src SOURCES := main.c \ vars.c \ input_handling.c \ - tree.c \ + tokenization.c \ parsing.c \ + expansion.c \ execution.c \ + helpers.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/parsing.h b/about_grammar similarity index 55% rename from inc/parsing.h rename to about_grammar index 53c1181..bd34367 100644 --- a/inc/parsing.h +++ b/about_grammar @@ -1,29 +1,10 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parsing.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/03 15:24:31 by ljiriste #+# #+# */ -/* Updated: 2024/06/14 10:30:40 by ljiriste ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef PARSING_H -# define PARSING_H - -# include "minishell.h" -# include "libft.h" - -/* -// As this is a minishell I used the -// The Open Group Base Specifications Issue 7, 2018 edition -// chapter 2. Shell Command Language as a main source. -// which can be found at the address -// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html -// Because minishell is mini, I trim the grammar quite a bit, -// so I think it appropriate to include the grammar here: +As this is a minishell I used the +The Open Group Base Specifications Issue 7, 2018 edition +chapter 2. Shell Command Language as a main source. +which can be found at the address +https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +Because minishell is mini, I trim the grammar quite a bit, +so I think it appropriate to include the grammar here: Grammar Rules: 2. Redirection to or from file @@ -150,50 +131,3 @@ io_file -> < filename io_file -> > filename io_file -> >> filename filename -> WORD - -*/ - -// Common type for WORD, ASSIGNMENT_WORD and IO_NUMBER is TOKEN. -// The TOKEN type is identifies as WORD, ASSIGNMENT_WORD or IO_NUMBER -// after tokenization itself -typedef enum e_token_type -{ - TOKEN, - WORD, - ASSIGNMENT_WORD, - IO_NUMBER, - AND_IF, - OR_IF, - LESS, - GREAT, - DLESS, - DGREAT, - PIPE, - LPARA, - RPARA, -} t_token_type; - -// Only TOKEN (WORD, ASSIGNMENT_WORD, IO_NUMBER) type tokens -// have the str member non-empty -typedef struct s_token -{ - t_token_type type; - char *str; -} t_token; - -typedef struct s_tree_node -{ - t_token token; - struct s_tree_node *children[3]; -} t_tree_node; - -typedef t_tree_node t_tree; - -void handle_input(const char *input, t_vars *vars); -int parse(t_vec *tokens, t_tree_node *parse_tree, t_vars *vars); -int execute(t_tree *parse_tree, t_vars *vars); - -int init_tree(t_tree *tree); -void free_tree(t_tree *tree); - -#endif // PARSING_H diff --git a/inc/minishell.h b/inc/minishell.h index 68727cf..100011c 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */ -/* Updated: 2024/05/03 13:01:59 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 16:35:59 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ typedef struct s_vars t_vec other; } t_vars; +typedef t_parse_tree_node t_tree; + int init_vars(t_vars *vars, char **envp); int add_var_line(t_vec *vec, const char *line); int add_var(t_vec *vec, const char *key, const char *value); @@ -28,4 +30,13 @@ void clean_vars(t_vars *vars); void handle_input(const char *line, t_vars *vars); +int tokenize(const char *line, t_vec *tokens); +int parse(t_vec *tokens, t_tree *parse_tree); +int expand(t_tree *parse_tree, t_vars *vars); +int execute(t_tree *parse_tree, t_vars *vars); + +int init_tree(t_tree *tree); +void free_tree(t_tree *tree); +void free_token(void *token); + #endif // MINISHELL_H diff --git a/src/execution.c b/src/execution.c index d350ec0..238734c 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 15:59:58 by ljiriste #+# #+# */ -/* Updated: 2024/05/03 16:21:02 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 17:00:46 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parsing.h" +#include "minishell.h" int execute(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars) { diff --git a/src/tree.c b/src/expansion.c similarity index 61% rename from src/tree.c rename to src/expansion.c index c06af96..697241e 100644 --- a/src/tree.c +++ b/src/expansion.c @@ -1,25 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* tree.c :+: :+: :+: */ +/* expansion.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/03 15:56:55 by ljiriste #+# #+# */ -/* Updated: 2024/05/03 16:03:37 by ljiriste ### ########.fr */ +/* Created: 2024/06/21 16:29:48 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:31:57 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parsing.h" +#include "minishell.h" -int init_tree(__attribute__((unused))t_tree *tree) +int expand(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars) { - ft_printf("Function init_tree has to be implemented.\n"); + ft_printf("Variable expansion has yet to be implemented.\n"); return (0); } - -void free_tree(__attribute__((unused))t_tree *tree) -{ - ft_printf("Function free_tree has to by implemented.\n"); - return ; -} diff --git a/src/helpers.c b/src/helpers.c new file mode 100644 index 0000000..3f193e1 --- /dev/null +++ b/src/helpers.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 16:35:16 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:40:21 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include + +void free_token(void *token) +{ + free(((t_token *)token)->str); + return ; +} diff --git a/src/input_handling.c b/src/input_handling.c index b580fa9..1c1a763 100644 --- a/src/input_handling.c +++ b/src/input_handling.c @@ -6,56 +6,28 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 09:00:00 by ljiriste #+# #+# */ -/* Updated: 2024/06/14 11:00:16 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 16:39:27 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parsing.h" #include "minishell.h" #include "libft.h" #include -static void free_token(void *token) -{ - free(((t_token *)token)->str); - return ; -} - -static int delimit(__attribute__((unused))const char *line, __attribute__((unused))t_vec *tokens) -{ - ft_printf("Function delimit has to be implemented.\n"); - return (0); -} - -static int identify(__attribute__((unused))t_vec *tokens) -{ - ft_printf("Function identify has to be implemented.\n"); - return (0); -} - -// This function turns the input char string into a string of tokens -static int tokenize(const char *line, t_vec *tokens) -{ - if (delimit(line, tokens)) - return (1); - if (identify(tokens)) - return (1); - return (0); -} - void handle_input(const char *input, t_vars *vars) { int res; t_vec tokens; - t_tree parse_tree; + t_tree *parse_tree; ft_vec_init(&tokens, sizeof(t_token)); - init_tree(&parse_tree); + parse_tree = NULL; res = tokenize(input, &tokens); - res = res || parse(&tokens, &parse_tree, vars); - res = res || execute(&parse_tree, vars); + res = res || parse(&tokens, parse_tree); + res = res || expand(parse_tree, vars); + res = res || execute(parse_tree, vars); ft_vec_free(&tokens, free_token); - free_tree(&parse_tree); + ft_parse_tree_free(parse_tree); if (res != 0) ft_printf("An error has occured.\n"); return ; diff --git a/src/parsing.c b/src/parsing.c index 9fa2128..c28aa37 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -6,14 +6,22 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 15:58:55 by ljiriste #+# #+# */ -/* Updated: 2024/05/03 16:20:45 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 16:32:50 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parsing.h" +#include "minishell.h" +#include "libft.h" -int parse(__attribute__((unused))t_vec *tokens, __attribute__((unused))t_tree_node *parse_tree, __attribute__((unused))t_vars *vars) +// table should be static or loaded at start and passed in as argument +int parse(t_vec *tokens, t_tree *parse_tree) { - ft_printf("Function parse has to be implemented.\n"); + t_parsing_table table; + + ft_parsing_table_init(&table); + ft_parsing_table_load(&table, "shell_parsing_table", "shell_rules"); + parse_tree = ft_parse(tokens, &table); + ft_parse_tree_print(parse_tree); + ft_parsing_table_free(&table); return (0); } diff --git a/src/tokenization.c b/src/tokenization.c new file mode 100644 index 0000000..6324d31 --- /dev/null +++ b/src/tokenization.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tokenize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 16:34:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:35:59 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int delimit(__attribute__((unused))const char *line, __attribute__((unused))t_vec *tokens) +{ + ft_printf("Function delimit has to be implemented.\n"); + return (0); +} + +static int identify(__attribute__((unused))t_vec *tokens) +{ + ft_printf("Function identify has to be implemented.\n"); + return (0); +} + +// This function turns the input char string into a string of tokens +int tokenize(const char *line, t_vec *tokens) +{ + if (delimit(line, tokens)) + return (1); + if (identify(tokens)) + return (1); + return (0); +} -- 2.30.2