Make parse_tree traversal a little bit easier
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Nov 2024 14:31:39 +0000 (15:31 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Nov 2024 16:00:36 +0000 (17:00 +0100)
The ft_get_node_child function is very simple but may save a lot of
code, because ft_vec_access returns void* which needs to be cast before
further actions can be done.
(And casting to (t_parse_tree_node *) is a lot of code).

Makefile
ft_parse/ft_get_node_child.c [new file with mode: 0644]
inc/ft_parse.h

index dd57c8188bc601a6195f9a1060ce0eeff62e69ff..5a8f48ff6e504faeb9b47d9d6e697394fba04d0b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -30,6 +30,7 @@ SRCparse:=    ft_parse.c                                                                                              \
                        ft_parsing_table_save.c                                                                 \
                        ft_parsing_table_free.c                                                                 \
                        ft_parse_tree_print.c                                                                   \
+                       ft_get_node_child.c                                                                             \
                        ft_parse_tree_free.c                                                                    \
                        load_rules.c                                                                                    \
                        load_rules_helpers.c                                                                    \
diff --git a/ft_parse/ft_get_node_child.c b/ft_parse/ft_get_node_child.c
new file mode 100644 (file)
index 0000000..bb63c8b
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_get_node_child.c                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/11/28 15:19:13 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/11/28 15:40:33 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_parse.h"
+#include "libft.h"
+
+t_parse_tree_node      *ft_get_node_child(t_parse_tree_node *node, size_t index)
+{
+       return (ft_vec_access(&node->children, index));
+}
+
+const t_parse_tree_node        *ft_cget_node_child(
+                                                       const t_parse_tree_node *node, size_t index)
+{
+       return (ft_vec_caccess(&node->children, index));
+}
index b06a1a81fa9769b849f686a18cd5e0375f70c3b4..f7c37bd977007eb30e692ed0f23a60d59005073c 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/27 21:21:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/11/28 11:46:05 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/11/28 16:29:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -91,26 +91,30 @@ typedef struct s_parser_stack_element
 //
 //     Tokens should not contain whitespace as it is used as separator
 
-t_ft_stat                      ft_parsing_table_init(t_parsing_table *table);
-t_ft_stat                      ft_parsing_table_load_name(t_parsing_table *table,
-                                               const char *filename, const char *rules_filename);
-t_ft_stat                      ft_parsing_table_generate(t_parsing_table *table,
-                                               const char *rules_filename);
-t_ft_stat                      ft_parsing_table_load_fd(t_parsing_table *table,
-                                               int table_fd, int rules_fd);
-t_ft_stat                      ft_parsing_table_load_str(t_parsing_table *table,
-                                               const char *table_str, const char *rules_str);
-t_parse_tree_node      *ft_parse(const t_vec *tokens,
-                                               const t_parsing_table *table);
-
-t_ft_stat                      ft_parsing_table_save(const t_parsing_table *table,
-                                               const char *filename);
-
-void                           ft_parsing_table_print(t_parsing_table *table,
-                                               unsigned int column_width);
-void                           ft_parse_tree_print(t_parse_tree_node *root);
-
-void                           ft_parse_tree_free(void *node);
-void                           ft_parsing_table_free(t_parsing_table *table);
+t_ft_stat                              ft_parsing_table_init(t_parsing_table *table);
+t_ft_stat                              ft_parsing_table_load_name(t_parsing_table *table,
+                                                       const char *filename, const char *rules_filename);
+t_ft_stat                              ft_parsing_table_generate(t_parsing_table *table,
+                                                       const char *rules_filename);
+t_ft_stat                              ft_parsing_table_load_fd(t_parsing_table *table,
+                                                       int table_fd, int rules_fd);
+t_ft_stat                              ft_parsing_table_load_str(t_parsing_table *table,
+                                                       const char *table_str, const char *rules_str);
+t_parse_tree_node              *ft_parse(const t_vec *tokens,
+                                                       const t_parsing_table *table);
+
+t_ft_stat                              ft_parsing_table_save(const t_parsing_table *table,
+                                                       const char *filename);
+
+void                                   ft_parsing_table_print(t_parsing_table *table,
+                                                       unsigned int column_width);
+void                                   ft_parse_tree_print(t_parse_tree_node *root);
+
+t_parse_tree_node              *ft_get_node_child(t_parse_tree_node *node, size_t index);
+const t_parse_tree_node        *ft_cget_node_child(
+                                                       const t_parse_tree_node *node, size_t index);
+
+void                                   ft_parse_tree_free(void *node);
+void                                   ft_parsing_table_free(t_parsing_table *table);
 
 #endif // FT_PARSE_H