From ffb0542c425991503b8439f359917ab4ef274bf5 Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Sun, 5 Feb 2017 21:13:55 +0100 Subject: [PATCH] parsing loop stable --- 42sh/Makefile | 1 - 42sh/sample/while.sh | 19 +++++------ 42sh/src/main/shell_script.c | 8 ++--- 42sh/src/parser/ft_parse.c | 6 ++-- 42sh/src/parser/get_instruction.c | 6 ++-- 42sh/src/parser/get_instruction2.c | 4 +-- 42sh/src/parser/parse.c | 3 +- 42sh/src/parser/parse_newline.c | 27 --------------- 42sh/src/parser/parse_separator.c | 4 +-- 42sh/src/parser/parse_while.c | 53 +++++++----------------------- 10 files changed, 35 insertions(+), 96 deletions(-) delete mode 100644 42sh/src/parser/parse_newline.c diff --git a/42sh/Makefile b/42sh/Makefile index c6721347..1795c690 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -182,7 +182,6 @@ parser/parse_greatand.c\ parser/parse_less.c\ parser/parse_lessand.c\ parser/parse_list.c\ -parser/parse_newline.c\ parser/parse_separator.c\ parser/parse_subshell.c\ parser/parse_while.c\ diff --git a/42sh/sample/while.sh b/42sh/sample/while.sh index 29163c66..5adc86a6 100644 --- a/42sh/sample/while.sh +++ b/42sh/sample/while.sh @@ -1,12 +1,11 @@ -while - - ls | cat +while ls do - while [ 1 ] + while ls do - while [ 1 ] - do - echo - done - done -done > file1 > file2 + pwd + done + while ls + do + cd + done +done diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 5a7c525f..d91f4579 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/05 19:36:56 by ariard ### ########.fr */ +/* Updated: 2017/02/05 21:12:36 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,14 +26,14 @@ int shell_script() DG("after post_tokenize"); token_print(token); -// while (token) -// { + while (token) + { if (parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); // if (ft_exec(&ast)) // return (1); ast = NULL; -// } + } return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index e66f5ab9..9ba1ff15 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/05 18:54:19 by ariard ### ########.fr */ +/* Updated: 2017/02/05 21:10:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ t_parser g_parser[] = { - {TK_WHILE, &parse_while}, - {TK_SEMI | TK_NEWLINE, &parse_separator}, + {TK_WHILE | TK_NEWLINE, &get_instruction2}, + {TK_SEMI, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AMP, &parse_separator}, {TK_PIPE, &parse_separator}, diff --git a/42sh/src/parser/get_instruction.c b/42sh/src/parser/get_instruction.c index 8495ae39..2d6891b3 100644 --- a/42sh/src/parser/get_instruction.c +++ b/42sh/src/parser/get_instruction.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ -/* Updated: 2017/02/04 22:20:59 by ariard ### ########.fr */ +/* Updated: 2017/02/05 20:52:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ static int get_simple_instruction(t_list **start, t_list **lst) { t_list *temp; - DG("simple instruction"); temp = (*lst)->next; (*lst)->next = NULL; ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); @@ -30,7 +29,6 @@ static int get_compound_instruction(t_list **start, t_list **lst) t_token *token; int nest; - DG("compound instruction"); nest = 0; while (((*lst) = (*lst)->next)) { @@ -57,7 +55,7 @@ static int get_compound_instruction(t_list **start, t_list **lst) } return (0); } - + int get_instruction(t_list **lst) { t_token *token; diff --git a/42sh/src/parser/get_instruction2.c b/42sh/src/parser/get_instruction2.c index 02f0ee90..4d20972c 100644 --- a/42sh/src/parser/get_instruction2.c +++ b/42sh/src/parser/get_instruction2.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/04 18:20:53 by ariard #+# #+# */ -/* Updated: 2017/02/04 19:00:55 by ariard ### ########.fr */ +/* Updated: 2017/02/05 20:24:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int get_instruction2(t_btree **ast, t_list **start, t_list **lst) token = (*lst)->content; if (token->type == TK_NEWLINE) - return (parse_newline(ast, start, lst)); + return (parse_separator(ast, start, lst)); else if (token->type == TK_WHILE) return (parse_while(ast, start, lst)); return (0); diff --git a/42sh/src/parser/parse.c b/42sh/src/parser/parse.c index 3e881779..f7a24358 100644 --- a/42sh/src/parser/parse.c +++ b/42sh/src/parser/parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ -/* Updated: 2017/02/04 20:43:45 by ariard ### ########.fr */ +/* Updated: 2017/02/05 20:53:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int parse(t_btree **ast, t_list **token) { t_list *start; + (void)ast; start = *token; if (get_instruction(token)) return (1); diff --git a/42sh/src/parser/parse_newline.c b/42sh/src/parser/parse_newline.c deleted file mode 100644 index f007d56d..00000000 --- a/42sh/src/parser/parse_newline.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_newline.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/26 19:26:41 by ariard #+# #+# */ -/* Updated: 2017/02/05 17:17:00 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_newline(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - - token = (*lst)->content; - node = (*ast)->item; - node->type = TK_NEWLINE; - ft_parse(&(*ast)->right, &(*lst)->next); - ft_lstdelone(lst, &token_free); - ft_parse(&(*ast)->left, start); - return (0); -} diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 34a09c65..8e754cde 100644 --- a/42sh/src/parser/parse_separator.c +++ b/42sh/src/parser/parse_separator.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ -/* Updated: 2017/02/05 00:24:54 by ariard ### ########.fr */ +/* Updated: 2017/02/05 21:11:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,11 @@ int parse_separator(t_btree **ast, t_list **start, t_list **lst) t_token *token; t_astnode *node; - DG("newline"); token = (*lst)->content; node = (*ast)->item; node->type = token->type; ft_parse(&(*ast)->right, &(*lst)->next); + (*lst)->next = NULL; ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_parse(&(*ast)->left, start); return (0); diff --git a/42sh/src/parser/parse_while.c b/42sh/src/parser/parse_while.c index 1ba7cbc9..9ef48617 100644 --- a/42sh/src/parser/parse_while.c +++ b/42sh/src/parser/parse_while.c @@ -82,45 +82,21 @@ static int parse_head(t_btree **ast, node->type = token->type; ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); *lst = (*lst)->next; + delete_newline(start, lst); return (0); } -/*static int parse_condition(t_btree **ast, t_list **start, t_list **lst) -{ - t_token *token; - int nest; - - delete_newline(start, lst); - nest = 0; - while ((*lst)) - { - token = (*lst)->content; - if (token->type & TK_DO) - nest++; - else if (token->type & TK_DONE) - nest--; - if (nest == 1 && (token->type & TK_DO)) - break; - *lst = (*lst)->next; - } - (*lst)->next = NULL; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - DG("new token"); - token_print(*start); - ft_parse(&(*ast)->left, start); - return (0); -}*/ - -static int parse_execution(t_btree **ast, t_list **start, t_list **lst) +static int parse_loop(t_btree **ast, t_list **start, t_list **lst) { t_token *token; t_list *temp; + t_list *new_start; int nest; - (void)ast; nest = 0; while ((*lst)->next) *lst = (*lst)->next; + new_start = *start; ft_lst_reverse(start); temp = *lst; while ((*lst)) @@ -134,12 +110,13 @@ static int parse_execution(t_btree **ast, t_list **start, t_list **lst) break; *lst = (*lst)->next; } - ft_lst_reverse(&temp); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - *lst = (*lst)->next; - delete_newline(start, lst); - ft_parse(&(*ast)->right, lst); + temp = (*lst)->next; + (*lst)->next = NULL; + ft_lst_delif(&new_start, (*lst)->content, &ft_addrcmp, &token_free); + delete_newline(start, &temp); + ft_parse(&(*ast)->right, &temp); + ft_parse(&(*ast)->left, &new_start); return (0); } @@ -149,14 +126,6 @@ int parse_while(t_btree **ast, t_list **start, t_list **lst) parse_after_loop(ast, start, lst); parse_head(ast, &new_ast, start, lst); - parse_execution(&new_ast, start, lst); + parse_loop(&new_ast, start, lst); return (0); - -/* - temp = temp->next; - ft_parse(&(temp3)->right, &temp); - - return (0); - delete_all_newline(&temp); -*/ }