From bcaa2fb9436e4cba5b5e9be91cb5982c7139ce04 Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Thu, 26 Jan 2017 01:00:03 +0100 Subject: [PATCH] lexing while_clause, passage en buffer dynamique au readscript todo tomorrow --- 42sh/file1 | 1 - 42sh/includes/builtin.h | 2 +- 42sh/includes/lexer.h | 19 +++++++++++---- 42sh/sample/comment.sh | 4 +-- 42sh/script.sh | 9 ++++++- 42sh/src/lexer/ft_tokenize.c | 10 +++++++- 42sh/src/lexer/get_lexer_state.c | 2 +- 42sh/src/lexer/get_reserved_words.c | 23 +++++++++++++++++ 42sh/src/lexer/lexer_default.c | 2 +- 42sh/src/lexer/lexer_delim.c | 2 +- 42sh/src/lexer/lexer_do_group.c | 38 +++++++++++++++++++++++++++++ 42sh/src/lexer/lexer_dquote.c | 2 +- 42sh/src/lexer/lexer_greatand.c | 2 +- 42sh/src/lexer/lexer_list.c | 34 ++++++++++++++++++++++++++ 42sh/src/lexer/lexer_loop.c | 28 +++++++++++++++++++++ 42sh/src/lexer/lexer_newline.c | 2 +- 42sh/src/lexer/lexer_number.c | 2 +- 42sh/src/lexer/lexer_sep.c | 2 +- 42sh/src/lexer/lexer_special.c | 2 +- 42sh/src/lexer/lexer_word.c | 4 ++- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_script.c | 3 ++- 42sh/src/parser/parse_separator.c | 2 +- 23 files changed, 173 insertions(+), 24 deletions(-) delete mode 100644 42sh/file1 create mode 100644 42sh/src/lexer/get_reserved_words.c create mode 100644 42sh/src/lexer/lexer_do_group.c create mode 100644 42sh/src/lexer/lexer_list.c create mode 100644 42sh/src/lexer/lexer_loop.c diff --git a/42sh/file1 b/42sh/file1 deleted file mode 100644 index 1acf61e2..00000000 --- a/42sh/file1 +++ /dev/null @@ -1 +0,0 @@ - 0 diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 4ffb9a44..b1c2c834 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/01/19 20:54:43 by ariard ### ########.fr */ +/* Updated: 2017/01/25 15:49:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 909a1a8c..4d3b7e75 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/24 20:13:54 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:57:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,9 +37,10 @@ typedef long long t_type; # define TK_COMMAND (1 << 17) # define TK_SUBSHELL (1 << 18) # define TK_NEWLINE (1 << 19) -# define TK_LOOP (1 << 20) -# define TK_B_LOOP (1 << 21) -# define TK_E_LOOP (1 << 22) +# define TK_WHILE (1 << 20) +# define TK_DO (1 << 21) +# define TK_DONE (1 << 22) +# define TK_LIST (1 << 23) # define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD) # define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) @@ -62,6 +63,9 @@ enum e_lexstate BACKSLASH, VAR, SPECIAL, + WHILE, + DO_GROUP, + LIST, COMMENT, }; @@ -92,10 +96,11 @@ int reduce_bquotes(t_list **alst, char **str); char *command_getoutput(char *command); int ft_is_delim(char c); +int ft_is_delim_list(char c); t_lexstate get_lexer_state(char *str); +t_lexstate get_reserved_words(char *str); int lexer_default(t_list **alst, char *str); -int lexer_comment(t_list **alst, char *str); int lexer_newline(t_list **alst, char *str); int lexer_delim(t_list **alst, char *str); int lexer_sep(t_list **alst, char *str); @@ -110,5 +115,9 @@ int lexer_dquote(t_list **alst, char *str); int lexer_backslash(t_list **alst, char *str); int lexer_var(t_list **alst, char *str); int lexer_special(t_list **alst, char *str); +int lexer_while(t_list **alst, char *str); +int lexer_do_group(t_list **alst, char *str); +int lexer_list(t_list **alst, char *str); +int lexer_comment(t_list **alst, char *str); #endif diff --git a/42sh/sample/comment.sh b/42sh/sample/comment.sh index 2d580555..9d021c74 100644 --- a/42sh/sample/comment.sh +++ b/42sh/sample/comment.sh @@ -1,2 +1,2 @@ -ls #ls -pwd +ls | cat +pwd ; cd diff --git a/42sh/script.sh b/42sh/script.sh index deda5282..c195c1d7 100755 --- a/42sh/script.sh +++ b/42sh/script.sh @@ -1,6 +1,13 @@ #!/bin/bash -while [ 1 ]; do +echo "debut script" + +do + +while [ 1 ] ; [ 1 ] +do sleep 1 echo "a" done + +echo "fin script" diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 5135db09..e62331f1 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/01/24 20:13:42 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:56:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,9 @@ int (*g_lexer[])(t_list **alst, char *str) = &lexer_backslash, &lexer_var, &lexer_special, + &lexer_while, + &lexer_do_group, + &lexer_list, &lexer_comment, }; @@ -37,6 +40,11 @@ int ft_is_delim(char c) return (c == ' ' || c == '\t'); } +int ft_is_delim_list(char c) +{ + return (c == ';' || c == '\n' || c == '&'); +} + int ft_tokenize(t_list **alst, char *str, t_lexstate state) { t_token *token; diff --git a/42sh/src/lexer/get_lexer_state.c b/42sh/src/lexer/get_lexer_state.c index aea725da..08d730bd 100644 --- a/42sh/src/lexer/get_lexer_state.c +++ b/42sh/src/lexer/get_lexer_state.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 13:45:46 by jhalford #+# #+# */ -/* Updated: 2017/01/24 19:09:26 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:31:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c new file mode 100644 index 00000000..ece59508 --- /dev/null +++ b/42sh/src/lexer/get_reserved_words.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_reserved_words.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ +/* Updated: 2017/01/26 00:51:26 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +t_lexstate get_reserved_words(char *str) +{ + if (ft_strncmp(str, "while", 5) == 0) + return (WHILE); + else if (ft_strncmp(str, "done", 4) == 0 + || ft_strncmp(str, "do" , 2) == 0) + return (DO_GROUP); + return (0); +} diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 28bb9821..a34da9ea 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/01/24 19:22:58 by ariard ### ########.fr */ +/* Updated: 2017/01/25 23:01:37 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 2ad6374b..8f05a9bb 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ -/* Updated: 2017/01/24 19:48:31 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:08:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_do_group.c b/42sh/src/lexer/lexer_do_group.c new file mode 100644 index 00000000..c3e6c771 --- /dev/null +++ b/42sh/src/lexer/lexer_do_group.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_do_group.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/26 00:48:48 by ariard #+# #+# */ +/* Updated: 2017/01/26 00:59:00 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_do_group(t_list **alst, char *str) +{ + t_token *token; + t_lexstate state; + int type; + + type = (str[0] == 'd' && str[1] == 'o' && str[2] != 'n' ? + 2 : 4); + if (*alst) + { + if (ft_is_delim_list(*(str + type)) || *(str + type) == ' ') + return (lexer_do_group(&(*alst)->next, str)); + return (ft_tokenize(alst, str + 1, LIST)); + } + else + { + token = token_init(); + *alst = ft_lstnew(token, sizeof(*token)); + } + token = (*alst)->content; + token->type = (type == 2 ? TK_DO : TK_DONE); + state = (token->type == TK_DO) ? LIST : DEFAULT; + return (ft_tokenize(&(*alst)->next, str + 1, state)); +} diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index 35485a56..492c8924 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ -/* Updated: 2017/01/19 22:04:11 by ariard ### ########.fr */ +/* Updated: 2017/01/25 22:52:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index f1f20e8e..6ed64cd7 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ -/* Updated: 2017/01/24 19:02:45 by ariard ### ########.fr */ +/* Updated: 2017/01/25 22:52:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_list.c b/42sh/src/lexer/lexer_list.c new file mode 100644 index 00000000..16589c64 --- /dev/null +++ b/42sh/src/lexer/lexer_list.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/26 00:55:33 by ariard #+# #+# */ +/* Updated: 2017/01/26 00:58:58 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_list(t_list **alst, char *str) +{ + t_token *token; + + token = (*alst)->content; + token->type = TK_LIST; + while (*str) + { + if (ft_is_delim_list(*str)) + { + while (ft_is_delim(*str) || *str == '\n') + str++; + if (ft_strncmp(str, "done", 4) == 0 + || ft_strncmp(str, "do", 2) == 0) + return (ft_tokenize(alst, str, DO_GROUP)); + } + token_append(token, *str++); + } + return (0); +} diff --git a/42sh/src/lexer/lexer_loop.c b/42sh/src/lexer/lexer_loop.c new file mode 100644 index 00000000..476205af --- /dev/null +++ b/42sh/src/lexer/lexer_loop.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_loop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 21:58:12 by ariard #+# #+# */ +/* Updated: 2017/01/26 00:58:57 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_while(t_list **alst, char *str) +{ + t_token *token; + + token = (*alst)->content; + if (ft_strncmp(str, "while", 5) == 0) + token->type = TK_WHILE; + else if (ft_isalnum(*str)) + { + token_append(token, *str); + return (ft_tokenize(alst, str + 1, WORD)); + } + return (ft_tokenize(&(*alst)->next, str + 6, LIST)); +} diff --git a/42sh/src/lexer/lexer_newline.c b/42sh/src/lexer/lexer_newline.c index 69f178f6..0a98df66 100644 --- a/42sh/src/lexer/lexer_newline.c +++ b/42sh/src/lexer/lexer_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 23:19:46 by ariard #+# #+# */ -/* Updated: 2017/01/24 20:13:45 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:41:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 2309f084..31aebba2 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/23 22:54:33 by ariard ### ########.fr */ +/* Updated: 2017/01/25 23:05:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index 2a96e303..3e3c6ef3 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ -/* Updated: 2017/01/24 01:25:42 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:48:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_special.c b/42sh/src/lexer/lexer_special.c index 7d3d7800..53f35075 100644 --- a/42sh/src/lexer/lexer_special.c +++ b/42sh/src/lexer/lexer_special.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 15:35:38 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:36:10 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 22:53:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 836f9f0a..9ccd4f04 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/24 18:50:42 by ariard ### ########.fr */ +/* Updated: 2017/01/26 00:58:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,8 @@ int lexer_word(t_list **alst, char *str) return (ft_tokenize(&(*alst)->next, str, GREAT)); else if (*str == '<') return (ft_tokenize(&(*alst)->next, str, LESS)); + else if ((state = get_reserved_words(str))) + return (ft_tokenize(alst, str, state)); token_append(token, *str); return (ft_tokenize(alst, str + 1, WORD)); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 1833a11f..90b776d7 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/01/23 23:51:35 by ariard ### ########.fr */ +/* Updated: 2017/01/25 16:25:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 34f5f04d..327a19b1 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/01/24 20:14:52 by ariard ### ########.fr */ +/* Updated: 2017/01/25 22:54:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ int shell_script() DG("after post_tokenize"); token_print(head); list_tmp = NULL; + return (0); while (head) { if (ft_parse(&ast, &head)) diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 91d3fbc4..24c84777 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/01/24 19:42:12 by ariard ### ########.fr */ +/* Updated: 2017/01/25 16:35:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */