ast viewer

This commit is contained in:
Nathan Lebrun
2025-01-28 16:58:46 +01:00
parent 16e351d92d
commit 7f144ab089
4 changed files with 122 additions and 20 deletions

View File

@@ -15,6 +15,8 @@
# include "../../libft.h" # include "../../libft.h"
# define NL "
"
typedef enum e_dio_elemtype typedef enum e_dio_elemtype
{ {
RECT, RECT,

View File

@@ -51,18 +51,51 @@ t_ast_n *return_hardcode_ast(char **envp)
{ {
t_ast_n *head; t_ast_n *head;
// head
head = created_ast_n(_AND, NULL, NULL); head = created_ast_n(_AND, NULL, NULL);
head->env = init_env(envp); 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 = created_ast_n(_PLINE, head, head);
head->right->pline = malloc(sizeof(t_ast_n *) * 4); head->right->pline = malloc(sizeof(t_ast_n *) * 4);
// ===0===
head->right->pline[0] = created_ast_n(_CMD, head->right, head); head->right->pline[0] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[0], "ls", "ls"); setup_cmd(head->right->pline[0], "ls", "ls");
head->right->pline[1] = created_ast_n(_CMD, head->right, head); // ===1===
setup_cmd(head->right->pline[1], "cat", "cat -e"); 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); 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; head->right->pline[3] = NULL;
return (head); return (head);
} }

View File

@@ -13,7 +13,8 @@
#ifndef AST_H #ifndef AST_H
# define AST_H # define AST_H
# include "../../includes/env.h" /*# include "../../includes/env.h"*/
# include "../../lib/libft/libft.h"
typedef enum e_state typedef enum e_state
{ {
@@ -55,4 +56,7 @@ typedef struct s_ast_n
t_ast_n *return_hardcode_ast(char **envp); t_ast_n *return_hardcode_ast(char **envp);
// env TMP
char **init_env(char **envp);
#endif #endif

View File

@@ -19,7 +19,7 @@ void set_ast_rect(t_dio_elem *rect)
rect->x = 50; rect->x = 50;
rect->y = 150; rect->y = 150;
rect->w = 150; rect->w = 150;
rect->h = 100; rect->h = 150;
} }
const char *translate_state(t_state state) const char *translate_state(t_state state)
@@ -36,31 +36,86 @@ const char *translate_state(t_state state)
out = "SIMPLE_CMD"; out = "SIMPLE_CMD";
else else
out = "UNDEFINED"; out = "UNDEFINED";
return (out); 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 : < &#10;";
else if (redir == _RED_R)
out = "redir : > &#10;";
else if (redir == _RED_DR)
out = "redir : >> &#10;";
else
out = "Not redirected &#10;";
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; int i;
const char *state; int node_id;
if (!node || !rect) if (!node || !rect)
return; return -1;
state = translate_state(node->state); rect->text = get_node_txt(node);
rect->text = ft_sprintf("%s\ntest", state); node_id = drawio_create_elem(fd, rect);
drawio_create_elem(fd, rect);
if (node->state != _PLINE) if (node->state != _PLINE)
{ {
rect->y += rect->h + 50; 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; 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->y -= rect->h + 50;
rect->x -= rect->w + 50; if (node->state == _CMD)
rect->x -= rect->w + 50;
} }
else else
{ {
@@ -68,16 +123,24 @@ void print_ast(t_ast_n *node, t_dio_elem *rect, int fd)
rect->y += rect->h + 50; rect->y += rect->h + 50;
while (node->pline[i]) 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; rect->x += rect->w + 50;
} }
} }
return (node_id);
} }
void gen_dio_ast(t_ast_n *head, int fd) void gen_dio_ast(t_ast_n *head, int fd)
{ {
t_dio_elem rect; t_dio_elem rect;
t_dio_elem arrow;
set_ast_rect(&rect); 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 ; return ;
} }