diff --git a/42sh/includes/.parser.h.swn b/42sh/includes/.parser.h.swn deleted file mode 100644 index dbc83314..00000000 Binary files a/42sh/includes/.parser.h.swn and /dev/null differ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index ddb94fab..30b55db3 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/20 14:57:52 by ariard ### ########.fr */ +/* Updated: 2017/02/20 17:07:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -217,9 +217,11 @@ int add_file(t_btree **ast, t_list **lst); int add_loop_cmd(t_btree **ast, t_list **lst); int add_loop_sep(t_btree **ast, t_list **lst); int add_condition_cmd(t_btree **ast, t_list **lst); +int add_condition_sep(t_btree **ast, t_list **lst); +int add_branch(t_btree **ast, t_list **lst); int isloop(t_btree **ast); int isdir(t_btree **ast); -int iscondition(t_btree **ast); +int iscondition(t_btree **ast, t_list **lst); int join_ast(t_btree **ast, t_btree **new_node); int gen_node(t_btree **ast); diff --git a/42sh/sample/if/01_if_easy.sh b/42sh/sample/if/01_if_easy.sh index 4676e8ca..cc8f6b09 100644 --- a/42sh/sample/if/01_if_easy.sh +++ b/42sh/sample/if/01_if_easy.sh @@ -1,4 +1,10 @@ -if ls ; +if ls +then + pwd +elif ls +then + pwd +elif ls then pwd fi diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index c4d1f46c..02ad0f73 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/17 23:41:26 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:42:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,13 @@ char *ft_putast(void *nodein) t_type type; }; node = nodein; - if (node->type == TK_DO) + if (node->type == TK_THEN) + return ("THEN"); + else if (node->type == TK_FI) + return ("FI"); + else if (node->type == TK_DO) return ("TK_DO"); - if (node->type == TK_AMP) + else if (node->type == TK_AMP) return (" & "); else if (node->type == TK_N_WORD) return (" TK_N_WORD"); diff --git a/42sh/src/parser/.eval_sym.c.swn b/42sh/src/parser/.eval_sym.c.swn deleted file mode 100644 index 1835f54b..00000000 Binary files a/42sh/src/parser/.eval_sym.c.swn and /dev/null differ diff --git a/42sh/src/parser/.produce_sym.c.swn b/42sh/src/parser/.produce_sym.c.swn deleted file mode 100644 index 994916b7..00000000 Binary files a/42sh/src/parser/.produce_sym.c.swn and /dev/null differ diff --git a/42sh/src/parser/.read_stack.c.swn b/42sh/src/parser/.read_stack.c.swn deleted file mode 100644 index 1013cf57..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 b7a5217e..01e2ab7b 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/19 18:46:04 by ariard ### ########.fr */ +/* Updated: 2017/02/20 17:03:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,10 @@ 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 if (iscondition(ast)) + else if (iscondition(ast, lst) == 1) return (add_condition_cmd(ast, lst)); + else if (iscondition(ast, lst) == 2) + return (add_branch(ast, lst)); else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN) return (add_cmd(&(*ast)->right, lst)); my_tab = NULL; diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index c4279323..1b790042 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -6,51 +6,76 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 18:12:52 by ariard #+# #+# */ -/* Updated: 2017/02/19 18:46:01 by ariard ### ########.fr */ +/* Updated: 2017/02/20 17:17:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int iscondition(t_btree **ast) +int iscondition(t_btree **ast, t_list **lst) { t_astnode *node; + t_token *token; node = NULL; + token = (*lst)->content; if (*ast) { node = (*ast)->item; - if ((node->type == TK_NEWLINE || node->type == TK_SEMI - || node->type == TK_AMP) && iscondition(&(*ast)->right) == 1) - return (1); - if ((node->type == TK_IF || node->type == TK_ELIF) - && node->full == 1) + if ((node->type == TK_IF || iscondition(&(*ast)->right, lst)) + && token->type == TK_ELIF && node->full == 0) return (2); if ((node->type == TK_IF || node->type == TK_ELIF) && node->full == 0) return (1); + if ((node->type == TK_NEWLINE || node->type == TK_SEMI + || node->type == TK_AMP) && iscondition(&(*ast)->right, lst) == 1) + return (1); } return (0); } -int add_condition_cmd(t_btree **ast, t_list **lst) +int add_condition_cmd(t_btree **ast, t_list **lst) { t_token *token; t_astnode *node; token = (*lst)->content; node = (*ast)->item; - if (token->type == TK_IF && node->type == TK_IF) + if (token->type == TK_IF && (node->type == TK_IF || node->type == TK_ELIF)) node->nest++; - if (token->type == TK_FI && node->type == TK_IF && node->nest > 0) + if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF) + && node->nest > 0) node->nest--; - else if (token->type == TK_FI && node->type == TK_IF && node->nest == 0) + else if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF)) return ((node->full = 1)); if (token->type == TK_THEN) return (add_cmd(&(*ast)->right, lst)); - else if (!(*ast)->right && iscondition(&(*ast)->left) != 2) + else if (!(*ast)->right) return (add_cmd(&(*ast)->left, lst)); else return (add_cmd(&(*ast)->right, lst)); return (0); +} + +int add_condition_sep(t_btree **ast, t_list **lst) +{ + if (!(*ast)->right) + return (add_sep(&(*ast)->left, lst)); + else + return (add_sep(&(*ast)->right, lst)); + return (0); +} + +int add_branch(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_btree *new_node; + + new_node = NULL; + gen_node(&new_node); + join_ast(ast, &new_node); + node = (new_node)->item; + node->type = TK_NEWLINE; + return (add_cmd(ast, lst)); } diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index 6a82ae1f..17eb4f5d 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/19 18:38:25 by ariard ### ########.fr */ +/* Updated: 2017/02/20 17:18:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index ed591f3a..59335fbf 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/19 18:46:06 by ariard ### ########.fr */ +/* Updated: 2017/02/20 17:07:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,10 @@ int add_sep(t_btree **ast, t_list **lst) DG("add sep"); if (isloop(ast) == 1) return (add_loop_sep(ast, lst)); + else if (iscondition(ast, lst) == 1) + return (add_condition_sep(ast, lst)); if (!*ast) gen_node(ast); - node = (*ast)->item; token = (*lst)->content; // if (node->type != TK_DO) // { @@ -32,7 +33,6 @@ int add_sep(t_btree **ast, t_list **lst) join_ast(ast, &new_node); node = (new_node)->item; // } - node->type = token->type; - + node->type = token->type; return (0); } diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index a9d1494c..1d176325 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/20 16:06:08 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:10:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -232,20 +232,20 @@ 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 && MATCH_STACK(**stack, g_aggrematch[i].under)) { - DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); +// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; 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 d4497c29..b8a0b4f4 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/19 18:40:15 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:53:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ t_treematch g_treematch[] = static int isseparator(int type, int cache) { if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO - || cache == TK_NEWLINE)) + || cache == TK_NEWLINE || cache == TK_THEN)) return (0); return (1); } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index a37e7558..677e0bb8 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/20 16:06:15 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:10:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -600,7 +600,7 @@ int eval_sym(t_sym stack, t_sym new_sym) { int i; - DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); +// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 54349019..09329b2d 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/19 18:41:29 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:32:27 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,6 @@ int ft_parse2(t_btree **ast, t_list **token) t_sym *stack; t_parstate state; - (void)ast; state = UNDEFINED; new_sym = ft_memalloc(sizeof(t_sym)); stack = ft_memalloc(sizeof(t_sym) * 1000); @@ -57,8 +56,8 @@ int ft_parse2(t_btree **ast, t_list **token) return (error_syntax(token)); if (state == SUCCESS) ft_putstr("success"); -// build_tree(ast, token); -// btree_print(STDBUG, *ast, &ft_putast); + build_tree(ast, token); + btree_print(STDBUG, *ast, &ft_putast); if ((end_instruction(*stack) && !(*token)->next) || *stack == PROGRAM) insert_linebreak(token); else diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index fc35aea6..f1c6bf0b 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/20 15:54:45 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:09:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,8 +86,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) @@ -95,7 +95,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 56435060..fa73330d 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/20 15:04:21 by ariard ### ########.fr */ +/* Updated: 2017/02/20 16:09:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */