fucking newline to parse

This commit is contained in:
ariard@student.42.fr 2017-02-05 00:15:12 +01:00
parent 9bbd6d7403
commit cd2019c0fd
17 changed files with 207 additions and 64 deletions

View file

@ -169,8 +169,10 @@ main/shell_get_opts.c\
main/shell_init.c\ main/shell_init.c\
main/shell_script.c\ main/shell_script.c\
main/sig_handler.c\ main/sig_handler.c\
parser/parse.c\
parser/ft_parse.c\ parser/ft_parse.c\
parser/get_instruction.c \ parser/get_instruction.c \
parser/get_instruction2.c \
parser/parse_dgreat.c\ parser/parse_dgreat.c\
parser/parse_dless.c\ parser/parse_dless.c\
parser/parse_do.c\ parser/parse_do.c\

26
42sh/file1 Normal file
View file

@ -0,0 +1,26 @@
42sh
Makefile
debug
file1
includes
libft
objs
pdf
sample
script.sh
src
update_makefile.sh
/Users/ariard/Projects/42sh
42sh
Makefile
debug
file1
includes
libft
objs
pdf
sample
script.sh
src
update_makefile.sh
monde

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/03 17:02:27 by ariard ### ########.fr */ /* Updated: 2017/02/04 19:17:26 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -59,9 +59,11 @@ struct s_astnode
extern t_parser g_parser[]; extern t_parser g_parser[];
int ft_parse(t_btree **ast, t_list **token); int parse(t_btree **ast, t_list **token);
int get_instruction(t_btree **ast, t_list **start, t_list **lst); int ft_parse(t_btree **ast, t_list **token);
int get_instruction(t_list **lst);
int get_instruction2(t_btree **ast, t_list **start, t_list **lst);
int parse_newline(t_btree **ast, t_list **start, t_list **lst); int parse_newline(t_btree **ast, t_list **start, t_list **lst);
int parse_separator(t_btree **ast, t_list **start, t_list **lst); int parse_separator(t_btree **ast, t_list **start, t_list **lst);

View file

@ -1,3 +1 @@
ls ; echo ; pwd | cat > file2 ls ; pwd > file1 ; ls | cat
ls | cat
pwd ; echo hello

View file

