From 7d660fec7ce51c09ae247d20f2da602b540bd26e Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Sat, 4 Mar 2017 18:02:38 +0100 Subject: [PATCH] execution if/elif/else + while ok --- 42sh/sample/if/01_if_easy.sh | 13 ++++++------- 42sh/sample/while/while01.sh | 10 +++++----- 42sh/src/exec/ast_free.c | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_elif.c | 7 ++----- 42sh/src/exec/exec_else.c | 11 ++++------- 42sh/src/exec/exec_if.c | 5 +---- 42sh/src/exec/exec_semi.c | 2 +- 42sh/src/exec/exec_while.c | 7 +++++-- 42sh/src/exec/ft_exec.c | 6 +----- 42sh/src/main/main.c | 5 +---- 42sh/src/parser/add_cmd.c | 7 +++++-- 42sh/src/parser/ft_parse.c | 2 +- 13 files changed, 34 insertions(+), 45 deletions(-) diff --git a/42sh/sample/if/01_if_easy.sh b/42sh/sample/if/01_if_easy.sh index f7d0a10c..198b0a62 100644 --- a/42sh/sample/if/01_if_easy.sh +++ b/42sh/sample/if/01_if_easy.sh @@ -1,10 +1,9 @@ -if ls +if cat wef4eeef then - ls -elif ls + echo Conditon 1 +elif cat yulu then - ls -elif ls -then - ls + echo Condition 2 +else + echo Condition 3 fi diff --git a/42sh/sample/while/while01.sh b/42sh/sample/while/while01.sh index f2897742..2c1dad18 100644 --- a/42sh/sample/while/while01.sh +++ b/42sh/sample/while/while01.sh @@ -1,11 +1,11 @@ -while ls +while pwd do - while ls + while cat rwgwghe do - pwd + echo Hello World done - while ls + while pwd do - pwd + echo Bonjour ca va done done diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 487cf063..efee8bea 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:59:33 by ariard ### ########.fr */ +/* Updated: 2017/03/04 17:09:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index b867ae89..f2b1394f 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/03/03 20:28:21 by wescande ### ########.fr */ +/* Updated: 2017/03/04 17:11:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_elif.c b/42sh/src/exec/exec_elif.c index be3bc119..34ad5d9a 100644 --- a/42sh/src/exec/exec_elif.c +++ b/42sh/src/exec/exec_elif.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:08:53 by ariard #+# #+# */ -/* Updated: 2017/03/03 16:14:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 17:45:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,11 @@ int exec_elif(t_btree **ast) t_exec *exec; exec = &data_singleton()->exec; - /* if (data_singleton()->exec.process.if_branch == 1) */ if (EXEC_IS_IF_BRANCH(exec->attrs)) return (0); ft_exec(&(*ast)->left); - if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0")) - /* if (data_singleton()->exec.process.status == 1) */ + if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) { - /* data_singleton()->exec.process.if_branch = 1; */ exec->attrs |= EXEC_IF_BRANCH; ft_exec(&(*ast)->right); } diff --git a/42sh/src/exec/exec_else.c b/42sh/src/exec/exec_else.c index 739753c9..7bc6b172 100644 --- a/42sh/src/exec/exec_else.c +++ b/42sh/src/exec/exec_else.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:55:07 by ariard #+# #+# */ -/* Updated: 2017/03/03 15:56:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 18:00:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,8 @@ int exec_else(t_btree **ast) exec = &data_singleton()->exec; if (EXEC_IS_IF_BRANCH(exec->attrs)) - /* if (data_singleton()->exec.process.if_branch == 0) */ - { - exec->attrs |= EXEC_IF_BRANCH; - /* data_singleton()->exec.process.if_branch = 1; */ - ft_exec(&(*ast)->right); - } + return (0); + exec->attrs |= EXEC_IF_BRANCH; + ft_exec(&(*ast)->left); return (0); } diff --git a/42sh/src/exec/exec_if.c b/42sh/src/exec/exec_if.c index cd03b39f..02f972e6 100644 --- a/42sh/src/exec/exec_if.c +++ b/42sh/src/exec/exec_if.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/03 16:30:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 17:37:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,10 @@ int exec_if(t_btree **ast) exec = &data_singleton()->exec; - /* data_singleton()->exec.process.if_branch = 0; */ exec->attrs &= ~EXEC_IF_BRANCH; ft_exec(&(*ast)->left); - /* if (data_singleton()->exec.process.status == 1) */ if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) { - /* data_singleton()->exec.process.if_branch = 1; */ exec->attrs |= EXEC_IF_BRANCH; ft_exec(&(*ast)->right); } diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index f664b25e..5f03aa23 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/03/03 18:10:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 17:23:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index e8ff4ac8..192913ca 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/03/03 16:05:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 17:19:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,12 @@ int exec_while(t_btree **ast) { + DG("exec while condition"); ft_exec(&(*ast)->left); - while (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0")) + DG("ret :[%s]", ft_getenv(data_singleton()->env, "?")); + while (!(ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0"))) { + DG("in the while"); ft_exec(&(*ast)->right); ft_exec(&(*ast)->left); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index b4b3c578..ae3234c0 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/03/03 19:03:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/04 17:16:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,11 +42,7 @@ int ft_exec(t_btree **ast) while (g_execmap[i].type) { if (item->type == g_execmap[i].type) - { - DG("match : %s and %s", - read_state(item->type), read_state(g_execmap[i].type)); return ((*g_execmap[i].f)(ast)); - } i++; } return (0); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index b1bedc83..3a713be8 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/03/04 16:49:23 by ariard ### ########.fr */ +/* Updated: 2017/03/04 17:35:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,14 +36,11 @@ int handle_instruction(int fd) return (parser.state == UNDEFINED ? error_EOF(&token, &parser, &ast) : 1); } - DG("after readline %s", str); - DG("in lexer %s", lexer.str); ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); else if (get_lexer_stack(lexer) == DLESS) lexer.state = DLESS; - DG("after readline %s", lexer.str); ltoken = ft_lstlast(token); if (lexer_lex(token ? <oken : &token, &lexer)) return (1); diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 32368c49..f4fd2dd8 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/03/03 20:04:08 by ariard ### ########.fr */ +/* Updated: 2017/03/04 17:08:04 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,7 +51,10 @@ int add_cmd(t_btree **ast, t_list **lst) && node->type != REDIR) return (add_cmd(&(*ast)->right, lst)); node = (*ast)->item; - node->type = CMD; + if (token->type != TK_WORD) + node->type = token->type; + else + node->type = CMD; if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) { DG("add data"); diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 2aae5a10..cd00ce9a 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -52,7 +52,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else parser->state = UNDEFINED; build_tree(ast, token); - // btree_print(STDBUG, *ast, &ft_putast); + btree_print(STDBUG, *ast, &ft_putast); if ((end_instruction(&parser->stack) && !(*token)->next)) insert_linebreak(token); else