diff --git a/42sh/Makefile b/42sh/Makefile index e99bd9a8..e42b63b4 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/24 21:33:24 by ariard ### ########.fr # +# Updated: 2017/02/25 00:13:39 by ariard ### ########.fr # # # # **************************************************************************** # @@ -233,6 +233,7 @@ parser/add_file.c\ parser/add_loop.c\ parser/add_sep.c\ parser/add_case.c\ +parser/add_func.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 34290b31..2485e058 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -226,11 +226,14 @@ int add_case_sep(t_btree **ast, t_list **lst); int add_pattern(t_btree **ast, t_list **lst); int add_subshell_cmd(t_btree **ast, t_list **lst); int add_subshell_sep(t_btree **ast, t_list **lst); +int add_func_cmd(t_btree **ast, t_list **lst); +int add_func_sep(t_btree **ast, t_list **lst); int isloop(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscase(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst); int issubshell(t_btree **ast, t_list **lst); +int isfunc(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/func/func01.sh b/42sh/sample/func/func01.sh index b3bc2ba1..d559c7d2 100644 --- a/42sh/sample/func/func01.sh +++ b/42sh/sample/func/func01.sh @@ -1,4 +1,6 @@ hello() { - ls | cat - pwd ; cd + yolo() { + echo bonjour + } } + diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 134e3370..60d40e35 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/24 20:59:07 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:17:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,13 @@ char *ft_putast(void *nodein) { t_astnode *node; node = nodein; + + if (node->type == CLOSE_LIST) + return ("CLOSE_LIST"); + if (node->type == FNAME) + return ("FNAME"); + if (node->type == TK_LBRACE) + return ("TK_LBRACE"); if (node->type == TK_ASSIGNEMENT_WORD) return ("ASSIGNEMENT_WORD"); if (node->type == SUBSHELL) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index ec5af014..aa10e74b 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/02/24 21:48:51 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:00:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index b1843f82..05f7a70e 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/24 19:40:07 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:11:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int add_cmd(t_btree **ast, t_list **lst) char **my_tab; DG("add cmd"); - if ((token = (*lst)->content)->type == TK_IN) + if ((token = (*lst)->content)->type == TK_IN || token->type == TK_PAREN_OPEN) return (0); else if (!*ast) gen_node(ast); @@ -41,6 +41,8 @@ int add_cmd(t_btree **ast, t_list **lst) return (add_branch(ast, lst)); else if (issubshell(ast, lst)) return (add_subshell_cmd(ast, lst)); + else if (isfunc(ast, lst)) + return (add_func_cmd(ast, lst)); else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN && node->type != TK_PAREN_CLOSE) return (add_cmd(&(*ast)->right, lst)); diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index b95c45ab..abadea9c 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 18:12:52 by ariard #+# #+# */ -/* Updated: 2017/02/24 15:45:39 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:11:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c new file mode 100644 index 00000000..6bd17009 --- /dev/null +++ b/42sh/src/parser/add_func.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_func.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/24 23:43:07 by ariard #+# #+# */ +/* Updated: 2017/02/25 00:28:35 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int isfunc(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) + node->type = FNAME; + if (node->type == FNAME && node->full == 0) + return (1); + if (isfunc(&(*ast)->right, lst) == 1) + return (1); + } + return (0); +} + +int add_func_cmd(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + 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) + && 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) + && node->type == FNAME && node->nest == 0) + node->full = 1; + return (add_cmd(&(*ast)->right, lst)); +} + +int add_func_sep(t_btree **ast, t_list **lst) +{ + return (add_sep(&(*ast)->right, lst)); +} + + diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index 5a084a01..d30c5c34 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/24 19:40:12 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:13:07 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,8 @@ int add_sep(t_btree **ast, t_list **lst) return (add_case_sep(ast, lst)); else if (issubshell(ast, lst)) return (add_subshell_sep(ast, lst)); + else if (isfunc(ast, lst)) + return (add_func_sep(ast, lst)); if (!*ast) gen_node(ast); token = (*lst)->content; diff --git a/42sh/src/parser/add_subshell.c b/42sh/src/parser/add_subshell.c index 3341234e..17e83953 100644 --- a/42sh/src/parser/add_subshell.c +++ b/42sh/src/parser/add_subshell.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 18:41:50 by ariard #+# #+# */ -/* Updated: 2017/02/24 23:15:24 by ariard ### ########.fr */ +/* Updated: 2017/02/24 23:38:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index 9fdcc75c..47ba3781 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/24 23:14:00 by ariard ### ########.fr */ +/* Updated: 2017/02/24 23:58:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,9 @@ t_treematch g_treematch[] = {TK_FOR, &add_cmd}, {TK_ASSIGNEMENT_WORD, &add_cmd}, {SUBSHELL, &add_cmd}, + {TK_LBRACE, &add_cmd}, + {TK_RBRACE, &add_cmd}, + {CLOSE_LIST, &add_cmd}, {0, NULL}, }; @@ -47,8 +50,9 @@ static int isseparator(t_token *token, int cache) { if (token->type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN - || cache == TK_WORD || cache == TK_DSEMI)) + || cache == TK_DSEMI)) return (0); +//check cache == WORD return (1); } @@ -62,8 +66,11 @@ int build_tree(t_btree **ast, t_list **lst) token = (*lst)->content; //check bug de cache case ? // cache = token->type; - if (token->type == TK_PAREN_OPEN && cache != TK_IN && cache != TK_DSEMI) + 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/ft_parse.c b/42sh/src/parser/ft_parse.c index f971f12f..01f978b8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -49,8 +49,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) parser->state = SUCCESS; else parser->state = UNDEFINED; -// build_tree(ast, token); -// btree_print(STDBUG, *ast, &ft_putast); + build_tree(ast, token); + btree_print(STDBUG, *ast, &ft_putast); if ((end_instruction(*parser->stack) && !(*token)->next)) insert_linebreak(token); else diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index f5cf146b..ebb6e4d8 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/24 22:59:52 by ariard ### ########.fr */ +/* Updated: 2017/02/25 00:15:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */