]> git.dkaiser.de - 42/minishell.git/commitdiff
Add print_ast function
authorDominik Kaiser <dkaiser@1-E-17.42heilbronn.de>
Mon, 22 Jul 2024 14:33:08 +0000 (16:33 +0200)
committerDominik Kaiser <dkaiser@1-E-17.42heilbronn.de>
Mon, 22 Jul 2024 14:33:08 +0000 (16:33 +0200)
Makefile
include/minishell.h
src/print_ast.c [new file with mode: 0644]

index 8896dedf9895bb6149016d899001e4e4cf5941ca..a56ada344e6791ae54fd9adbf8b93959f42cc25b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,8 @@ HEADERS =  -I include -I $(LIB_DIR)/libft
 
 VPATH   := src
 SRC     := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \
-           free_token.c new_node.c free_node.c parser.c parse_cmd.c
+           free_token.c new_node.c free_node.c parser.c parse_cmd.c \
+           print_ast.c
 
 OBJ_DIR := _obj
 OBJ     := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
index 2411cc5f824c5272a009845493bf918bef2d2b77..d980d425fe2d4e0a31b7ddf2b2e7e7cbe002fed2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/09 11:36:09 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/07/22 15:49:34 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -30,8 +30,8 @@ int           init_signal_handling(void);
 
 void   repl(const char *prompt);
 
-
 t_list *parse(t_token *tokens);
 t_node *parse_cmd(t_token *tokens);
 
+void   print_ast(t_node *ast);
 #endif
diff --git a/src/print_ast.c b/src/print_ast.c
new file mode 100644 (file)
index 0000000..d1f5095
--- /dev/null
@@ -0,0 +1,65 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   print_ast.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/07/22 15:16:53 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/07/22 16:32:49 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static void    print_ast_rec(t_node *ast, int indent);
+static void    print_cmd_node(t_node *ast, int indent);
+
+void   print_ast(t_node *ast)
+{
+       if (DEBUG)
+       {
+               printf("\e[94m[AST]\n");
+               print_ast_rec(ast, 0);
+               printf("\e[0m\n");
+       }
+}
+
+static void    print_ast_rec(t_node *ast, int indent)
+{
+       if (ast->type == CMD_NODE)
+               print_cmd_node(ast, indent);
+       else if (ast->type == PIPE_NODE)
+       {
+               printf("%*s%s", indent, "", "* PIPE");
+               print_ast_rec(ast->content.pipe.left, indent + 2);
+               print_ast_rec(ast->content.pipe.right, indent + 2);
+       }
+}
+
+static void    print_cmd_node(t_node *ast, int indent)
+{
+       int     i;
+
+       printf("\n%*s%s", indent, "", "* CMD");
+       i = 0;
+       printf("\n%*sARGS:", indent + 2, "");
+       while (ast->content.cmd.args[i] != NULL)
+       {
+               printf(" %s", ast->content.cmd.args[i]);
+               i++;
+       }
+       i = 0;
+       printf("\n%*sASSIGNS:", indent + 2, "");
+       while (ast->content.cmd.assigns[i] != NULL)
+       {
+               printf(" %s=%s", ast->content.cmd.assigns[i]->var,
+                       ast->content.cmd.assigns[i]->value);
+               i++;
+       }
+       printf("\n%*sREDIRS:", indent + 2, "");
+       printf("\n%*sIN: %d %s", indent + 4, "", ast->content.cmd.redirs[0].type,
+               ast->content.cmd.redirs[0].specifier);
+       printf("\n%*sOUT: %d %s", indent + 4, "", ast->content.cmd.redirs[1].type,
+               ast->content.cmd.redirs[1].specifier);
+}