diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-10 08:11:59 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-10 08:11:59 +0200 |
| commit | 25ca78b5a53c931b0bc388aef099974dbba4b6ff (patch) | |
| tree | 64a78d39acd408cc3dc0acaaf4c304ed2e1227c6 /src/debug | |
| parent | a55e414f6709c6b22ff9805cd32a40fae9da2538 (diff) | |
| download | minishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.tar.gz minishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.tar.bz2 minishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.zip | |
Norming debugging function, Enabling them only when MINISHELL_TEST is defined
Diffstat (limited to 'src/debug')
| -rw-r--r-- | src/debug/lexer.c | 37 | ||||
| -rw-r--r-- | src/debug/parser.c | 112 |
2 files changed, 149 insertions, 0 deletions
diff --git a/src/debug/lexer.c b/src/debug/lexer.c new file mode 100644 index 0000000..4d25662 --- /dev/null +++ b/src/debug/lexer.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/10 07:51:39 by cacharle #+# #+# */ +/* Updated: 2020/10/10 07:56:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "lexer.h" + +#ifdef MINISHELL_TEST + +int debug_lexer(char *input) +{ + int status; + t_tok_lst *out; + + status = lexer(input, &out); + if (status != 0) + return (status); + while (out != NULL) + { + ft_putnbr(out->tag); + ft_putchar(' '); + ft_putstr(out->content); + ft_putendl(out->tag & TAG_STICK ? " STICK" : ""); + out = out->next; + } + return (status); +} + +#endif diff --git a/src/debug/parser.c b/src/debug/parser.c new file mode 100644 index 0000000..4eda29a --- /dev/null +++ b/src/debug/parser.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/10 07:53:11 by cacharle #+# #+# */ +/* Updated: 2020/10/10 08:06:35 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" +#include "minishell.h" +#include "parser.h" + +#ifdef MINISHELL_TEST + +static void st_tok_lst_put(t_tok_lst *tokens) +{ + while (tokens != NULL) + { + ft_putchar('|'); + ft_putstr(tokens->content); + ft_putstr("| "); + tokens = tokens->next; + } +} + +static void st_print_level(int level) +{ + while (level-- > 0) + ft_putstr(" "); +} + +void st_ast_print_parent_pipeline(int level, t_ast *ast) +{ + t_ftlst *curr; + + if (ast->tag == AST_PARENT) + { + st_print_level(level); + ft_putstr("parent: redir [ "); + st_tok_lst_put(ast->redirs); + ft_putstr(" ]\n"); + ast_print(level + 1, ast->parent_ast); + } + else if (ast->tag == AST_PIPELINE) + { + st_print_level(level); + ft_putstr("pipeline: {\n"); + curr = ast->pipeline; + while (curr != NULL) + { + ast_print(level + 1, (t_ast *)curr->data); + ft_putstr("\n"); + curr = curr->next; + } + st_print_level(level); + ft_putstr(" }\n"); + } +} + +void ast_print(int level, t_ast *ast) +{ + if (ast->tag == AST_PARENT || ast->tag == AST_PIPELINE) + st_ast_print_parent_pipeline(level, ast); + else if (ast->tag == AST_CMD) + { + st_print_level(level); + ft_putstr("cmd: [ "); + st_tok_lst_put(ast->cmd_argv); + ft_putstr(" ] redirs: ["); + st_tok_lst_put(ast->redirs); + ft_putstr(" ]"); + } + else if (ast->tag == AST_OP) + { + ft_putstr("{\n"); + st_print_level(level); + ft_putstr(" left:\n"); + ast_print(level + 1, ast->op.left); + ft_putstr("\n"); + st_print_level(level); + ft_putstr(" right:\n"); + ast_print(level + 1, ast->op.right); + ft_putstr("\n"); + st_print_level(level); + ft_putstr("}\n"); + } +} + +int debug_parser(char *input) +{ + int status; + t_tok_lst *out; + t_parsed *ret; + + status = lexer(input, &out); + if (status != 0) + { + ft_putendl("Lexer error\n"); + return (status); + } + ret = parse(out); + if (ret == NULL || ret->syntax_error) + return (1); + ast_print(0, ret->ast); + return (status); +} + +#endif |
