From bc1c16a45bfb70b7023fe60e376787437b725083 Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Tue, 7 Mar 2017 01:17:58 +0100 Subject: [PATCH] parsing func stable, todo parsing brace_group --- 42sh/includes/parser.h | 1 + 42sh/sample/func/func01.sh | 9 +++---- 42sh/src/parser/add_case.c | 5 ++-- 42sh/src/parser/add_cmd.c | 6 ++--- 42sh/src/parser/add_condition.c | 3 +-- 42sh/src/parser/add_func.c | 48 ++++++++++++++++++++++++--------- 42sh/src/parser/add_loop.c | 4 +-- 42sh/src/parser/add_subshell.c | 4 --- 42sh/src/parser/aggregate_sym.c | 4 +-- 42sh/src/parser/build_tree.c | 3 --- 42sh/src/parser/read_stack.c | 2 ++ 11 files changed, 49 insertions(+), 40 deletions(-) diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 25c8bcd7..16e97a50 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -136,6 +136,7 @@ int iscondition(t_btree **ast, t_list **lst); int iscondition_branch(t_btree **ast, t_list **lst); int issubshell(t_btree **ast, t_list **lst); int isfunc(t_btree **ast, t_list **lst); +int isfunc_name(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); diff --git a/42sh/sample/func/func01.sh b/42sh/sample/func/func01.sh index d559c7d2..b60f425b 100644 --- a/42sh/sample/func/func01.sh +++ b/42sh/sample/func/func01.sh @@ -1,6 +1,3 @@ -hello() { - yolo() { - echo bonjour - } -} - +hello () ( + echo HELLO +) diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index 111d9397..f5862cef 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -15,11 +15,9 @@ int iscase(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; @@ -80,7 +78,8 @@ int add_case_cmd(t_btree **ast, t_list **lst) else if (token->type == TK_DSEMI && node->type == TK_PAREN_OPEN && node->nest == 0) return ((node->full = 1)); - else if (token->type == TK_ESAC) + else if ((token->type == TK_ESAC || token->type == TK_PAREN_CLOSE) + && node->nest == 0) return (0); return (add_cmd(&(*ast)->right, lst)); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index b4e6b650..b86635e1 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -26,10 +26,10 @@ t_distrostree g_distrostree[] = {&iscase_branch, &add_branch}, {&iscase, &add_case_cmd}, {&issubshell, &add_subshell_cmd}, + {&isfunc_name, &add_null}, {&isfunc, &add_func_cmd}, {&isionumber, &add_ionumber}, {&isnull, &add_null}, - }; int superflous_token(t_btree **ast, t_list **lst) @@ -40,8 +40,7 @@ int superflous_token(t_btree **ast, t_list **lst) if (*lst) { token = (*lst)->content; - if (token->type == TK_IN || token->type == TK_PAREN_CLOSE - || token->type == TK_DSEMI) + if (token->type == TK_IN || token->type == TK_DSEMI) return (1); } return (0); @@ -71,7 +70,6 @@ int add_cmd(t_btree **ast, t_list **lst) int i; i = 0; - DG("add cmd"); while (i < 14) { if (g_distrostree[i].test(ast, lst) == 1) diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index 44bf623b..bc62c84f 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -15,10 +15,9 @@ int iscondition(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; + (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index e29c7d8b..ab3e0ae2 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -12,25 +12,41 @@ #include "parser.h" -int isfunc(t_btree **ast, t_list **lst) +int isfunc_name(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_WORD && token->type == CLOSE_LIST) - { + if (node->type == CMD && token->type == TK_PAREN_OPEN) + { + DG("add func name"); node->type = FNAME; - add_one_func(ast, lst); - } + return (1); + } + if (node->type == FNAME && token->type == TK_PAREN_CLOSE && node->nest == 0) + return (1); + } + return (0); +} + +int isfunc(t_btree **ast, t_list **lst) +{ + t_astnode *node; + + (void)lst; + node = NULL; + if (*ast) + { + node = (*ast)->item; + if ((node->type == TK_NEWLINE || node->type == TK_SEMI + || node->type == TK_AMP) && isfunc(&(*ast)->right, lst) == 1) + return (1); if (node->type == FNAME && node->full == 0) return (1); - if (isfunc(&(*ast)->right, lst) == 1) - return (1); } return (0); } @@ -40,23 +56,25 @@ int add_func_cmd(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; + DG("add func cmd"); token = (*lst)->content; node = (*ast)->item; - if (token->type == CLOSE_LIST && node->nest == 0) - return (0); if ((token->type == TK_CASE || token->type == TK_WHILE || token->type == TK_IF || token->type == TK_UNTIL || token->type == TK_FOR || token->type == SUBSHELL || token->type == TK_LBRACE) && node->type == FNAME) node->nest++; if ((token->type == TK_DONE || token->type == TK_ESAC || token->type == TK_FI - || token->type == TK_RBRACE || token->type == TK_PAREN_OPEN) + || token->type == TK_RBRACE || token->type == TK_PAREN_CLOSE) && node->type == FNAME && node->nest > 0) node->nest--; if ((token->type == TK_DONE || token->type == TK_ESAC || token->type == TK_FI - || token->type == TK_RBRACE || token->type == TK_PAREN_OPEN) + || token->type == TK_RBRACE || token->type == TK_PAREN_CLOSE) && node->type == FNAME && node->nest == 0) + { node->full = 1; + add_one_func(ast, lst); + } return (add_cmd(&(*ast)->right, lst)); } @@ -67,7 +85,11 @@ int add_func_sep(t_btree **ast, t_list **lst) int add_one_func(t_btree **ast, t_list **lst) { + t_btree *func_ast; + (void)lst; - ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(ast, sizeof(*ast))); + func_ast = btree_map(*ast, &id); + ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast))); + DG("arbre ajoute"); return (0); } diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index f15c044d..427b0741 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -15,11 +15,9 @@ int isloop(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; + (void)lst; node = NULL; -// token = (*lst)->content; - DG("isloop"); if (*ast) { node = (*ast)->item; diff --git a/42sh/src/parser/add_subshell.c b/42sh/src/parser/add_subshell.c index 02618324..80dea8e7 100644 --- a/42sh/src/parser/add_subshell.c +++ b/42sh/src/parser/add_subshell.c @@ -15,18 +15,14 @@ int issubshell(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; if (node->type == SUBSHELL && node->full == 0) return (1); - if (node->type == TK_LBRACE && node->full == 0) - return (1); } return (0); } diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 4bcd5f45..cb2617fb 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -61,7 +61,6 @@ t_aggrematch g_aggrematch[] = //to abstract TK_ESAC {LINEBREAK, TK_PAREN_CLOSE, FUNC, FNAME}, -//paren open {LINEBREAK, COMPLETE_COMMANDS, PROGRAM, LINEBREAK}, {LINEBREAK, CMD_SUPERIOR, SEPARATOR_OP, 0}, {LINEBREAK, PIPE_SEMI_SEQUENCE, PIPE_SEQUENCE, PIPE_SEMI_SEQUENCE}, @@ -72,6 +71,7 @@ t_aggrematch g_aggrematch[] = {NEWLINE_LIST, SEQUENTIAL_SEP, SEQUENTIAL_SEP, SEQUENTIAL_SEP}, {NEWLINE_LIST, TK_DO, TK_DO, TK_DO}, {NEWLINE_LIST, TK_PAREN_CLOSE, TK_PAREN_CLOSE, TK_PAREN_CLOSE}, + {NEWLINE_LIST, TK_PAREN_OPEN, TK_PAREN_OPEN, TK_PAREN_OPEN}, {NEWLINE_LIST, TK_IN, TK_IN, TK_IN}, {NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN}, {NEWLINE_LIST, TK_IF, TK_IF, TK_IF}, @@ -329,7 +329,7 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) if (g_aggrematch[i].erase_sym) { if (pop_stack(stack, g_aggrematch[i].erase_sym)) - return (1); + return (1); head = (*stack)->content; DG("stack after pop: %s", read_state(*head)); } diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index ff3a467b..e8b79714 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -70,12 +70,9 @@ int build_tree(t_btree **ast, t_list **lst) i = 0; token = (*lst)->content; //check bug de cache case ? -// cache = token->type; if (token->type == TK_PAREN_OPEN && cache != TK_IN && cache != TK_DSEMI && cache != TK_WORD) token->type = SUBSHELL; - if (token->type == TK_PAREN_CLOSE && cache == TK_PAREN_OPEN) - token->type = CLOSE_LIST; while (g_treematch[i].type) { if ((isseparator(token, cache) && g_treematch[i].type == token->type)) diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index d16bac78..0e7450af 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -14,6 +14,8 @@ char *read_state(t_sym current) { + if (current == TERMINUS) + return ("TERMINUS"); if (current == SEQUENCE) return ("SEQUENCE"); if (current == LINEBREAK)