new parsing des if

This commit is contained in:
Antoine Riard 2017-03-10 18:09:13 +01:00
parent 0852e7c807
commit a2628f4302
15 changed files with 10306 additions and 38 deletions

10245
42sh/file1 Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/10 16:19:30 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:39:42 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -124,6 +124,7 @@ int add_null(t_btree **ast, t_list **lst);
int add_ionumber(t_btree **ast, t_list **lst); int add_ionumber(t_btree **ast, t_list **lst);
int add_bang(t_btree **ast, t_list **lst); int add_bang(t_btree **ast, t_list **lst);
int add_bang_sep(t_btree **ast, t_list **lst); int add_bang_sep(t_btree **ast, t_list **lst);
int add_if(t_btree **ast, t_list **lst);
int isloop(t_btree **ast, t_list **lst); int isloop(t_btree **ast, t_list **lst);
int isloop_condition(t_btree **ast, t_list **lst); int isloop_condition(t_btree **ast, t_list **lst);

View file

@ -0,0 +1,5 @@
ls
while [ 1 ] ;
do
ls | cat
done

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/10 14:46:27 by ariard #+# #+# */ /* Created: 2017/03/10 14:46:27 by ariard #+# #+# */
/* Updated: 2017/03/10 15:51:37 by ariard ### ########.fr */ /* Updated: 2017/03/10 16:25:35 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/03/09 18:54:07 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:56:38 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/03/10 16:20:35 by ariard ### ########.fr */ /* Updated: 2017/03/10 16:29:09 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,8 +72,8 @@ int handle_instruction(int fd)
} }
DG("Before execution:"); DG("Before execution:");
btree_print(STDBUG, ast, &ft_putast); btree_print(STDBUG, ast, &ft_putast);
if (ft_exec(&ast)) // if (ft_exec(&ast))
return (1); // return (1);
instruction_free(&token, &parser, &ast); instruction_free(&token, &parser, &ast);
ft_add_str_in_history(lexer.str); ft_add_str_in_history(lexer.str);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/10 14:57:45 by ariard #+# #+# */ /* Created: 2017/03/10 14:57:45 by ariard #+# #+# */
/* Updated: 2017/03/10 15:43:54 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:35:55 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/04 20:42:13 by ariard #+# #+# */ /* Created: 2017/03/04 20:42:13 by ariard #+# #+# */
/* Updated: 2017/03/09 20:00:19 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:34:11 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ int iscase(t_btree **ast, t_list **lst)
if (*ast) if (*ast)
{ {
node = (*ast)->item; node = (*ast)->item;
if (node->type == TK_CASE || node->type == TK_PAREN_OPEN) if (node->type == TK_CASE|| node->type == TK_PAREN_OPEN)
return (1); return (1);
} }
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/15 20:49:15 by ariard #+# #+# */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/10 15:22:44 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:55:08 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -84,7 +84,10 @@ int add_cmd(t_btree **ast, t_list **lst)
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
if (token->type != TK_WORD) DG("add cmd");
if (token->type == TK_IF)
add_if(ast, lst);
else if (token->type != TK_WORD)
node->type = token->type; node->type = token->type;
else else
node->type = CMD; node->type = CMD;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* add_condition.c :+: :+: :+: */ /* add_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 18:12:52 by ariard #+# #+# */ /* Created: 2017/03/10 17:06:16 by ariard #+# #+# */
/* Updated: 2017/03/07 22:46:10 by ariard ### ########.fr */ /* Updated: 2017/03/10 18:06:27 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,18 +15,19 @@
int iscondition(t_btree **ast, t_list **lst) int iscondition(t_btree **ast, t_list **lst)
{ {
t_astnode *node; t_astnode *node;
t_token *token;
(void)lst;
node = NULL; node = NULL;
token = (*lst)->content;
if (*ast) if (*ast)
{ {
node = (*ast)->item; node = (*ast)->item;
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscondition(&(*ast)->right, lst) == 1)
return (1);
if ((node->type == TK_IF || node->type == TK_ELIF || node->type == TK_ELSE) if ((node->type == TK_IF || node->type == TK_ELIF || node->type == TK_ELSE)
&& node->full == 0) && node->full == 0)
return (1); return (1);
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscondition(&(*ast)->right, lst))
return (1);
} }
return (0); return (0);
} }
@ -41,20 +42,19 @@ int iscondition_branch(t_btree **ast, t_list **lst)
if (*ast) if (*ast)
{ {
node = (*ast)->item; node = (*ast)->item;
if ((node->type == TK_IF || iscondition(&(*ast)->right, lst)) if ((node->type == TK_IF || node->type == TK_ELIF)
&& (token->type == TK_ELIF || token->type == TK_ELSE) && (token->type == TK_ELIF || token->type == TK_ELSE)
&& node->nest == 0) && node->nest == 0)
return (1); return (1);
} }
return (0); return (0);
} }
int add_condition_cmd(t_btree **ast, t_list **lst) int add_condition_cmd(t_btree **ast, t_list **lst)
{ {
t_token *token; t_token *token;
t_astnode *node; t_astnode *node;
DG("add condition");
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
if (token->type == TK_IF && (node->type == TK_IF || node->type == TK_ELIF if (token->type == TK_IF && (node->type == TK_IF || node->type == TK_ELIF
@ -64,7 +64,7 @@ int add_condition_cmd(t_btree **ast, t_list **lst)
|| node->type == TK_ELSE) && node->nest > 0) || node->type == TK_ELSE) && node->nest > 0)
node->nest--; node->nest--;
else if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF else if (token->type == TK_FI && (node->type == TK_IF || node->type == TK_ELIF
|| node->type == TK_ELSE)) || node->type == TK_ELSE))
return ((node->full = 1)); return ((node->full = 1));
if (token->type == TK_THEN) if (token->type == TK_THEN)
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
@ -73,9 +73,10 @@ int add_condition_cmd(t_btree **ast, t_list **lst)
else else
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
return (0); return (0);
} }
int add_condition_sep(t_btree **ast, t_list **lst)
int add_condition_sep(t_btree **ast, t_list **lst)
{ {
if (!(*ast)->right) if (!(*ast)->right)
return (add_sep(&(*ast)->left, lst)); return (add_sep(&(*ast)->left, lst));
@ -84,15 +85,16 @@ int add_condition_sep(t_btree **ast, t_list **lst)
return (0); return (0);
} }
int add_branch(t_btree **ast, t_list **lst) int add_if(t_btree **ast, t_list **lst)
{ {
t_token *token;
t_astnode *node; t_astnode *node;
t_btree *new_node;
new_node = NULL; DG("add if");
gen_node(&new_node); token = (*lst)->content;
join_ast(ast, &new_node); node = (*ast)->item;
node = (new_node)->item; node->type = TK_IF;
node->type = TK_NEWLINE; token->type = TK_ELIF;
return (add_cmd(ast, lst)); add_cmd(ast, lst);
return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 22:17:14 by ariard #+# #+# */ /* Created: 2017/02/17 22:17:14 by ariard #+# #+# */
/* Updated: 2017/03/07 22:46:20 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:14:21 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 19:12:07 by ariard #+# #+# */ /* Created: 2017/02/15 19:12:07 by ariard #+# #+# */
/* Updated: 2017/03/10 15:43:01 by ariard ### ########.fr */ /* Updated: 2017/03/10 18:05:35 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,6 @@ int add_sep(t_btree **ast, t_list **lst)
t_astnode *node; t_astnode *node;
t_btree *new_node; t_btree *new_node;
DG("add sep");
if (isloop(ast, lst)) if (isloop(ast, lst))
return (add_loop_sep(ast, lst)); return (add_loop_sep(ast, lst));
else if (iscondition(ast, lst)) else if (iscondition(ast, lst))
@ -33,6 +32,7 @@ int add_sep(t_btree **ast, t_list **lst)
return (add_bang_sep(ast, lst)); return (add_bang_sep(ast, lst));
if (!*ast) if (!*ast)
gen_node(ast); gen_node(ast);
DG(" flag");
token = (*lst)->content; token = (*lst)->content;
new_node = NULL; new_node = NULL;
gen_node(&new_node); gen_node(&new_node);

View file

@ -55,7 +55,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
else else
parser->state = UNDEFINED; parser->state = UNDEFINED;
build_tree(ast, token); build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast); btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(&parser->stack) && !(*token)->next)) if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token); insert_linebreak(token);
else else

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:57:44 by ariard #+# #+# */ /* Created: 2017/02/15 18:57:44 by ariard #+# #+# */
/* Updated: 2017/03/08 00:02:46 by ariard ### ########.fr */ /* Updated: 2017/03/10 17:49:28 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,3 +49,15 @@ int add_null(t_btree **ast, t_list **lst)
return (-1); return (-1);
} }
int add_branch(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_btree *new_node;
new_node = NULL;
gen_node(&new_node);
join_ast(ast, &new_node);
node = (new_node)->item;
node->type = TK_NEWLINE;
return (add_cmd(ast, lst));
}