From 4a0fc35cbe98d75f64113643768b16c9b29d773f Mon Sep 17 00:00:00 2001 From: nass1pro Date: Wed, 17 Jun 2020 15:03:39 +0200 Subject: Clean lexe --- src/parse/parse.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index 37192b0..1fc24be 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -7,6 +7,7 @@ #include "lexer.h" // stdio.h est deja include dans minishell.h temporairement // (comme ca on doit le retirer a un seul endroit a la fin) +// oui je sais maintenant tu peux effacer se petit commantaire ;) @@ -27,9 +28,7 @@ t_ret *parse(t_ftlst *input) { tag = ((t_token *)ret->rest->data)->tag; if (parse_cmd_str_true_false(tag)) - { ret->ast = push_cmd(ret->ast, ret->rest); - } else if (parse_redir_true_false(tag)) { while(ret->rest != NULL) @@ -40,10 +39,7 @@ t_ret *parse(t_ftlst *input) else if (tag & TAG_IS_REDIR) ret->rest = ret->rest->next; else - { - //ret->rest = ret->rest->next; break; - } tag = ((t_token *)ret->rest->data)->tag; } } -- cgit From ae510a886b202926b4a5502de02a938054844ad4 Mon Sep 17 00:00:00 2001 From: nass1pro Date: Wed, 17 Jun 2020 19:01:25 +0200 Subject: Update line op --- src/parse/parse.c | 90 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 30 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index 1fc24be..b9bb156 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nahaddac +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ +/* Updated: 2020/06/17 18:36:54 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + /* ** \file parse.c ** \brief Parser @@ -11,11 +23,43 @@ +t_ast *cmd_push_ast(t_ftlst *rest) +{ + enum e_token_tag tag; + t_ast *ast; + + ast = NULL; + while (rest != NULL) + { + tag = ((t_token *)rest->data)->tag; + if (parse_cmd_str_true_false(tag)) + { + ast = push_cmd(ast, rest); + } + else if (parse_redir_true_false(tag)) + { + while(rest != NULL) + { + ast = push_redir(ast, rest); + if (tag & TAG_IS_STR && tag & TAG_STICK) + rest = rest->next; + else if (tag & TAG_IS_REDIR) + rest = rest->next; + else + break; + tag = ((t_token *)rest->data)->tag; + } + } + rest = rest->next; + } + return ast; +} + t_ret *parse(t_ftlst *input) { t_ret *ret; t_ret *first; - enum e_token_tag tag; + t_ast *new_ast; if(!(ret = malloc(sizeof(t_ret) * 1))) return(NULL); @@ -24,38 +68,24 @@ t_ret *parse(t_ftlst *input) ret->unexpected = NULL; first = ret; - while (ret->rest != NULL) + if(!(new_ast = malloc(sizeof(t_ast) * 1))) + return(0); + new_ast = cmd_push_ast(ret->rest); + printf("%s\n",((t_token *)ret->rest->data)->content); + if (ret->ast) { - tag = ((t_token *)ret->rest->data)->tag; - if (parse_cmd_str_true_false(tag)) - ret->ast = push_cmd(ret->ast, ret->rest); - else if (parse_redir_true_false(tag)) + while(ret->ast->cmd_argv != NULL) + { + printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content); + ret->ast->cmd_argv = ret->ast->cmd_argv->next; + } + while(ret->ast->redirs != NULL) { - while(ret->rest != NULL) - { - ret->ast = push_redir(ret->ast, ret->rest); - if (tag & TAG_IS_STR && tag & TAG_STICK) - ret->rest = ret->rest->next; - else if (tag & TAG_IS_REDIR) - ret->rest = ret->rest->next; - else - break; - tag = ((t_token *)ret->rest->data)->tag; - } + printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content); + ret->ast->redirs = ret->ast->redirs->next; } - ret->rest = ret->rest->next; } - /* while(ret->ast->cmd_argv != NULL) */ - /* { */ - /* printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content); */ - /* ret->ast->cmd_argv = ret->ast->cmd_argv->next; */ - /* } */ - /* while(ret->ast->redirs != NULL) */ - /* { */ - /* printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content); */ - /* ret->ast->redirs = ret->ast->redirs->next; */ - /* } */ - /* ast_destroy(ret->ast); */ - /* ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); */ + ast_destroy(ret->ast); + ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); return first; } -- cgit From 9a11d78abf7dc8fb8c8f3430538e80622a7854d3 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 17 Jun 2020 20:47:59 +0200 Subject: Show nass the power of parser combinator --- src/parse/parse.c | 191 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 140 insertions(+), 51 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index b9bb156..6a80e37 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/17 18:36:54 by nahaddac ### ########.fr */ +/* Updated: 2020/06/17 20:39:25 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,75 +17,164 @@ #include "parser.h" #include "lexer.h" -// stdio.h est deja include dans minishell.h temporairement -// (comme ca on doit le retirer a un seul endroit a la fin) -// oui je sais maintenant tu peux effacer se petit commantaire ;) +t_ftlst *push_token(t_ftlst **tokens, t_token *pushed) +{ + t_ftlst *tmp; + + if ((tmp = ft_lstnew(pushed)) == NULL) + return (NULL); + ft_lstpush_back(tokens, tmp); + return (tmp); +} + +t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest) +{ + t_ret *ret; + + if ((ret = malloc(sizeof(t_ret))) == NULL) + return (NULL); + ret->unexpected = NULL; + ret->rest = rest; + ret->ast = ast; + return ret; +} -t_ast *cmd_push_ast(t_ftlst *rest) +t_ret *parse_cmd(t_ftlst *input) { - enum e_token_tag tag; - t_ast *ast; + enum e_token_tag tag; + t_ast *ast; - ast = NULL; - while (rest != NULL) + ast = ast_new(AST_CMD); + while (input != NULL) { - tag = ((t_token *)rest->data)->tag; - if (parse_cmd_str_true_false(tag)) + tag = ((t_token *)input->data)->tag; + if (tag & TAG_IS_STR) { - ast = push_cmd(ast, rest); + push_token(&ast->cmd_argv, input->data); } - else if (parse_redir_true_false(tag)) + else if (tag & TAG_IS_REDIR) { - while(rest != NULL) + while(input != NULL) { - ast = push_redir(ast, rest); + push_token(&ast->redirs, input->data); if (tag & TAG_IS_STR && tag & TAG_STICK) - rest = rest->next; + input = input->next; else if (tag & TAG_IS_REDIR) - rest = rest->next; + input = input->next; else break; - tag = ((t_token *)rest->data)->tag; + tag = ((t_token *)input->data)->tag; } } - rest = rest->next; + else + return ret_wrap_ast(ast, input); + input = input->next; } - return ast; + return ret_wrap_ast(ast, input); } -t_ret *parse(t_ftlst *input) +// ::= ... +// ::= | + +t_ret *parse_op(t_ftlst *input) { - t_ret *ret; - t_ret *first; - t_ast *new_ast; - - if(!(ret = malloc(sizeof(t_ret) * 1))) - return(NULL); - ret->rest = input; - ret->ast = NULL; - ret->unexpected = NULL; - first = ret; + t_ast *ast; + t_ret *left_ret; + t_ret *right_ret; + enum e_token_tag sep_tag; - if(!(new_ast = malloc(sizeof(t_ast) * 1))) - return(0); - new_ast = cmd_push_ast(ret->rest); - printf("%s\n",((t_token *)ret->rest->data)->content); - if (ret->ast) - { - while(ret->ast->cmd_argv != NULL) - { - printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content); - ret->ast->cmd_argv = ret->ast->cmd_argv->next; - } - while(ret->ast->redirs != NULL) - { - printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content); - ret->ast->redirs = ret->ast->redirs->next; - } - } - ast_destroy(ret->ast); - ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); - return first; + left_ret = parse_cmd(input); + input = left_ret->rest; + + if (input == NULL)//((t_token*)input->data)->tag & TAG_IS_SEP) + return ret_wrap_ast(left_ret->ast, input); + + sep_tag = ((t_token*)input->data)->tag; + input = input->next; + + right_ret = parse_op(input); + + ast = ast_new(AST_OP); + ast->op.left = left_ret->ast; + ast->op.right = right_ret->ast; + ast->op.sep = sep_tag; + return ret_wrap_ast(ast, right_ret->rest); } + +t_ret *parse(t_ftlst *input) +{ + return parse_op(input); +} + +//////////////////////////////////////////////////////////////// +// saved +//////////////////////////////////////////////////////////////// + +/* t_ast *cmd_push_ast(t_ftlst *rest) */ +/* { */ +/* enum e_token_tag tag; */ +/* t_ast *ast; */ +/* */ +/* ast = NULL; */ +/* while (rest != NULL) */ +/* { */ +/* tag = ((t_token *)rest->data)->tag; */ +/* if (parse_cmd_str_true_false(tag)) */ +/* { */ +/* ast = push_cmd(ast, rest); */ +/* } */ +/* else if (parse_redir_true_false(tag)) */ +/* { */ +/* while(rest != NULL) */ +/* { */ +/* ast = push_redir(ast, rest); */ +/* if (tag & TAG_IS_STR && tag & TAG_STICK) */ +/* rest = rest->next; */ +/* else if (tag & TAG_IS_REDIR) */ +/* rest = rest->next; */ +/* else */ +/* break; */ +/* tag = ((t_token *)rest->data)->tag; */ +/* } */ +/* } */ +/* rest = rest->next; */ +/* } */ +/* return ast; */ +/* } */ +/* */ +/* t_ret *parse(t_ftlst *input) */ +/* { */ +/* t_ret *ret; */ +/* t_ret *first; */ +/* t_ast *new_ast; */ +/* */ +/* if(!(ret = malloc(sizeof(t_ret) * 1))) */ +/* return(NULL); */ +/* ret->rest = input; */ +/* ret->ast = NULL; */ +/* ret->unexpected = NULL; */ +/* first = ret; */ +/* */ +/* if(!(new_ast = malloc(sizeof(t_ast) * 1))) */ +/* return(0); */ +/* new_ast = cmd_push_ast(ret->rest); */ +/* printf("%s\n",((t_token *)ret->rest->data)->content); */ +/* if (ret->ast) */ +/* { */ +/* while(ret->ast->cmd_argv != NULL) */ +/* { */ +/* printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content); */ +/* ret->ast->cmd_argv = ret->ast->cmd_argv->next; */ +/* } */ +/* while(ret->ast->redirs != NULL) */ +/* { */ +/* printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content); */ +/* ret->ast->redirs = ret->ast->redirs->next; */ +/* } */ +/* } */ +/* ast_destroy(ret->ast); */ +/* ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); */ +/* return first; */ +/* } */ -- cgit From f4a8ff23a590dad1b43a2e92df5c5c6b8951976a Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 17 Jun 2020 23:01:07 +0200 Subject: Added parse_expr draft --- src/parse/parse.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index 6a80e37..7217fdb 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/17 20:39:25 by charles ### ########.fr */ +/* Updated: 2020/06/17 23:00:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ t_ret *parse_op(t_ftlst *input) left_ret = parse_cmd(input); input = left_ret->rest; - if (input == NULL)//((t_token*)input->data)->tag & TAG_IS_SEP) + if (input == NULL) //((t_token*)input->data)->tag & TAG_IS_SEP) return ret_wrap_ast(left_ret->ast, input); sep_tag = ((t_token*)input->data)->tag; @@ -103,6 +103,29 @@ t_ret *parse_op(t_ftlst *input) return ret_wrap_ast(ast, right_ret->rest); } +t_ret *parse_expr(t_ftlst *input) +{ + t_ret *tmp; + enum e_token_tag tag; + + tag = input->data->tag; + if (tag == TAG_PARENT_OPEN) + { + tmp = parse_expr(input->next); + input = tmp->rest; + tag = input->data->tag; + if (tag != TAG_PARENT_CLOSE) + return (NULL); + input = input->next; + tmp->rest = input; + return tmp; + } + tmp = parse_op(input); + if (tmp->unexpected != NULL) + return tmp; + return parse_cmd(input); +} + t_ret *parse(t_ftlst *input) { return parse_op(input); @@ -143,7 +166,7 @@ t_ret *parse(t_ftlst *input) /* } */ /* return ast; */ /* } */ -/* */ + /* t_ret *parse(t_ftlst *input) */ /* { */ /* t_ret *ret; */ -- cgit From 3b21eea3ff3c803aa6f07c3a5bd048795c586b46 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 18 Jun 2020 11:29:56 +0200 Subject: Fixing parse_expr and parse_op to work with parenthesis --- src/parse/parse.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index 7217fdb..f49f1f3 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/17 23:00:29 by charles ### ########.fr */ +/* Updated: 2020/06/18 11:29:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,8 +75,11 @@ t_ret *parse_cmd(t_ftlst *input) return ret_wrap_ast(ast, input); } -// ::= ... -// ::= | +// ::= ( | )+ +// ::= | +// ::= '(' ')' | + +t_ret *parse_expr(t_ftlst *input); t_ret *parse_op(t_ftlst *input) { @@ -85,10 +88,10 @@ t_ret *parse_op(t_ftlst *input) t_ret *right_ret; enum e_token_tag sep_tag; - left_ret = parse_cmd(input); + left_ret = parse_expr(input); input = left_ret->rest; - if (input == NULL) //((t_token*)input->data)->tag & TAG_IS_SEP) + if (input == NULL || ((t_token*)input->data)->tag == TAG_PARENT_CLOSE) return ret_wrap_ast(left_ret->ast, input); sep_tag = ((t_token*)input->data)->tag; @@ -105,24 +108,21 @@ t_ret *parse_op(t_ftlst *input) t_ret *parse_expr(t_ftlst *input) { - t_ret *tmp; + t_ret *tmp; enum e_token_tag tag; - tag = input->data->tag; + tag = ((t_token*)input->data)->tag; if (tag == TAG_PARENT_OPEN) { - tmp = parse_expr(input->next); + tmp = parse_op(input->next); input = tmp->rest; - tag = input->data->tag; + tag = ((t_token*)input->data)->tag; if (tag != TAG_PARENT_CLOSE) return (NULL); input = input->next; tmp->rest = input; return tmp; } - tmp = parse_op(input); - if (tmp->unexpected != NULL) - return tmp; return parse_cmd(input); } -- cgit From 1f0fde016765d33dc43c3c37d654be28f11c758e Mon Sep 17 00:00:00 2001 From: nass1pro Date: Thu, 18 Jun 2020 12:41:46 +0200 Subject: Clean parse --- src/parse/parse.c | 73 +------------------------------------------------------ 1 file changed, 1 insertion(+), 72 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index f49f1f3..a46de84 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/18 11:29:09 by charles ### ########.fr */ +/* Updated: 2020/06/18 12:40:23 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -130,74 +130,3 @@ t_ret *parse(t_ftlst *input) { return parse_op(input); } - -//////////////////////////////////////////////////////////////// -// saved -//////////////////////////////////////////////////////////////// - -/* t_ast *cmd_push_ast(t_ftlst *rest) */ -/* { */ -/* enum e_token_tag tag; */ -/* t_ast *ast; */ -/* */ -/* ast = NULL; */ -/* while (rest != NULL) */ -/* { */ -/* tag = ((t_token *)rest->data)->tag; */ -/* if (parse_cmd_str_true_false(tag)) */ -/* { */ -/* ast = push_cmd(ast, rest); */ -/* } */ -/* else if (parse_redir_true_false(tag)) */ -/* { */ -/* while(rest != NULL) */ -/* { */ -/* ast = push_redir(ast, rest); */ -/* if (tag & TAG_IS_STR && tag & TAG_STICK) */ -/* rest = rest->next; */ -/* else if (tag & TAG_IS_REDIR) */ -/* rest = rest->next; */ -/* else */ -/* break; */ -/* tag = ((t_token *)rest->data)->tag; */ -/* } */ -/* } */ -/* rest = rest->next; */ -/* } */ -/* return ast; */ -/* } */ - -/* t_ret *parse(t_ftlst *input) */ -/* { */ -/* t_ret *ret; */ -/* t_ret *first; */ -/* t_ast *new_ast; */ -/* */ -/* if(!(ret = malloc(sizeof(t_ret) * 1))) */ -/* return(NULL); */ -/* ret->rest = input; */ -/* ret->ast = NULL; */ -/* ret->unexpected = NULL; */ -/* first = ret; */ -/* */ -/* if(!(new_ast = malloc(sizeof(t_ast) * 1))) */ -/* return(0); */ -/* new_ast = cmd_push_ast(ret->rest); */ -/* printf("%s\n",((t_token *)ret->rest->data)->content); */ -/* if (ret->ast) */ -/* { */ -/* while(ret->ast->cmd_argv != NULL) */ -/* { */ -/* printf("[%s]\n", ((t_token *)ret->ast->cmd_argv->data)->content); */ -/* ret->ast->cmd_argv = ret->ast->cmd_argv->next; */ -/* } */ -/* while(ret->ast->redirs != NULL) */ -/* { */ -/* printf("[%s]\n", ((t_token *)ret->ast->redirs->data)->content); */ -/* ret->ast->redirs = ret->ast->redirs->next; */ -/* } */ -/* } */ -/* ast_destroy(ret->ast); */ -/* ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); */ -/* return first; */ -/* } */ -- cgit From 4d5e2f861331989f8de16e3b0458e45b34bd0b6f Mon Sep 17 00:00:00 2001 From: nass1pro Date: Thu, 18 Jun 2020 13:19:21 +0200 Subject: Change parse --- src/parse/parse.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/parse/parse.c') diff --git a/src/parse/parse.c b/src/parse/parse.c index a46de84..198ac65 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/18 12:40:23 by nahaddac ### ########.fr */ +/* Updated: 2020/06/18 12:48:20 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,8 +79,6 @@ t_ret *parse_cmd(t_ftlst *input) // ::= | // ::= '(' ')' | -t_ret *parse_expr(t_ftlst *input); - t_ret *parse_op(t_ftlst *input) { t_ast *ast; -- cgit