From 54394620893f7245c7697a57d724d430d06b57d1 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 6 Oct 2020 15:57:29 +0200 Subject: Added pipeline ast and pipeline evaluation (not working when first command is infinite) --- src/debug.c | 167 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 65 deletions(-) (limited to 'src/debug.c') diff --git a/src/debug.c b/src/debug.c index 3fe7784..42c34eb 100644 --- a/src/debug.c +++ b/src/debug.c @@ -6,19 +6,31 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/16 15:58:35 by charles #+# #+# */ -/* Updated: 2020/09/16 16:18:11 by charles ### ########.fr */ +/* Updated: 2020/10/06 13:14:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "lexer.h" +#include "parser.h" void debug_tok_lst(t_tok_lst *tokens) { while (tokens != NULL) { // FIXME libft for safer correction - printf("[%#06x] |%s|%s\n", tokens->tag, tokens->content, tokens->tag & TAG_STICK ? " STICK" : ""); + printf("[%#06x] |%s|%s\n", tokens->tag, tokens->content, + tokens->tag & TAG_STICK ? " STICK" : ""); + tokens = tokens->next; + } +} + +void debug_tok_lst_line(t_tok_lst *tokens) +{ + while (tokens != NULL) + { + // FIXME libft for safer correction + printf("|%s| ", tokens->content); tokens = tokens->next; } } @@ -35,66 +47,91 @@ int debug_lexer(char *input) return (status); } -/* void token_debug(void *v) */ -/* { */ -/* t_token *t; */ -/* */ -/* t= v; */ -/* printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content); */ -/* } */ -/* */ -/* void token_put(void *v) */ -/* { */ -/* t_token *t; */ -/* */ -/* t= v; */ -/* printf("%s ", t->content); */ -/* } */ -/* */ -/* void print_level(int level) */ -/* { */ -/* while (level-- > 0) */ -/* printf(" "); */ -/* } */ -/* */ -/* void ast_print(int level, t_ast *ast) */ -/* { */ -/* if (ast->tag == AST_PARENT) */ -/* { */ -/* print_level(level); */ -/* printf("parent: redir [ "); */ -/* ft_lstiter(ast->redirs, token_put); */ -/* printf(" ]\n"); */ -/* ast_print(level + 1, ast->parent_ast); */ -/* } */ -/* else if (ast->tag == AST_CMD) */ -/* { */ -/* print_level(level); */ -/* printf("cmd: [ "); */ -/* ft_lstiter(ast->cmd_argv, token_put); */ -/* printf(" ] redirs: ["); */ -/* ft_lstiter(ast->redirs, token_put); */ -/* printf(" ]"); */ -/* } */ -/* else if (ast->tag == AST_OP) { */ -/* #<{(| printf("SEP: %d\n", ast->op.sep); |)}># */ -/* print_level(level); */ -/* #<{(| printf("redirs: ["); |)}># */ -/* #<{(| ft_lstiter(ast->redirs, token_put); |)}># */ -/* #<{(| printf(" ] "); |)}># */ -/* printf("{\n"); */ -/* */ -/* print_level(level); */ -/* printf(" left:\n"); */ -/* ast_print(level + 1, ast->op.left); */ -/* */ -/* printf("\n"); */ -/* print_level(level); */ -/* printf(" right:\n"); */ -/* ast_print(level + 1, ast->op.right); */ -/* */ -/* printf("\n"); */ -/* print_level(level); */ -/* printf("}\n"); */ -/* } */ -/* } */ +void print_level(int level) +{ + while (level-- > 0) + printf(" "); +} + +void ast_print(int level, t_ast *ast) +{ + /* printf("%p\n", ast); */ + if (ast == NULL) + return ; + if (ast->tag == AST_PARENT) + { + print_level(level); + printf("parent: redir [ "); + debug_tok_lst_line(ast->redirs); + printf(" ]\n"); + ast_print(level + 1, ast->parent_ast); + } + else if (ast->tag == AST_PIPELINE) + { + print_level(level); + printf("pipeline: {\n"); + t_ftlst *curr = ast->pipeline; + /* printf("%p\n", curr); */ + /* printf("%p\n", curr->next); */ + /* printf("%p\n", curr->next->next); */ + + /* printf("%p\n", curr->data); */ + /* printf("%p\n", curr->next->); */ + while (curr != NULL) + { + ast_print(level + 1, (t_ast*)curr->data); + printf("\n"); + curr = curr->next; + } + print_level(level); + printf(" }\n"); + } + else if (ast->tag == AST_CMD) + { + print_level(level); + printf("cmd: [ "); + debug_tok_lst_line(ast->cmd_argv); + printf(" ] redirs: ["); + debug_tok_lst_line(ast->redirs); + printf(" ]"); + } + else if (ast->tag == AST_OP) { + /* printf("SEP: %d\n", ast->op.sep); */ + /* printf("redirs: ["); */ + /* ft_lstiter(ast->redirs, token_put); */ + /* printf(" ] "); */ + printf("{\n"); + + print_level(level); + printf(" left:\n"); + ast_print(level + 1, ast->op.left); + + printf("\n"); + print_level(level); + printf(" right:\n"); + ast_print(level + 1, ast->op.right); + + printf("\n"); + print_level(level); + printf("}\n"); + } +} + +int debug_parser(char *input) +{ + int status; + t_tok_lst *out; + + status = lexer(input, &out); + if (status != 0) + { + printf("Lexer error\n"); + debug_tok_lst(out); + return (status); + } + t_parsed *ret = parse(out); + if (ret == NULL || ret->syntax_error) + return (1); + ast_print(0, ret->ast); + return (status); +} -- cgit