From d0b821c483863918972534d06ca5c4f6fdc2bda9 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Fri, 3 Mar 2017 17:19:39 +0100 Subject: [PATCH] pipe fix grammar + assignement word parsing --- 42sh/Makefile | 3 ++- 42sh/includes/parser.h | 10 +++++--- 42sh/src/addls | 1 + 42sh/src/main/ft_putast.c | 2 +- 42sh/src/parser/add_cmd.c | 4 ++- 42sh/src/parser/add_sep.c | 2 +- 42sh/src/parser/add_var.c | 45 +++++++++++++++++++++++++++++++++ 42sh/src/parser/aggregate_sym.c | 11 ++++---- 42sh/src/parser/build_tree.c | 2 +- 42sh/src/parser/eval_sym.c | 3 ++- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/produce_sym.c | 8 +++--- 12 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 42sh/src/addls create mode 100644 42sh/src/parser/add_var.c diff --git a/42sh/Makefile b/42sh/Makefile index dfca0908..3dc7d3f8 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/03 14:36:32 by ariard ### ########.fr # +# Updated: 2017/03/03 16:05:17 by ariard ### ########.fr # # # # **************************************************************************** # @@ -237,6 +237,7 @@ parser/add_loop.c\ parser/add_redir.c\ parser/add_sep.c\ parser/add_subshell.c\ +parser/add_var.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 925cb1d9..7ca24e5f 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/03/03 14:16:02 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:07:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,17 +125,21 @@ int add_func_cmd(t_btree **ast, t_list **lst); int add_func_sep(t_btree **ast, t_list **lst); int add_one_func(t_btree **ast, t_list **lst); int add_pipe(t_btree **ast, t_list **lst); +int add_var(t_btree **ast, t_list **lst); + int isloop(t_btree **ast, t_list **lst); 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); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); int isdir_word(t_btree **ast, t_list **list); +int isvar(t_btree **ast, t_list **list); + +int join_ast(t_btree **ast, t_btree **new_node); +int gen_node(t_btree **ast); /* * Build AST diff --git a/42sh/src/addls b/42sh/src/addls new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/42sh/src/addls @@ -0,0 +1 @@ + diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 7bce3fb5..5fdc87b3 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/03/03 14:34:25 by ariard ### ########.fr */ +/* Updated: 2017/03/03 14:56:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index ff54406f..9605f7a1 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 14:33:19 by ariard ### ########.fr */ +/* Updated: 2017/03/03 15:34:38 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ int add_cmd(t_btree **ast, t_list **lst) gen_node(ast); else if (isdir_word(ast, lst)) return (add_redir_word(ast, lst)); + else if (isvar(ast, lst)) + return (add_var(ast, lst)); else if (isloop(ast, lst) == 3) return (add_loop_condition(ast, lst)); else if (isloop(ast, lst)) diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index 5aa2d8f5..04a6fde0 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/03/03 14:27:40 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:05:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_var.c b/42sh/src/parser/add_var.c new file mode 100644 index 00000000..ad11bf54 --- /dev/null +++ b/42sh/src/parser/add_var.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/03 15:08:16 by ariard #+# #+# */ +/* Updated: 2017/03/03 16:17:27 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int isvar(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_ASSIGNEMENT_WORD && token->type == TK_ASSIGNEMENT_WORD) + return (1); + } + return (0); +} + +int add_var(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_btree *new_node; + + if (!*ast) + return (0); + new_node = NULL; + gen_node(&new_node); + join_ast(ast, &new_node); + node = (new_node)->item; + node->type = TK_SEMI; + add_cmd(&new_node, lst); + return (0); +} diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 113d812c..0a47c660 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/03/03 14:27:46 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:35:04 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ t_aggrematch g_aggrematch[] = {TK_PAREN_OPEN, CMD_SUPERIOR, FUNC_NAME, CMD_SUPERIOR}, {TK_ASSIGNEMENT_WORD, CMD_PREFIX,CMD_PREFIX, 0}, {TK_PIPE, CMD_SUPERIOR, SIMPLE_COMMAND, CMD_SUPERIOR}, + {TK_PIPE, PIPE_SEMI_SEQUENCE, PIPE_SEMI_SEQUENCE, PIPE_SEMI_SEQUENCE}, {TK_FI, ELSE_PART, IF_CLAUSE, TK_IF}, {TK_FI, COMPOUND_LIST, IF_CLAUSE, COMPLETE_CONDITION}, {TK_FI, COMPLETE_CONDITION, IF_CLAUSE, COMPLETE_CONDITION}, @@ -306,20 +307,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 a2929cb6..246cd41d 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/03/03 14:35:40 by ariard ### ########.fr */ +/* Updated: 2017/03/03 15:08:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index cae28a05..8add5de6 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -393,6 +393,7 @@ t_stackmatch g_stackmatch[] = {TK_AMP, TERM}, {TK_PIPE, PATTERN}, {TK_PIPE, CMD_SUPERIOR}, + {TK_PIPE, PIPE_SEMI_SEQUENCE}, {PATTERN_CASE, TK_IN}, {PATTERN_CASE, CASE_LIST_NS}, {TK_PAREN_OPEN, COMPLETE_COMMANDS}, @@ -1009,7 +1010,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 ef80af07..5889b603 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -32,7 +32,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) while (*token) { produce_sym(*parser->stack, parser->new_sym, token); - //DG("new sym %s", read_state(*parser->new_sym)); + DG("new sym %s", read_state(*parser->new_sym)); if (eval_sym(*parser->stack, *parser->new_sym)) return ((parser->state = ERROR)); diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index c21fec05..af056626 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/03/03 14:28:05 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:22:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,8 +105,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) @@ -114,7 +114,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++;