diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index f6b22c1e..e9e37fe4 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/01/31 22:38:56 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:20:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index bd6c40ea..f7bd6830 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/31 22:14:11 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:52:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,10 @@ typedef long long t_type; # define TK_WHILE (1 << 20) # define TK_DO (1 << 21) # define TK_DONE (1 << 22) -# define TK_LIST (1 << 23) +# define TK_IF (1 << 23) +# define TK_THEN (1 << 24) +# define TK_FI (1 << 25) +# define TK_LIST (1 << 26) # define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD) # define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) @@ -73,6 +76,9 @@ enum e_lexstate WHILE, DO, DONE, + IF, + THEN, + FI, LIST, COMMENT, }; @@ -126,6 +132,9 @@ int lexer_special(t_list **alst, char *str); int lexer_while(t_list **alst, char *str); int lexer_do(t_list **alst, char *str); int lexer_done(t_list **alst, char *str); +int lexer_if(t_list **alst, char *str); +int lexer_then(t_list **alst, char *str); +int lexer_fi(t_list **alst, char *str); int lexer_list(t_list **alst, char *str); int lexer_comment(t_list **alst, char *str); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 5f3ebf1d..944c850c 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/01/31 21:59:25 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:57:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/sample/if.sh b/42sh/sample/if.sh new file mode 100644 index 00000000..bba0efb5 --- /dev/null +++ b/42sh/sample/if.sh @@ -0,0 +1,6 @@ +echo "begin if" +if [ 1 ] +then + echo "hello world" +fi +echo "end if" diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index 0892a0d9..c7cd2d58 100644 --- a/42sh/src/exec/exec_while.c +++ b/42sh/src/exec/exec_while.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/01/31 22:52:11 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:20:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/loop_del.c b/42sh/src/exec/loop_del.c index dc9614d7..1bdda0fd 100644 --- a/42sh/src/exec/loop_del.c +++ b/42sh/src/exec/loop_del.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 22:39:20 by ariard #+# #+# */ -/* Updated: 2017/01/31 22:51:56 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:20:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 51ef8fc6..4da8fbdf 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/31 22:52:22 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:58:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,9 @@ int (*g_lexer[])(t_list **alst, char *str) = &lexer_while, &lexer_do, &lexer_done, + &lexer_if, + &lexer_then, + &lexer_fi, &lexer_list, &lexer_comment, }; diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index ea064d56..79dc8b62 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/01/31 21:11:47 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:51:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,5 +20,11 @@ t_lexstate get_reserved_words(char *str) return (DONE); else if (ft_strncmp(str, "do" , 2) == 0 && ft_isalpha(*(str + 2)) == 0) return (DO); + else if (ft_strncmp(str, "if", 2) == 0 && ft_isalpha(*(str + 2)) == 0) + return (IF); + else if (ft_strncmp(str, "then", 4) == 0 && ft_isalpha(*(str + 4)) == 0) + return (THEN); + else if (ft_strncmp(str, "fi", 2) == 0 && ft_isalpha(*(str + 2)) == 0) + return (FI); return (0); } diff --git a/42sh/src/lexer/lexer_do.c b/42sh/src/lexer/lexer_do.c index ce34feca..60d401ff 100644 --- a/42sh/src/lexer/lexer_do.c +++ b/42sh/src/lexer/lexer_do.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* lexer_do_group.c :+: :+: :+: */ +/* lexer_do.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/26 00:48:48 by ariard #+# #+# */ -/* Updated: 2017/01/31 22:14:26 by ariard ### ########.fr */ +/* Created: 2017/01/31 23:29:09 by ariard #+# #+# */ +/* Updated: 2017/01/31 23:32:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,5 @@ int lexer_do(t_list **alst, char *str) token = (*alst)->content; token->type = TK_DO; state = LIST; -// data_singleton()->scope |= (token->type == TK_DO) ? IN_LIST : OUT_LIST; return (ft_tokenize(&(*alst)->next, str + 2, state)); } diff --git a/42sh/src/lexer/lexer_done.c b/42sh/src/lexer/lexer_done.c index daa57f81..6af42378 100644 --- a/42sh/src/lexer/lexer_done.c +++ b/42sh/src/lexer/lexer_done.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 20:49:09 by ariard #+# #+# */ -/* Updated: 2017/01/31 22:14:28 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:33:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,5 @@ int lexer_done(t_list **alst, char *str) token = (*alst)->content; token->type = TK_DONE; state = DEFAULT; -// data_singleton()->scope |= (token->type == TK_DO) ? IN_LIST : OUT_LIST; return (ft_tokenize(&(*alst)->next, str + 4, state)); } diff --git a/42sh/src/lexer/lexer_fi.c b/42sh/src/lexer/lexer_fi.c new file mode 100644 index 00000000..f6e4d4c0 --- /dev/null +++ b/42sh/src/lexer/lexer_fi.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_fi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 23:35:08 by ariard #+# #+# */ +/* Updated: 2017/01/31 23:59:50 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_fi(t_list **alst, char *str) +{ + t_token *token; + t_lexstate state; + + if (*alst) + return (lexer_fi(&(*alst)->next, str)); + else + { + token = token_init(); + *alst = ft_lstnew(token, sizeof(*token)); + } + token = (*alst)->content; + token->type = TK_FI; + state = DEFAULT; + return (ft_tokenize(&(*alst)->next, str + 2, state)); +} diff --git a/42sh/src/lexer/lexer_if.c b/42sh/src/lexer/lexer_if.c new file mode 100644 index 00000000..e22002ff --- /dev/null +++ b/42sh/src/lexer/lexer_if.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 23:25:47 by ariard #+# #+# */ +/* Updated: 2017/01/31 23:28:52 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_if(t_list **alst, char *str) +{ + t_token *token; + + token = (*alst)->content; + if (ft_strncmp(str, "if", 2) == 0) + token->type = TK_IF; + else if (ft_isalnum(*str)) + { + token_append(token, *str); + return (ft_tokenize(alst, str + 1, WORD)); + } + return (ft_tokenize(&(*alst)->next, str + 3, LIST)); +} diff --git a/42sh/src/lexer/lexer_list.c b/42sh/src/lexer/lexer_list.c index 4012135f..fc2b30d7 100644 --- a/42sh/src/lexer/lexer_list.c +++ b/42sh/src/lexer/lexer_list.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:55:33 by ariard #+# #+# */ -/* Updated: 2017/01/31 22:14:23 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:58:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,12 @@ int lexer_list(t_list **alst, char *str) else if (ft_strncmp(str, "do", 2) == 0 && (ft_is_delim_list(*(str + 2)) || *(str + 2) == ' ')) return (ft_tokenize(alst, str, DO)); + else if (ft_strncmp(str, "then", 4) == 0 && (ft_is_delim_list(*(str + 4)) + || *(str + 4) == ' ')) + return (ft_tokenize(alst, str, THEN)); + else if (ft_strncmp(str, "fi", 2) == 0 && (ft_is_delim_list(*(str + 2)) + || *(str + 2) == ' ')) + return (ft_tokenize(alst, str, FI)); } token_append(token, *str++); } diff --git a/42sh/src/lexer/lexer_then.c b/42sh/src/lexer/lexer_then.c new file mode 100644 index 00000000..ad4f937b --- /dev/null +++ b/42sh/src/lexer/lexer_then.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_then.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 23:29:49 by ariard #+# #+# */ +/* Updated: 2017/01/31 23:59:37 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_then(t_list **alst, char *str) +{ + t_token *token; + t_lexstate state; + + if (*alst) + return (lexer_then(&(*alst)->next, str)); + else + { + token = token_init(); + *alst = ft_lstnew(token, sizeof(*token)); + } + token = (*alst)->content; + token->type = TK_THEN; + state = LIST; + return (ft_tokenize(&(*alst)->next, str + 4, state)); +} diff --git a/42sh/src/lexer/lexer_while.c b/42sh/src/lexer/lexer_while.c index e03bcff1..ec150596 100644 --- a/42sh/src/lexer/lexer_while.c +++ b/42sh/src/lexer/lexer_while.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 21:58:12 by ariard #+# #+# */ -/* Updated: 2017/01/26 18:09:46 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:28:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 3c5971b4..43b19555 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/31 22:21:14 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:56:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ int shell_script() return (1); DG("after post_tokenize"); token_print(token); + return (0); if (ft_parse(&list_ast, &ast, &token)) return (1); tmp2 = list_ast; diff --git a/42sh/src/parser/.ft_parse.c.swn b/42sh/src/parser/.ft_parse.c.swn deleted file mode 100644 index 1b104c55..00000000 Binary files a/42sh/src/parser/.ft_parse.c.swn and /dev/null differ diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 557c561e..58b124a4 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/01/31 22:15:59 by ariard ### ########.fr */ +/* Updated: 2017/01/31 23:20:37 by ariard ### ########.fr */ /* */ /* ************************************************************************** */