From 9bb45abb3d1bf5441548900891e5caab796a708e Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Mon, 6 Feb 2017 00:05:32 +0100 Subject: [PATCH] currently parsing if, to-do:syntax error, clean code& interactive shell --- 42sh/Makefile | 7 +- 42sh/includes/lexer.h | 3 +- 42sh/includes/parser.h | 7 +- 42sh/sample/if.sh | 13 +- 42sh/sample/while.sh | 25 +++- 42sh/sample/while2.sh | 16 +++ 42sh/src/exec/exec_ampersand.c | 8 +- 42sh/src/exec/exec_and_if.c | 7 +- 42sh/src/exec/exec_command.c | 4 +- 42sh/src/exec/exec_default.c | 2 +- 42sh/src/exec/exec_dgreat.c | 7 +- 42sh/src/exec/exec_great.c | 7 +- 42sh/src/exec/exec_less.c | 7 +- 42sh/src/exec/exec_list.c | 20 --- 42sh/src/exec/exec_or_if.c | 7 +- 42sh/src/exec/exec_pipe.c | 7 +- 42sh/src/exec/exec_semi.c | 8 +- 42sh/src/exec/exec_while.c | 15 +-- 42sh/src/exec/ft_exec.c | 6 +- 42sh/src/lexer/get_reserved_words.c | 8 +- 42sh/src/main/ft_putast.c | 10 +- 42sh/src/main/ft_putast2.c | 2 +- 42sh/src/main/shell_script.c | 12 +- 42sh/src/parser/ft_parse.c | 5 +- 42sh/src/parser/get_instruction.c | 41 ++++++- ...t_instruction2.c => get_sub_instruction.c} | 10 +- 42sh/src/parser/parse.c | 3 +- 42sh/src/parser/parse_if.c | 116 ++++++++++++++++++ 42sh/src/parser/parse_list.c | 26 ---- 42sh/src/parser/parse_separator.c | 2 +- 42sh/src/parser/parse_while.c | 6 +- 31 files changed, 266 insertions(+), 151 deletions(-) create mode 100644 42sh/sample/while2.sh delete mode 100644 42sh/src/exec/exec_list.c rename 42sh/src/parser/{get_instruction2.c => get_sub_instruction.c} (74%) create mode 100644 42sh/src/parser/parse_if.c delete mode 100644 42sh/src/parser/parse_list.c diff --git a/42sh/Makefile b/42sh/Makefile index 1795c690..85c18d92 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -48,7 +48,6 @@ exec/exec_default.c\ exec/exec_dgreat.c\ exec/exec_great.c\ exec/exec_less.c\ -exec/exec_list.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ exec/exec_semi.c\ @@ -171,8 +170,8 @@ main/shell_script.c\ main/sig_handler.c\ parser/parse.c\ parser/ft_parse.c\ -parser/get_instruction.c \ -parser/get_instruction2.c \ +parser/get_instruction.c\ +parser/get_sub_instruction.c\ parser/parse_dgreat.c\ parser/parse_dless.c\ parser/parse_do.c\ @@ -181,10 +180,10 @@ parser/parse_great.c\ parser/parse_greatand.c\ parser/parse_less.c\ parser/parse_lessand.c\ -parser/parse_list.c\ parser/parse_separator.c\ parser/parse_subshell.c\ parser/parse_while.c\ +parser/parse_if.c\ parser/parse_word.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 5491555e..5ffe84d8 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/02/05 18:12:04 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:45:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,6 @@ typedef long long t_type; # 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) diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 84eab220..55d46ec2 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/05 17:59:47 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:44:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ int parse(t_btree **ast, t_list **token); 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 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); @@ -77,8 +77,11 @@ 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_list(t_btree **ast, t_list **start, t_list **lst); +int delete_newline(t_list **start, t_list **lst); + #endif diff --git a/42sh/sample/if.sh b/42sh/sample/if.sh index bba0efb5..5cf17ebb 100644 --- a/42sh/sample/if.sh +++ b/42sh/sample/if.sh @@ -1,6 +1,11 @@ -echo "begin if" if [ 1 ] then - echo "hello world" -fi -echo "end if" + if [ 1 ] + then + echo "hello world" + fi + if [ 1 ] + then + echo "salut monde" + fi +fi > file1 diff --git a/42sh/sample/while.sh b/42sh/sample/while.sh index 5adc86a6..4dd61c2f 100644 --- a/42sh/sample/while.sh +++ b/42sh/sample/while.sh @@ -2,10 +2,27 @@ while ls do while ls do - pwd - done + while ls + do + while ls + do + while ls + do + while ls + do + echo hello + done + done + done + done + done + ls while ls do - cd - done + while ls + do + echo world + done + done + pwd done diff --git a/42sh/sample/while2.sh b/42sh/sample/while2.sh new file mode 100644 index 00000000..d3d0f5e4 --- /dev/null +++ b/42sh/sample/while2.sh @@ -0,0 +1,16 @@ +ONE=11 +while [ $ONE -gt 1 ] +do + TWO=11 + while [ $TWO -gt 1 ] + do + THREE=11 + while [ $THREE -gt 1 ] + do + echo hello + ((THREE--)) + done + ((TWO--)) + done + ((ONE--)) +done diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index 0a0c7a0b..55da0220 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/01/30 21:11:38 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:10:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,6 @@ int exec_ampersand(t_btree **ast) { - t_process *p; - if (SHELL_HAS_JOBC(data_singleton()->opts)) data_singleton()->exec.job.attributes |= JOB_BG; ft_exec(&(*ast)->left); @@ -23,8 +21,6 @@ int exec_ampersand(t_btree **ast) data_singleton()->exec.job.attributes &= ~JOB_BG; ft_exec(&(*ast)->right); - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_and_if.c b/42sh/src/exec/exec_and_if.c index 4b26c5d4..794cfdb9 100644 --- a/42sh/src/exec/exec_and_if.c +++ b/42sh/src/exec/exec_and_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ -/* Updated: 2017/01/30 20:53:56 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:10:38 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ int exec_and_if(t_btree **ast) { t_data *data; - t_process *p; data = data_singleton(); if (data->exec.aol_status == NULL @@ -34,8 +33,6 @@ int exec_and_if(t_btree **ast) data->exec.aol_status = NULL; data->exec.aol_search = 0; - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 6697b4a5..dace8040 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:40:45 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:09:37 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,6 @@ int exec_command(t_btree **ast) p->av = NULL; p->pid = 0; p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_default.c b/42sh/src/exec/exec_default.c index 5b02f375..287cc995 100644 --- a/42sh/src/exec/exec_default.c +++ b/42sh/src/exec/exec_default.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 19:29:59 by ariard #+# #+# */ -/* Updated: 2017/02/03 15:31:39 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:18:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c index ad73e6b6..e045ee8c 100644 --- a/42sh/src/exec/exec_dgreat.c +++ b/42sh/src/exec/exec_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2017/01/30 20:55:31 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:10:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ int exec_dgreat(t_btree **ast) { t_astnode *node; - t_process *p; int fd; node = (*ast)->item; @@ -24,8 +23,6 @@ int exec_dgreat(t_btree **ast) ft_exec(&(*ast)->left); data_singleton()->exec.process.fdout = STDOUT; - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_great.c index f4399234..8190f929 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/01/30 21:09:36 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:11:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ int exec_great(t_btree **ast) { t_astnode *node; - t_process *p; int fd; node = (*ast)->item; @@ -24,8 +23,6 @@ int exec_great(t_btree **ast) ft_exec(&(*ast)->left); data_singleton()->exec.process.fdout = STDOUT; - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c index 4c0d226d..3629ff65 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2017/01/30 20:56:55 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:12:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ int exec_less(t_btree **ast) { t_astnode *node; - t_process *p; int fd; node = (*ast)->item; @@ -27,8 +26,6 @@ int exec_less(t_btree **ast) data_singleton()->exec.process.fdin = STDIN; /* data->exec.process.command = NULL; */ - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_list.c b/42sh/src/exec/exec_list.c deleted file mode 100644 index 061c4291..00000000 --- a/42sh/src/exec/exec_list.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_list.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 20:19:29 by ariard #+# #+# */ -/* Updated: 2017/02/02 16:00:40 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_list(t_btree **ast) -{ - (void)ast; -// btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c index c7abb167..63995843 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2017/01/30 21:09:55 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:12:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ int exec_or_if(t_btree **ast) { t_data *data; - t_process *p; data = data_singleton(); if (data->exec.aol_status == NULL @@ -34,8 +33,6 @@ int exec_or_if(t_btree **ast) data->exec.aol_status = NULL; data->exec.aol_search = 0; - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index d876dd9e..1ba905b0 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2017/01/30 21:01:33 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:14:32 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,8 +43,7 @@ int exec_pipe(t_btree **ast) close(fds[PIPE_READ]); p->fdin = STDIN; - - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); + + btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 9cabc19f..83084619 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/01/31 19:31:59 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:19:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,9 @@ int exec_semi(t_btree **ast) { - t_process *p; - ft_exec(&(*ast)->left); ft_exec(&(*ast)->right); - p = &data_singleton()->exec.process; - if (!(p->script & SCRIPT_LOOP)) - btree_delone(ast, &ast_free); +// btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index 7b4e8f5e..5cd3deb4 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/02/03 16:58:02 by ariard ### ########.fr */ +/* Updated: 2017/02/05 22:38:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,12 @@ int exec_while(t_btree **ast) { - t_list **consequent_commands; - t_astnode *node; int test; - node = ((*ast)->right)->item; - DG("consequent command data '%s'", node->data.str); - consequent_commands = NULL; - - test = 10; while (test--) - { - loop_exec(*consequent_commands); - DG("end loop : '%d'", test); - } + ft_exec(&(*ast)->right); -// loop_del(*consequent_commands); // btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 831d7500..62ded660 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:31:42 by ariard ### ########.fr */ +/* Updated: 2017/02/06 00:04:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_execmap g_execmap[] = { - {TK_NEWLINE, &exec_default}, + {TK_NEWLINE, &exec_semi}, {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, {TK_SEMI, &exec_semi}, @@ -24,7 +24,7 @@ t_execmap g_execmap[] = {TK_GREAT, &exec_great}, {TK_DGREAT, &exec_dgreat}, {TK_WHILE, &exec_while}, - {TK_LIST, &exec_list}, + {TK_IF, &exec_if}, {TK_COMMAND | TK_SUBSHELL, &exec_command}, {0, 0}, }; diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index b81df18d..4d91d79b 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 17:15:59 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:15:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,12 @@ int get_reserved_words(t_list **alst) token->type = TK_DONE; else if (ft_strncmp(token->data, "do" , 2) == 0) token->type = TK_DO; + else if (ft_strncmp(token->data, "if", 2) == 0) + token->type = TK_IF; + else if (ft_strncmp(token->data, "then", 4) == 0) + token->type = TK_THEN; + else if(ft_strncmp(token->data, "fi", 4) == 0) + token->type = TK_FI; } } previous_token = token; diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index cf95aa07..cee5f84d 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/05 00:32:21 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:50:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,14 +34,10 @@ char *ft_putast(void *nodein) return (" COM "); else if (node->type == TK_WHILE) return (" WHILE "); - else if (node->type == TK_DO) - return ("DO"); - else if (node->type == TK_DONE) - return ("DONE"); + else if (node->type == TK_IF) + return ("IF"); else if (node->type == TK_NEWLINE) return ("NEW"); - else if (node->type == TK_LIST) - return (" LIST "); else if (node->type == TK_GREAT) return (" > "); else if (node->type == TK_LESS) diff --git a/42sh/src/main/ft_putast2.c b/42sh/src/main/ft_putast2.c index 5947a315..35668b46 100644 --- a/42sh/src/main/ft_putast2.c +++ b/42sh/src/main/ft_putast2.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/12/05 13:17:50 by jhalford ### ########.fr */ +/* Updated: 2017/02/05 22:48:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index d91f4579..0461ac8e 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 21:12:36 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:40:13 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); + // 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 9ba1ff15..268ff4ba 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 21:10:03 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:38:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,7 @@ t_parser g_parser[] = { - {TK_WHILE | TK_NEWLINE, &get_instruction2}, - {TK_SEMI, &parse_separator}, + {TK_WHILE | TK_IF | TK_NEWLINE | TK_SEMI, &get_sub_instruction}, {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 2d6891b3..66deb843 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/05 20:52:30 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:45:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ static int get_simple_instruction(t_list **start, t_list **lst) return (0); } -static int get_compound_instruction(t_list **start, t_list **lst) +static int get_loop_instruction(t_list **start, t_list **lst) { t_list *temp; t_token *token; @@ -56,6 +56,39 @@ static int get_compound_instruction(t_list **start, t_list **lst) return (0); } +static int get_condition_instruction(t_list **start, t_list **lst) +{ + t_list *temp; + t_token *token; + int nest; + + nest = 0; + while (((*lst) = (*lst)->next)) + { + token = (*lst)->content; + if (token->type & TK_THEN) + nest++; + else if (token->type & TK_FI) + nest--; + if (nest == 0 && token->type & TK_FI) + 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; @@ -68,7 +101,9 @@ int get_instruction(t_list **lst) if (token->type & TK_NEWLINE) return (get_simple_instruction(&start, lst)); else if (token->type & TK_WHILE) - return (get_compound_instruction(&start, lst)); + return (get_loop_instruction(&start, lst)); + else if (token->type & TK_IF) + return (get_condition_instruction(&start, lst)); (*lst) = (*lst)->next; } return (0); diff --git a/42sh/src/parser/get_instruction2.c b/42sh/src/parser/get_sub_instruction.c similarity index 74% rename from 42sh/src/parser/get_instruction2.c rename to 42sh/src/parser/get_sub_instruction.c index 4d20972c..7b3ef9fa 100644 --- a/42sh/src/parser/get_instruction2.c +++ b/42sh/src/parser/get_sub_instruction.c @@ -5,14 +5,14 @@ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/04 18:20:53 by ariard #+# #+# */ -/* Updated: 2017/02/05 20:24:57 by ariard ### ########.fr */ +/* Created: 2017/02/05 22:05:26 by ariard #+# #+# */ +/* Updated: 2017/02/05 23:15:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int get_instruction2(t_btree **ast, t_list **start, t_list **lst) +int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst) { t_token *token; @@ -21,5 +21,9 @@ int get_instruction2(t_btree **ast, t_list **start, t_list **lst) return (parse_separator(ast, start, lst)); else if (token->type == TK_WHILE) return (parse_while(ast, start, lst)); + else if (token->type == TK_IF) + return (parse_if(ast, start, lst)); + else if (token->type == TK_SEMI) + return (parse_separator(ast, start, lst)); return (0); } diff --git a/42sh/src/parser/parse.c b/42sh/src/parser/parse.c index f7a24358..2158663d 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/05 20:53:58 by ariard ### ########.fr */ +/* Updated: 2017/02/05 23:46:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ 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_if.c b/42sh/src/parser/parse_if.c new file mode 100644 index 00000000..7679a70f --- /dev/null +++ b/42sh/src/parser/parse_if.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/05 23:15:23 by ariard #+# #+# */ +/* Updated: 2017/02/05 23:57:18 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +static int parse_after_condition(t_btree **ast, t_list **start, t_list **lst) +{ + t_list *temp; + t_list *del; + t_token *token; + int nest; + + + temp = (*lst); + nest = 0; + while ((temp = temp->next)) + { + token = temp->content; + if (token->type & TK_THEN) + nest++; + else if (token->type & TK_FI) + nest--; + if (nest == 0 && (token->type & TK_FI)) + break; + } + del = temp; + temp = temp->next; + del->next = NULL; + ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); + if (temp) + ft_parse(ast, &temp); + return (0); +} + +static int parse_head(t_btree **ast, + t_btree **new_ast, t_list **start, t_list **lst) +{ + t_btree *father; + t_token *token; + t_list *del; + t_astnode *node; + t_astnode item; + + father = *ast; + while (father->left) + father = father->left; + *new_ast = btree_create_node(&item, sizeof(item)); + ((t_astnode *)(*new_ast)->item)->data.token = NULL; + node = (*ast)->item; + if (node->type > 0) + father->left = *new_ast; + else + *new_ast = *ast; + + token = (*lst)->content; + node = (*new_ast)->item; + node->type = token->type; + del = (*lst); + *lst = (*lst)->next; + ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); + delete_newline(start, lst); + return (0); +} + +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; + + nest = 0; + while ((*lst)->next) + *lst = (*lst)->next; + new_start = *start; + ft_lst_reverse(start); + temp = *lst; + while ((*lst)) + { + token = (*lst)->content; + if (token->type & TK_THEN) + nest++; + else if (token->type & TK_FI) + nest--; + if (nest == 1 && (token->type & TK_THEN)) + break; + *lst = (*lst)->next; + } + ft_lst_reverse(&temp); + 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); +} + +int parse_if(t_btree **ast, t_list **start, t_list **lst) +{ + t_btree *new_ast; + + parse_after_condition(ast, start, lst); + parse_head(ast, &new_ast, start, lst); + parse_loop(&new_ast, start, lst); + return (0); +} diff --git a/42sh/src/parser/parse_list.c b/42sh/src/parser/parse_list.c deleted file mode 100644 index 2acf1c61..00000000 --- a/42sh/src/parser/parse_list.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_list.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 16:34:21 by ariard #+# #+# */ -/* Updated: 2017/02/03 16:51:39 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser.h" - -int parse_list(t_btree **ast, t_list **start, t_list **lst) -{ - t_astnode *node; - t_token *token; - - token = (*lst)->content; - node = (*ast)->item; - node->type = TK_LIST; - node->data.str = ft_strdup(token->data); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); - return (0); -} diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 8e754cde..9de5b390 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 21:11:56 by ariard ### ########.fr */ +/* Updated: 2017/02/05 21:54:28 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_while.c b/42sh/src/parser/parse_while.c index 9ef48617..4b4dfd05 100644 --- a/42sh/src/parser/parse_while.c +++ b/42sh/src/parser/parse_while.c @@ -12,7 +12,7 @@ #include "parser.h" -static int delete_newline(t_list **start, t_list **lst) +int delete_newline(t_list **start, t_list **lst) { t_token *token; t_list *del; @@ -63,6 +63,7 @@ static int parse_head(t_btree **ast, { t_btree *father; t_token *token; + t_list *del; t_astnode *node; t_astnode item; @@ -80,8 +81,9 @@ static int parse_head(t_btree **ast, token = (*lst)->content; node = (*new_ast)->item; node->type = token->type; - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); + del = (*lst); *lst = (*lst)->next; + ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); delete_newline(start, lst); return (0); }