From 49b79603b374d29ee2ccfdeaabd9085c8a278a04 Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Tue, 21 Feb 2017 22:45:59 +0100 Subject: [PATCH] insert newline add --- 42sh/Makefile | 3 ++- 42sh/case.sh | 8 ++++++++ 42sh/includes/lexer.h | 4 +++- 42sh/includes/parser.h | 2 +- 42sh/sample/case/case.sh | 8 ++++++++ 42sh/sample/mix/mix.sh | 2 +- 42sh/src/hash_table/hash.c | 2 +- 42sh/src/hash_table/hash_str.c | 2 +- 42sh/src/lexer/get_reserved_words.c | 2 +- 42sh/src/lexer/insert_newline.c | 23 +++++++++++++++++++++++ 42sh/src/lexer/lexer_default.c | 2 +- 42sh/src/lexer/lexer_delim.c | 4 +--- 42sh/src/lexer/lexer_great.c | 2 +- 42sh/src/lexer/lexer_lex.c | 2 +- 42sh/src/lexer/token_append.c | 2 +- 42sh/src/lexer/token_print.c | 13 +++---------- 42sh/src/main/main.c | 15 +++++++++------ 42sh/src/parser/aggregate_sym.c | 10 +++++----- 42sh/src/parser/ft_parse.c | 6 ++++-- 42sh/src/parser/produce_sym.c | 8 ++++---- 42sh/src/parser/read_stack.c | 10 +++++----- 21 files changed, 84 insertions(+), 46 deletions(-) create mode 100644 42sh/case.sh create mode 100644 42sh/sample/case/case.sh create mode 100644 42sh/src/lexer/insert_newline.c diff --git a/42sh/Makefile b/42sh/Makefile index e647c32f..88ecb85b 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/21 16:38:00 by ariard ### ########.fr # +# Updated: 2017/02/21 21:32:36 by ariard ### ########.fr # # # # **************************************************************************** # @@ -171,6 +171,7 @@ lexer/get_lexer_stack.c\ lexer/get_reserved_words.c\ lexer/get_state_global.c\ lexer/get_state_redir.c\ +lexer/insert_newline.c\ lexer/lexer_backslash.c\ lexer/lexer_bquote.c\ lexer/lexer_comment.c\ diff --git a/42sh/case.sh b/42sh/case.sh new file mode 100644 index 00000000..d415d6aa --- /dev/null +++ b/42sh/case.sh @@ -0,0 +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.";; +esac diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 042aed97..28b0962a 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/20 22:26:26 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:12:01 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,6 +91,8 @@ char *stack_to_prompt(t_list *stack); t_lexstate get_state_global(t_lexer *lexer); t_lexstate get_state_redir(t_lexer *lexer); int get_lexer_stack(t_lexer lexer); +int get_reserved_words(t_list **alst); +int insert_newline(t_list **alst); void lexer_init(t_lexer *lexer); int lexer_lex(t_list **alst, t_lexer *lexer); int lexer_default(t_list **alst, t_lexer *lexer); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 4960d21f..503a37f9 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -21,9 +21,9 @@ enum e_parstate { + ERROR = 0, UNDEFINED, SUCCESS, - ERROR, }; enum e_sym diff --git a/42sh/sample/case/case.sh b/42sh/sample/case/case.sh new file mode 100644 index 00000000..d415d6aa --- /dev/null +++ b/42sh/sample/case/case.sh @@ -0,0 +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.";; +esac diff --git a/42sh/sample/mix/mix.sh b/42sh/sample/mix/mix.sh index d9e02158..36437d5c 100644 --- a/42sh/sample/mix/mix.sh +++ b/42sh/sample/mix/mix.sh @@ -1,6 +1,6 @@ if ls then - ls | cat + ls ; cat while ls do until ls diff --git a/42sh/src/hash_table/hash.c b/42sh/src/hash_table/hash.c index fa3604fd..8a9a1eea 100644 --- a/42sh/src/hash_table/hash.c +++ b/42sh/src/hash_table/hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:06:19 by gwojda #+# #+# */ -/* Updated: 2017/02/18 14:16:35 by gwojda ### ########.fr */ +/* Updated: 2017/02/21 20:57:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/hash_table/hash_str.c b/42sh/src/hash_table/hash_str.c index f7e70f11..a7aa38f1 100644 --- a/42sh/src/hash_table/hash_str.c +++ b/42sh/src/hash_table/hash_str.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:32:18 by gwojda #+# #+# */ -/* Updated: 2017/02/19 12:58:47 by gwojda ### ########.fr */ +/* Updated: 2017/02/21 20:57:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 7a24e372..413b4c35 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:06:20 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:06:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/insert_newline.c b/42sh/src/lexer/insert_newline.c new file mode 100644 index 00000000..1057c24e --- /dev/null +++ b/42sh/src/lexer/insert_newline.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insert_newline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/21 21:05:23 by ariard #+# #+# */ +/* Updated: 2017/02/21 21:12:13 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int insert_newline(t_list **alst) +{ + t_token *token; + + token = token_init(); + token->type = TK_NEWLINE; + ft_lsteadd(alst, ft_lstnew(token, sizeof(*token))); + return (0); +} diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 4d67d8cc..200bdec0 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:50:30 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:40:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 4c7f4eb7..c7e789f1 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:52:10 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:42:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,7 @@ int lexer_delim(t_list **alst, t_lexer *lexer) lexer->pos++; lexer->state = DEFAULT; if (token->type) - { return (lexer_lex(&(*alst)->next, lexer)); - } else { if (lexer->str[lexer->pos] == 0) diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 48c1a33b..cce8f831 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:52:38 by ariard ### ########.fr */ +/* Updated: 2017/02/21 21:09:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 8e9a7720..222b5616 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:55:26 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:44:37 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index 8a65c2b5..6d83ee13 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:03:57 by ariard ### ########.fr */ +/* Updated: 2017/02/21 21:34:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 18d9b38e..d4f1f901 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:55:43 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:20:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,19 +15,12 @@ void token_print(t_list *lst) { t_token *token; - int i; - t_type type; - int index; while (lst) { - i = -1; + token = lst->content; - type = token->type; - while (type >> (i++ + 2)) - ; - DG("%02i:[%s]", i, token->data); - index = -1; + DG("token : %s data [%s]\n", read_state(token->type), token->data); lst = lst->next; } } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 2568d77e..ddee0ec0 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/21 20:31:43 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:44:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,13 +31,11 @@ int handle_instruction(int fd) while (1) { if ((ret = readline(fd, get_lexer_stack(lexer), &str))) - { - DG("ret=%i, str=%s, state=%i", ret, str, parser.state); + { if (ret == -1) return (-1); return (parser.state == UNDEFINED ? error_EOF() : 1); } - DG("ret=%i, str=%s", ret, str); ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); @@ -46,11 +44,16 @@ int handle_instruction(int fd) ltoken = ft_lstlast(token); if (lexer_lex(token ? <oken : &token, &lexer)) return (1); - //token_print(token); if (get_lexer_stack(lexer)) - continue ; + continue ; + lexer.state = DEFAULT; + 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 ; diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 96a82d56..2f509444 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ -/* Updated: 2017/02/21 10:49:53 by ariard ### ########.fr */ +/* Updated: 2017/02/21 20:40:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -240,20 +240,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/ft_parse.c b/42sh/src/parser/ft_parse.c index 1818c48c..acc024b8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -14,6 +14,7 @@ static void insert_linebreak(t_list **lst) { t_token *token; + token = (*lst)->content; token->type = LINEBREAK; } @@ -32,12 +33,14 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) produce_sym(*parser->stack, parser->new_sym, token); DG("new sym %s", read_state(*parser->new_sym)); if (eval_sym(*parser->stack, *parser->new_sym)) - parser->state = ERROR; + return ((parser->state = ERROR)); else { aggregate_sym(&parser->stack, parser->new_sym, &parser->state); push_stack(++parser->stack, *parser->new_sym); } + ft_read_stack(parser->stack); + DG("\n"); if (*parser->stack == PROGRAM) parser->state = SUCCESS; else @@ -50,6 +53,5 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); } - ft_read_stack(parser->stack); return (0); } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 306c9db1..332e6bf6 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:37:22 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:21:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,8 +88,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) @@ -97,7 +97,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/read_stack.c b/42sh/src/parser/read_stack.c index 95f5c3a1..e8fb06c2 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:38:48 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 20:39:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ char *read_state(t_sym current) { + if (current == 0) + return ("NULL"); if (current == UNTIL_CLAUSE) return ("UNTIL_CLAUSE"); if (current == TK_UNTIL) @@ -161,10 +163,8 @@ char *read_state(t_sym current) int ft_read_stack(t_sym *stack) { + DG("read stack :"); while (*stack) - { - ft_putstr(read_state(*stack--)); - ft_putchar(10); - } + DG("%s", read_state(*stack--)); return (0); }