42-archive/42sh/src/parser/ft_parse.c
Antoine Riard 4ba4d19dc8 inchallah
2017-03-29 14:33:23 +02:00

60 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:17:38 by ariard #+# #+# */
/* Updated: 2017/03/29 14:27:02 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void insert_linebreak(t_list **lst)
{
t_token *token;
token = (*lst)->content;
token->type = LINEBREAK;
}
static int end_instruction(t_list **stack)
{
t_sym *head;
head = (*stack)->content;
if (*head == CMD_SUPERIOR || *head == PIPE_SEMI_SEQUENCE
|| *head == COMPLETE_COMMANDS || *head == END_COMMAND)
return (1);
return (0);
}
int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
{
t_sym *head;
if (pop_heredoc(token))
return (0);
while (parser && token && *token)
{
produce_sym(&parser->stack, parser->new_sym, token);
DG("produce sym %s", read_state(*parser->new_sym));
if (parser->new_sym && eval_sym(&parser->stack, *parser->new_sym))
return ((parser->state = ERROR));
if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state))
return (0);
push_stack(&parser->stack, *parser->new_sym);
if (*(head = (parser->stack)->content) == PROGRAM)
parser->state = SUCCESS;
else
parser->state = UNDEFINED;
build_tree(ast, token);
if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token);
else
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
}
return (0);
}