aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--include/minishell.h4
-rw-r--r--src/print_ast.c65
3 files changed, 69 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 8896ded..a56ada3 100644
--- 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))
diff --git a/include/minishell.h b/include/minishell.h
index 2411cc5..d980d42 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -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
index 0000000..d1f5095
--- /dev/null
+++ b/src/print_ast.c
@@ -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);
+}