@ -1,9 +1,7 @@
while [ 1 ] while ls | cat ; pwd
do while pwd do
do while ls | cat
while ls do
do echo monde ; cat
echo hello done
done done > file1
done
done

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */ /* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */
/* Updated: 2017/02/04 15:33:04 by ariard ### ########.fr */ /* Updated: 2017/02/04 15:54:27 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/04 15:27:49 by ariard ### ########.fr */ /* Updated: 2017/02/04 17:15:59 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/03 14:49:11 by ariard ### ########.fr */ /* Updated: 2017/02/04 21:27:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/02/04 15:30:00 by ariard ### ########.fr */ /* Updated: 2017/02/04 22:16:49 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,13 +26,14 @@ int shell_script()
DG("after post_tokenize"); DG("after post_tokenize");
token_print(token); token_print(token);
while (token) // while (token)
{ // {
if (ft_parse(&ast, &token)) if (parse(&ast, &token))
return (1); return (1);
btree_print(STDBUG, ast, &ft_putast); btree_print(STDBUG, ast, &ft_putast);
// if (ft_exec(&ast)) // if (ft_exec(&ast))
// return (1); // return (1);
} ast = NULL;
// }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/03 19:37:05 by ariard ### ########.fr */ /* Updated: 2017/02/05 00:14:17 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
t_parser g_parser[] = t_parser g_parser[] =
{ {
{TK_NEWLINE | TK_WHILE, &get_instruction}, {TK_WHILE, &parse_while},
{TK_SEMI, &parse_separator}, {TK_SEMI, &parse_separator},
{TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_AMP, &parse_separator}, {TK_AMP, &parse_separator},
@ -26,7 +26,6 @@ t_parser g_parser[] =
{TK_LESSAND, &parse_lessand}, {TK_LESSAND, &parse_lessand},
{TK_GREATAND, &parse_greatand}, {TK_GREATAND, &parse_greatand},
{TK_SUBSHELL, &parse_subshell}, {TK_SUBSHELL, &parse_subshell},
{TK_WHILE, &parse_while},
{TK_WORD, &parse_word}, {TK_WORD, &parse_word},
{0, 0}, {0, 0},
}; };

View file

@ -6,20 +6,72 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */
/* Updated: 2017/02/03 17:16:53 by ariard ### ########.fr */ /* Updated: 2017/02/04 22:20:59 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parser.h" #include "parser.h"
int get_instruction(t_btree **ast, t_list **start, t_list **lst) static int get_simple_instruction(t_list **start, t_list **lst)
{ {
t_token *token; t_list *temp;
token = (*lst)->content; DG("simple instruction");
if (token->type == TK_NEWLINE) temp = (*lst)->next;
return (parse_newline(ast, start, lst)); (*lst)->next = NULL;
else if (token->type == TK_WHILE) ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
return (parse_while(ast, start, lst)); (*lst) = temp;
return (0);
}
static int get_compound_instruction(t_list **start, t_list **lst)
{
t_list *temp;
t_token *token;
int nest;
DG("compound instruction");
nest = 0;
while (((*lst) = (*lst)->next))
{
token = (*lst)->content;
if (token->type & TK_DO)
nest++;
else if (token->type & TK_DONE)
nest--;
if (nest == 0 && token->type & TK_DONE)
break;
}
while (((*lst) = (*lst)->next))
{
token = (*lst)->content;
if (token->type & (TK_NEWLINE | TK_SEMI))
break;
}
if ((*lst))
{
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
*lst = temp;
}
return (0);
}
int get_instruction(t_list **lst)
{
t_token *token;
t_list *start;
start = *lst;
while ((*lst))
{
token = (*lst)->content;
if (token->type & TK_NEWLINE)
return (get_simple_instruction(&start, lst));
else if (token->type & TK_WHILE)
return (get_compound_instruction(&start, lst));
(*lst) = (*lst)->next;
}
return (0); return (0);
} }

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_instruction2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 18:20:53 by ariard #+# #+# */
/* Updated: 2017/02/04 19:00:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int get_instruction2(t_btree **ast, t_list **start, t_list **lst)
{
t_token *token;
token = (*lst)->content;
if (token->type == TK_NEWLINE)
return (parse_newline(ast, start, lst));
else if (token->type == TK_WHILE)
return (parse_while(ast, start, lst));
return (0);
}

25
42sh/src/parser/parse.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */
/* Updated: 2017/02/04 20:43:45 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int parse(t_btree **ast, t_list **token)
{
t_list *start;
start = *token;
if (get_instruction(token))
return (1);
if (ft_parse(ast, &start))
return (1);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/03 16:42:45 by ariard ### ########.fr */ /* Updated: 2017/02/04 23:44:59 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 19:26:41 by ariard #+# #+# */ /* Created: 2017/01/26 19:26:41 by ariard #+# #+# */
/* Updated: 2017/02/03 20:04:40 by ariard ### ########.fr */ /* Updated: 2017/02/04 20:22:57 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/02/03 17:29:32 by ariard ### ########.fr */ /* Updated: 2017/02/05 00:03:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,7 @@ int parse_separator(t_btree **ast, t_list **start, t_list **lst)
t_token *token; t_token *token;
t_astnode *node; t_astnode *node;
DG("newline");
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
node->type = token->type; node->type = token->type;

View file

@ -6,21 +6,23 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 16:03:28 by ariard #+# #+# */ /* Created: 2017/01/30 16:03:28 by ariard #+# #+# */
/* Updated: 2017/02/04 15:51:24 by ariard ### ########.fr */ /* Updated: 2017/02/05 00:14:32 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parser.h" #include "parser.h"
static int delete_all_newline(t_list **start, t_list **lst) static int delete_all_newline(t_list **lst)
{ {
t_token *token; t_token *token;
t_list *start;
while ((*lst)) while ((*lst))
{ {
token = (*lst)->content; token = (*lst)->content;
start = *lst;
if (token->type & TK_NEWLINE) if (token->type & TK_NEWLINE)
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(&start, (*lst)->content, &ft_addrcmp, &token_free);
else else
break; break;
(*lst) = (*lst)->next; (*lst) = (*lst)->next;
@ -31,41 +33,53 @@ static int delete_all_newline(t_list **start, t_list **lst)
int parse_while(t_btree **ast, t_list **start, t_list **lst) int parse_while(t_btree **ast, t_list **start, t_list **lst)
{ {
t_list *temp; t_list *temp;
t_list *temp4;
t_btree *temp2;
t_btree *temp3;
t_astnode *node; t_astnode *node;
t_astnode item;
t_token *token; t_token *token;
int nest; int nest;
token = (*lst)->content; temp = (*lst);
node = (*ast)->item; nest = 0;
while ((temp = temp->next))
{
token = temp->content;
if (token->type & TK_DO)
nest++;
else if (token->type & TK_DONE)
nest--;
if (nest == 0 && (token->type & TK_DONE))
break;
}
temp4 = temp;
temp = temp->next;
temp4->next = NULL;
ft_lst_delif(start, temp4->content, &ft_addrcmp, &token_free);
if (temp)
ft_parse(ast, &temp);
temp2 = *ast;
while (temp2->left)
temp2 = temp2->left;
temp3 = btree_create_node(&item, sizeof(item));
((t_astnode *)(*ast)->item)->data.token = NULL;
temp2->left = temp3;
node = temp3->item;
node->type = TK_WHILE; node->type = TK_WHILE;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
while (((*lst) = (*lst)->next)) while (((*lst) = (*lst)->next))
if ((token = (*lst)->content)->type & TK_DO) if ((token = (*lst)->content)->type & TK_DO)
break; break;
temp = (*lst)->next; temp = (*lst)->next;
(*lst)->next = NULL; (*lst)->next = NULL;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_parse(&(*ast)->left, start); ft_parse(&(temp3)->left, start);
delete_all_newline(start, &temp); delete_all_newline(&temp);
ft_parse(&(temp3)->right, &temp);
(*lst) = temp;
*start = temp;
nest = 1;
while (((*lst) = (*lst)->next))
{
token = (*lst)->content;
if (token->type & TK_DO)
nest++;
else if (token->type & TK_DONE)
nest--;
if (nest == 0)
break;
}
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_parse(&(*ast)->right, start);
*start = temp;
return (0); return (0);
} }