main good

This commit is contained in:
Antoine Riard 2017-03-16 22:30:02 +01:00
parent 3da58bcb52
commit 64d5500c8d
29 changed files with 221 additions and 135 deletions

View file

@ -220,6 +220,7 @@ lexer/lexer_bquote.c\
lexer/lexer_curly_braces.c\ lexer/lexer_curly_braces.c\
lexer/lexer_default.c\ lexer/lexer_default.c\
lexer/lexer_delim.c\ lexer/lexer_delim.c\
lexer/lexer_destroy.c\
lexer/lexer_dquote.c\ lexer/lexer_dquote.c\
lexer/lexer_end.c\ lexer/lexer_end.c\
lexer/lexer_great.c\ lexer/lexer_great.c\
@ -287,11 +288,13 @@ parser/error_syntax.c\
parser/eval_sym.c\ parser/eval_sym.c\
parser/ft_parse.c\ parser/ft_parse.c\
parser/heredoc_parser.c\ parser/heredoc_parser.c\
parser/parser_destroy.c\
parser/parser_init.c\ parser/parser_init.c\
parser/pop_stack.c\ parser/pop_stack.c\
parser/produce_sym.c\ parser/produce_sym.c\
parser/push_stack.c\ parser/push_stack.c\
parser/read_stack.c\ parser/read_stack.c\
parser/stack_init.c\
parser/tree_wrapper.c parser/tree_wrapper.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/08 23:20:20 by ariard ### ########.fr */ /* Updated: 2017/03/16 20:37:26 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -101,7 +101,7 @@ int get_reserved_words(t_list **alst);
int insert_newline(t_list **alst); int insert_newline(t_list **alst);
void lexer_init(t_lexer *lexer); void lexer_init(t_lexer *lexer);
void lexer_destroy(t_lexer *lexer);
int lexer_lex(t_list **alst, t_lexer *lexer); int lexer_lex(t_list **alst, t_lexer *lexer);
int lexer_default(t_list **alst, t_lexer *lexer); int lexer_default(t_list **alst, t_lexer *lexer);
int lexer_newline(t_list **alst, t_lexer *lexer); int lexer_newline(t_list **alst, t_lexer *lexer);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/03/16 15:41:33 by jhalford ### ########.fr */ /* Updated: 2017/03/16 22:18:45 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,18 +34,21 @@
struct s_data struct s_data
{ {
t_flag opts; t_flag opts;
int fd; int fd;
char **env; char **env;
char **local_var; int argc;
int argc; char **argv;
char **argv; t_line line;
t_line line; t_list *token;
t_comp *comp; t_btree *ast;
t_exec exec; t_lexer lexer;
t_jobc jobc; t_parser parser;
t_list *heredoc_queue; t_comp *comp;
t_list *lst_func; t_exec exec;
t_jobc jobc;
char **local_var;
t_list *lst_func;
}; };
int shell_init(int ac, char **av); int shell_init(int ac, char **av);

View file

@ -30,6 +30,7 @@ struct s_parser
t_parstate state; t_parstate state;
t_list *stack; t_list *stack;
t_sym *new_sym; t_sym *new_sym;
t_list *heredoc_queue;
}; };
struct s_aggrematch struct s_aggrematch
@ -64,6 +65,9 @@ struct s_errormatch
/* extern t_stackmatch g_stackmatch[]; */ /* extern t_stackmatch g_stackmatch[]; */
void parser_init(t_parser *parser); void parser_init(t_parser *parser);
void parser_destroy(t_parser *parser);
int stack_init(t_parser *parser);
int ft_parse(t_btree **ast, t_list **token, t_parser *parser); int ft_parse(t_btree **ast, t_list **token, t_parser *parser);
int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst); int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst);
@ -75,7 +79,7 @@ int pop_stack(t_list **stack, t_sym erase_sym);
int pop_heredoc(t_list **lst); int pop_heredoc(t_list **lst);
int error_syntax(t_list **token, t_parser *parser, t_btree **ast); int error_syntax(t_list **token);
int error_eof(void); int error_eof(void);
int ft_read_stack(t_sym *stack); int ft_read_stack(t_sym *stack);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */
/* Updated: 2017/03/14 22:53:17 by ariard ### ########.fr */ /* Updated: 2017/03/16 18:17:16 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,6 +36,8 @@ typedef struct s_line t_line;
typedef struct s_comp t_comp; typedef struct s_comp t_comp;
typedef struct s_exec t_exec; typedef struct s_exec t_exec;
typedef struct s_jobc t_jobc; typedef struct s_jobc t_jobc;
typedef struct s_lexer t_lexer;
typedef struct s_parser t_parser;
typedef enum e_mode t_mode; typedef enum e_mode t_mode;
typedef struct s_data_template t_btexport; typedef struct s_data_template t_btexport;
@ -43,7 +45,6 @@ typedef struct s_data_template t_btexport;
** Lexer types ** Lexer types
*/ */
typedef struct s_lexer t_lexer;
typedef enum e_lexstate t_lexstate; typedef enum e_lexstate t_lexstate;
typedef struct s_token t_token; typedef struct s_token t_token;
typedef struct s_rvwords t_rvwords; typedef struct s_rvwords t_rvwords;
@ -77,7 +78,6 @@ typedef struct s_redir t_redir;
typedef struct s_cmd t_cmd; typedef struct s_cmd t_cmd;
typedef union u_astdata t_astdata; typedef union u_astdata t_astdata;
typedef union u_word t_word; typedef union u_word t_word;
typedef struct s_parser t_parser;
typedef int t_sym; typedef int t_sym;
typedef enum e_parstate t_parstate; typedef enum e_parstate t_parstate;
typedef struct s_aggrematch t_aggrematch; typedef struct s_aggrematch t_aggrematch;

