aboutsummaryrefslogtreecommitdiff
path: root/src/print_ast.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-07-22 16:33:08 +0200
committerDominik Kaiser2024-07-22 16:33:08 +0200
commitc16b4655f77db2f152625f226dc66ccc922671b4 (patch)
treeaab9e94c2e9668c60a0214b0808a6787913a7f99 /src/print_ast.c
parent47a68b82e0463f5dfd4bd8a9c4b32b4c5fbcb610 (diff)
downloadminishell-c16b4655f77db2f152625f226dc66ccc922671b4.tar.gz
minishell-c16b4655f77db2f152625f226dc66ccc922671b4.zip
Add print_ast function
Diffstat (limited to 'src/print_ast.c')
-rw-r--r--src/print_ast.c65
1 files changed, 65 insertions, 0 deletions
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);
+}