aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/ast.h18
-rw-r--r--include/eval.h4
-rw-r--r--include/minishell.h9
-rw-r--r--include/parser.h25
-rw-r--r--src/ast.c11
-rw-r--r--src/debug.c126
-rw-r--r--src/error.c12
-rw-r--r--src/eval/cmd.c4
-rw-r--r--src/eval/redir.c37
-rw-r--r--src/main.c24
-rw-r--r--src/parse/cmd_parse.c27
-rw-r--r--src/parse/parse.c192
-rw-r--r--src/parse/parse_error.c13
-rw-r--r--src/parse/redir_parse.c26
-rw-r--r--src/preprocess.c95
15 files changed, 244 insertions, 379 deletions
diff --git a/include/ast.h b/include/ast.h
index 7fec75d..8048dd2 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/07/14 09:57:43 by charles ### ########.fr */
+/* Updated: 2020/08/27 18:38:45 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -46,7 +46,7 @@ typedef struct s_op
** \param TAG_OP Operation AST node
*/
-enum e_ast_tag
+enum e_ast
{
AST_CMD,
AST_OP,
@@ -65,24 +65,24 @@ enum e_ast_tag
typedef struct s_ast
{
- enum e_ast_tag tag;
+ enum e_ast tag;
union
{
t_op op;
- t_ftlst *cmd_argv;
+ t_tok_lst *cmd_argv;
struct s_ast *parent_ast;
};
- t_ftlst *redirs;
+ t_tok_lst *redirs;
} t_ast;
-typedef struct s_ret
+typedef struct s_parsed
{
bool syntax_error;
t_ast *ast;
- t_ftlst *rest;
-} t_ret;
+ t_tok_lst *rest;
+} t_parsed;
-t_ast *ast_new(enum e_ast_tag tag);
+t_ast *ast_new(enum e_ast tag);
void ast_destroy(t_ast *ast);
#endif
diff --git a/include/eval.h b/include/eval.h
index f2920b2..c9adf72 100644
--- a/include/eval.h
+++ b/include/eval.h
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:30 by charles #+# #+# */
-/* Updated: 2020/07/20 13:22:26 by nahaddac ### ########.fr */
+/* Updated: 2020/08/27 17:15:19 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -64,7 +64,7 @@ t_ftlst *split_token(t_ftlst **lst, enum e_tok);
** redir.c
*/
-bool redir_extract(t_ftlst *redirs, t_env env, int fds[2]);
+bool redir_extract(t_tok_lst *redirs, t_env env, int fds[2]);
/*
** exec.c
diff --git a/include/minishell.h b/include/minishell.h
index a934bb7..1b23952 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */
-/* Updated: 2020/08/20 14:45:19 by charles ### ########.fr */
+/* Updated: 2020/08/27 17:18:43 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,6 +37,8 @@
# include "libft_vec.h"
# include "libft_dstr.h"
+# include "lexer.h"
+
/*
** \brief Value of pipe entry if closed
*/
@@ -107,8 +109,8 @@ int builtin_exit(char **argv, t_env env);
** preprocess.c
*/
-char **preprocess(t_ftlst **tokens, t_env env);
-char *preprocess_filename(t_ftlst **tokens, t_env env);
+char **preprocess(t_tok_lst **tokens, t_env env);
+char *preprocess_filename(t_tok_lst **tokens, t_env env);
/*
** error.c
@@ -125,6 +127,7 @@ typedef enum
} t_err;
void errorf(const char *format, ...);
+void verrorf(const char *format, va_list ap);
/*
** signal.c
diff --git a/include/parser.h b/include/parser.h
index d228dca..7103d0b 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 16:34:18 by nahaddac ### ########.fr */
+/* Updated: 2020/08/27 18:40:55 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -38,24 +38,9 @@
** parse.c
*/
-
-t_ret *parse(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_tok tag);
-int parse_redir_true_false(enum e_tok tag);
-
-// error
-t_token *error_syntax_simple(t_ftlst *in);
+t_parsed *parse(t_tok_lst *input);
+t_parsed *parse_op(t_tok_lst *input);
+t_parsed *parse_expr(t_tok_lst *input);
+t_parsed *parse_cmd(t_tok_lst *input);
#endif
diff --git a/src/ast.c b/src/ast.c
index 4e67c52..e2c4017 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:42 by charles #+# #+# */
-/* Updated: 2020/06/18 14:46:07 by nahaddac ### ########.fr */
+/* Updated: 2020/08/27 18:39:31 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,7 +23,7 @@
** \return Created node or NULL on error
*/
-t_ast *ast_new(enum e_ast_tag tag)
+t_ast *ast_new(enum e_ast tag)
{
t_ast *ast;
@@ -46,16 +46,17 @@ void ast_destroy(t_ast *ast)
{
if (ast == NULL)
return ;
- //ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy);
if (ast->tag == AST_CMD)
{
- ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy);
- ft_lstdestroy(&ast->redirs, (void (*)(void*))token_destroy);
+ tok_lst_destroy(&ast->cmd_argv, free);
+ tok_lst_destroy(&ast->redirs, free);
}
else if (ast->tag == AST_OP)
{
ast_destroy(ast->op.left);
ast_destroy(ast->op.right);
}
+ else if (ast->tag == AST_PARENT)
+ ast_destroy(ast->parent_ast);
free(ast);
}
diff --git a/src/debug.c b/src/debug.c
index c2b416e..8255016 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -3,66 +3,66 @@
#include "lexer.h"
#include "ast.h"
-void token_debug(void *v)
-{
- t_token *t;
-
- t= 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_PARENT)
- {
- print_level(level);
- printf("parent: redir [ ");
- ft_lstiter(ast->redirs, token_put);
- printf(" ]\n");
- ast_print(level + 1, ast->parent_ast);
- }
- else if (ast->tag == AST_CMD)
- {
- print_level(level);
- printf("cmd: [ ");
- ft_lstiter(ast->cmd_argv, token_put);
- printf(" ] redirs: [");
- ft_lstiter(ast->redirs, token_put);
- printf(" ]");
- }
- else if (ast->tag == AST_OP) {
- /* printf("SEP: %d\n", ast->op.sep); */
- print_level(level);
- /* printf("redirs: ["); */
- /* ft_lstiter(ast->redirs, token_put); */
- /* printf(" ] "); */
- printf("{\n");
-
- 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);
-
- printf("\n");
- print_level(level);
- printf("}\n");
- }
-}
+/* void token_debug(void *v) */
+/* { */
+/* t_token *t; */
+/* */
+/* t= 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_PARENT) */
+/* { */
+/* print_level(level); */
+/* printf("parent: redir [ "); */
+/* ft_lstiter(ast->redirs, token_put); */
+/* printf(" ]\n"); */
+/* ast_print(level + 1, ast->parent_ast); */
+/* } */
+/* else if (ast->tag == AST_CMD) */
+/* { */
+/* print_level(level); */
+/* printf("cmd: [ "); */
+/* ft_lstiter(ast->cmd_argv, token_put); */
+/* printf(" ] redirs: ["); */
+/* ft_lstiter(ast->redirs, token_put); */
+/* printf(" ]"); */
+/* } */
+/* else if (ast->tag == AST_OP) { */
+/* #<{(| printf("SEP: %d\n", ast->op.sep); |)}># */
+/* print_level(level); */
+/* #<{(| printf("redirs: ["); |)}># */
+/* #<{(| ft_lstiter(ast->redirs, token_put); |)}># */
+/* #<{(| printf(" ] "); |)}># */
+/* printf("{\n"); */
+/* */
+/* 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); */
+/* */
+/* printf("\n"); */
+/* print_level(level); */
+/* printf("}\n"); */
+/* } */
+/* } */
diff --git a/src/error.c b/src/error.c
index 0d2095e..e19a9ed 100644
--- a/src/error.c
+++ b/src/error.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
-/* Updated: 2020/07/19 19:20:38 by charles ### ########.fr */
+/* Updated: 2020/08/27 10:44:08 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,9 +22,16 @@
void errorf(const char *format, ...)
{
va_list ap;
- char *arg;
va_start(ap, format);
+ verrorf(format, ap);
+ va_end(ap);
+}
+
+void verrorf(const char *format, va_list ap)
+{
+ char *arg;
+
ft_putstr_fd(g_basename, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
while (*format != '\0')
@@ -41,5 +48,4 @@ void errorf(const char *format, ...)
format++;
}
}
- va_end(ap);
}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index f8884d6..b76666c 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/08/20 17:32:32 by charles ### ########.fr */
+/* Updated: 2020/08/27 17:19:14 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -92,7 +92,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_cmd param;
char **argv;
- char *id;
+ /* char *id; */
/* t_ftlst *tmp; */
if (!redir_extract(ast->redirs, env, fds))
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 8f3a266..3e67c74 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,17 +6,12 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/08/27 09:28:55 by charles ### ########.fr */
+/* Updated: 2020/08/27 17:06:45 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
-static enum e_tok st_lst_tag(t_ftlst *lst)
-{
- return (((t_token*)lst->data)->tag);
-}
-
static bool st_open_replace(int *fd, char *filename, int oflag)
{
if (*fd != MS_NO_FD)
@@ -35,24 +30,24 @@ static bool st_open_replace(int *fd, char *filename, int oflag)
}
bool redir_extract(
- t_ftlst *redirs,
+ t_tok_lst *redirs,
t_env env,
int fds[2])
{
- t_ftlst *after;
- t_ftlst *curr;
- char *filename;
+ t_tok_lst *after;
+ t_tok_lst *curr;
+ char *filename;
if (redirs == NULL)
return (true);
- if (!(st_lst_tag(redirs) & TAG_IS_REDIR) || redirs->next == NULL
- || !(st_lst_tag(redirs->next) & TAG_IS_STR))
+ if (!(redirs->tag & TAG_IS_REDIR) || redirs->next == NULL
+ || !(redirs->next->tag & TAG_IS_STR))
return (false);
curr = redirs->next;
after = NULL;
- while (curr != NULL && st_lst_tag(curr) & TAG_IS_STR)
+ while (curr != NULL && curr->tag & TAG_IS_STR)
{
- if (curr->next == NULL || st_lst_tag(curr->next) & TAG_IS_REDIR)
+ if (curr->next == NULL || curr->next->tag & TAG_IS_REDIR)
{
after = curr->next;
curr->next = NULL;
@@ -61,20 +56,22 @@ bool redir_extract(
}
if ((filename = preprocess_filename(&redirs->next, env)) == NULL)
{
- token_destroy_lst2(redirs, after);
+ ft_lstdestroy((t_ftlst**)&redirs, free);
+ ft_lstdestroy((t_ftlst**)&after, free);
return (false);
}
- if ((st_lst_tag(redirs) == TAG_REDIR_IN
+ if ((redirs->tag == TAG_REDIR_IN
&& !st_open_replace(&fds[FDS_READ], filename, O_RDONLY))
- || (st_lst_tag(redirs) == TAG_REDIR_OUT
+ || (redirs->tag == TAG_REDIR_OUT
&& !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC))
- || (st_lst_tag(redirs) == TAG_REDIR_APPEND
+ || (redirs->tag == TAG_REDIR_APPEND
&& !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND)))
{
- token_destroy_lst2(redirs, after);
+ ft_lstdestroy((t_ftlst**)&redirs, free);
+ ft_lstdestroy((t_ftlst**)&after, free);
return (false);
}
- token_destroy_lst(redirs);
+ ft_lstdestroy((t_ftlst**)&redirs, free);
free(filename);
return (redir_extract(after, env, fds));
}
diff --git a/src/main.c b/src/main.c
index 31c6370..b02c68b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/08/19 17:54:22 by charles ### ########.fr */
+/* Updated: 2020/08/27 18:41:53 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,16 +51,6 @@ char *g_basename = "minishell";
int main(int argc, char **argv, char **envp)
{
- /* char **strs = ms_split_notrim(ft_strdup(" bar "), ' '); */
- /* printf("p %p\n", strs); */
- /* */
- /* for (size_t i = 0; strs[i] != NULL; i++) */
- /* printf("|%s|\n", strs[i]); */
- /* return 0; */
-
-
-
-
t_path path;
t_env env;
@@ -97,20 +87,20 @@ int main(int argc, char **argv, char **envp)
if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
{
- t_ftlst *lex_out = lexer(ft_strdup(argv[2]));
+ t_tok_lst *lex_out = lexer(ft_strdup(argv[2]));
if (lex_out == NULL)
return (1);
- ft_lstiter(lex_out, token_debug);
+ /* ft_lstiter((t_ftlst*)lex_out, token_debug); */
}
else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
{
- t_ftlst *lex_out = lexer(ft_strdup(argv[2]));
+ t_tok_lst *lex_out = lexer(ft_strdup(argv[2]));
if (lex_out == NULL)
return (1);
/* ft_lstiter(lex_out, token_debug); */
- t_ret *parser_out = parse(lex_out);
+ t_parsed *parser_out = parse(lex_out);
if (parser_out == NULL || parser_out->syntax_error)
return (1);
@@ -137,11 +127,11 @@ int main(int argc, char **argv, char **envp)
print_prompt();
continue;
}
- t_ftlst *lex_out = lexer(line);
+ t_tok_lst *lex_out = lexer(line);
if (lex_out == NULL)
return (1);
- t_ret *parser_out = parse(lex_out);
+ t_parsed *parser_out = parse(lex_out);
if (parser_out == NULL)
return (1);
if (parser_out->syntax_error)
diff --git a/src/parse/cmd_parse.c b/src/parse/cmd_parse.c
deleted file mode 100644
index bb68dac..0000000
--- a/src/parse/cmd_parse.c
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
-#include "parser.h"
-
-int parse_cmd_str_true_false(enum e_tok tag)
-{
- if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE)
- return (1);
- return(0);
-}
-
-t_ast *push_cmd(t_ast *ast, t_ftlst *rest)
-{
- t_ftlst *new;
-
- if (ast == NULL)
- {
- ast = ast_new(AST_CMD);
- ast->cmd_argv = ft_lstnew((t_token *)rest->data);
- }
- else
- {
- new = ft_lstnew((t_token *)rest->data);
- ft_lstpush_back(&ast->cmd_argv, new);
- }
- return (ast);
-}
diff --git a/src/parse/parse.c b/src/parse/parse.c
index b1df5ce..df39cd0 100644
--- 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/07/20 10:26:14 by nahaddac ### ########.fr */
+/* Updated: 2020/08/27 11:02:33 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,21 +18,11 @@
#include "parser.h"
#include "lexer.h"
-t_ftlst *push_token(t_ftlst **tokens, t_token *pushed)
+t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
{
- t_ftlst *tmp;
+ t_parsed *ret;
- 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)
+ if ((ret = malloc(sizeof(t_parsed))) == NULL)
return (NULL);
ret->syntax_error = false;
ret->rest = rest;
@@ -40,61 +30,55 @@ t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest)
return ret;
}
-t_ret *parse_redir(t_ftlst *input, t_ftlst **redirs)
+t_parsed *parsed_error(const char *format, ...)
{
- enum e_tok tag;
- t_ret *tmp;
+ t_parsed *err;
+ va_list ap;
+
+ if ((err = parsed_new(NULL, NULL)) == NULL)
+ return (NULL);
+ err->syntax_error = true;
+ va_start(ap, format);
+ verrorf(format, ap);
+ va_end(ap);
+ return (err);
+}
- push_token(redirs, input->data);
+t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
+{
+ tok_lst_push_back(redirs, input);
input = input->next;
if (input == NULL)
- {
- errorf("syntax error near unexpected token `newline'\n", NULL);
- tmp = ret_wrap_ast(NULL, NULL);
- tmp->syntax_error = true;
- return tmp;
- }
- tag = ((t_token *)input->data)->tag;
+ return (parsed_error("syntax error near unexpected token `newline'\n"));
while(input != NULL
&& input->next != NULL
- && (((t_token*)input->next->data)->tag & TAG_IS_STR)
- && tag & TAG_IS_STR && tag & TAG_STICK)
+ && input->next->tag & TAG_IS_STR
+ && input->tag & TAG_IS_STR && input->tag & TAG_STICK)
{
- push_token(redirs, input->data);
+ tok_lst_push_back(redirs, input);
input = input->next;
- tag = ((t_token *)input->data)->tag;
- }
- if (!(tag & TAG_IS_STR))
- {
- errorf("syntax error near unexpected token `%s'\n", ((t_token *)input->data)->content);
- tmp = ret_wrap_ast(NULL, NULL);
- tmp->syntax_error = true;
- return tmp;
}
- push_token(redirs, input->data);
- return (ret_wrap_ast(NULL, input));
+ if (!(input->tag & TAG_IS_STR))
+ return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
+ tok_lst_push_back(redirs, input);
+ return (parsed_new(NULL, input));
}
-t_ret *parse_cmd(t_ftlst *input)
+t_parsed *parse_cmd(t_tok_lst *input)
{
enum e_tok tag;
t_ast *ast;
- t_ret *tmp;
+ t_parsed *tmp;
- tag = ((t_token *)input->data)->tag;
+ tag = input->tag;
if (tag & TAG_IS_SEP)
- {
- errorf("syntax error near unexpected token `%s'\n", ((t_token *)input->data)->content);
- tmp = ret_wrap_ast(NULL, NULL);
- tmp->syntax_error = true;
- return tmp;
- }
+ return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
ast = ast_new(AST_CMD);
while (input != NULL)
{
- tag = ((t_token *)input->data)->tag;
+ tag = input->tag;
if (tag & TAG_IS_STR)
- push_token(&ast->cmd_argv, input->data);
+ tok_lst_push_back(&ast->cmd_argv, input);
else if (tag & TAG_IS_REDIR)
{
tmp = parse_redir(input, &ast->redirs);
@@ -108,85 +92,60 @@ t_ret *parse_cmd(t_ftlst *input)
break;
input = input->next;
}
- return ret_wrap_ast(ast, input);
+ return parsed_new(ast, input);
}
// <cmd> ::= (<string> | <redir>)+
// <op> ::= <expr> <sep> <op> | <expr>
// <expr> ::= '(' <op> ')' | <cmd>
-t_ret *parse_op(t_ftlst *input)
+t_parsed *parse_op(t_tok_lst *input)
{
- t_ast *ast;
- t_ret *left_ret;
- t_ret *right_ret;
- enum e_tok tag;
- t_ret *tmp;
-
- left_ret = parse_expr(input);
- if (left_ret == NULL || left_ret->syntax_error)
- return left_ret;
- input = left_ret->rest;
- if (input == NULL || ((t_token*)input->data)->tag & TAG_PARENT_CLOSE)
- return ret_wrap_ast(left_ret->ast, input);
-
- tag = ((t_token*)input->data)->tag;
+ t_ast *ast;
+ t_parsed *left_parsed;
+ t_parsed *right_parsed;
+ enum e_tok tag;
+
+ left_parsed = parse_expr(input);
+ if (left_parsed == NULL || left_parsed->syntax_error)
+ return left_parsed;
+ input = left_parsed->rest;
+ if (input == NULL || input->tag & TAG_PARENT_CLOSE)
+ return parsed_new(left_parsed->ast, input);
+
+ tag = input->tag;
if (!(tag & TAG_IS_SEP))
- {
- errorf("syntax error near unexpected token `%s'\n", ((t_token *)input->data)->content);
- tmp = ret_wrap_ast(NULL, NULL);
- tmp->syntax_error = true;
- return tmp;
- }
+ return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
input = input->next;
if (input == NULL)
- {
- errorf("syntax error expected token\n");
- tmp = ret_wrap_ast(NULL, NULL);
- tmp->syntax_error = true;
- return tmp;
- }
- right_ret = parse_op(input);
- if (right_ret == NULL || right_ret->syntax_error)
- return right_ret;
+ return (parsed_error("syntax error expected token\n"));
+ right_parsed = parse_op(input);
+ if (right_parsed == NULL || right_parsed->syntax_error)
+ return right_parsed;
ast = ast_new(AST_OP);
- ast->op.left = left_ret->ast;
- ast->op.right = right_ret->ast;
+ ast->op.left = left_parsed->ast;
+ ast->op.right = right_parsed->ast;
ast->op.sep = tag;
- return ret_wrap_ast(ast, right_ret->rest);
+ return parsed_new(ast, right_parsed->rest);
}
-t_ret *parse_expr(t_ftlst *input)
+t_parsed *parse_expr(t_tok_lst *input)
{
- t_ret *tmp;
- enum e_tok tag;
- t_ast *new_ast;
+ t_parsed *tmp;
+ enum e_tok tag;
+ t_ast *new_ast;
- tag = ((t_token*)input->data)->tag;
+ tag = input->tag;
if (tag & TAG_PARENT_OPEN)
{
if (!(tmp = parse_op(input->next)) || tmp->syntax_error)
return tmp;
input = tmp->rest;
if (input == NULL)
- {
- errorf("syntax error expected token\n");
- tmp->syntax_error = true;
- return (tmp);
- }
- tag = ((t_token*)input->data)->tag;
+ return (parsed_error("syntax error expected token\n"));
+ tag = input->tag;
if (!(tag & TAG_PARENT_CLOSE))
- {
- errorf("syntax error expected token `)'\n");
- tmp->syntax_error = true;
- return (tmp);
- }
- // if (tag & TAG_IS_SEP)
- // {
- // errorf("syntax error expected token `)'\n");
- // tmp->syntax_error = true;
- // return (tmp);
- // }
+ return (parsed_error("syntax error expected token\n"));
input = input->next;
new_ast = ast_new(AST_PARENT);
new_ast->parent_ast = tmp->ast;
@@ -194,13 +153,13 @@ t_ret *parse_expr(t_ftlst *input)
if (input == NULL)
return tmp;
// could reuse parse_redir instead
- tag = ((t_token*)input->data)->tag;
+ tag = input->tag;
while (tag & TAG_IS_REDIR)
{
while(input != NULL)
{
- tag = ((t_token *)input->data)->tag;
- push_token(&tmp->ast->redirs, input->data);
+ tag = input->tag;
+ tok_lst_push_back(&tmp->ast->redirs, input);
if (tag & TAG_IS_STR && tag & TAG_STICK)
input = input->next;
else if (tag & TAG_IS_REDIR)
@@ -211,7 +170,7 @@ t_ret *parse_expr(t_ftlst *input)
input = input->next;
if (input == NULL)
break;
- tag = ((t_token*)input->data)->tag;
+ tag = input->tag;
}
tmp->rest = input;
return tmp;
@@ -219,20 +178,9 @@ t_ret *parse_expr(t_ftlst *input)
return parse_cmd(input);
}
-t_ret *parse(t_ftlst *input)
+t_parsed *parse(t_tok_lst *input)
{
- t_ret *ret;
-
if (input == NULL)
return NULL;
- if (!(ret = malloc(sizeof(t_ret) * 1)))
- return (NULL);
- ret->ast = NULL;
- ret->rest = NULL;
- ret->syntax_error = false;
- // ret = error_syntax_simple(input, ret);
- // if (ret->syntax_error == true)
- // return ret;
- ret = parse_op(input);
- return (ret);
+ return (parse_op(input));
}
diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c
deleted file mode 100644
index 84b06b2..0000000
--- a/src/parse/parse_error.c
+++ /dev/null
@@ -1,13 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* parse_error.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */
-/* Updated: 2020/07/20 10:46:26 by nahaddac ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "parser.h"
diff --git a/src/parse/redir_parse.c b/src/parse/redir_parse.c
deleted file mode 100644
index ebd6583..0000000
--- a/src/parse/redir_parse.c
+++ /dev/null
@@ -1,26 +0,0 @@
-
-#include "parser.h"
-
-int parse_redir_true_false(enum e_tok tag)
-{
- if (tag & TAG_IS_REDIR)
- return (1);
- return (0);
-}
-
-t_ast *push_redir(t_ast *ast, t_ftlst *rest)
-{
- t_ftlst *new;
-
- if (ast == NULL)
- {
- ast = ast_new(AST_CMD);
- ast->redirs = ft_lstnew((t_token *)rest->data);
- }
- else
- {
- new = ft_lstnew((t_token *)rest->data);
- ft_lstpush_back(&ast->redirs, new);
- }
- return (ast);
-}
diff --git a/src/preprocess.c b/src/preprocess.c
index f594969..2e477d0 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/08/20 17:28:28 by charles ### ########.fr */
+/* Updated: 2020/08/27 17:33:55 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,46 +14,46 @@
#include "lexer.h"
#include "eval.h"
-static t_ftlst *st_field_split(char *str)
+static t_tok_lst *st_field_split(char *str)
{
- t_ftlst *ret;
- char *match;
+ t_tok_lst *ret;
+ char *match;
ret = NULL;
while (*str != '\0')
{
if ((match = ft_strchr(str, ' ')) == NULL)
{
- if (ft_lstpush_front_node(&ret, token_new(TAG_STR, str)) == NULL)
- return (ft_lstdestroy(&ret, free));
+ if (tok_lst_push_front(&ret, tok_lst_new(TAG_STR, str)) == NULL)
+ return (tok_lst_destroy(&ret, free));
break;
}
- if (ft_lstpush_front_node(&ret, token_new_until(TAG_STR, str, match - str)) == NULL)
- return (ft_lstdestroy(&ret, free));
+ if (tok_lst_push_front(&ret, tok_lst_new_until(TAG_STR, str, match - str)) == NULL)
+ return (tok_lst_destroy(&ret, free));
while (*++match == ' ')
;
str = match;
- if (*str == '\0' && ft_lstpush_front_node(&ret, token_new(TAG_STR, str)) == NULL)
- return (ft_lstdestroy(&ret, free));
+ if (*str == '\0' && tok_lst_push_front(&ret, tok_lst_new(TAG_STR, str)) == NULL)
+ return (tok_lst_destroy(&ret, free));
}
- return (ft_lstreverse_ret(ret));
+ return ((t_tok_lst*)ft_lstreverse_ret((t_ftlst*)ret));
}
-t_ftlst *st_stick_tokens(t_ftlst *tokens)
+t_tok_lst *st_stick_tokens(t_tok_lst *tokens)
{
- t_ftlst *curr;
+ t_tok_lst *curr;
curr = tokens;
while (curr != NULL)
{
// curr->next shouldn't be null
- if (token_tag(curr) & TAG_STICK)
+ if (curr->tag & TAG_STICK)
{
- token_set_content(curr, ft_strjoinf_fst(token_content(curr), token_content(curr->next)));
- t_ftlst *tmp = curr->next->next;
- token_set_tag(curr, token_tag(curr->next));
- ft_lstdelone(curr->next, free);
+ curr->content = ft_strjoinf_fst(curr->content, curr->next->content);
+ t_tok_lst *tmp = curr->next->next;
+ curr->tag = curr->next->tag;
+ ft_lstdelone((t_ftlst*)curr->next, free);
curr->next = tmp;
continue;
}
@@ -62,44 +62,44 @@ t_ftlst *st_stick_tokens(t_ftlst *tokens)
return (tokens);
}
-char **st_tokens_to_argv(t_ftlst *tokens)
+char **st_tokens_to_argv(t_tok_lst *tokens)
{
char **ret;
size_t i;
- if ((ret = ft_calloc(ft_lstsize(tokens) + 1, sizeof(char*))) == NULL)
+ if ((ret = ft_calloc(ft_lstsize((t_ftlst*)tokens) + 1, sizeof(char*))) == NULL)
return (NULL);
i = 0;
while (tokens != NULL)
{
- ret[i++] = token_content(tokens);
+ ret[i++] = tokens->content;
tokens = tokens->next;
}
- ft_lstdestroy(&tokens, free);
+ tok_lst_destroy(&tokens, free);
return (ret);
}
-char **preprocess(t_ftlst **tokens, t_env env)
+char **preprocess(t_tok_lst **tokens, t_env env)
{
- t_ftlst *curr;
+ t_tok_lst *curr;
enum e_tok prev_tag;
prev_tag = 0;
curr = *tokens;
while (curr != NULL)
{
- if (token_tag(curr) & (TAG_STR | TAG_STR_DOUBLE))
+ if (curr->tag & (TAG_STR | TAG_STR_DOUBLE))
{
- char *str = token_content(curr);
+ char *str = curr->content;
size_t i = -1;
while (str[++i] != '\0')
{
- str = token_content(curr);
+ str = curr->content;
// escape
if (str[i] == '\\'
- && (token_tag(curr)& TAG_STR
- || ((token_tag(curr) & TAG_STR_DOUBLE) && ft_strchr("\\\"$", str[i + 1]))))
+ && (curr->tag & TAG_STR
+ || ((curr->tag & TAG_STR_DOUBLE) && ft_strchr("\\\"$", str[i + 1]))))
{
ft_memmove(&str[i], &str[i + 1], ft_strlen(&str[i + 1]) + 1);
continue;
@@ -125,48 +125,49 @@ char **preprocess(t_ftlst **tokens, t_env env)
str[i] = '\0';
before = str;
after = &str[i + var_len];
- if (token_tag(curr) & TAG_STR)
+ if (curr->tag & TAG_STR)
{
- t_ftlst *fields = st_field_split(match);
+ t_tok_lst *fields = st_field_split(match);
- len = ft_strlen(token_content(ft_lstlast(fields)));
+ len = ft_strlen(tok_lst_last(fields)->content);
- if (!(prev_tag & TAG_STICK) && *before == '\0' && *token_content(fields) == '\0')
- ft_lstpop_front(&fields, (void (*)(void*))token_destroy);
- if (!(token_tag(curr) & TAG_STICK) && *after == '\0' && *token_content(ft_lstlast(fields)) == '\0')
- ft_lstpop_back(&fields, (void (*)(void*))token_destroy);
+ if (!(prev_tag & TAG_STICK) && *before == '\0' && *fields->content == '\0')
+ ft_lstpop_front((t_ftlst**)&fields, free);
+ if (!(curr->tag & TAG_STICK) && *after == '\0'
+ && *tok_lst_last(fields)->content == '\0')
+ ft_lstpop_back((t_ftlst**)&fields, free);
if (fields == NULL)
// delete curr?
;
else if (fields->next == NULL)
{
- token_set_content(curr, ft_strjoin3(before, token_content(fields), after));
+ curr->content = ft_strjoin3(before, fields->content, after);
i += len - 1;
}
else
{
- token_set_content(curr, ft_strjoin(before, token_content(fields)));
- token_set_content(ft_lstlast(fields),
- ft_strjoin(token_content(ft_lstlast(fields)), after));
+ curr->content = ft_strjoin(before, fields->content);
+ tok_lst_last(fields)->content =
+ ft_strjoin(tok_lst_last(fields)->content, after);
- t_ftlst *tmp = curr->next;
+ t_tok_lst *tmp = curr->next;
curr->next = fields->next;
- curr = ft_lstlast(fields);
+ curr = tok_lst_last(fields);
curr->next = tmp;
i = len - 1;
- str = token_content(curr);
+ str = curr->content;
}
}
- else if (token_tag(curr) & TAG_STR_DOUBLE)
+ else if (curr->tag & TAG_STR_DOUBLE)
{
- token_set_content(curr, ft_strjoin3(before, match, after));
+ curr->content = ft_strjoin3(before, match, after);
i += ft_strlen(match) - 1;
}
}
}
}
- prev_tag = token_tag(curr);
+ prev_tag = curr->tag;
curr = curr->next;
}
@@ -175,7 +176,7 @@ char **preprocess(t_ftlst **tokens, t_env env)
}
// need to free tokens
-char *preprocess_filename(t_ftlst **tokens, t_env env)
+char *preprocess_filename(t_tok_lst **tokens, t_env env)
{
char **strs;
char *ret;