From 79bceb05a96e93a0ca99ca98a8720c18a5729b03 Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Thu, 23 Feb 2017 01:55:09 +0100 Subject: [PATCH] trop fatigue pour debug les case nesting, fuck it, todo tomorrow --- 42sh/Makefile | 1 + 42sh/includes/parser.h | 5 ++ 42sh/sample/case/case.sh | 15 ++---- 42sh/sample/for/for.sh | 11 +++++ 42sh/sample/if/01_if_easy.sh | 12 +++-- 42sh/src/main/ft_putast.c | 12 ++++- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_case.c | 81 +++++++++++++++++++++++++++++++++ 42sh/src/parser/add_cmd.c | 18 +++++++- 42sh/src/parser/add_loop.c | 1 - 42sh/src/parser/add_sep.c | 2 + 42sh/src/parser/aggregate_sym.c | 8 ++-- 42sh/src/parser/build_tree.c | 11 +++-- 42sh/src/parser/eval_sym.c | 2 +- 42sh/src/parser/ft_parse.c | 6 +-- 42sh/src/parser/produce_sym.c | 6 +-- 42sh/src/parser/tree_wrapper.c | 3 ++ 17 files changed, 163 insertions(+), 33 deletions(-) create mode 100644 42sh/sample/for/for.sh create mode 100644 42sh/src/parser/add_case.c diff --git a/42sh/Makefile b/42sh/Makefile index 88ecb85b..d6ca50db 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -229,6 +229,7 @@ parser/add_condition.c\ parser/add_file.c\ parser/add_loop.c\ parser/add_sep.c\ +parser/add_case.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 66f9a5cd..3f418fbe 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -216,8 +216,12 @@ int add_loop_sep(t_btree **ast, t_list **lst); int add_condition_cmd(t_btree **ast, t_list **lst); int add_condition_sep(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst); +int add_case_cmd(t_btree **ast, t_list **lst); +int add_case_sep(t_btree **ast, t_list **lst); +int add_pattern(t_btree **ast, t_list **lst); int isloop(t_btree **ast); int isdir(t_btree **ast); +int iscase(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst); int join_ast(t_btree **ast, t_btree **new_node); @@ -255,6 +259,7 @@ struct s_astnode int nest; int full; + int pattern; t_type type; t_astdata data; }; diff --git a/42sh/sample/case/case.sh b/42sh/sample/case/case.sh index f84e34fc..90d45405 100644 --- a/42sh/sample/case/case.sh +++ b/42sh/sample/case/case.sh @@ -1,12 +1,7 @@ case $rental in - ("bus") case yolo in - ("bonjour") echo hello ;; - ("hello") echo bonjour ;; - esac - case yala in - ("bonjour") echo hello ;; - ("hello") echo yolo ;; - esac ;; - ("van") echo "For $rental rental is Rs.10 per k/m.";; - ("jeep") echo "For $rental rental is Rs.5 per k/m.";; + ("bus") case $rental in + ("yolo") echo hello ;; + ("bonjour") echo yolo ;; + esac ;; + ("van") echo yolo ;; esac diff --git a/42sh/sample/for/for.sh b/42sh/sample/for/for.sh new file mode 100644 index 00000000..676abb4b --- /dev/null +++ b/42sh/sample/for/for.sh @@ -0,0 +1,11 @@ +for i in ls + do + for i in echo + do + pwd + done + for i in cd + do + cat + done +done diff --git a/42sh/sample/if/01_if_easy.sh b/42sh/sample/if/01_if_easy.sh index 171c2a03..f7d0a10c 100644 --- a/42sh/sample/if/01_if_easy.sh +++ b/42sh/sample/if/01_if_easy.sh @@ -1,8 +1,10 @@ if ls then - if ls - then - ls | cat - pwd ; ls - fi + ls +elif ls +then + ls +elif ls +then + ls fi diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 43f53e0a..8720b1df 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -16,7 +16,17 @@ char *ft_putast(void *nodein) { t_astnode *node; node = nodein; - if (node->type == TK_THEN) + if (node->type == TK_CASE) + return ("TK_CASE"); + else if (node->type == TK_PAREN_OPEN) + return ("TK_OPE"); + else if (node->type == TK_PAREN_CLOSE) + return ("TK_CLO"); + else if (node->type == TK_IN) + return ("TK_IN"); + else if (node->type ==TK_ESAC) + return ("TK_ESAC"); + else if (node->type == TK_THEN) return ("THEN"); else if (node->type == TK_FI) return ("FI"); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5c98209b..f321b2a8 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -63,7 +63,7 @@ int handle_instruction(int fd) token = NULL; } DG("succesful parsing:"); -// btree_print(STDBUG, ast, &ft_putast); + btree_print(STDBUG, ast, &ft_putast); /* if (ft_exec(&ast)) */ /* return (1); */ ft_add_str_in_history(lexer.str); diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c new file mode 100644 index 00000000..8ee56bc8 --- /dev/null +++ b/42sh/src/parser/add_case.c @@ -0,0 +1,81 @@ +#include "parser.h" + +int iscase(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + node = NULL; + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type == TK_PAREN_OPEN && node->full == 0) + { + // DG("go right"); + return (1); + } + if ((node->type == TK_CASE || node->type == TK_PAREN_OPEN + || iscase(&(*ast)->right, lst) == 1) && token->type == TK_WORD + && node->pattern == 0) + { + // DG("add pattern"); + return (2); + } + if ((node->type == TK_CASE || iscase(&(*ast)->right, lst) == 4) + && token->type == TK_PAREN_OPEN) + { + // DG("new branch"); + return (3); + } + if ((node->type == TK_NEWLINE || node->type == TK_SEMI + || node->type == TK_AMP) && iscase(&(*ast)->right, lst) == 1) + { +// DG(" go right"); + return (1); + } + if (node->type == TK_PAREN_OPEN && node->nest == 0) + return (4); + } + return (0); +} + +int add_case_cmd(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + token = (*lst)->content; + node = (*ast)->item; + DG("add case cmd"); + if (token->type == TK_CASE && node->type == TK_PAREN_OPEN) + { + DG("nesting"); + node->nest++; + } + if (token->type == TK_ESAC && node->type == TK_PAREN_OPEN && node->nest > 0) + { + DG("nesting less"); + node->nest--; + } + else if (token->type == TK_DSEMI && node->type == TK_PAREN_OPEN) + return ((node->full = 1)); + return (add_cmd(&(*ast)->right, lst)); +} + +int add_case_sep(t_btree **ast, t_list **lst) +{ + return (add_sep(&(*ast)->right, lst)); +} + +int add_pattern(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + token = (*lst)->content; + node = (*ast)->item; + node->data.redir.word.word = ft_strdup(token->data); + node->pattern = 1; + return (0); +} diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 23bf28bf..b8d1112c 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -29,8 +29,24 @@ int add_cmd(t_btree **ast, t_list **lst) return (add_condition_cmd(ast, lst)); else if (iscondition(ast, lst) == 2) return (add_branch(ast, lst)); - else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN) + else if (iscase(ast, lst) == 1) + { + DG("go add cmd"); + return (add_case_cmd(ast, lst)); + } + else if (iscase(ast, lst) == 2) + return (add_pattern(ast, lst)); + else if (iscase(ast, lst) == 3) + { + DG("add branc"); + return (add_branch(ast, lst)); + } + else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN + && node->type != TK_PAREN_CLOSE && node->type != TK_ESAC) + { + DG("return cmd : %s", read_state(node->type)); return (add_cmd(&(*ast)->right, lst)); + } my_tab = NULL; token = (*lst)->content; node = (*ast)->item; diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index 22c7865b..81d9e016 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -20,7 +20,6 @@ int isloop(t_btree **ast) if (*ast) { node = (*ast)->item; - DG("TEST LOOP"); if ((node->type == TK_NEWLINE || node->type == TK_SEMI || node->type == TK_AMP) && isloop(&(*ast)->right) == 1) return (1); diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index e9243154..950b17f5 100644 --- a/42sh/src/parser/add_sep.c +++ b/42sh/src/parser/add_sep.c @@ -23,6 +23,8 @@ int add_sep(t_btree **ast, t_list **lst) return (add_loop_sep(ast, lst)); else if (iscondition(ast, lst) == 1) return (add_condition_sep(ast, lst)); + else if (iscase(ast, lst)) + return (add_case_sep(ast, lst)); if (!*ast) gen_node(ast); token = (*lst)->content; diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 1ba587c6..e33fa3a5 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -279,20 +279,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) int i; i = 0; - DG("aggregate head %s && sym %s", - read_state(**stack), read_state(*new_sym)); +// DG("aggregate head %s && sym %s", +// read_state(**stack), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top && MATCH_STACK(**stack, g_aggrematch[i].under)) { - DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); +// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); - DG("stack after pop: %s", read_state(**stack)); +// DG("stack after pop: %s", read_state(**stack)); } if (eval_sym(**stack, *new_sym)) return ((*state = ERROR)); diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index fa58745c..cbebde17 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -31,13 +31,17 @@ t_treematch g_treematch[] = {TK_THEN, &add_cmd}, {TK_FI, &add_cmd}, {TK_NEWLINE, &add_sep}, + {TK_CASE, &add_cmd}, + {TK_ESAC, &add_cmd}, + {TK_PAREN_OPEN, &add_cmd}, + {TK_PAREN_CLOSE, &add_cmd}, {0, NULL}, }; static int isseparator(int type, int cache) { if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO - || cache == TK_NEWLINE || cache == TK_THEN)) + || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN)) return (0); return (1); } @@ -50,6 +54,8 @@ int build_tree(t_btree **ast, t_list **lst) i = 0; token = (*lst)->content; +//check bug de cache + cache = token->type; while (g_treematch[i].type) { if (g_treematch[i].type == token->type @@ -57,8 +63,7 @@ int build_tree(t_btree **ast, t_list **lst) { DG("func TK : '%s' TK : '%s'", - read_state(g_treematch[i].type) ,read_state(token->type)); - cache = token->type; + read_state(g_treematch[i].type) ,read_state(token->type)); return (g_treematch[i].add(ast, lst)); } i++; diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index ad767ee5..eeb9b093 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -823,7 +823,7 @@ int eval_sym(t_sym stack, t_sym new_sym) { int i; - DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); +// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 42f0b249..714d5b4e 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -43,14 +43,14 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) aggregate_sym(&parser->stack, parser->new_sym, &parser->state); push_stack(++parser->stack, *parser->new_sym); } - ft_read_stack(parser->stack); +// ft_read_stack(parser->stack); DG("\n"); if (*parser->stack == PROGRAM) parser->state = SUCCESS; else parser->state = UNDEFINED; -// build_tree(ast, token); -// btree_print(STDBUG, *ast, &ft_putast); + build_tree(ast, token); + btree_print(STDBUG, *ast, &ft_putast); if ((end_instruction(*parser->stack) && !(*token)->next)) insert_linebreak(token); else diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index bcbef58d..505a21a6 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -97,8 +97,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) int i; token = (*lst)->content; - DG("produce stack : %s && token : %s", read_state(stack), - read_state(token->type)); +// DG("produce stack : %s && token : %s", read_state(stack), +// read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) @@ -106,7 +106,7 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) if (token->type == g_prodmatch[i].token && stack == g_prodmatch[i].stack) { - DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); +// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); *new_sym = g_prodmatch[i].new_sym; } i++; diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index 505f2050..7fbc3c2a 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -31,6 +31,9 @@ int gen_node(t_btree **ast) ((t_astnode *)(*ast)->item)->nest = 0; ((t_astnode *)(*ast)->item)->full = 0; ((t_astnode *)(*ast)->item)->type = 0; + ((t_astnode *)(*ast)->item)->pattern = 0; } return (0); } + +