From cd2019c0fde9ac69c414fcf66cc6bb910b84f97d Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Sun, 5 Feb 2017 00:15:12 +0100 Subject: [PATCH] fucking newline to parse --- 42sh/Makefile | 2 + 42sh/file1 | 26 +++++++++++ 42sh/includes/parser.h | 8 ++-- 42sh/sample/2lines.sh | 4 +- 42sh/sample/while.sh | 16 +++---- 42sh/src/lexer/ft_post_tokenize.c | 2 +- 42sh/src/lexer/get_reserved_words.c | 2 +- 42sh/src/lexer/lexer_great.c | 2 +- 42sh/src/main/shell_script.c | 15 ++++--- 42sh/src/parser/ft_parse.c | 5 +-- 42sh/src/parser/get_instruction.c | 68 +++++++++++++++++++++++++---- 42sh/src/parser/get_instruction2.c | 25 +++++++++++ 42sh/src/parser/parse.c | 25 +++++++++++ 42sh/src/parser/parse_great.c | 2 +- 42sh/src/parser/parse_newline.c | 2 +- 42sh/src/parser/parse_separator.c | 3 +- 42sh/src/parser/parse_while.c | 64 ++++++++++++++++----------- 17 files changed, 207 insertions(+), 64 deletions(-) create mode 100644 42sh/file1 create mode 100644 42sh/src/parser/get_instruction2.c create mode 100644 42sh/src/parser/parse.c diff --git a/42sh/Makefile b/42sh/Makefile index 1f9b5d6f..c6721347 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -169,8 +169,10 @@ main/shell_get_opts.c\ main/shell_init.c\ main/shell_script.c\ main/sig_handler.c\ +parser/parse.c\ parser/ft_parse.c\ parser/get_instruction.c \ +parser/get_instruction2.c \ parser/parse_dgreat.c\ parser/parse_dless.c\ parser/parse_do.c\ diff --git a/42sh/file1 b/42sh/file1 new file mode 100644 index 00000000..92953fdf --- /dev/null +++ b/42sh/file1 @@ -0,0 +1,26 @@ +42sh +Makefile +debug +file1 +includes +libft +objs +pdf +sample +script.sh +src +update_makefile.sh +/Users/ariard/Projects/42sh +42sh +Makefile +debug +file1 +includes +libft +objs +pdf +sample +script.sh +src +update_makefile.sh +monde diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 67731f84..68047eb3 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/03 17:02:27 by ariard ### ########.fr */ +/* Updated: 2017/02/04 19:17:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,9 +59,11 @@ struct s_astnode extern t_parser g_parser[]; -int ft_parse(t_btree **ast, t_list **token); +int parse(t_btree **ast, t_list **token); -int get_instruction(t_btree **ast, t_list **start, t_list **lst); +int ft_parse(t_btree **ast, t_list **token); +int get_instruction(t_list **lst); +int get_instruction2(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); diff --git a/42sh/sample/2lines.sh b/42sh/sample/2lines.sh index 654889de..cf36d0bf 100644 --- a/42sh/sample/2lines.sh +++ b/42sh/sample/2lines.sh @@ -1,3 +1 @@ -ls ; echo ; pwd | cat > file2 -ls | cat -pwd ; echo hello +ls ; pwd > file1 ; ls | cat diff --git a/42sh/sample/while.sh b/42sh/sample/while.sh index 88d0b181..5c8513f7 100644 --- a/42sh/sample/while.sh +++ b/42sh/sample/while.sh @@ -1,9 +1,7 @@ -while [ 1 ] -do while pwd - do - while ls - do - echo hello - done - done -done +while ls | cat ; pwd +do + while ls | cat + do + echo monde ; cat + done +done > file1 diff --git a/42sh/src/lexer/ft_post_tokenize.c b/42sh/src/lexer/ft_post_tokenize.c index 1f806fe1..f951fe2b 100644 --- a/42sh/src/lexer/ft_post_tokenize.c +++ b/42sh/src/lexer/ft_post_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */ -/* Updated: 2017/02/04 15:33:04 by ariard ### ########.fr */ +/* Updated: 2017/02/04 15:54:27 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index f02c9e0d..b81df18d 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/02/04 15:27:49 by ariard ### ########.fr */ +/* Updated: 2017/02/04 17:15:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 8457ef2b..84da01ab 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:49:11 by ariard ### ########.fr */ +/* Updated: 2017/02/04 21:27:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 9e038f8f..8c9042a1 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/04 15:30:00 by ariard ### ########.fr */ +/* Updated: 2017/02/04 22:16:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,13 +26,14 @@ int shell_script() DG("after post_tokenize"); token_print(token); - while (token) - { - if (ft_parse(&ast, &token)) +// while (token) +// { + if (parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); - // if (ft_exec(&ast)) - // return (1); - } +// 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 93430d33..3f2d2659 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/03 19:37:05 by ariard ### ########.fr */ +/* Updated: 2017/02/05 00:14:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_parser g_parser[] = { - {TK_NEWLINE | TK_WHILE, &get_instruction}, + {TK_WHILE, &parse_while}, {TK_SEMI, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AMP, &parse_separator}, @@ -26,7 +26,6 @@ t_parser g_parser[] = {TK_LESSAND, &parse_lessand}, {TK_GREATAND, &parse_greatand}, {TK_SUBSHELL, &parse_subshell}, - {TK_WHILE, &parse_while}, {TK_WORD, &parse_word}, {0, 0}, }; diff --git a/42sh/src/parser/get_instruction.c b/42sh/src/parser/get_instruction.c index 02f44d6a..8495ae39 100644 --- a/42sh/src/parser/get_instruction.c +++ b/42sh/src/parser/get_instruction.c @@ -6,20 +6,72 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ -/* Updated: 2017/02/03 17:16:53 by ariard ### ########.fr */ +/* Updated: 2017/02/04 22:20:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int get_instruction(t_btree **ast, t_list **start, t_list **lst) +static int get_simple_instruction(t_list **start, t_list **lst) { - t_token *token; + t_list *temp; - token = (*lst)->content; - if (token->type == TK_NEWLINE) - return (parse_newline(ast, start, lst)); - else if (token->type == TK_WHILE) - return (parse_while(ast, start, lst)); + DG("simple instruction"); + temp = (*lst)->next; + (*lst)->next = NULL; + ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); + (*lst) = temp; + return (0); +} + +static int get_compound_instruction(t_list **start, t_list **lst) +{ + t_list *temp; + t_token *token; + int nest; + + DG("compound instruction"); + nest = 0; + while (((*lst) = (*lst)->next)) + { + token = (*lst)->content; + if (token->type & TK_DO) + nest++; + else if (token->type & TK_DONE) + nest--; + if (nest == 0 && token->type & TK_DONE) + break; + } + while (((*lst) = (*lst)->next)) + { + token = (*lst)->content; + if (token->type & (TK_NEWLINE | TK_SEMI)) + break; + } + if ((*lst)) + { + temp = (*lst)->next; + (*lst)->next = NULL; + ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); + *lst = temp; + } + return (0); +} + +int get_instruction(t_list **lst) +{ + t_token *token; + t_list *start; + + start = *lst; + while ((*lst)) + { + token = (*lst)->content; + if (token->type & TK_NEWLINE) + return (get_simple_instruction(&start, lst)); + else if (token->type & TK_WHILE) + return (get_compound_instruction(&start, lst)); + (*lst) = (*lst)->next; + } return (0); } diff --git a/42sh/src/parser/get_instruction2.c b/42sh/src/parser/get_instruction2.c new file mode 100644 index 00000000..02f0ee90 --- /dev/null +++ b/42sh/src/parser/get_instruction2.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_instruction2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/04 18:20:53 by ariard #+# #+# */ +/* Updated: 2017/02/04 19:00:55 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int get_instruction2(t_btree **ast, t_list **start, t_list **lst) +{ + t_token *token; + + token = (*lst)->content; + if (token->type == TK_NEWLINE) + return (parse_newline(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 new file mode 100644 index 00000000..3e881779 --- /dev/null +++ b/42sh/src/parser/parse.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ +/* Updated: 2017/02/04 20:43:45 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int parse(t_btree **ast, t_list **token) +{ + t_list *start; + + start = *token; + if (get_instruction(token)) + return (1); + if (ft_parse(ast, &start)) + return (1); + return (0); +} diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index 6369f303..fd15c29d 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/03 16:42:45 by ariard ### ########.fr */ +/* Updated: 2017/02/04 23:44:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_newline.c b/42sh/src/parser/parse_newline.c index f3faa810..3078fe2f 100644 --- a/42sh/src/parser/parse_newline.c +++ b/42sh/src/parser/parse_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 19:26:41 by ariard #+# #+# */ -/* Updated: 2017/02/03 20:04:40 by ariard ### ########.fr */ +/* Updated: 2017/02/04 20:22:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 8a9ea9de..a51f5dfe 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/03 17:29:32 by ariard ### ########.fr */ +/* Updated: 2017/02/05 00:03:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ 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; diff --git a/42sh/src/parser/parse_while.c b/42sh/src/parser/parse_while.c index f3c53869..14262d84 100644 --- a/42sh/src/parser/parse_while.c +++ b/42sh/src/parser/parse_while.c @@ -6,21 +6,23 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 16:03:28 by ariard #+# #+# */ -/* Updated: 2017/02/04 15:51:24 by ariard ### ########.fr */ +/* Updated: 2017/02/05 00:14:32 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -static int delete_all_newline(t_list **start, t_list **lst) +static int delete_all_newline(t_list **lst) { t_token *token; + t_list *start; while ((*lst)) { token = (*lst)->content; + start = *lst; if (token->type & TK_NEWLINE) - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); + ft_lst_delif(&start, (*lst)->content, &ft_addrcmp, &token_free); else break; (*lst) = (*lst)->next; @@ -31,41 +33,53 @@ static int delete_all_newline(t_list **start, t_list **lst) int parse_while(t_btree **ast, t_list **start, t_list **lst) { t_list *temp; + t_list *temp4; + t_btree *temp2; + t_btree *temp3; t_astnode *node; + t_astnode item; t_token *token; int nest; - token = (*lst)->content; - node = (*ast)->item; + temp = (*lst); + nest = 0; + while ((temp = temp->next)) + { + token = temp->content; + if (token->type & TK_DO) + nest++; + else if (token->type & TK_DONE) + nest--; + if (nest == 0 && (token->type & TK_DONE)) + break; + } + temp4 = temp; + temp = temp->next; + temp4->next = NULL; + ft_lst_delif(start, temp4->content, &ft_addrcmp, &token_free); + if (temp) + ft_parse(ast, &temp); + temp2 = *ast; + while (temp2->left) + temp2 = temp2->left; + temp3 = btree_create_node(&item, sizeof(item)); + ((t_astnode *)(*ast)->item)->data.token = NULL; + temp2->left = temp3; + + node = temp3->item; node->type = TK_WHILE; ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); + while (((*lst) = (*lst)->next)) if ((token = (*lst)->content)->type & TK_DO) break; temp = (*lst)->next; (*lst)->next = NULL; ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->left, start); + ft_parse(&(temp3)->left, start); - delete_all_newline(start, &temp); + delete_all_newline(&temp); + ft_parse(&(temp3)->right, &temp); - (*lst) = temp; - *start = temp; - nest = 1; - while (((*lst) = (*lst)->next)) - { - token = (*lst)->content; - if (token->type & TK_DO) - nest++; - else if (token->type & TK_DONE) - nest--; - if (nest == 0) - break; - } - temp = (*lst)->next; - (*lst)->next = NULL; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - ft_parse(&(*ast)->right, start); - *start = temp; return (0); }