From d50a0d1b1374a797dd3d5a31ff3e021a621f143c Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Fri, 24 Feb 2017 16:29:51 +0100 Subject: [PATCH] parsing for ok --- 42sh/includes/parser.h | 31 ++---------------- 42sh/sample/for/for.sh | 8 ++--- 42sh/sample/while/while01.sh | 11 +++++++ 42sh/src/lexer/insert_newline.c | 2 +- 42sh/src/main/ft_putast.c | 6 +++- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_case.c | 2 +- 42sh/src/parser/add_cmd.c | 6 ++-- 42sh/src/parser/add_condition.c | 2 +- 42sh/src/parser/add_loop.c | 57 ++++++++++++++++++--------------- 42sh/src/parser/add_sep.c | 16 ++++----- 42sh/src/parser/build_tree.c | 13 +++++--- 42sh/src/parser/tree_wrapper.c | 2 +- 13 files changed, 78 insertions(+), 80 deletions(-) create mode 100644 42sh/sample/while/while01.sh diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 3f418fbe..f3c28da4 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -213,13 +213,14 @@ int add_cmd(t_btree **ast, t_list **lst); int add_file(t_btree **ast, t_list **lst); int add_loop_cmd(t_btree **ast, t_list **lst); int add_loop_sep(t_btree **ast, t_list **lst); +int add_loop_condition(t_btree **ast, t_list **lst); int add_condition_cmd(t_btree **ast, t_list **lst); int add_condition_sep(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst); int add_case_cmd(t_btree **ast, t_list **lst); int add_case_sep(t_btree **ast, t_list **lst); int add_pattern(t_btree **ast, t_list **lst); -int isloop(t_btree **ast); +int isloop(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscase(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst); @@ -250,6 +251,7 @@ union u_astdata { t_redir redir; t_ld *token; + t_list *wordlist; char **sstr; char *str; }; @@ -264,31 +266,4 @@ struct s_astnode t_astdata data; }; -int parse(t_btree **ast, t_list **token); - -int get_instruction(t_list **lst); -int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst); - -int parse_newline(t_btree **ast, t_list **start, t_list **lst); -int parse_separator(t_btree **ast, t_list **start, t_list **lst); -int parse_redir(t_btree **ast, t_list **start, t_list **lst); -int parse_less(t_btree **ast, t_list **start, t_list **lst); -int parse_great(t_btree **ast, t_list **start, t_list **lst); -int parse_dless(t_btree **ast, t_list **start, t_list **lst); -int parse_dgreat(t_btree **ast, t_list **start, t_list **lst); -int parse_lessand(t_btree **ast, t_list **start, t_list **lst); -int parse_greatand(t_btree **ast, t_list **start, t_list **lst); -int parse_word(t_btree **ast, t_list **start, t_list **lst); -int parse_subshell(t_btree **ast, t_list **start, t_list **lst); -int parse_newline(t_btree **ast, t_list **start, t_list **lst); -int parse_while(t_btree **ast, t_list **start, t_list **lst); -int parse_if(t_btree **ast, t_list **start, t_list **lst); -int parse_do(t_btree **ast, t_list **start, t_list **lst); -int parse_done(t_btree **ast, t_list **start, t_list **lst); -int parse_elif(t_btree **ast, t_list **start, t_list **lst); -int parse_else(t_btree **ast, t_list **start, t_list **lst); - -int delete_newline(t_list **start, t_list **lst); -int parse_head(t_btree **ast, t_btree **new_ast, t_list **start, t_list **lst); - #endif diff --git a/42sh/sample/for/for.sh b/42sh/sample/for/for.sh index 676abb4b..90cfffe6 100644 --- a/42sh/sample/for/for.sh +++ b/42sh/sample/for/for.sh @@ -1,11 +1,11 @@ -for i in ls - do +for i in hello bonjour salut comment +do for i in echo do pwd done - for i in cd + for i in echo do - cat + pwd done done diff --git a/42sh/sample/while/while01.sh b/42sh/sample/while/while01.sh new file mode 100644 index 00000000..f2897742 --- /dev/null +++ b/42sh/sample/while/while01.sh @@ -0,0 +1,11 @@ +while ls +do + while ls + do + pwd + done + while ls + do + pwd + done +done diff --git a/42sh/src/lexer/insert_newline.c b/42sh/src/lexer/insert_newline.c index ea17e055..6d39ec5d 100644 --- a/42sh/src/lexer/insert_newline.c +++ b/42sh/src/lexer/insert_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 21:05:23 by ariard #+# #+# */ -/* Updated: 2017/02/21 21:12:13 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:22:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 8720b1df..a535e33d 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/02/21 19:20:20 by jhalford ### ########.fr */ +/* Updated: 2017/02/24 16:24:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,10 @@ char *ft_putast(void *nodein) { t_astnode *node; node = nodein; + if (node->type == TK_NAME) + return ("TK_NAME"); + if (node->type == TK_FOR) + return ("TK_FOR"); if (node->type == TK_CASE) return ("TK_CASE"); else if (node->type == TK_PAREN_OPEN) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index f321b2a8..03b10ea2 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/02/21 22:44:41 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:17:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index ff0d1532..7eba9636 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -55,7 +55,7 @@ int add_pattern(t_btree **ast, t_list **lst) token = (*lst)->content; node = (*ast)->item; - node->data.redir.word.word = ft_strdup(token->data); + node->data.str = ft_strdup(token->data); node->pattern = 1; return (0); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 40da190d..f976f875 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/02/24 15:30:23 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:29:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,9 @@ int add_cmd(t_btree **ast, t_list **lst) gen_node(ast); else if (isdir(ast)) return (add_file(ast, lst)); - else if (isloop(ast)) + else if (isloop(ast, lst) == 3) + return (add_loop_condition(ast, lst)); + else if (isloop(ast, lst)) return (add_loop_cmd(ast, lst)); else if (iscondition(ast, lst) == 1) return (add_condition_cmd(ast, lst)); diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index 4c2af18c..b95c45ab 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 18:12:52 by ariard #+# #+# */ -/* Updated: 2017/02/24 14:53:49 by ariard ### ########.fr */ +/* Updated: 2017/02/24 15:45:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index c830f5b4..7a03b988 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -6,33 +6,33 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 22:17:14 by ariard #+# #+# */ -/* Updated: 2017/02/24 15:08:08 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:28:27 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int isloop(t_btree **ast) +int isloop(t_btree **ast, t_list **lst) { t_astnode *node; + t_token *token; node = NULL; + token = (*lst)->content; if (*ast) { node = (*ast)->item; + if (node->type == TK_FOR && token->type == TK_WORD && node->pattern == 0) + return (3); if ((node->type == TK_NEWLINE || node->type == TK_SEMI - || node->type == TK_AMP) && isloop(&(*ast)->right) == 1) + || node->type == TK_AMP) && isloop(&(*ast)->right, lst) == 1) return (1); - if ((node->type == TK_WHILE || node->type == TK_UNTIL) && node->full == 1) - { - DG("DON ENTER"); + if ((node->type == TK_WHILE || node->type == TK_UNTIL + || node->type == TK_FOR) && node->full == 1) return (2); - } - if ((node->type == TK_WHILE || node->type == TK_UNTIL) && node->full == 0) - { - DG(" NOFULL"); + if ((node->type == TK_WHILE || node->type == TK_UNTIL + || node->type == TK_FOR) && node->full == 0) return (1); - } } return (0); } @@ -45,27 +45,20 @@ int add_loop_cmd(t_btree **ast, t_list **lst) token = (*lst)->content; node = (*ast)->item; DG("add loop cmd"); - if ((token->type == TK_WHILE || token->type == TK_UNTIL) - && (node->type == TK_WHILE || node->type == TK_UNTIL)) - { - DG("nest one more"); + if (token->type == TK_DO && node->type == TK_FOR) + node->pattern = 1; + if ((token->type == TK_WHILE || token->type == TK_UNTIL || token->type == TK_FOR) + && (node->type == TK_WHILE || node->type == TK_UNTIL || node->type == TK_FOR)) node->nest++; - } - if (token->type == TK_DONE && (node->type == TK_WHILE || node->type == TK_UNTIL) - && node->nest > 0) - { + if (token->type == TK_DONE && (node->type == TK_WHILE + || node->type == TK_UNTIL || node->type == TK_FOR) && node->nest > 0) node->nest--; - DG("nest one less"); - } else if (token->type == TK_DONE && (node->type == TK_WHILE - || node->type == TK_UNTIL) && node->nest == 0) - { - DG("WHILE FULL"); + || node->type == TK_UNTIL || node->type == TK_FOR) && node->nest == 0) return ((node->full = 1)); - } if (token->type == TK_DO && node->nest == 0) return (add_cmd(&(*ast)->right, lst)); - else if (!(*ast)->right && isloop(&(*ast)->left) != 2) + else if (!(*ast)->right && isloop(&(*ast)->left, lst) != 2) return (add_cmd(&(*ast)->left, lst)); else return (add_cmd(&(*ast)->right, lst)); @@ -80,3 +73,15 @@ int add_loop_sep(t_btree **ast, t_list **lst) return (add_sep(&(*ast)->right, lst)); return (0); } + +int add_loop_condition(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + token = (*lst)->content; + node = (*ast)->item; + ft_lsteadd(&node->data.wordlist, ft_lstnew(ft_strdup(token->data), + ft_strlen(token->data))); + return (0); +} diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index a30ccb71..7dbbb270 100644 --- a/42sh/src/parser/add_sep.c +++ b/42sh/src/parser/add_sep.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 19:12:07 by ariard #+# #+# */ -/* Updated: 2017/02/24 15:15:33 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:08:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int add_sep(t_btree **ast, t_list **lst) t_btree *new_node; DG("add sep"); - if (isloop(ast) == 1) + if (isloop(ast, lst) == 1) return (add_loop_sep(ast, lst)); else if (iscondition(ast, lst) == 1) return (add_condition_sep(ast, lst)); @@ -28,13 +28,11 @@ int add_sep(t_btree **ast, t_list **lst) if (!*ast) gen_node(ast); token = (*lst)->content; -// if (node->type != TK_DO) -// { - new_node = NULL; - gen_node(&new_node); - join_ast(ast, &new_node); - node = (new_node)->item; -// } +//watch != TK_DO + new_node = NULL; + gen_node(&new_node); + join_ast(ast, &new_node); + node = (new_node)->item; node->type = token->type; return (0); } diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index a0c573a7..021aef29 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ -/* Updated: 2017/02/24 15:21:16 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:24:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,13 +35,15 @@ t_treematch g_treematch[] = {TK_ESAC, &add_cmd}, {TK_PAREN_OPEN, &add_cmd}, {TK_PAREN_CLOSE, &add_cmd}, + {TK_FOR, &add_cmd}, {0, NULL}, }; static int isseparator(int type, int cache) { if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO - || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN)) + || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN + || cache == TK_WORD)) return (0); return (1); } @@ -54,8 +56,8 @@ int build_tree(t_btree **ast, t_list **lst) i = 0; token = (*lst)->content; -//check bug de cache - cache = token->type; +//check bug de cache case ? +// cache = token->type; while (g_treematch[i].type) { if (g_treematch[i].type == token->type @@ -63,7 +65,8 @@ int build_tree(t_btree **ast, t_list **lst) { DG("func TK : '%s' TK : '%s'", - read_state(g_treematch[i].type) ,read_state(token->type)); + read_state(g_treematch[i].type) ,read_state(token->type)); + cache = token->type; return (g_treematch[i].add(ast, lst)); } i++; diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index 7fbc3c2a..f667a6fe 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:57:44 by ariard #+# #+# */ -/* Updated: 2017/02/19 16:34:17 by ariard ### ########.fr */ +/* Updated: 2017/02/24 16:00:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */