maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
visualiser.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* visualiser.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/07 19:00:03 by rmikhayl #+# #+# */
9/* Updated: 2024/06/07 19:16:18 by rmikhayl ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14
16{
17 if (type == PHRASE)
18 return ("PHRASE");
19 else if (type == PIPE)
20 return ("PIPE");
21 else if (type == ENV_VAR)
22 return ("ENV_VAR");
23 else if (type == REDIR_IN)
24 return ("REDIR_IN");
25 else if (type == REDIR_OUT)
26 return ("REDIR_OUT");
27 else if (type == REDIR_APPEND)
28 return ("REDIR_APPEND");
29 else if (type == REDIR_HEREDOC)
30 return ("REDIR_HEREDOC");
31 else
32 return ("UNKNOWN");
33}
34
35void print_ast_node(t_ast *node, int depth, char *prefix, int is_left)
36{
37 int i;
38
39 if (!node)
40 return ;
41 ft_printf("%s", prefix);
42 if (depth == 0)
43 ft_printf("Root-> ");
44 else
45 {
46 if (is_left)
47 ft_printf("L-> ");
48 else
49 ft_printf("R-> ");
50 }
51 ft_printf("Type: %s\n", get_token_type_name(node->type));
52 if (node->args)
53 {
54 i = 0;
55 while (node->args[i])
56 {
57 ft_printf("%s Arg: %s\n", prefix, node->args[i]);
58 i++;
59 }
60 }
61}
62
63void build_new_prefix(char *new_prefix, char *prefix, int is_left)
64{
65 int i;
66 int j;
67
68 i = 0;
69 j = 0;
70 while (prefix[i])
71 {
72 new_prefix[j] = prefix[i];
73 i++;
74 j++;
75 }
76 if (is_left)
77 {
78 new_prefix[j++] = '|';
79 new_prefix[j++] = ' ';
80 new_prefix[j++] = ' ';
81 }
82 else
83 {
84 new_prefix[j++] = ' ';
85 new_prefix[j++] = ' ';
86 new_prefix[j++] = ' ';
87 }
88 new_prefix[j] = '\0';
89}
90
91void print_ast_graphical(t_ast *node, int depth, char *prefix, int is_left)
92{
93 char new_prefix[256];
94
95 print_ast_node(node, depth, prefix, is_left);
96 build_new_prefix(new_prefix, prefix, is_left);
97 if (node->left)
98 print_ast_graphical(node->left, depth + 1, new_prefix, 1);
99 if (node->right)
100 print_ast_graphical(node->right, depth + 1, new_prefix, 0);
101}
102
104{
105 if (!root)
106 return ;
107 print_ast_graphical(root, 0, "", 0);
108}
int ft_printf(const char *format,...)
Definition ft_printf.c:37
Definition tokens.h:50
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
t_token_type type
Definition tokens.h:51
@ REDIR_IN
Definition tokens.h:34
@ PHRASE
Definition tokens.h:31
@ REDIR_HEREDOC
Definition tokens.h:37
@ PIPE
Definition tokens.h:32
@ REDIR_OUT
Definition tokens.h:35
@ REDIR_APPEND
Definition tokens.h:36
@ ENV_VAR
Definition tokens.h:33
enum e_token_type t_token_type
void print_ast_node(t_ast *node, int depth, char *prefix, int is_left)
Definition visualiser.c:35
void print_ast_root(t_ast *root)
Definition visualiser.c:103
void print_ast_graphical(t_ast *node, int depth, char *prefix, int is_left)
Definition visualiser.c:91
void build_new_prefix(char *new_prefix, char *prefix, int is_left)
Definition visualiser.c:63
const char * get_token_type_name(t_token_type type)
Definition visualiser.c:15