From 4a0fc35cbe98d75f64113643768b16c9b29d773f Mon Sep 17 00:00:00 2001 From: nass1pro Date: Wed, 17 Jun 2020 15:03:39 +0200 Subject: Clean lexe --- minishell_test | 2 +- src/parse/cmd_parse.c | 1 - src/parse/parse.c | 6 +----- src/parse/redir_parse.c | 1 - 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/minishell_test b/minishell_test index b636afa..f36a8cc 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit b636afa67abd97c61259071922d2f4f6f34a60b7 +Subproject commit f36a8ccb91cb71c1e4f15dc12cdecf3167eb1420 diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c index c655c5c..ef5a518 100755 --- a/src/parse/cmd_parse.c +++ b/src/parse/cmd_parse.c @@ -15,7 +15,6 @@ t_ast *push_cmd(t_ast *ast, t_ftlst *rest) { t_ftlst *new; - /* new = rest->data; */ if (ast == NULL) { ast = ast_new(AST_CMD); 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; } } diff --git a/src/parse/redir_parse.c b/src/parse/redir_parse.c index 839e37c..768506e 100755 --- a/src/parse/redir_parse.c +++ b/src/parse/redir_parse.c @@ -12,7 +12,6 @@ t_ast *push_redir(t_ast *ast, t_ftlst *rest) { t_ftlst *new; - /* new = rest->data; */ if (ast == NULL) { ast = ast_new(AST_CMD); -- 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 --- include/ast.h | 13 ++++---- include/eval.h | 6 ++-- include/parser.h | 5 +-- src/ast.c | 14 ++++---- src/main.c | 8 ++--- src/parse/cmd_parse.c | 1 - src/parse/parse.c | 90 ++++++++++++++++++++++++++++++++++----------------- 7 files changed, 84 insertions(+), 53 deletions(-) diff --git a/include/ast.h b/include/ast.h index bcb5e11..317e1b8 100644 --- a/include/ast.h +++ b/include/ast.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ast.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ +/* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:38 by charles #+# #+# */ -/* Updated: 2020/06/14 17:47:10 by charles ### ########.fr */ +/* Updated: 2020/06/17 16:43:12 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,12 +33,12 @@ struct s_ast; */ -typedef struct s_line +typedef struct s_op { struct s_ast *left; struct s_ast *right; enum e_token_tag sep; -} t_line; +} t_op; /* ** \brief AST node tag (type) @@ -49,7 +49,8 @@ typedef struct s_line enum e_ast_tag { AST_CMD, - AST_LINE, + AST_OP, + AST_OP_PARENT, }; /* @@ -67,7 +68,7 @@ typedef struct s_ast enum e_ast_tag tag; union { - t_line line; + t_op op; t_ftlst *cmd_argv; }; t_ftlst *redirs; diff --git a/include/eval.h b/include/eval.h index f834d45..bde0cd7 100644 --- a/include/eval.h +++ b/include/eval.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* eval.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ +/* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:30 by charles #+# #+# */ -/* Updated: 2020/06/15 11:09:49 by charles ### ########.fr */ +/* Updated: 2020/06/17 15:49:49 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ typedef struct s_eval_status typedef struct { t_eval_state *state; - t_line *line; + t_op *op; int fd_in; int fd_out; } t_fork_param_line; diff --git a/include/parser.h b/include/parser.h index 2bfa9d5..bc1e549 100644 --- a/include/parser.h +++ b/include/parser.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parser.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ +/* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */ -/* Updated: 2020/06/14 10:31:20 by charles ### ########.fr */ +/* Updated: 2020/06/17 18:03:33 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ */ t_ret *parse(t_ftlst *input); +t_ret *parse_c(t_ftlst *input); t_ast *push_cmd(t_ast *ast, t_ftlst *ret); t_ast *push_redir(t_ast *ast, t_ftlst *rest); diff --git a/src/ast.c b/src/ast.c index 1a5f7eb..8a551ef 100644 --- a/src/ast.c +++ b/src/ast.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ +/* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:42 by charles #+# #+# */ -/* Updated: 2020/06/15 13:01:20 by charles ### ########.fr */ +/* Updated: 2020/06/17 15:48:56 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,8 +31,8 @@ t_ast *ast_new(enum e_ast_tag tag) return (NULL); ast->tag = tag; ast->redirs = NULL; - ast->line.left = NULL; - ast->line.right = NULL; + ast->op.left = NULL; + ast->op.right = NULL; ast->cmd_argv = NULL; return (ast); } @@ -52,10 +52,10 @@ void ast_destroy(t_ast *ast) ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); ft_lstdestroy(&ast->redirs, (void (*)(void*))token_destroy); } - else if (ast->tag == AST_LINE) + else if (ast->tag == AST_OP) { - ast_destroy(ast->line.left); - ast_destroy(ast->line.right); + ast_destroy(ast->op.left); + ast_destroy(ast->op.right); } free(ast); } diff --git a/src/main.c b/src/main.c index 1dee5b1..9b902b8 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ +/* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/16 14:49:59 by charles ### ########.fr */ +/* Updated: 2020/06/17 15:50:33 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,8 +52,8 @@ int main(int argc, char **argv, char **envp) /* printf("===redirs===\n"); */ /* ft_lstiter(parser_out->ast->redirs, token_debug); */ - int eval_out = eval_cmd(env, path, parser_out->ast); - (void)eval_out; + //int eval_out = eval_cmd(env, path, parser_out->ast); + //(void)eval_out; } ft_htdestroy(path, free); diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c index ef5a518..15a5054 100755 --- a/src/parse/cmd_parse.c +++ b/src/parse/cmd_parse.c @@ -10,7 +10,6 @@ int parse_cmd_str_true_false(enum e_token_tag tag) return(0); } - t_ast *push_cmd(t_ast *ast, t_ftlst *rest) { t_ftlst *new; 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 --- include/lexer.h | 1 + minishell_test | 2 +- src/main.c | 40 +++++++++++- src/parse/parse.c | 191 +++++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 181 insertions(+), 53 deletions(-) diff --git a/include/lexer.h b/include/lexer.h index b0e7f65..43c3821 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -24,6 +24,7 @@ enum e_token_tag TAG_IS_STR = TAG_STR | TAG_STR_SINGLE | TAG_STR_DOUBLE, TAG_IS_REDIR = TAG_REDIR_IN | TAG_REDIR_OUT | TAG_REDIR_APPEND, + TAG_IS_SEP = TAG_AND | TAG_END | TAG_OR | TAG_PIPE, }; typedef struct diff --git a/minishell_test b/minishell_test index f36a8cc..d845a68 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit f36a8ccb91cb71c1e4f15dc12cdecf3167eb1420 +Subproject commit d845a683e67a89c699109dab660f6ec0eb74a2c4 diff --git a/src/main.c b/src/main.c index 9b902b8..4420f9d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/17 15:50:33 by nahaddac ### ########.fr */ +/* Updated: 2020/06/17 20:46:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,43 @@ void token_debug(void *v) printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content); } +void token_put(void *v) +{ + t_token *t; + + t= v; + printf("%s ", t->content); +} + +void print_level(int level) +{ + while (level-- > 0) + printf(" "); +} + +void ast_print(int level, t_ast *ast) +{ + if (ast->tag == AST_CMD) + { + print_level(level); + ft_lstiter(ast->cmd_argv, token_put); + } + else + { + print_level(level); + printf("SEP: %d\n", ast->op.sep); + + print_level(level); + printf("left:\n"); + ast_print(level + 1, ast->op.left); + + printf("\n"); + print_level(level); + printf("right:\n"); + ast_print(level + 1, ast->op.right); + } +} + int main(int argc, char **argv, char **envp) { t_path path; @@ -46,6 +83,7 @@ int main(int argc, char **argv, char **envp) //ft_lstiter(lex_out, token_debug); t_ret *parser_out = parse(lex_out); + ast_print(0, parser_out->ast); /* printf("===cmd_argv===\n"); */ /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */ 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/lexer/lexer_utils.c | 11 ++++++++--- src/main.c | 10 ++++++---- src/parse/cmd_parse.c | 1 - src/parse/parse.c | 29 ++++++++++++++++++++++++++--- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index 616c0d3..3ee41ff 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -1,5 +1,6 @@ #include "lexer.h" +// check for append tag enum e_token_tag ret_token_sep_redir_append(char *input, int i) { if (input[i + 1] == '>') @@ -8,13 +9,14 @@ enum e_token_tag ret_token_sep_redir_append(char *input, int i) } +// return token tag corresponding to string id enum e_token_tag ret_token(char *input, int i) { if (input[i] == ';') - return(TAG_AND); - if (input[i] == '&') return(TAG_END); - if (input[i] == '|' && input[i + 1] == '|') + if (input[i] == '&') + return(TAG_AND); + if (input[i] == '|' && input[i + 1] == '|') return(TAG_OR); if(input[i] == '|') return(TAG_PIPE); @@ -30,6 +32,8 @@ enum e_token_tag ret_token(char *input, int i) } +// check is char is separator +// ft_strchr(";&|><()", input) int lexer_sep(char input) { char *sep; @@ -46,6 +50,7 @@ int lexer_sep(char input) return (0); } +// skip spaces int lexe_space(char *input) { int i; diff --git a/src/main.c b/src/main.c index 4420f9d..533c9a5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/17 20:46:39 by charles ### ########.fr */ +/* Updated: 2020/06/17 22:11:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,9 @@ void ast_print(int level, t_ast *ast) if (ast->tag == AST_CMD) { print_level(level); + printf("[ "); ft_lstiter(ast->cmd_argv, token_put); + printf(" ]"); } else { @@ -80,7 +82,7 @@ int main(int argc, char **argv, char **envp) //printf("%s\n", argv[2]); t_ftlst *lex_out = lexer(ft_strdup(argv[2])); - //ft_lstiter(lex_out, token_debug); + ft_lstiter(lex_out, token_debug); t_ret *parser_out = parse(lex_out); ast_print(0, parser_out->ast); @@ -90,8 +92,8 @@ int main(int argc, char **argv, char **envp) /* printf("===redirs===\n"); */ /* ft_lstiter(parser_out->ast->redirs, token_debug); */ - //int eval_out = eval_cmd(env, path, parser_out->ast); - //(void)eval_out; + /* int eval_out = eval_cmd(env, path, parser_out->ast); */ + /* (void)eval_out; */ } ft_htdestroy(path, free); diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c index 15a5054..25802c1 100755 --- a/src/parse/cmd_parse.c +++ b/src/parse/cmd_parse.c @@ -2,7 +2,6 @@ #include "parser.h" - int parse_cmd_str_true_false(enum e_token_tag tag) { if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE) 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/main.c | 20 ++++++++++++++------ src/parse/parse.c | 24 ++++++++++++------------ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index 533c9a5..e3391bf 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/17 22:11:31 by charles ### ########.fr */ +/* Updated: 2020/06/18 11:26:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,23 +48,31 @@ void ast_print(int level, t_ast *ast) if (ast->tag == AST_CMD) { print_level(level); - printf("[ "); + printf("cmd: [ "); ft_lstiter(ast->cmd_argv, token_put); + printf(" ] redirs: ["); + ft_lstiter(ast->redirs, token_put); printf(" ]"); } else { + /* print_level(level); */ + /* printf("SEP: %d\n", ast->op.sep); */ print_level(level); - printf("SEP: %d\n", ast->op.sep); + printf("{\n"); print_level(level); - printf("left:\n"); + printf(" left:\n"); ast_print(level + 1, ast->op.left); printf("\n"); print_level(level); - printf("right:\n"); + printf(" right:\n"); ast_print(level + 1, ast->op.right); + + printf("\n"); + print_level(level); + printf("}\n"); } } @@ -82,7 +90,7 @@ int main(int argc, char **argv, char **envp) //printf("%s\n", argv[2]); t_ftlst *lex_out = lexer(ft_strdup(argv[2])); - ft_lstiter(lex_out, token_debug); + /* ft_lstiter(lex_out, token_debug); */ t_ret *parser_out = parse(lex_out); ast_print(0, parser_out->ast); 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(-) 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 --- include/parser.h | 15 ++++++++++++--- src/eval/eval.c | 4 ++-- src/lexer/lexer.c | 6 +++--- src/parse/parse.c | 4 +--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/parser.h b/include/parser.h index bc1e549..302c45f 100644 --- a/include/parser.h +++ b/include/parser.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */ -/* Updated: 2020/06/17 18:03:33 by nahaddac ### ########.fr */ +/* Updated: 2020/06/18 13:18:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,11 +37,20 @@ ** parse.c */ + t_ret *parse(t_ftlst *input); -t_ret *parse_c(t_ftlst *input); +t_ret *parse_op(t_ftlst *input); +t_ret *parse_expr(t_ftlst *input); +t_ret *parse_cmd(t_ftlst *input); + +// utils +t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest); +t_ftlst *push_token(t_ftlst **tokens, t_token *pushed); + + + t_ast *push_cmd(t_ast *ast, t_ftlst *ret); t_ast *push_redir(t_ast *ast, t_ftlst *rest); - int parse_cmd_str_true_false(enum e_token_tag tag); int parse_redir_true_false(enum e_token_tag tag); diff --git a/src/eval/eval.c b/src/eval/eval.c index c1b580f..a831237 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* eval.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ +/* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:21 by charles #+# #+# */ -/* Updated: 2020/06/14 10:42:37 by charles ### ########.fr */ +/* Updated: 2020/06/18 13:16:34 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 2b1bdce..9b43616 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -9,9 +9,9 @@ int len_is_not_sep(char *input) { if (lexer_sep(input[i])) { - if (input[i + 1] == ' ') - while(input[++i] == ' ') - ; + //if (input[i + 1] == ' ') + // while(input[++i] == ' ') + // ; return(i); } if (input[i] == '\'' || input[i] == '"') 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