From 7f144ab0891141397d7a9c0c1657c80e1312277e Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Tue, 28 Jan 2025 16:58:46 +0100 Subject: [PATCH] ast viewer --- lib/libft/srcs/drawio/lib_drawio.h | 2 + tests/ast/ast.c | 43 ++++++++++++-- tests/ast/ast.h | 6 +- tests/drawio/drawio_ast.c | 91 +++++++++++++++++++++++++----- 4 files changed, 122 insertions(+), 20 deletions(-) diff --git a/lib/libft/srcs/drawio/lib_drawio.h b/lib/libft/srcs/drawio/lib_drawio.h index 70d72cd..d39e229 100644 --- a/lib/libft/srcs/drawio/lib_drawio.h +++ b/lib/libft/srcs/drawio/lib_drawio.h @@ -15,6 +15,8 @@ # include "../../libft.h" +# define NL " " + typedef enum e_dio_elemtype { RECT, diff --git a/tests/ast/ast.c b/tests/ast/ast.c index 0681640..2b9f111 100644 --- a/tests/ast/ast.c +++ b/tests/ast/ast.c @@ -51,18 +51,51 @@ t_ast_n *return_hardcode_ast(char **envp) { t_ast_n *head; + // head head = created_ast_n(_AND, NULL, NULL); head->env = init_env(envp); - head->left = created_ast_n(_CMD, head, head); - setup_cmd(head->left, "echo", "echo coucou"); + + // left + head->left = created_ast_n(_AND, head, head); + + // left + head->left->left = created_ast_n(_CMD, head->left, head); + setup_cmd(head->left->left, "echo", "echo coucou"); + // + // right + head->left->right = created_ast_n(_OR, head->left, head); + // + // left + head->left->right->left = created_ast_n(_CMD, head->left->right, head); + setup_cmd(head->left->right->left, "echo", "echo coucou"); + + // right + head->left->right->right = created_ast_n(_CMD, head->left->right, head); + setup_cmd(head->left->right->right, "echo", "echo coucou"); + + // right head->right = created_ast_n(_PLINE, head, head); head->right->pline = malloc(sizeof(t_ast_n *) * 4); + // ===0=== head->right->pline[0] = created_ast_n(_CMD, head->right, head); setup_cmd(head->right->pline[0], "ls", "ls"); - head->right->pline[1] = created_ast_n(_CMD, head->right, head); - setup_cmd(head->right->pline[1], "cat", "cat -e"); + // ===1=== + head->right->pline[1] = created_ast_n(_AND, head->right, head); + + // left + head->right->pline[1]->left = created_ast_n(_CMD, head->right->pline[1], head); + setup_cmd(head->right->pline[1]->left, "echo", "echo coucou"); + + // right + head->right->pline[1]->right = created_ast_n(_CMD, head->right->pline[1], head); + setup_cmd(head->right->pline[1]->right, "echo", "echo coucou"); + + // ===2=== head->right->pline[2] = created_ast_n(_CMD, head->right, head); - setup_cmd(head->right->pline[2], "wc", "wc -l"); + setup_cmd(head->right->pline[2], "cat", "cat -e"); + + // ===NULL=== head->right->pline[3] = NULL; + return (head); } diff --git a/tests/ast/ast.h b/tests/ast/ast.h index a099805..3e964c0 100644 --- a/tests/ast/ast.h +++ b/tests/ast/ast.h @@ -13,7 +13,8 @@ #ifndef AST_H # define AST_H -# include "../../includes/env.h" +/*# include "../../includes/env.h"*/ +# include "../../lib/libft/libft.h" typedef enum e_state { @@ -55,4 +56,7 @@ typedef struct s_ast_n t_ast_n *return_hardcode_ast(char **envp); +// env TMP +char **init_env(char **envp); + #endif diff --git a/tests/drawio/drawio_ast.c b/tests/drawio/drawio_ast.c index 5cc49e4..b0ded76 100644 --- a/tests/drawio/drawio_ast.c +++ b/tests/drawio/drawio_ast.c @@ -19,7 +19,7 @@ void set_ast_rect(t_dio_elem *rect) rect->x = 50; rect->y = 150; rect->w = 150; - rect->h = 100; + rect->h = 150; } const char *translate_state(t_state state) @@ -36,31 +36,86 @@ const char *translate_state(t_state state) out = "SIMPLE_CMD"; else out = "UNDEFINED"; - return (out); - } -void print_ast(t_ast_n *node, t_dio_elem *rect, int fd) +const char *translate_redir(t_redir redir) +{ + const char *out; + + if (redir == _RED_L) + out = "redir : < "; + else if (redir == _RED_R) + out = "redir : > "; + else if (redir == _RED_DR) + out = "redir : >> "; + else + out = "Not redirected "; + return (out); +} + +char *get_node_txt(t_ast_n *node) +{ + const char *st; + char *out; + char *cmd; + char *args; + const char *redir; + char *inf; + char *outf; + + st = translate_state(node->state); + if (node->state == _CMD) + { + cmd = ft_sprintf("%s%s%s", NL, node->cmd, NL); + args = ft_sprintf("future args", NL); + redir = translate_redir(node->redir); + inf = ft_sprintf("Infile : %s%s", node->infile, NL); + outf = ft_sprintf("Outfile : %s%s", node->outfile, NL); + } + else + { + cmd = ft_calloc(1, 1); + args = ft_calloc(1, 1); + redir = ""; + inf = ft_calloc(1, 1); + outf = ft_calloc(1, 1); + } + + out = ft_sprintf("%s%s%s%s%s%s%s", st, cmd, args, NL, redir, inf, outf); + free(cmd); + free(args); + free(inf); + free(outf); + return (out); +} + +int print_ast(t_ast_n *node, t_dio_elem *rect, t_dio_elem *arrow, int fd) { int i; - const char *state; + int node_id; if (!node || !rect) - return; + return -1; - state = translate_state(node->state); - rect->text = ft_sprintf("%s\ntest", state); - drawio_create_elem(fd, rect); + rect->text = get_node_txt(node); + node_id = drawio_create_elem(fd, rect); if (node->state != _PLINE) { rect->y += rect->h + 50; - print_ast(node->left, rect, fd); + arrow->id_dst = print_ast(node->left, rect, arrow, fd); + arrow->id_src = node_id; + drawio_create_elem(fd, arrow); + rect->x += rect->w + 50; - print_ast(node->right, rect, fd); + arrow->id_dst = print_ast(node->right, rect, arrow, fd); + arrow->id_src = node_id; + drawio_create_elem(fd, arrow); + rect->y -= rect->h + 50; - rect->x -= rect->w + 50; + if (node->state == _CMD) + rect->x -= rect->w + 50; } else { @@ -68,16 +123,24 @@ void print_ast(t_ast_n *node, t_dio_elem *rect, int fd) rect->y += rect->h + 50; while (node->pline[i]) { - print_ast(node->pline[i++], rect, fd); + arrow->id_dst = print_ast(node->pline[i++], rect, arrow, fd); + arrow->id_src = node_id; + drawio_create_elem(fd, arrow); rect->x += rect->w + 50; } } + return (node_id); } void gen_dio_ast(t_ast_n *head, int fd) { t_dio_elem rect; + t_dio_elem arrow; set_ast_rect(&rect); - print_ast(head, &rect, fd); + arrow.type = ARROW; + arrow.id_src = 0; + arrow.id_dst = 0; + + print_ast(head, &rect, &arrow, fd); return ; }