Implement printing function for parsing tree
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 08:34:09 +0000 (10:34 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 08:34:09 +0000 (10:34 +0200)
This is mainly done for debugging. This does not scale to arbitrary
large trees automatically - the output will be garbled.

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

index a33caea5c911099c930d38801b27c6b3e8df6e7b..00ad5e065ecea041ed6379570a6798b7657585ef 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,7 @@ SRCparse:=    ft_parse.c                                      \
                        ft_parsing_table_load.c         \
                        ft_parsing_table_print.c        \
                        ft_parsing_table_free.c         \
+                       ft_parse_tree_print.c           \
                        load_rules.c                            \
                        add_line.c                                      \
 
diff --git a/ft_parse/ft_parse_tree_print.c b/ft_parse/ft_parse_tree_print.c
new file mode 100644 (file)
index 0000000..f8e7885
--- /dev/null
@@ -0,0 +1,69 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_parse_tree_print.c                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/06/21 09:51:43 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/21 10:33:09 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_parse.h"
+#include "libft.h"
+
+#define MAX_WIDTH 80
+
+static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]);
+static void print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]);
+
+static void    print_children_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+       size_t  i;
+
+       i = 0;
+       while (i + 1 < node->children.size)
+       {
+               print_node_at_depth(ft_vec_access(&node->children, i), depth + 1, branches);
+               ++i;
+       }
+       if (i < node->children.size)
+               print_last_at_depth(ft_vec_access(&node->children, i), depth + 1, branches);
+       if (depth > 0)
+               branches[ft_strlen(branches) - 1] = '\0';
+       branches[ft_strlen(branches) - 1] = '\0';
+       return ;
+}
+
+static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+       ft_printf("%s-%s\n", branches, node->token.type);
+       branches[ft_strlen(branches) - 1] = ' ';
+       ft_strlcat(branches, " |", MAX_WIDTH);
+       print_children_at_depth(node, depth, branches);
+}
+
+static void    print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+       if (depth == 0)
+       {
+               ft_printf("%s\n", node->token.type);
+               ft_strlcat(branches, "|", MAX_WIDTH);
+       }
+       else
+       {
+               ft_printf("%s-%s\n", branches, node->token.type);
+               ft_strlcat(branches, " |", MAX_WIDTH);
+       }
+       print_children_at_depth(node, depth, branches);
+}
+
+void   ft_parse_tree_print(t_parse_tree_node *root)
+{
+       char    branches[MAX_WIDTH];
+
+       branches[0] = '\0';
+       print_node_at_depth(root, 0, branches);
+       return ;
+}
index 69fea39054b7a98ee02635cb29c33ab880d0a3af..b4cffcdc3a3974411c68bc5d6423fad38be7aa2a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/27 21:21:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 09:04:04 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 09:57:12 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -96,8 +96,10 @@ t_ft_stat    ft_parsing_table_load(t_parsing_table *table,
                                const char *filename,
                                const char *rules_filename);
 t_parse_tree_node      *ft_parse(t_vec *tokens, t_parsing_table *table);
+
 void           ft_parsing_table_print(t_parsing_table *table,
                                unsigned int column_width);
+void   ft_parse_tree_print(t_parse_tree_node *root);
 
 void           free_token(void *v_token);
 void           ft_parse_tree_free(void *v_node);