Make the parsing table structure static
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 2 Aug 2024 16:51:44 +0000 (18:51 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 2 Aug 2024 16:51:44 +0000 (18:51 +0200)
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.

src/main.c
src/parsing.c

index f09b0edbbf1e5c27bac371f60bdf1466310828ed..d990bff847e286b1623de95ac79d7630336b13de 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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");
index 06f93f31d7aeed7555ca560910635b5552631aed..506701032b3927aaba1cdb7d1097995038c9d6e0 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 //     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);
 }