@ -1 +1 @@
Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf Subproject commit 89d3f6e67e707e42fca42cc89e3f39e5f840908a

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ /* Created: 2017/03/07 10:58:49 by ariard #+# #+# */
/* Updated: 2017/03/16 14:24:12 by jhalford ### ########.fr */ /* Updated: 2017/03/16 17:44:15 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */
/* Updated: 2017/03/13 19:03:43 by jhalford ### ########.fr */ /* Updated: 2017/03/16 19:00:30 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:37:28 by jhalford #+# #+# */ /* Created: 2017/02/09 20:37:28 by jhalford #+# #+# */
/* Updated: 2017/03/10 13:12:51 by jhalford ### ########.fr */ /* Updated: 2017/03/16 19:00:44 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */
/* Updated: 2017/03/05 16:20:18 by jhalford ### ########.fr */ /* Updated: 2017/03/16 19:05:25 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@ int lexer_default(t_list **alst, t_lexer *lexer)
t_token *token; t_token *token;
char c; char c;
c = lexer->str[lexer->pos]; c = lexer->str[lexer->pos];
if ((lexer->state = get_state_global(lexer))) if ((lexer->state = get_state_global(lexer)))
return (lexer_lex(alst, lexer)); return (lexer_lex(alst, lexer));
if ((lexer->state = get_state_redir(lexer))) if ((lexer->state = get_state_redir(lexer)))

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 20:25:08 by ariard #+# #+# */
/* Updated: 2017/03/16 20:43:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void lexer_destroy(t_lexer *lexer)
{
ft_strdel(&lexer->str);
lexer->pos = 0;
lexer->state = 0;
lexer->stack = NULL;
lexer->heredoc_stack = NULL;
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 16:14:08 by ariard #+# #+# */ /* Created: 2017/02/21 16:14:08 by ariard #+# #+# */
/* Updated: 2017/03/04 16:32:15 by ariard ### ########.fr */ /* Updated: 2017/03/16 17:57:47 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:45:57 by jhalford ### ########.fr */ /* Updated: 2017/03/16 19:04:55 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */
/* Updated: 2017/03/09 18:48:00 by ariard ### ########.fr */ /* Updated: 2017/03/16 18:21:30 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lexer.h" #include "minishell.h"
int lexer_sep(t_list **alst, t_lexer *lexer) int lexer_sep(t_list **alst, t_lexer *lexer)
{ {

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
/* Updated: 2017/03/08 12:14:32 by jhalford ### ########.fr */ /* Updated: 2017/03/16 19:10:40 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,7 @@ void token_print(t_list *lst)
while (lst) while (lst)
{ {
DG("in token print");
if (lst->content) if (lst->content)
token = lst->content; token = lst->content;
if (token->type) if (token->type)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/16 15:22:11 by jhalford ### ########.fr */ /* Updated: 2017/03/16 18:40:39 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/15 16:23:18 by wescande ### ########.fr */ /* Updated: 2017/03/16 22:15:29 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,7 +34,10 @@ int data_init(int ac, char **av)
data->opts = 0; data->opts = 0;
exec_reset(); exec_reset();
data->lst_func = NULL; data->lst_func = NULL;
data->heredoc_queue = NULL; data->token = NULL;
data->ast = NULL;
lexer_init(&data->lexer);
parser_init(&data->parser);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL) if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
return (-1); return (-1);
if (tgetent(NULL, term_name) != 1) if (tgetent(NULL, term_name) != 1)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */ /* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */
/* Updated: 2017/03/16 14:13:33 by jhalford ### ########.fr */ /* Updated: 2017/03/16 18:27:57 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ int instruction_free(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *pa
token = NULL; token = NULL;
ft_lstdel(&parser->stack, &ft_lst_cfree); ft_lstdel(&parser->stack, &ft_lst_cfree);
btree_del(ast, &ft_lst_cfree); btree_del(ast, &ft_lst_cfree);
ft_lstdel(&data_singleton()->heredoc_queue, &redir_free); ft_lstdel(&data_singleton()->parser.heredoc_queue, &redir_free);
ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree); ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree);
ft_strdel(&lexer->str); ft_strdel(&lexer->str);
free(parser->new_sym); free(parser->new_sym);

View file

@ -6,113 +6,123 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/16 15:45:57 by jhalford ### ########.fr */ /* Updated: 2017/03/16 22:26:56 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int do_lexer_routine(t_list **token, t_lexer *lexer, t_parser *parser, char *str) static int do_readline_routine(char **stream)
{
t_list *ltoken;
if (lexer->state == HEREDOC || parser->state == UNDEFINED)
{
ft_strappend(&lexer->str, (char[]){'\n', 0});
lexer->pos++;
}
ft_strappend(&lexer->str, str);
if (get_lexer_stack(*lexer) == BACKSLASH)
pop(&lexer->stack);
ltoken = ft_lstlast(*token);
if (lexer_lex(token ? &ltoken : token, lexer) < 1)
exit(1);
if (get_lexer_stack(*lexer) > 2)
return (1);
lexer->state = DEFAULT;
return (0);
}
static int do_parser_routine(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser)
{
if (get_reserved_words(token))
return (1);
token_print(*token);
if (insert_newline(token))
return (1);
ft_parse(ast, token, parser);
if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT))
return (0);
if (parser->state == ERROR)
{
error_syntax(token, parser, ast);
return (1);
}
else if (parser->state == SUCCESS)
return (1);
return (0);
}
static int do_readline_routine(t_lexer *lexer, t_parser *parser, char **str)
{ {
int ret; int ret;
int has_prompt; int has_prompt;
t_data *data;
has_prompt = !(get_lexer_stack(*lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC); data = data_singleton();
ret = readline(has_prompt, str); has_prompt = !(get_lexer_stack(data->lexer)
|| data->parser.state == UNDEFINED || data->lexer.state == HEREDOC);
ret = readline(has_prompt, stream);
if (ret == -1) if (ret == -1)
exit(1); exit(1);
if (ret == 1 && parser->state == UNDEFINED) if (ret == 1 && data->parser.state == UNDEFINED)
error_eof(); error_eof();
return (ret); return (ret);
} }
static int handle_instruction(t_lexer *lexer, t_parser *parser) static int do_lexer_routine(char *stream)
{ {
t_list *token; t_list *ltoken;
t_btree *ast; t_data *data;
int ret;
char *str;
token = NULL; data = data_singleton();
ast = NULL; if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED)
str = NULL;
while (1)
{ {
if ((ret = do_readline_routine(lexer, parser, &str)) > 0) ft_strappend(&data->lexer.str, (char[]){'\n', 0});
return (ret); data->lexer.pos++;
if (do_lexer_routine(&token, lexer, parser, str) > 0)
continue ;
if (do_parser_routine(&token, &ast, lexer, parser) > 0)
break ;
} }
/* btree_print(STDBUG, ast, &ft_putast); */ ft_strappend(&data->lexer.str, stream);
if (parser->state == SUCCESS && ft_exec(&ast) < 0) if (get_lexer_stack(data->lexer) == BACKSLASH)
pop(&data->lexer.stack);
ltoken = ft_lstlast(data->token);
if (lexer_lex(data->token ? &ltoken : &data->token, &data->lexer) < 0)
exit(1); exit(1);
if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str) if (get_lexer_stack(data->lexer) > 2)
ft_add_str_in_history(lexer->str); return (1);
data->lexer.state = DEFAULT;
return (0); return (0);
} }
static int do_parser_routine(void)
{
t_data *data;
data = data_singleton();
if (get_reserved_words(&data->token))
return (1);
if (insert_newline(&data->token))
return (2);
if (data->parser.state == SUCCESS && stack_init(&data->parser))
exit(1);
if (ft_parse(&data->ast, &data->token, &data->parser))
exit(1);
if ((data->lexer.state = data->parser.heredoc_queue ? HEREDOC : DEFAULT))
return (0);
if (data->parser.state == ERROR)
{
error_syntax(&data->token);
data->parser.state = SUCCESS;
return (1);
}
else if (data->parser.state == SUCCESS)
return (1);
return (0);
}
static int handle_instruction()
{
int ret;
char *stream;
t_data *data;
stream = NULL;
data = data_singleton();
while (1)
{
if ((ret = do_readline_routine(&stream)) > 0)
return (ret);
if (do_lexer_routine(stream) > 0)
continue ;
if (do_parser_routine() > 0)
break ;
}
/* btree_print(STDBUG, ast, &ft_putast); */
if (data->parser.state == SUCCESS && ft_exec(&data->ast) < 0)
exit(1);
DG("stream [%s]", data->lexer.str);
if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str)
ft_add_str_in_history(data->lexer.str);
return (0);
}
int main(int ac, char **av) int main(int ac, char **av)
{ {
int ret; int ret;
t_lexer lexer; t_data *data;
t_parser parser;
g_argv = av; g_argv = av;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
DG("{inv}{bol}{gre}start of shell{eoc}"); DG("{inv}{bol}{gre}start of shell{eoc}");
if (shell_init(ac, av)) if (shell_init(ac, av))
return (1); return (1);
lexer_init(&lexer);
parser_init(&parser);
DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF");
data = data_singleton();
while (1) while (1)
{ {
ret = handle_instruction(&lexer, &parser); ret = handle_instruction();
/* instruction_free(&token, &ast, &lexer, &parser); */ lexer_destroy(&data->lexer);
parser_destroy(&data->parser);
ft_lstdel(&data->token, &ft_lst_cfree);
btree_del(&data->ast, &ft_lst_cfree);
if (ret == 1) if (ret == 1)
break ; break ;
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 17:28:31 by ariard #+# #+# */ /* Created: 2017/03/05 17:28:31 by ariard #+# #+# */
/* Updated: 2017/03/13 16:58:33 by ariard ### ########.fr */ /* Updated: 2017/03/16 17:56:16 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/03/15 02:09:54 by ariard ### ########.fr */ /* Updated: 2017/03/16 18:34:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -87,7 +87,7 @@ static int add_redir_type_number(t_btree **ast, t_list **lst)
t_list **queue; t_list **queue;
t_list *redir_lst; t_list *redir_lst;
queue = &data_singleton()->heredoc_queue; queue = &data_singleton()->parser.heredoc_queue;
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
redir_lst = ft_lstlast(node->data.cmd.redir); redir_lst = ft_lstlast(node->data.cmd.redir);
@ -116,7 +116,7 @@ int add_redir_type(t_btree **ast, t_list **lst)
t_list *redir_lst; t_list *redir_lst;
t_list **queue; t_list **queue;
queue = &data_singleton()->heredoc_queue; queue = &data_singleton()->parser.heredoc_queue;
if (!*ast) if (!*ast)
gen_node(ast); gen_node(ast);
token = (*lst)->content; token = (*lst)->content;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
/* Updated: 2017/03/16 15:34:39 by jhalford ### ########.fr */ /* Updated: 2017/03/16 17:49:23 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -51,14 +51,11 @@ static t_errormatch g_errormatch[] =
{0, NULL}, {0, NULL},
}; };
int error_syntax(t_list **lst, t_parser *parser, int error_syntax(t_list **lst)
t_btree **ast)
{ {
t_token *token; t_token *token;
int i; int i;
(void)parser;
(void)ast;
if (!*lst) if (!*lst)
return (0); return (0);
token = (*lst)->content; token = (*lst)->content;
@ -67,18 +64,18 @@ int error_syntax(t_list **lst, t_parser *parser,
{ {
if (g_errormatch[i].token == token->type) if (g_errormatch[i].token == token->type)
{ {
ft_dprintf(2, "syntax error near unexpected token « %s »\n", ft_dprintf(2, "{red}syntax error near unexpected token « %s »{eoc}\n",
g_errormatch[i].error); g_errormatch[i].error);
return (0); return (0);
} }
i++; i++;
} }
ft_dprintf(2, "syntax error near unexpected token « %s »\n", token->data); ft_dprintf(2, "{red}syntax error near unexpected token « %s »\n{eoc}", token->data);
return (0); return (0);
} }
int error_eof(void) int error_eof(void)
{ {
ft_putstr_fd("syntax error near unexpected EOF", 2); ft_dprintf(2, "{red}syntax error near unexpected EOF{eoc}");
return (1); return (1);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */
/* Updated: 2017/03/14 22:01:19 by ariard ### ########.fr */ /* Updated: 2017/03/16 21:24:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -1243,7 +1243,7 @@ int eval_sym(t_list **stack, t_sym new_sym)
if (!*stack) if (!*stack)
return (1); return (1);
head = (*stack)->content; head = (*stack)->content;
// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0; i = 0;
while (g_stackmatch[i].top) while (g_stackmatch[i].top)
{ {

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:17:38 by ariard #+# #+# */ /* Created: 2017/03/11 16:17:38 by ariard #+# #+# */
/* Updated: 2017/03/16 14:32:34 by jhalford ### ########.fr */ /* Updated: 2017/03/16 22:11:41 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,13 +35,12 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
{ {
t_sym *head; t_sym *head;
(void)ast;
if (pop_heredoc(token)) if (pop_heredoc(token))
return (0); return (0);
while (*token) while (*token)
{ {
produce_sym(&parser->stack, parser->new_sym, 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)) if (eval_sym(&parser->stack, *parser->new_sym))
return ((parser->state = ERROR)); return ((parser->state = ERROR));
else else
@ -62,7 +61,5 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
else else
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
} }
if (parser->state == SUCCESS)
DG("sucessful parsing");
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */
/* Updated: 2017/03/14 15:02:34 by gwojda ### ########.fr */ /* Updated: 2017/03/16 17:51:54 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,9 +20,9 @@ int pop_heredoc(t_list **lst)
char *tmp; char *tmp;
token = (*lst)->content; token = (*lst)->content;
if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) if (token->type == HEREDOCDATA && data_singleton()->parser.heredoc_queue != NULL)
{ {
head = &data_singleton()->heredoc_queue; head = &data_singleton()->parser.heredoc_queue;
redir = *(t_redir**)(*head)->content; redir = *(t_redir**)(*head)->content;
if (head && token) if (head && token)
{ {

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 19:30:17 by ariard #+# #+# */
/* Updated: 2017/03/16 22:13:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void parser_destroy(t_parser *parser)
{
parser->state = SUCCESS;
ft_lstdel(&parser->stack, &ft_lst_cfree);
ft_lstdel(&data_singleton()->parser.heredoc_queue, &ft_lst_cfree);
free(parser->new_sym);
parser->new_sym = NULL;
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */
/* Updated: 2017/03/11 16:20:31 by ariard ### ########.fr */ /* Updated: 2017/03/16 22:13:39 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,7 @@
void parser_init(t_parser *parser) void parser_init(t_parser *parser)
{ {
parser->state = SUCCESS; parser->state = SUCCESS;
parser->new_sym = ft_memalloc(sizeof(t_sym)); parser->new_sym = NULL;
parser->stack = NULL; parser->stack = NULL;
push_stack(&parser->stack, TERMINUS); parser->heredoc_queue = NULL;
push_stack(&parser->stack, LINEBREAK);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/03/14 22:03:42 by ariard ### ########.fr */ /* Updated: 2017/03/16 21:31:55 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -127,12 +127,14 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
t_sym *head; t_sym *head;
int i; int i;
if (!*stack)
DG("stack absente");
if (!*stack || !*lst) if (!*stack || !*lst)
return (1); return (1);
token = (*lst)->content; token = (*lst)->content;
head = (*stack)->content; head = (*stack)->content;
// DG("produce stack : %s && token : %s", read_state(*head), DG("produce stack : %s && token : %s", read_state(*head),
// read_state(token->type)); read_state(token->type));
i = 0; i = 0;
*new_sym = 0; *new_sym = 0;
while (g_prodmatch[i].new_sym) while (g_prodmatch[i].new_sym)

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 20:31:32 by ariard #+# #+# */
/* Updated: 2017/03/16 22:11:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int stack_init(t_parser *parser)
{
ft_lstdel(&parser->stack, NULL);
push_stack(&parser->stack, TERMINUS);
push_stack(&parser->stack, LINEBREAK);
if (!parser->new_sym && !(parser->new_sym = ft_memalloc(sizeof(t_sym))))
return (-1);
return (0);
}