stack is now a beautiful linked list

This commit is contained in:
AntoHesse 2017-03-04 02:17:06 +01:00
parent af311380f4
commit dc423db858
10 changed files with 70 additions and 33 deletions

View file

@ -33,7 +33,7 @@ enum e_parstate
struct s_parser
{
t_parstate state;
t_sym *stack;
t_list *stack;
t_sym *new_sym;
};
@ -75,13 +75,20 @@ extern t_errormatch g_errormatch[];
void parser_init(t_parser *parser);
int ft_parse(t_btree **ast, t_list **token, t_parser *parser);
int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst);
int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst);
int eval_sym(t_list **stack, t_sym new_sym);
int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state);
int push_stack(t_list **stack, t_sym new_sym);
int pop_stack(t_list **stack, t_sym erase_sym);
/*int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst);
int eval_sym(t_sym stack, t_sym new_sym);
int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state);
int push_stack(t_sym *stack, t_sym new_sym);
int pop_stack(t_sym **stack, t_sym erase_sym);
*/
int error_syntax(t_list **token);
int error_EOF(void);

View file

@ -162,6 +162,7 @@ enum e_sym
REDIR,
CMD,
ALL = 200,
TERMINUS = 300,
};
#endif

0
42sh/parser_init.c Normal file
View file

View file

@ -308,17 +308,19 @@ t_aggrematch g_aggrematch[] =
{0, 0, 0, 0},
};
int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state)
{
t_sym *head;
int i;
i = 0;
head = (*stack)->content;
DG("aggregate head %s && sym %s",
read_state(**stack), read_state(*new_sym));
read_state(*head), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under))
&& MATCH_STACK(*head, g_aggrematch[i].under))
{
DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
@ -326,9 +328,10 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
DG("stack after pop: %s", read_state(**stack));
head = (*stack)->content;
DG("stack after pop: %s", read_state(*head));
}
if (eval_sym(**stack, *new_sym))
if (eval_sym(stack, *new_sym))
return ((*state = ERROR));
aggregate_sym(stack, new_sym, state);
return (0);

View file

@ -1020,15 +1020,17 @@ t_stackmatch g_stackmatch[] =
{0, 0},
};
int eval_sym(t_sym stack, t_sym new_sym)
int eval_sym(t_list **stack, t_sym new_sym)
{
int i;
t_sym *head;
int i;
DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
head = (*stack)->content;
DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{
if (new_sym == g_stackmatch[i].top && stack == g_stackmatch[i].under)
if (new_sym == g_stackmatch[i].top && *head == g_stackmatch[i].under)
return (0);
i++;
}

View file

@ -19,37 +19,41 @@ static void insert_linebreak(t_list **lst)
token->type = LINEBREAK;
}
static int end_instruction(t_sym sym)
static int end_instruction(t_list **stack)
{
if (sym == CMD_SUPERIOR || sym == PIPE_SEMI_SEQUENCE
|| sym == COMPLETE_COMMANDS || sym == END_COMMAND)
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;
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("number of token to treat: %d", ft_lstsize(*token));
if (eval_sym(*parser->stack, *parser->new_sym))
if (eval_sym(&parser->stack, *parser->new_sym))
return ((parser->state = ERROR));
else
{
aggregate_sym(&parser->stack, parser->new_sym, &parser->state);
push_stack(++parser->stack, *parser->new_sym);
push_stack(&parser->stack, *parser->new_sym);
}
// ft_read_stack(parser->stack);
DG("\n");
if (*parser->stack == PROGRAM)
if (*(head = (parser->stack)->content) == PROGRAM)
parser->state = SUCCESS;
else
parser->state = UNDEFINED;
build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*parser->stack) && !(*token)->next))
if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token);
else
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);

View file

@ -16,6 +16,8 @@ void parser_init(t_parser *parser)
{
parser->state = SUCCESS;
parser->new_sym = ft_memalloc(sizeof(t_sym));
parser->stack = ft_memalloc(sizeof(t_sym) * 1000);
push_stack(parser->stack, LINEBREAK);
push_stack(&parser->stack, TERMINUS);
push_stack(&parser->stack, LINEBREAK);
// parser->stack = ft_memalloc(sizeof(t_sym) * 1000);
// push_stack(parser->stack, LINEBREAK);
}

View file

@ -12,15 +12,29 @@
#include "parser.h"
int pop_stack(t_sym **stack, t_sym erase_sym)
int pop_stack(t_list **stack, t_sym erase_sym)
{
t_sym *temp;
t_sym *head;
t_list *temp;
// DG("pop until :%s", read_state(erase_sym));
DG("pop until :%s", read_state(erase_sym));
head = (*stack)->content;
while ((*stack) && *head != erase_sym)
{
temp = *stack;
(*stack) = (*stack)->next;
ft_lstdelone(&temp, NULL);
head = (*stack)->content;
}
temp = *stack;
(*stack) = (*stack)->next;
ft_lstdelone(&temp, NULL);
/*
DG("pop until :%s", read_state(erase_sym));
temp = *stack;
while (*temp != erase_sym)
*temp-- = 0;
*temp-- = 0;
*stack = temp;
return (0);
*/ return (0);
}

View file

@ -99,20 +99,22 @@ t_prodmatch g_prodmatch[] =
{0, 0, 0},
};
int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst)
int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
{
t_token *token;
int i;
t_sym *head;
int i;
token = (*lst)->content;
DG("produce stack : %s && token : %s", read_state(stack),
head = (*stack)->content;
DG("produce stack : %s && token : %s", read_state(*head),
read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)
{
if (token->type == g_prodmatch[i].token
&& stack == g_prodmatch[i].stack)
&& *head == g_prodmatch[i].stack)
{
DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
*new_sym = g_prodmatch[i].new_sym;

View file

@ -12,8 +12,10 @@
#include "parser.h"
int push_stack(t_sym *stack, t_sym new_sym)
//int push_stack(t_sym *stack, t_sym new_sym)
int push_stack(t_list **stack, t_sym sym)
{
*stack = new_sym;
ft_lstadd(stack, ft_lstnew(&sym, sizeof(sym)));
// *stack = sym;
return (0);
}