diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index c5257a09..49220b89 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -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); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 202fd14e..d107d0b4 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -162,6 +162,7 @@ enum e_sym REDIR, CMD, ALL = 200, + TERMINUS = 300, }; #endif diff --git a/42sh/parser_init.c b/42sh/parser_init.c new file mode 100644 index 00000000..e69de29b diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 764c1bd5..5bb0d31a 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -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); diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 38303faf..81a62e54 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -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++; } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 8672cfc8..2aae5a10 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -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); diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 083e3fd3..9d1f40dc 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -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); } diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c index 07f967bd..eea77345 100644 --- a/42sh/src/parser/pop_stack.c +++ b/42sh/src/parser/pop_stack.c @@ -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); } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index ed3e0fca..717c15ca 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -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; diff --git a/42sh/src/parser/push_stack.c b/42sh/src/parser/push_stack.c index d94e722b..0ac6061b 100644 --- a/42sh/src/parser/push_stack.c +++ b/42sh/src/parser/push_stack.c @@ -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); }