diff options
| -rw-r--r-- | include/ast.h | 9 | ||||
| -rw-r--r-- | include/lexer.h | 12 | ||||
| -rw-r--r-- | include/parser.h | 6 | ||||
| -rw-r--r-- | memo | 4 | ||||
| -rw-r--r-- | src/ast.c | 4 | ||||
| -rw-r--r-- | src/builtin/export.c | 10 | ||||
| -rw-r--r-- | src/eval/eval.c | 82 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rwxr-xr-x | src/parse/parse.c | 69 | ||||
| -rw-r--r-- | src/parse/parse_error.c | 106 |
10 files changed, 272 insertions, 32 deletions
diff --git a/include/ast.h b/include/ast.h index 18f1d93..597c444 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:38 by charles #+# #+# */ -/* Updated: 2020/06/19 10:40:34 by charles ### ########.fr */ +/* Updated: 2020/06/19 13:29:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,9 +48,9 @@ typedef struct s_op enum e_ast_tag { - AST_CMD = 1 << 0, - AST_OP = 1 << 1, - AST_PARENT = 1 << 2, + AST_CMD, + AST_OP, + AST_PARENT, }; /* @@ -70,6 +70,7 @@ typedef struct s_ast { t_op op; t_ftlst *cmd_argv; + struct s_ast *parent_ast; }; t_ftlst *redirs; } t_ast; diff --git a/include/lexer.h b/include/lexer.h index 43c3821..df05c20 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/19 10:51:26 by nahaddac #+# #+# */ +/* Updated: 2020/06/19 10:51:30 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef LEXER_H # define LEXER_H diff --git a/include/parser.h b/include/parser.h index 302c45f..0bdc679 100644 --- a/include/parser.h +++ b/include/parser.h @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */ -/* Updated: 2020/06/18 13:18:13 by charles ### ########.fr */ +/* Updated: 2020/06/18 16:34:18 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define PARSE_H # include "minishell.h" +#include "libft_str.h" # include "ast.h" /* @@ -54,4 +55,7 @@ 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); +// error +t_token *error_syntax_simple(t_ftlst *in); + #endif @@ -0,0 +1,4 @@ +export A=a B=b C=c + + +parenthèse dans parser_error.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:42 by charles #+# #+# */ -/* Updated: 2020/06/18 13:39:30 by charles ### ########.fr */ +/* Updated: 2020/06/18 14:46:07 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ void ast_destroy(t_ast *ast) { if (ast == NULL) return ; - ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); + //ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); if (ast->tag == AST_CMD) { ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); diff --git a/src/builtin/export.c b/src/builtin/export.c index c60d8f0..f41bcb0 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/06/18 13:50:47 by charles ### ########.fr */ +/* Updated: 2020/06/19 11:21:50 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int builtin_export(char **argv, t_env env) (void)env; if (argv[1] == NULL) - return (4); + return (0); if(ft_isdigit(argv[1][0])) return(0); i = 0; @@ -36,14 +36,14 @@ int builtin_export(char **argv, t_env env) while(temp[i] != '\0') { if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0) - return(2); + return(0); if (temp[i] == '=') { temp[i] = '\0'; - env_export(env, temp, argv[1][i + 1]); + env_export(env, temp, &argv[1][i + 1]); return(0); } i++; } - return (3); + return (0); } diff --git a/src/eval/eval.c b/src/eval/eval.c new file mode 100644 index 0000000..a831237 --- /dev/null +++ b/src/eval/eval.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* eval.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 17:05:21 by charles #+# #+# */ +/* Updated: 2020/06/18 13:16:34 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** \file eval.c +** \brief Evaluation of an AST +*/ + +/* #include "eval.h" */ + +/* #<{(| */ +/* ** \brief Evaluate a line */ +/* ** \param state State of the evaluation */ +/* ** \param line Line to evaluate */ +/* ** \return Last Executed command status or -1 on error */ +/* |)}># */ +/* */ +/* static int eval_line(void *param) */ +/* { */ +/* int status; */ +/* t_eval_state *state; */ +/* t_line *line; */ +/* int fd_in; */ +/* int fd_out; */ +/* */ +/* state = ((t_fork_param_line*)param)->state; */ +/* line = ((t_fork_param_line*)param)->line; */ +/* fd_in = ((t_fork_param_line*)param)->fd_in; */ +/* fd_out = ((t_fork_param_line*)param)->fd_out; */ +/* */ +/* #<{(| if (line->right == NULL) |)}># */ +/* #<{(| return (eval(state, line->left)); |)}># */ +/* */ +/* #<{(| if (line->sep == SEP_PIPE) |)}># */ +/* #<{(| pipe(state->p); |)}># */ +/* */ +/* if (line->left->tag == AST_LINE) */ +/* { */ +/* return (fork_wrap(fd_in, fd_out, param, &eval_line)); */ +/* } */ +/* if ((status = eval(fd_in, fd_out, state, line->left)) == -1) */ +/* return (-1); */ +/* if ((line->sep == SEP_AND && status != 0) || */ +/* (line->sep == SEP_OR && status == 0)) */ +/* return (status); */ +/* */ +/* return (eval(fd_in, fd_out, state, line->right)); */ +/* } */ +/* */ +/* #<{(| */ +/* ** \brief Evaluate an AST */ +/* ** \param state State of the evaluation */ +/* ** \param ast Abstract syntax tree to evaluate */ +/* ** \return Last command status or -1 on error */ +/* |)}># */ +/* */ +/* int eval(int fd_in, int fd_out, t_eval_state *state, t_ast *ast) */ +/* { */ +/* t_fork_param_line param; */ +/* */ +/* errno = 0; */ +/* if (ast->tag == TAG_LINE) */ +/* { */ +/* param.state = state; */ +/* param.line = &ast->line; */ +/* param.fd_in = fd_in; */ +/* param.fd_out = fd_out; */ +/* return (eval_line(¶m)); */ +/* } */ +/* if (ast->tag == TAG_CMD) */ +/* return (eval_cmd(fd_in, fd_out, state, &ast->cmd)); */ +/* return (-1); */ +/* } */ @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/19 12:16:20 by charles ### ########.fr */ +/* Updated: 2020/06/19 13:30:40 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/parse/parse.c b/src/parse/parse.c index 198ac65..cc4d56d 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/18 12:48:20 by nahaddac ### ########.fr */ +/* Updated: 2020/06/19 13:28:12 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -104,27 +104,58 @@ t_ret *parse_op(t_ftlst *input) return ret_wrap_ast(ast, right_ret->rest); } -t_ret *parse_expr(t_ftlst *input) +t_ret *parse_expr(t_ftlst *input) { - t_ret *tmp; - enum e_token_tag tag; - - tag = ((t_token*)input->data)->tag; - if (tag == TAG_PARENT_OPEN) - { - tmp = parse_op(input->next); - input = tmp->rest; - tag = ((t_token*)input->data)->tag; - if (tag != TAG_PARENT_CLOSE) - return (NULL); - input = input->next; - tmp->rest = input; - return tmp; - } - return parse_cmd(input); + t_ret *tmp; + enum e_token_tag tag; + tag = ((t_token*)input->data)->tag; + if (tag == TAG_PARENT_OPEN) + { + tmp = parse_op(input->next); + input = tmp->rest; + tag = ((t_token*)input->data)->tag; + if (tag != TAG_PARENT_CLOSE) + return (NULL); + input = input->next; + t_ast *new_ast = ast_new(AST_PARENT); + new_ast = tmp->ast; + tmp->ast = new_ast; + if (tag & TAG_IS_REDIR) + { + while(input != NULL) + { + push_token(&tmp->ast->redirs, input->data); + if (tag & TAG_IS_STR && tag & TAG_STICK) + input = input->next; + else if (tag & TAG_IS_REDIR) + input = input->next; + else + break; + tag = ((t_token *)input->data)->tag; + } + } + tmp->rest = input; + return tmp; + } + return parse_cmd(input); } t_ret *parse(t_ftlst *input) { - return parse_op(input); + t_ret *ret; + t_ftlst *in_f; + + in_f = input; + if (input == NULL) + return NULL; + if (!(ret = malloc(sizeof(t_ret) * 1))) + return (NULL); + ret->ast = NULL; + ret->rest = NULL; + if((ret->unexpected = error_syntax_simple(input)) != NULL) + printf("%s\n", ret->unexpected->content); + ret = parse_op(in_f); + ast_destroy(ret->ast); + ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); + return (NULL); } diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c new file mode 100644 index 0000000..b721c28 --- /dev/null +++ b/src/parse/parse_error.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */ +/* Updated: 2020/06/19 12:46:56 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +t_token *error_syntax_parent(t_ftlst *in) +{ + int op; + int cl; + t_token *tk; + + op = 0; + cl = 0; + while(in != NULL) + { + tk = in->data; + if (tk->tag & TAG_PARENT_OPEN) + op++; + if(tk->tag & TAG_PARENT_CLOSE) + cl++; + if (cl && op == 0) + { + tk->content = ft_strjoin3( + "minishell: syntax error near unexpected token `", + tk->content, "'"); + return tk; + } + in = in->next; + } + return NULL; +} + +int out_error_first(t_token *tk) +{ + int i; + + i = 0; + if(tk->tag & TAG_IS_SEP) + return(1); + if (tk->tag & TAG_IS_REDIR) + { + while(tk->content[i]) + i++; + if (tk->tag & TAG_REDIR_APPEND && i <= 2) + return (0); + else + return(1); + } + return(0); +} + +t_token *error_syntax_simple(t_ftlst *in) +{ + t_token *tk; + size_t i; + t_ftlst *first; + + tk = in->data; + first = in; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (out_error_first(tk)) + { + i = ft_strlen(tk->content); + if (i >= 2) + tk->content[2] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + while(in != NULL) + { + i = 0; + tk = in->data; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (((t_token *)in->next->data)->tag & + ((t_token*)in->next->data)->tag & TAG_IS_SEP || + (((t_token*)in->next->data)->tag & TAG_IS_REDIR)) + { + tk = in->next->data; + i = ft_strlen(tk->content); + if (i >= 3) + tk->content[3] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + in = in->next; + } + return error_syntax_parent(first); + +} |
