parsing if/elsif stable

This commit is contained in:
ariard@student.42.fr 2017-02-06 17:59:48 +01:00
parent 9bb45abb3d
commit a211bfd1b0
22 changed files with 145 additions and 15678 deletions

View file

@ -130,10 +130,8 @@ lexer/lexer_comment.c\
lexer/lexer_default.c\
lexer/lexer_delim.c\
lexer/lexer_dquote.c\
lexer/lexer_fi.c\
lexer/lexer_great.c\
lexer/lexer_greatand.c\
lexer/lexer_if.c\
lexer/lexer_less.c\
lexer/lexer_lessand.c\
lexer/lexer_newline.c\
@ -184,6 +182,7 @@ parser/parse_separator.c\
parser/parse_subshell.c\
parser/parse_while.c\
parser/parse_if.c\
parser/parse_elif.c\
parser/parse_word.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))

3
42sh/blind.text Normal file
View file

@ -0,0 +1,3 @@
do en condition, aller a la fin
verifier les delimiters
impleter erreur syntax : EOF, unexpected do, unexpected done, pareil if

15537
42sh/file1

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/02/05 22:45:03 by ariard ### ########.fr */
/* Updated: 2017/02/06 15:13:20 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,11 +50,14 @@ typedef long long t_type;
# define TK_IF (1 << 23)
# define TK_THEN (1 << 24)
# define TK_FI (1 << 25)
# define TK_ELIF (1 << 26)
# define TK_ELSE (1 << 27)
# define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD)
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)
# define TK_NON_FREEABLE (TK_PAREN_OPEN | TK_PAREN_CLOSE | TK_BQUOTE)
# define SHELL_SEP (TK_NEWLINE | TK_AMP | TK_SEMI | TK_DO)
# define RW_SEP (TK_NEWLINE | TK_AMP | TK_SEMI | TK_WHILE | TK_DONE\
| TK_DO | TK_IF | TK_FI | TK_THEN | TK_ELIF | TK_ELSE)
enum e_lexstate
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/05 23:44:43 by ariard ### ########.fr */
/* Updated: 2017/02/06 15:45:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -80,8 +80,9 @@ int parse_while(t_btree **ast, t_list **start, t_list **lst);
int parse_if(t_btree **ast, t_list **start, t_list **lst);
int parse_do(t_btree **ast, t_list **start, t_list **lst);
int parse_done(t_btree **ast, t_list **start, t_list **lst);
int parse_list(t_btree **ast, t_list **start, t_list **lst);
int parse_elif(t_btree **ast, t_list **start, t_list **lst);
int delete_newline(t_list **start, t_list **lst);
int parse_head(t_btree **ast, t_btree **new_ast, t_list **start, t_list **lst);
#endif

View file

@ -1,11 +1,7 @@
if [ 1 ]
if ls
then
if [ 1 ]
then
echo "hello world"
fi
if [ 1 ]
then
echo "salut monde"
fi
fi > file1
echo hello
elif ls
then
echo world
fi

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */
/* Updated: 2017/02/05 22:38:03 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:05:21 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2017/02/06 00:04:12 by ariard ### ########.fr */
/* Updated: 2017/02/06 14:53:27 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,6 @@ t_execmap g_execmap[] =
{TK_GREAT, &exec_great},
{TK_DGREAT, &exec_dgreat},
{TK_WHILE, &exec_while},
{TK_IF, &exec_if},
{TK_COMMAND | TK_SUBSHELL, &exec_command},
{0, 0},
};

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/02/05 23:15:12 by ariard ### ########.fr */
/* Updated: 2017/02/06 15:10:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,7 +23,7 @@ int get_reserved_words(t_list **alst)
while (temp)
{
token = temp->content;
if (!previous_token || (previous_token->type & SHELL_SEP))
if (!previous_token || (previous_token->type & RW_SEP))
{
if (token->type == TK_N_WORD)
{
@ -39,6 +39,10 @@ int get_reserved_words(t_list **alst)
token->type = TK_THEN;
else if(ft_strncmp(token->data, "fi", 4) == 0)
token->type = TK_FI;
else if (ft_strncmp(token->data, "elif", 4) == 0)
token->type = TK_ELIF;
else if (ft_strncmp(token->data, "else", 4) == 0)
token->type = TK_ELSE;
}
}
previous_token = token;

View file

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_fi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:35:08 by ariard #+# #+# */
/* Updated: 2017/01/31 23:59:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_fi(t_list **alst, char *str)
{
t_token *token;
t_lexstate state;
if (*alst)
return (lexer_fi(&(*alst)->next, str));
else
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = TK_FI;
state = DEFAULT;
return (ft_tokenize(&(*alst)->next, str + 2, state));
}

View file

@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 23:25:47 by ariard #+# #+# */
/* Updated: 2017/02/03 15:12:24 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_if(t_list **alst, char *str)
{
t_token *token;
token = (*alst)->content;
if (ft_strncmp(str, "if", 2) == 0)
token->type = TK_IF;
else if (ft_isalnum(*str))
{
token_append(token, *str, 0);
return (ft_tokenize(alst, str + 1, WORD));
}
return (ft_tokenize(&(*alst)->next, str + 3, LIST));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/05 23:50:29 by ariard ### ########.fr */
/* Updated: 2017/02/06 16:14:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,6 +36,8 @@ char *ft_putast(void *nodein)
return (" WHILE ");
else if (node->type == TK_IF)
return ("IF");
else if (node->type == TK_ELIF)
return ("ELIF");
else if (node->type == TK_NEWLINE)
return ("NEW");
else if (node->type == TK_GREAT)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/02/05 23:40:13 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:59:20 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/05 23:38:31 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:11:26 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
t_parser g_parser[] =
{
{TK_WHILE | TK_IF | TK_NEWLINE | TK_SEMI, &get_sub_instruction},
{TK_WHILE | TK_IF | TK_ELIF | TK_NEWLINE | TK_SEMI, &get_sub_instruction},
{TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_AMP, &parse_separator},
{TK_PIPE, &parse_separator},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 16:56:55 by ariard #+# #+# */
/* Updated: 2017/02/05 23:45:50 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:17:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,15 +63,16 @@ static int get_condition_instruction(t_list **start, t_list **lst)
int nest;
nest = 0;
while (((*lst) = (*lst)->next))
while (*lst)
{
token = (*lst)->content;
if (token->type & TK_THEN)
if (token->type & TK_IF)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 0 && token->type & TK_FI)
break;
*lst = (*lst)->next;
}
while (((*lst) = (*lst)->next))
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/05 22:05:26 by ariard #+# #+# */
/* Updated: 2017/02/05 23:15:02 by ariard ### ########.fr */
/* Updated: 2017/02/06 15:49:31 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,6 +23,8 @@ int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst)
return (parse_while(ast, start, lst));
else if (token->type == TK_IF)
return (parse_if(ast, start, lst));
else if (token->type == TK_ELIF)
return (parse_elif(ast, start, lst));
else if (token->type == TK_SEMI)
return (parse_separator(ast, start, lst));
return (0);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */
/* Updated: 2017/02/05 23:46:00 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:42:08 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@ int parse(t_btree **ast, t_list **token)
{
t_list *start;
(void)ast;
start = *token;
if (get_instruction(token))
return (1);

View file

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_elif.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */
/* Updated: 2017/02/06 17:54:22 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int parse_after_elif(t_btree **ast, t_list **lst)
{
t_list *temp;
t_list *prev_prev;
t_list *prev;
t_token *token;
int nest;
temp = (*lst);
nest = 0;
while ((temp = temp->next))
{
token = temp->content;
if (nest == 0 && (token->type & TK_ELIF))
break;
else if (token->type & TK_IF)
nest++;
else if (token->type & TK_FI)
nest--;
prev_prev = prev;
prev = temp;
}
prev_prev->next = NULL;
if (prev)
ft_parse(ast, &prev);
return (0);
}
static int parse_loop(t_btree **ast, t_list **start, t_list **lst)
{
t_token *token;
t_list *temp;
int nest;
nest = 0;
while ((*lst))
{
token = (*lst)->content;
if (token->type & TK_THEN)
nest++;
else if (token->type & TK_ELIF)
nest--;
if (nest == 1 && (token->type & TK_THEN))
break;
*lst = (*lst)->next;
}
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
delete_newline(start, &temp);
ft_parse(&(*ast)->right, &temp);
ft_parse(&(*ast)->left, start);
return (0);
}
int parse_elif(t_btree **ast, t_list **start, t_list **lst)
{
t_btree *new_ast;
parse_after_elif(ast, lst);
parse_head(ast, &new_ast, start, lst);
*start = *lst;
parse_loop(&new_ast, start, lst);
return (0);
}

View file

@ -6,31 +6,31 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/05 23:15:23 by ariard #+# #+# */
/* Updated: 2017/02/05 23:57:18 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:46:01 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
static int parse_after_condition(t_btree **ast, t_list **start, t_list **lst)
static int parse_after_condition(t_btree **ast, t_list **start, t_list **lst)
{
t_list *temp;
t_list *del;
t_token *token;
int nest;
temp = (*lst);
nest = 0;
while ((temp = temp->next))
while (temp)
{
token = temp->content;
if (token->type & TK_THEN)
if (token->type & TK_IF)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 0 && (token->type & TK_FI))
break;
break;
temp = temp->next;
}
del = temp;
temp = temp->next;
@ -41,12 +41,11 @@ static int parse_after_condition(t_btree **ast, t_list **start, t_list **lst)
return (0);
}
static int parse_head(t_btree **ast,
t_btree **new_ast, t_list **start, t_list **lst)
static int parse_head_condition(t_btree **ast,
t_btree **new_ast, t_list **start, t_list **lst)
{
t_btree *father;
t_token *token;
t_list *del;
t_astnode *node;
t_astnode item;
@ -64,53 +63,17 @@ static int parse_head(t_btree **ast,
token = (*lst)->content;
node = (*new_ast)->item;
node->type = token->type;
del = (*lst);
*lst = (*lst)->next;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
delete_newline(start, lst);
token->type = TK_ELIF;
ft_parse(&(*new_ast)->right, start);
return (0);
}
static int parse_loop(t_btree **ast, t_list **start, t_list **lst)
{
t_token *token;
t_list *temp;
t_list *new_start;
int nest;
nest = 0;
while ((*lst)->next)
*lst = (*lst)->next;
new_start = *start;
ft_lst_reverse(start);
temp = *lst;
while ((*lst))
{
token = (*lst)->content;
if (token->type & TK_THEN)
nest++;
else if (token->type & TK_FI)
nest--;
if (nest == 1 && (token->type & TK_THEN))
break;
*lst = (*lst)->next;
}
ft_lst_reverse(&temp);
temp = (*lst)->next;
(*lst)->next = NULL;
ft_lst_delif(&new_start, (*lst)->content, &ft_addrcmp, &token_free);
delete_newline(start, &temp);
ft_parse(&(*ast)->right, &temp);
ft_parse(&(*ast)->left, &new_start);
return (0);
}
int parse_if(t_btree **ast, t_list **start, t_list **lst)
{
t_btree *new_ast;
parse_after_condition(ast, start, lst);
parse_head(ast, &new_ast, start, lst);
parse_loop(&new_ast, start, lst);
parse_head_condition(ast, &new_ast, start, lst);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/02/05 21:54:28 by ariard ### ########.fr */
/* Updated: 2017/02/06 17:52:31 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -58,7 +58,7 @@ static int parse_after_loop(t_btree **ast, t_list **start, t_list **lst)
return (0);
}
static int parse_head(t_btree **ast,
int parse_head(t_btree **ast,
t_btree **new_ast, t_list **start, t_list **lst)
{
t_btree *father;
@ -77,12 +77,13 @@ static int parse_head(t_btree **ast,
father->left = *new_ast;
else
*new_ast = *ast;
token = (*lst)->content;
node = (*new_ast)->item;
node->type = token->type;
del = (*lst);
*lst = (*lst)->next;
//check that//
del->next = NULL;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
delete_newline(start, lst);
return (0);

9
42sh/test.sh Normal file
View file

@ -0,0 +1,9 @@
if [ 0 -gt 1 ]
then
echo hello
if ls
then
echo world
echo yolo
fi