From: Lukas Jiriste Date: Fri, 2 Aug 2024 16:51:44 +0000 (+0200) Subject: Make the parsing table structure static X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=8d6959f58d6d783ce3a7e4b600b54cc9cc9b241a;p=42%2Fminishell.git Make the parsing table structure static This makes it a little more memory and CPU friendly, because the table doesn't need to be reallocated and parsed from the text form every command. The downside itt needs to be cleared at the end of the program. --- diff --git a/src/main.c b/src/main.c index f09b0ed..d990bff 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/07/21 21:24:30 by ljiriste ### ########.fr */ +/* Updated: 2024/08/02 18:50:53 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -147,6 +147,7 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) free(line); } clean_env(&env); + parse(NULL, NULL); get_next_line(-1); free(line); ft_printf("exit\n"); diff --git a/src/parsing.c b/src/parsing.c index 06f93f3..5067010 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 15:58:55 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 17:03:23 by ljiriste ### ########.fr */ +/* Updated: 2024/08/02 18:49:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,19 @@ // table should be static or loaded at start and passed in as argument int parse(t_vec *tokens, t_tree **parse_tree) { - t_parsing_table table; + static t_parsing_table table = {.terminal_tokens_num = 0}; - ft_parsing_table_init(&table); - ft_parsing_table_load_str(&table, g_str_table, g_str_rules); - *parse_tree = ft_parse(tokens, &table); - ft_parsing_table_free(&table); + if (table.terminal_tokens_num == 0) + { + ft_parsing_table_init(&table); + ft_parsing_table_load_str(&table, g_str_table, g_str_rules); + } + if (!parse_tree) + { + ft_parsing_table_free(&table); + return (0); + } + else + *parse_tree = ft_parse(tokens, &table); return (*parse_tree == NULL); }