From 1c1c75934a0d26a76568845afb797d6aad44c584 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 13 Mar 2017 22:50:02 +0100 Subject: [PATCH 1/2] news parsing if and case --- 42sh/Makefile | 1 - 42sh/file | 0 42sh/file1 | 16 +++++++ 42sh/includes/parser.h | 4 -- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_bang.c | 2 +- 42sh/src/parser/add_case.c | 16 ++----- 42sh/src/parser/add_cmd.c | 5 +- 42sh/src/parser/add_condition.c | 6 +-- 42sh/src/parser/add_redir_condition.c | 69 --------------------------- 42sh/src/parser/aggregate_sym.c | 15 +++--- 42sh/src/parser/eval_sym.c | 10 ++-- 42sh/src/parser/produce_sym.c | 8 ++-- 42sh/src/parser/read_stack.c | 4 +- 14 files changed, 47 insertions(+), 111 deletions(-) create mode 100644 42sh/file create mode 100644 42sh/file1 delete mode 100644 42sh/src/parser/add_redir_condition.c diff --git a/42sh/Makefile b/42sh/Makefile index b93d601d..9603b0b3 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -283,7 +283,6 @@ parser/add_loop.c\ parser/add_math.c\ parser/add_number.c\ parser/add_redir.c\ -parser/add_redir_condition.c\ parser/add_sep.c\ parser/add_subshell.c\ parser/add_var.c\ diff --git a/42sh/file b/42sh/file new file mode 100644 index 00000000..e69de29b diff --git a/42sh/file1 b/42sh/file1 new file mode 100644 index 00000000..8ef55af3 --- /dev/null +++ b/42sh/file1 @@ -0,0 +1,16 @@ +42sh +Makefile +STDBUG +TESTSHELL +condition_file +donovan_segaults_06-02 +file +file1 +includes +libft +objs +pdf +sample +src +test_framework.sh +update_makefile.sh diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 207cc49c..e1f60792 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -110,7 +110,6 @@ int add_condition_sep(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst); int add_redir_word(t_btree **ast, t_list **lst); int add_redir_type(t_btree **ast, t_list **lst); -int add_redir_condition(t_btree **ast, t_list **lst); int redir_init(t_type type, t_redir *redir); int add_case_cmd(t_btree **ast, t_list **lst); int add_case_sep(t_btree **ast, t_list **lst); @@ -141,7 +140,6 @@ int isfunc_name(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); -int isdir_condition(t_btree **ast, t_list **list); int isdir_word(t_btree **ast, t_list **list); int isvar(t_btree **ast, t_list **list); int isnull(t_btree **ast, t_list **list); @@ -155,8 +153,6 @@ int join_ast(t_btree **ast, t_btree **new_node); int gen_node(t_btree **ast); int superflous_token(t_btree **ast, t_list **list); -int ft_show_heredoc_data(t_astnode *node); - struct s_distrostree { int (*test)(t_btree **ast, t_list **lst); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index ef5c9483..59908643 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/13 17:51:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 21:36:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_bang.c b/42sh/src/parser/add_bang.c index 78c9df57..6a6752fb 100644 --- a/42sh/src/parser/add_bang.c +++ b/42sh/src/parser/add_bang.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/10 14:57:45 by ariard #+# #+# */ -/* Updated: 2017/03/13 19:32:08 by ariard ### ########.fr */ +/* Updated: 2017/03/13 20:36:01 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index f80d21ec..13f54c5f 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/04 20:42:13 by ariard #+# #+# */ -/* Updated: 2017/03/13 17:32:22 by ariard ### ########.fr */ +/* Updated: 2017/03/13 22:38:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int iscase(t_btree **ast, t_list **lst) || node->type == TK_AMP || node->type == TK_PIPE) && iscase(&(*ast)->right, lst) == 1) return (1); - if (node->type == TK_CASE || node->type == TK_PAREN_OPEN) + if (node->type == TK_CASE) return (1); } return (0); @@ -91,18 +91,12 @@ int add_pattern(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; - char **my_tab; token = (*lst)->content; node = (*ast)->item; - if ((my_tab = (char **)malloc(sizeof(char *) * 4))) - { - my_tab[0] = ft_strdup(token->data); - my_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3); - my_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3); - my_tab[3] = NULL; - } - ft_ld_pushback(&node->data.cmd.wordlist, my_tab); + DG("node type is %s", read_state(node->type)); + ft_ld_pushback(&node->data.cmd.wordlist, + gen_tab(token->data, token->esc, token->esc2, 1)); node->pattern = 1; return (0); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 5257d67d..2a2de835 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/03/13 17:37:50 by ariard ### ########.fr */ +/* Updated: 2017/03/13 22:45:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ t_distrostree g_distrostree[] = { {&superflous_token, &add_null}, - {&isdir_condition, &add_redir_condition}, {&isdir_sep, &add_redir_type}, {&isdir_word, &add_redir_word}, {&isvar, &add_var}, @@ -24,8 +23,8 @@ t_distrostree g_distrostree[] = {&iscondition_branch, &add_branch}, {&iscondition, &add_condition_cmd}, {&iscase_pattern, &add_pattern}, - {&iscase_branch, &add_branch}, {&iscase, &add_case_cmd}, + {&iscase_branch, &add_branch}, {&ismath, &add_null}, {&ismath_expr, &add_pattern}, {&issubshell, &add_subshell_cmd}, diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index d72d3cfd..db5ea7de 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/10 17:06:16 by ariard #+# #+# */ -/* Updated: 2017/03/13 16:10:30 by ariard ### ########.fr */ +/* Updated: 2017/03/13 20:46:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ int iscondition_branch(t_btree **ast, t_list **lst) if (*ast) { node = (*ast)->item; - if ((node->type == TK_IF || node->type == TK_ELIF) + if ((node->type == TK_ELIF) && (token->type == TK_ELIF || token->type == TK_ELSE) && node->nest == 0) return (1); @@ -93,6 +93,6 @@ int add_if(t_btree **ast, t_list **lst) node = (*ast)->item; node->type = TK_IF; token->type = TK_ELIF; - add_cmd(ast, lst); + add_cmd(&(*ast)->right, lst); return (0); } diff --git a/42sh/src/parser/add_redir_condition.c b/42sh/src/parser/add_redir_condition.c deleted file mode 100644 index 2ccf221d..00000000 --- a/42sh/src/parser/add_redir_condition.c +++ /dev/null @@ -1,69 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* add_redir_condition.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/11 14:54:18 by ariard #+# #+# */ -/* Updated: 2017/03/13 14:08:09 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -static int isdir_sep_condition(t_btree **ast)//, t_list **lst) -{ -// t_token *token; - t_astnode *node; - -// token = (*lst)->content; - if (*ast) - { - node = (*ast)->item; - if (node->type == TK_IF || node->type == TK_CASE) - return (1); - } - return (0); -} - -static int isdir_word_condition(t_btree **ast)//, t_list **lst) -{ -// t_token *token; - t_astnode *node; - -// token = (*lst)->content; - if (*ast) - { - node = (*ast)->item; - if (node->type == REDIR) - return (1); - } - return (0); -} - -int isdir_condition(t_btree **ast, t_list **lst) -{ - t_token *token; -// t_astnode *node; - - token = (*lst)->content; - if (*ast) - { -// node = (*ast)->item; - if ((token->type == TK_LESS || token->type == TK_GREAT - || token->type == TK_GREATAND || token->type == TK_LESSAND - || token->type == TK_DLESS || token->type == TK_DGREAT) - && isdir_sep_condition(&(*ast)->left) == 1) - return (1); - if (token->type == TK_WORD - && isdir_word_condition(&(*ast)->left) == 1) - return (1); - } - return (0); -} - -int add_redir_condition(t_btree **ast, t_list **lst) -{ - return (add_cmd(&(*ast)->left, lst)); -} diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index d3371fc0..e3c62d61 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 15:58:38 by ariard #+# #+# */ -/* Updated: 2017/03/13 19:40:56 by ariard ### ########.fr */ +/* Updated: 2017/03/13 20:36:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,13 +57,14 @@ t_aggrematch g_aggrematch[] = {TK_RBRACE, CMD_SUPERIOR, BRACE_CLAUSE, TK_LBRACE}, {TK_RBRACE, PIPE_SEMI_SEQUENCE, BRACE_CLAUSE, TK_LBRACE}, {TK_RBRACE, PIPE_CLOSE_SEQUENCE, BRACE_CLAUSE, TK_LBRACE}, - {TK_RBRACE, COMPOUND_LIST, BRACE_CLAUSE, TK_LBRACE}, {TK_RBRACE, CMD_SUPERIOR, BRACE_CLAUSE, TK_LBRACE}, {TK_AND_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR}, - {TK_OR_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR}, + {TK_AND_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE}, {TK_AND_IF, PIPE_SEMI_SEQUENCE, AND_OR_MINOR, PIPE_SEMI_SEQUENCE}, {TK_OR_IF, PIPE_SEMI_SEQUENCE, AND_OR_MINOR, PIPE_SEMI_SEQUENCE}, + {TK_OR_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR}, + {TK_OR_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE}, {SEPARATOR_OP, MATH_SUP, CMD_SUPERIOR, MATH_SUP}, {SEPARATOR_OP, CMD_SUPERIOR, SEPARATOR, 0}, {SEPARATOR_OP, COMPOUND_LIST, SEPARATOR, 0}, @@ -390,20 +391,20 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); i = 0; head = (*stack)->content; - DG("aggregate head %s && sym %s", - read_state(*head), read_state(*new_sym)); +// DG("aggregate head %s && sym %s", +// read_state(*head), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top && MATCH_STACK(*head, g_aggrematch[i].under)) { - DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); +// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); head = (*stack)->content; - DG("stack after pop: %s", read_state(*head)); + // DG("stack after pop: %s", read_state(*head)); } if (eval_sym(stack, *new_sym)) { diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 15628a30..7c2b27d7 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/13 19:39:30 by ariard ### ########.fr */ +/* Updated: 2017/03/13 20:36:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,10 +57,12 @@ t_stackmatch g_stackmatch[] = {TK_AND_IF, AND_OR}, {TK_AND_IF, CMD_SUPERIOR}, {TK_AND_IF, PIPE_SEMI_SEQUENCE}, + {TK_AND_IF, PIPE_CLOSE_SEQUENCE}, {TK_AND_IF, COMPOUND_LIST}, {TK_OR_IF, AND_OR}, {TK_OR_IF, CMD_SUPERIOR}, {TK_OR_IF, PIPE_SEMI_SEQUENCE}, + {TK_OR_IF, PIPE_CLOSE_SEQUENCE}, {TK_OR_IF, COMPOUND_LIST}, {TK_DSEMI, LINEBREAK}, {TK_DSEMI, TK_BANG}, @@ -542,8 +544,6 @@ t_stackmatch g_stackmatch[] = {TK_PAREN_CLOSE, SEPARATOR_OP}, {TK_PAREN_CLOSE, WORD}, {TK_PAREN_CLOSE, IN}, - {TK_PAREN_CLOSE, TK_AND_IF}, - {TK_PAREN_CLOSE, TK_OR_IF}, {TK_PAREN_CLOSE, TK_PIPE}, {TK_PAREN_CLOSE, COMPLETE_COMMANDS}, {TK_PAREN_CLOSE, CMD_SUPERIOR}, @@ -564,8 +564,6 @@ t_stackmatch g_stackmatch[] = {TK_RBRACE, SEPARATOR_OP}, {TK_RBRACE, WORD}, {TK_RBRACE, IN}, - {TK_RBRACE, TK_AND_IF}, - {TK_RBRACE, TK_OR_IF}, {TK_RBRACE, TK_PIPE}, {TK_RBRACE, TK_RBRACE}, {TK_RBRACE, COMPLETE_COMMANDS}, @@ -1266,7 +1264,7 @@ int eval_sym(t_list **stack, t_sym new_sym) if (!*stack) return (1); head = (*stack)->content; - DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); +// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 40b460b3..909d8d42 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/13 17:30:53 by ariard ### ########.fr */ +/* Updated: 2017/03/13 20:37:27 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -134,8 +134,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) return (1); token = (*lst)->content; head = (*stack)->content; - DG("produce stack : %s && token : %s", read_state(*head), - read_state(token->type)); +// DG("produce stack : %s && token : %s", read_state(*head), +// read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) @@ -143,7 +143,7 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) if (token->type == g_prodmatch[i].token && *head == g_prodmatch[i].stack) { - DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); +// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); *new_sym = g_prodmatch[i].new_sym; } i++; diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 4d91117c..e9c7a047 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/13 14:13:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 19:52:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ char *read_state(t_sym current) { + if (current == PIPE_CLOSE_SEQUENCE) + return ("PIPE_CLOSE_SEQUENCE"); if (current == TK_BANG) return ("TK_BANG"); if (current == OPEN_FUNC) From 8fa77616a2523a9ba5ce733cbe95b277135a0599 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 13 Mar 2017 22:50:13 +0100 Subject: [PATCH 2/2] news parsing if and case --- 42sh/file | 0 42sh/file1 | 16 ---------------- 2 files changed, 16 deletions(-) delete mode 100644 42sh/file delete mode 100644 42sh/file1 diff --git a/42sh/file b/42sh/file deleted file mode 100644 index e69de29b..00000000 diff --git a/42sh/file1 b/42sh/file1 deleted file mode 100644 index 8ef55af3..00000000 --- a/42sh/file1 +++ /dev/null @@ -1,16 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -condition_file -donovan_segaults_06-02 -file -file1 -includes -libft -objs -pdf -sample -src -test_framework.sh -update_makefile.sh