insert newline add

This commit is contained in:
ariard@student.42.fr 2017-02-21 22:45:59 +01:00
parent 04c4978c35
commit 49b79603b3
21 changed files with 84 additions and 46 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 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_reserved_words.c\
lexer/get_state_global.c\ lexer/get_state_global.c\
lexer/get_state_redir.c\ lexer/get_state_redir.c\
lexer/insert_newline.c\
lexer/lexer_backslash.c\ lexer/lexer_backslash.c\
lexer/lexer_bquote.c\ lexer/lexer_bquote.c\
lexer/lexer_comment.c\ lexer/lexer_comment.c\

8
42sh/case.sh Normal file
View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 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_global(t_lexer *lexer);
t_lexstate get_state_redir(t_lexer *lexer); t_lexstate get_state_redir(t_lexer *lexer);
int get_lexer_stack(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); void lexer_init(t_lexer *lexer);
int lexer_lex(t_list **alst, t_lexer *lexer); int lexer_lex(t_list **alst, t_lexer *lexer);
int lexer_default(t_list **alst, t_lexer *lexer); int lexer_default(t_list **alst, t_lexer *lexer);

View file

@ -21,9 +21,9 @@
enum e_parstate enum e_parstate
{ {
ERROR = 0,
UNDEFINED, UNDEFINED,
SUCCESS, SUCCESS,
ERROR,
}; };
enum e_sym enum e_sym

8
42sh/sample/case/case.sh Normal file
View file

@ -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

View file

@ -1,6 +1,6 @@
if ls if ls
then then
ls | cat ls ; cat
while ls while ls
do do
until ls until ls

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:06:19 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:32:18 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* insert_newline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:58:44 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->pos++;
lexer->state = DEFAULT; lexer->state = DEFAULT;
if (token->type) if (token->type)
{
return (lexer_lex(&(*alst)->next, lexer)); return (lexer_lex(&(*alst)->next, lexer));
}
else else
{ {
if (lexer->str[lexer->pos] == 0) if (lexer->str[lexer->pos] == 0)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:35 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:08:51 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 17:18:42 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 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) void token_print(t_list *lst)
{ {
t_token *token; t_token *token;
int i;
t_type type;
int index;
while (lst) while (lst)
{ {
i = -1;
token = lst->content; token = lst->content;
type = token->type; DG("token : %s data [%s]\n", read_state(token->type), token->data);
while (type >> (i++ + 2))
;
DG("%02i:[%s]", i, token->data);
index = -1;
lst = lst->next; lst = lst->next;
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,12 +32,10 @@ int handle_instruction(int fd)
{ {
if ((ret = readline(fd, get_lexer_stack(lexer), &str))) if ((ret = readline(fd, get_lexer_stack(lexer), &str)))
{ {
DG("ret=%i, str=%s, state=%i", ret, str, parser.state);
if (ret == -1) if (ret == -1)
return (-1); return (-1);
return (parser.state == UNDEFINED ? error_EOF() : 1); return (parser.state == UNDEFINED ? error_EOF() : 1);
} }
DG("ret=%i, str=%s", ret, str);
ft_strappend(&lexer.str, str); ft_strappend(&lexer.str, str);
if (get_lexer_stack(lexer) == BACKSLASH) if (get_lexer_stack(lexer) == BACKSLASH)
pop(&lexer.stack); pop(&lexer.stack);
@ -46,11 +44,16 @@ int handle_instruction(int fd)
ltoken = ft_lstlast(token); ltoken = ft_lstlast(token);
if (lexer_lex(token ? &ltoken : &token, &lexer)) if (lexer_lex(token ? &ltoken : &token, &lexer))
return (1); return (1);
//token_print(token);
if (get_lexer_stack(lexer)) 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)) if (ft_parse(&ast, &token, &parser))
continue ; continue ;
token = NULL;
DG("AFTER PARSING: state=%i", parser.state); DG("AFTER PARSING: state=%i", parser.state);
if (parser.state == SUCCESS) if (parser.state == SUCCESS)
break ; break ;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:39:18 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; int i;
i = 0; i = 0;
// DG("aggregate head %s && sym %s", DG("aggregate head %s && sym %s",
// read_state(**stack), read_state(*new_sym)); read_state(**stack), read_state(*new_sym));
while (g_aggrematch[i].top) while (g_aggrematch[i].top)
{ {
if (*new_sym == g_aggrematch[i].top if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under)) && 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; *new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym) if (g_aggrematch[i].erase_sym)
{ {
pop_stack(stack, 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)) if (eval_sym(**stack, *new_sym))
return ((*state = ERROR)); return ((*state = ERROR));

View file

@ -14,6 +14,7 @@
static void insert_linebreak(t_list **lst) static void insert_linebreak(t_list **lst)
{ {
t_token *token; t_token *token;
token = (*lst)->content; token = (*lst)->content;
token->type = LINEBREAK; 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); produce_sym(*parser->stack, parser->new_sym, token);
DG("new sym %s", read_state(*parser->new_sym)); DG("new sym %s", read_state(*parser->new_sym));
if (eval_sym(*parser->stack, *parser->new_sym)) if (eval_sym(*parser->stack, *parser->new_sym))
parser->state = ERROR; return ((parser->state = ERROR));
else else
{ {
aggregate_sym(&parser->stack, parser->new_sym, &parser->state); 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 (*parser->stack == PROGRAM)
parser->state = SUCCESS; parser->state = SUCCESS;
else else
@ -50,6 +53,5 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
else else
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
} }
ft_read_stack(parser->stack);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 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; int i;
token = (*lst)->content; token = (*lst)->content;
// DG("produce stack : %s && token : %s", read_state(stack), DG("produce stack : %s && token : %s", read_state(stack),
// read_state(token->type)); read_state(token->type));
i = 0; i = 0;
*new_sym = 0; *new_sym = 0;
while (g_prodmatch[i].new_sym) 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 if (token->type == g_prodmatch[i].token
&& stack == g_prodmatch[i].stack) && 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; *new_sym = g_prodmatch[i].new_sym;
} }
i++; i++;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 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) char *read_state(t_sym current)
{ {
if (current == 0)
return ("NULL");
if (current == UNTIL_CLAUSE) if (current == UNTIL_CLAUSE)
return ("UNTIL_CLAUSE"); return ("UNTIL_CLAUSE");
if (current == TK_UNTIL) if (current == TK_UNTIL)
@ -161,10 +163,8 @@ char *read_state(t_sym current)
int ft_read_stack(t_sym *stack) int ft_read_stack(t_sym *stack)
{ {
DG("read stack :");
while (*stack) while (*stack)
{ DG("%s", read_state(*stack--));
ft_putstr(read_state(*stack--));
ft_putchar(10);
}
return (0); return (0);
} }