diff --git a/42sh/includes/.parser.h.swn b/42sh/includes/.parser.h.swn new file mode 100644 index 00000000..ccd9ee3b Binary files /dev/null and b/42sh/includes/.parser.h.swn differ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 1be71b53..b0613516 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/17 23:18:23 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:06:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -249,11 +249,13 @@ union u_astdata t_ld *token; char **sstr; char *str; - int loop; }; struct s_astnode { + + int nest; + int full; t_type type; t_astdata data; }; diff --git a/42sh/sample/while.sh b/42sh/sample/while.sh index 6bad207e..c6a6867c 100644 --- a/42sh/sample/while.sh +++ b/42sh/sample/while.sh @@ -1,4 +1,8 @@ -while ls | cat ; +while ls do - pwd | cat ; + while ls + do + pwd + done + pwd ; ls done diff --git a/42sh/src/parser/.read_stack.c.swn b/42sh/src/parser/.read_stack.c.swn deleted file mode 100644 index 2644ddb7..00000000 Binary files a/42sh/src/parser/.read_stack.c.swn and /dev/null differ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 74532346..dbd3ddd9 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/02/17 23:40:13 by ariard ### ########.fr */ +/* Updated: 2017/02/18 18:56:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,9 +25,8 @@ int add_cmd(t_btree **ast, t_list **lst) return (add_file(ast, lst)); else if (isloop(ast)) return (add_loop_cmd(ast, lst)); - else + else if ((node = (*ast)->item)->type != TK_DO) return (add_cmd(&(*ast)->right, lst)); - DG("insert data cmd"); my_tab = NULL; token = (*lst)->content; node = (*ast)->item; @@ -37,7 +36,6 @@ int add_cmd(t_btree **ast, t_list **lst) my_tab = ft_sstradd(my_tab, token->data); my_tab = ft_sstradd(my_tab, (char *)token->esc); // ft_ld_pushback(&node->data.token, my_tab); - DG("still alive"); } return (0); } diff --git a/42sh/src/parser/add_file.c b/42sh/src/parser/add_file.c index 23660275..66f13d1b 100644 --- a/42sh/src/parser/add_file.c +++ b/42sh/src/parser/add_file.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:24:01 by ariard ### ########.fr */ +/* Updated: 2017/02/18 16:43:28 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,6 @@ int add_file(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; - DG("add file"); token = (*lst)->content; node = (*ast)->item; node->data.redir.n = ft_atoi(token->data); diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index 2709e6f6..dbfc7533 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 22:17:14 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:38:25 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:10:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,18 @@ int isloop(t_btree **ast) node = NULL; if (*ast) { - DG("end isloop"); node = (*ast)->item; - if (node->data.loop == 1) + DG("TEST LOOP"); + if (node->type == TK_WHILE && node->full == 1) + { + DG("DON ENTER"); return (2); - if (node->type == TK_WHILE) + } + if (node->type == TK_WHILE && node->full == 0) + { + DG(" NOFULL"); return (1); + } } return (0); } @@ -34,12 +40,26 @@ int add_loop_cmd(t_btree **ast, t_list **lst) t_token *token; t_astnode *node; - DG("add loop cmd"); token = (*lst)->content; node = (*ast)->item; - if (token->type == TK_DONE) - return ((node->data.loop = 1)); - else if (token->type == TK_DO) + DG("add loop cmd"); + if (token->type == TK_WHILE && node->type == TK_WHILE) + { + DG("nest one more"); + node->nest++; + } + if (token->type == TK_DONE && node->type == TK_WHILE && node->nest > 0) + { + node->nest--; + DG("nest one less"); + } + else if (token->type == TK_DONE && node->type == TK_WHILE + && node->nest == 0) + { + DG("WHILE FULL"); + return ((node->full = 1)); + } + if (token->type == TK_DO) return (add_cmd(&(*ast)->right, lst)); else if (!(*ast)->right && isloop(&(*ast)->left) != 2) return (add_cmd(&(*ast)->left, lst)); @@ -50,7 +70,6 @@ int add_loop_cmd(t_btree **ast, t_list **lst) int add_loop_sep(t_btree **ast, t_list **lst) { - DG("add loop sep"); if (!(*ast)->right) return (add_sep(&(*ast)->left, lst)); else diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index a7cb461a..3d7e8923 100644 --- a/42sh/src/parser/add_sep.c +++ b/42sh/src/parser/add_sep.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 19:12:07 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:34:43 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:06:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,18 @@ int add_sep(t_btree **ast, t_list **lst) t_btree *new_node; DG("add sep"); - if (isloop(&(*ast)->right) == 1 || isloop(ast)) + if (isloop(ast) == 1) return (add_loop_sep(ast, lst)); - new_node = NULL; - gen_node(&new_node); - join_ast(ast, &new_node); + node = (*ast)->item; token = (*lst)->content; - node = (new_node)->item; +// if (node->type != TK_DO) +// { + new_node = NULL; + gen_node(&new_node); + join_ast(ast, &new_node); + node = (new_node)->item; +// } node->type = token->type; + return (0); } diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 3315be3c..6f6933cc 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ -/* Updated: 2017/02/17 21:35:09 by ariard ### ########.fr */ +/* Updated: 2017/02/18 16:58:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -151,6 +151,7 @@ t_aggrematch g_aggrematch[] = {END_COMMAND, TK_WHILE, PIPE_SEQUENCE, 0}, {END_COMMAND, LINEBREAK, PIPE_SEQUENCE, 0}, {END_COMMAND, TK_DO, PIPE_SEQUENCE, 0}, + {END_COMMAND, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST}, {PIPE_SEQUENCE, TK_WHILE, PIPELINE, 0}, {PIPE_SEQUENCE, TK_BANG, PIPELINE, TK_BANG}, {PIPE_SEQUENCE, SEPARATOR_OP, PIPELINE, 0}, @@ -187,8 +188,8 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) int i; i = 0; - DG("aggregate head %s && sym %s", - read_state(**stack), read_state(*new_sym)); +// DG("aggregate head %s && sym %s", +// read_state(**stack), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top @@ -200,7 +201,7 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); - DG("stack after pop: %s", read_state(**stack)); +// DG("stack after pop: %s", read_state(**stack)); } if (eval_sym(**stack, *new_sym)) return ((*state = ERROR)); diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index df2a2152..7fc287f0 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:34:15 by ariard ### ########.fr */ +/* Updated: 2017/02/18 18:43:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,24 +24,36 @@ t_treematch g_treematch[] = {TK_WHILE, &add_cmd}, {TK_DO, &add_cmd}, {TK_DONE, &add_cmd}, + {TK_NEWLINE, &add_sep}, {0, NULL}, }; -int build_tree(t_btree **ast, t_list **lst) +static int isseparator(int type, int cache) { - int i; + if (type == TK_NEWLINE && (cache == TK_WHILE + || cache == TK_DO)) + return (0); + return (1); +} + +int build_tree(t_btree **ast, t_list **lst) +{ + int i; + static int cache; t_token *token; i = 0; token = (*lst)->content; while (g_treematch[i].type) { - if (g_treematch[i].type == token->type) + if (g_treematch[i].type == token->type + && isseparator(token->type, cache)) { DG("func TK : '%s' TK : '%s'", read_state(g_treematch[i].type) ,read_state(token->type)); - return (g_treematch[i].add(ast, lst)); + cache = token->type; + return (g_treematch[i].add(ast, lst)); } i++; } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 77833aa1..e56fce59 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 16:26:30 by ariard #+# #+# */ -/* Updated: 2017/02/17 21:35:11 by ariard ### ########.fr */ +/* Updated: 2017/02/18 18:50:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -195,6 +195,7 @@ t_stackmatch g_stackmatch[] = {TK_WHILE, SEPARATOR_OP}, {TK_WHILE, NEWLINE_LIST}, {TK_WHILE, PIPE_SEMI_SEQUENCE}, + {TK_WHILE, TK_DO}, {TK_UNTIL, LINEBREAK}, {TK_UNTIL, TK_BANG}, {TK_UNTIL, SEPARATOR_OP}, @@ -243,6 +244,7 @@ t_stackmatch g_stackmatch[] = {END_COMMAND, TK_WHILE}, {END_COMMAND, TK_DO}, {END_COMMAND, LINEBREAK}, + {END_COMMAND, COMPOUND_LIST}, {SEPARATOR, CMD_SUPERIOR}, {SEPARATOR, TERM}, {SEPARATOR, COMPOUND_LIST}, @@ -387,6 +389,7 @@ t_stackmatch g_stackmatch[] = {WHILE_CLAUSE, SEPARATOR_OP}, {WHILE_CLAUSE, NEWLINE_LIST}, {WHILE_CLAUSE, PIPE_SEMI_SEQUENCE}, + {WHILE_CLAUSE, TK_DO}, {ELSE_PART, COMPOUND_LIST}, {IF_CLAUSE, LINEBREAK}, {IF_CLAUSE, TK_BANG}, @@ -435,6 +438,7 @@ t_stackmatch g_stackmatch[] = {COMPOUND_COMMAND, NEWLINE_LIST}, {COMPOUND_COMMAND, PIPE_SEMI_SEQUENCE}, {COMPOUND_COMMAND, FUNC}, + {COMPOUND_COMMAND, TK_DO}, {COMMAND, TK_WHILE}, {COMMAND, LINEBREAK}, {COMMAND, TK_DO}, diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index f56dd3fb..bd0a89c9 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 14:30:22 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:39:18 by ariard ### ########.fr */ +/* Updated: 2017/02/18 18:06:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index af01c929..1244505a 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/02/17 21:29:20 by ariard ### ########.fr */ +/* Updated: 2017/02/18 18:42:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,8 +72,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) int i; token = (*lst)->content; -// DG("produce stack : %s && token : %s", read_state(stack), -// read_state(token->type)); + DG("produce stack : %s && token : %s", read_state(stack), + read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) @@ -81,7 +81,7 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) if (token->type == g_prodmatch[i].token && stack == g_prodmatch[i].stack) { -// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); + DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); *new_sym = g_prodmatch[i].new_sym; } i++; diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 8068646c..822bb0bb 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:40:50 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:03:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,12 @@ char *read_state(t_sym current) { + if (current == COMPOUND_COMMAND) + return ("COMPOUND_COMMAND"); + if (current == WHILE_CLAUSE) + return ("WHILE_CLAUSE"); + if (current == LOOP) + return ("LOOP"); if (current == TK_DONE) return ("DONE"); if (current == TK_DO) diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index 6d3d6d36..fb69e072 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:57:44 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:38:47 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:06:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,10 +25,13 @@ int gen_node(t_btree **ast) if (!*ast) { + DG("node create"); *ast = btree_create_node(&item, sizeof(item)); ((t_astnode *)(*ast)->item)->data.token = NULL; ((t_astnode *)(*ast)->item)->data.redir.word.word = NULL; - ((t_astnode *)(*ast)->item)->data.loop = 0; + ((t_astnode *)(*ast)->item)->nest = 0; + ((t_astnode *)(*ast)->item)->full = 0; + ((t_astnode *)(*ast)->item)->type = 0; } return (0); } diff --git a/42sh/test.sh b/42sh/test.sh deleted file mode 100644 index 27c1ef0f..00000000 --- a/42sh/test.sh +++ /dev/null @@ -1 +0,0 @@ -ls |