From 5976d0d281d766418e7f12e66fb13c646de2c4f5 Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Wed, 22 Feb 2017 01:43:25 +0100 Subject: [PATCH] grammar case part 1 motherfucker --- 42sh/case.sh | 8 -------- 42sh/includes/exec.h | 4 ++-- 42sh/sample/case/case.sh | 12 ++++++------ 42sh/src/lexer/get_reserved_words.c | 8 ++++++++ 42sh/src/main/main.c | 8 ++++---- 42sh/src/parser/aggregate_sym.c | 1 + 42sh/src/parser/eval_sym.c | 3 +++ 42sh/src/parser/ft_parse.c | 5 +++-- 42sh/src/parser/produce_sym.c | 3 ++- 42sh/src/parser/read_stack.c | 8 ++++++++ 10 files changed, 37 insertions(+), 23 deletions(-) delete mode 100644 42sh/case.sh diff --git a/42sh/case.sh b/42sh/case.sh deleted file mode 100644 index d415d6aa..00000000 --- a/42sh/case.sh +++ /dev/null @@ -1,8 +0,0 @@ -case $rental in - ("car") echo "For $rental rental is Rs.20 per k/m.";; - ("van") echo "For $rental rental is Rs.10 per k/m.";; - ("jeep") echo "For $rental rental is Rs.5 per k/m.";; - ("bicycle") echo "For $rental rental 20 paisa per k/m.";; - ("enfield") echo "For $rental rental Rs.3 per k/m.";; - ("thunderbird") echo "For $rental rental Rs.5 per k/m.";; -esac diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 74e303ee..58aabc7a 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -128,7 +128,7 @@ void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); -int loop_exec(t_list *list_ast); -int loop_del(t_list *list_ast); + + #endif diff --git a/42sh/sample/case/case.sh b/42sh/sample/case/case.sh index d415d6aa..3828871c 100644 --- a/42sh/sample/case/case.sh +++ b/42sh/sample/case/case.sh @@ -1,8 +1,8 @@ case $rental in - ("car") echo "For $rental rental is Rs.20 per k/m.";; - ("van") echo "For $rental rental is Rs.10 per k/m.";; - ("jeep") echo "For $rental rental is Rs.5 per k/m.";; - ("bicycle") echo "For $rental rental 20 paisa per k/m.";; - ("enfield") echo "For $rental rental Rs.3 per k/m.";; - ("thunderbird") echo "For $rental rental Rs.5 per k/m.";; + "cat") echo "For $rental rental is Rs.20 per k/m.";; + "van") echo "For $rental rental is Rs.10 per k/m.";; + "jeep") echo "For $rental rental is Rs.5 per k/m.";; + "bicycle") echo "For $rental rental 20 paisa per k/m.";; + "enfield") echo "For $rental rental Rs.3 per k/m.";; + "thunderbird") echo "For $rental rental Rs.5 per k/m.";; esac diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 413b4c35..84c2f493 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -16,10 +16,12 @@ int get_reserved_words(t_list **alst) { t_token *token; t_token *previous_token; + t_token *ante_token; t_list *temp; temp = *alst; previous_token = NULL; + ante_token = NULL; while (temp) { token = temp->content; @@ -45,8 +47,14 @@ int get_reserved_words(t_list **alst) token->type = TK_ELSE; else if (ft_strncmp(token->data, "until", 5) == 0) token->type = TK_UNTIL; + else if (ft_strncmp(token->data, "case", 4) == 0) + token->type = TK_CASE; } } + if (ante_token && ante_token->type == TK_CASE + && ft_strncmp(token->data, "in", 2) == 0) + token->type = TK_IN; + ante_token = previous_token; previous_token = token; temp = temp->next; } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index ddee0ec0..88606518 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -30,6 +30,7 @@ int handle_instruction(int fd) DG("START: state=%i", parser.state); while (1) { + DG("get input"); if ((ret = readline(fd, get_lexer_stack(lexer), &str))) { if (ret == -1) @@ -47,21 +48,20 @@ int handle_instruction(int fd) if (get_lexer_stack(lexer)) continue ; lexer.state = DEFAULT; + token_print(token); if (get_reserved_words(&token)) return (1); if (insert_newline(&token)) return (1); if (ft_parse(&ast, &token, &parser)) continue ; - token = NULL; - DG("AFTER PARSING: state=%i", parser.state); if (parser.state == SUCCESS) break ; else if (parser.state == ERROR) - return (error_syntax(&token)); + return (error_syntax(&token)); } 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/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 2f509444..b4beb401 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -57,6 +57,7 @@ t_aggrematch g_aggrematch[] = {LINEBREAK, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST}, {NEWLINE_LIST, CMD_NAME, CMD_SUPERIOR, CMD_NAME}, {NEWLINE_LIST, TK_DO, TK_DO, TK_DO}, + {NEWLINE_LIST, TK_IN, TK_IN, TK_IN}, {NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN}, {NEWLINE_LIST, TK_IF, TK_IF, TK_IF}, {NEWLINE_LIST, TK_ELIF, TK_ELIF, TK_ELIF}, diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index b082ed21..db4e1fcb 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -203,6 +203,7 @@ t_stackmatch g_stackmatch[] = {TK_CASE, NEWLINE_LIST}, {TK_CASE, SEPARATOR_OP}, {TK_CASE, PIPE_SEMI_SEQUENCE}, + {TK_IN, TK_WORD}, {TK_ESAC, CASE_LIST}, {TK_ESAC, CASE_LIST_NS}, {TK_ESAC, LINEBREAK}, @@ -304,6 +305,7 @@ t_stackmatch g_stackmatch[] = {LINEBREAK, COMPOUND_LIST}, {LINEBREAK, PROGRAM}, {NEWLINE_LIST, TK_DO}, + {NEWLINE_LIST, TK_IN}, {NEWLINE_LIST, TK_WHILE}, {NEWLINE_LIST, TK_UNTIL}, {NEWLINE_LIST, TK_IF}, @@ -497,6 +499,7 @@ t_stackmatch g_stackmatch[] = {PATTERN, TK_PAREN_OPEN}, {PATTERN, CASE_LIST}, + {PATTERN, TK_IN}, {PATTERN_CASE, CASE_LIST}, {CASE_ITEM, CASE_LIST}, {CASE_ITEM, LINEBREAK}, diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index acc024b8..f54351ed 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -28,6 +28,7 @@ static int end_instruction(t_sym sym) int ft_parse(t_btree **ast, t_list **token, t_parser *parser) { + (void)ast; while (*token) { produce_sym(*parser->stack, parser->new_sym, token); @@ -45,8 +46,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) 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)) /* || *parser->stack == PROGRAM) */ insert_linebreak(token); diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 332e6bf6..9164cb58 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -43,7 +43,7 @@ t_prodmatch g_prodmatch[] = {TK_WORD, TK_BANG, CMD_NAME}, {TK_WORD, PIPE_SEMI_SEQUENCE, CMD_NAME}, {TK_WORD, SEPARATOR_OP, CMD_NAME}, - {TK_WORD, IN, WORDLIST}, + {TK_WORD, TK_IN, PATTERN}, {TK_WORD, CASE_LIST, PATTERN}, {TK_WORD, TK_PAREN_OPEN, PATTERN}, {TK_ASSIGNEMENT_WORD, LINEBREAK, CMD_PREFIX}, @@ -56,6 +56,7 @@ t_prodmatch g_prodmatch[] = {TK_NAME, NEWLINE_LIST, FNAME}, {TK_NAME, TK_FOR, NAME}, {TK_NEWLINE, TK_DO, NEWLINE_LIST}, + {TK_NEWLINE, TK_IN, NEWLINE_LIST}, {TK_NEWLINE, TK_WHILE, NEWLINE_LIST}, {TK_NEWLINE, TK_UNTIL, NEWLINE_LIST}, {TK_NEWLINE, TK_IF, NEWLINE_LIST}, diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index e8fb06c2..2806b159 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -16,6 +16,14 @@ char *read_state(t_sym current) { if (current == 0) return ("NULL"); + if (current == PATTERN) + return ("PATTERN"); + if (current == TK_CASE) + return ("TK_CASE"); + if (current == TK_IN) + return ("TK_IN"); + if (current == TK_ESAC) + return ("TK_ESAC"); if (current == UNTIL_CLAUSE) return ("UNTIL_CLAUSE"); if (current == TK_UNTIL)