parsing for ok

This commit is contained in:
ariard@student.42.fr 2017-02-24 16:29:51 +01:00
parent 5f596310e1
commit d50a0d1b13
13 changed files with 78 additions and 80 deletions

View file

@ -213,13 +213,14 @@ int add_cmd(t_btree **ast, t_list **lst);
int add_file(t_btree **ast, t_list **lst); int add_file(t_btree **ast, t_list **lst);
int add_loop_cmd(t_btree **ast, t_list **lst); int add_loop_cmd(t_btree **ast, t_list **lst);
int add_loop_sep(t_btree **ast, t_list **lst); int add_loop_sep(t_btree **ast, t_list **lst);
int add_loop_condition(t_btree **ast, t_list **lst);
int add_condition_cmd(t_btree **ast, t_list **lst); int add_condition_cmd(t_btree **ast, t_list **lst);
int add_condition_sep(t_btree **ast, t_list **lst); int add_condition_sep(t_btree **ast, t_list **lst);
int add_branch(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst);
int add_case_cmd(t_btree **ast, t_list **lst); int add_case_cmd(t_btree **ast, t_list **lst);
int add_case_sep(t_btree **ast, t_list **lst); int add_case_sep(t_btree **ast, t_list **lst);
int add_pattern(t_btree **ast, t_list **lst); int add_pattern(t_btree **ast, t_list **lst);
int isloop(t_btree **ast); int isloop(t_btree **ast, t_list **lst);
int isdir(t_btree **ast); int isdir(t_btree **ast);
int iscase(t_btree **ast, t_list **lst); int iscase(t_btree **ast, t_list **lst);
int iscondition(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst);
@ -250,6 +251,7 @@ union u_astdata
{ {
t_redir redir; t_redir redir;
t_ld *token; t_ld *token;
t_list *wordlist;
char **sstr; char **sstr;
char *str; char *str;
}; };
@ -264,31 +266,4 @@ struct s_astnode
t_astdata data; t_astdata data;
}; };
int parse(t_btree **ast, t_list **token);
int get_instruction(t_list **lst);
int get_sub_instruction(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_redir(t_btree **ast, t_list **start, t_list **lst);
int parse_less(t_btree **ast, t_list **start, t_list **lst);
int parse_great(t_btree **ast, t_list **start, t_list **lst);
int parse_dless(t_btree **ast, t_list **start, t_list **lst);
int parse_dgreat(t_btree **ast, t_list **start, t_list **lst);
int parse_lessand(t_btree **ast, t_list **start, t_list **lst);
int parse_greatand(t_btree **ast, t_list **start, t_list **lst);
int parse_word(t_btree **ast, t_list **start, t_list **lst);
int parse_subshell(t_btree **ast, t_list **start, t_list **lst);
int parse_newline(t_btree **ast, t_list **start, t_list **lst);
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_elif(t_btree **ast, t_list **start, t_list **lst);
int parse_else(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 #endif

View file

@ -1,11 +1,11 @@
for i in ls for i in hello bonjour salut comment
do do
for i in echo for i in echo
do do
pwd pwd
done done
for i in cd for i in echo
do do
cat pwd
done done
done done

View file

@ -0,0 +1,11 @@
while ls
do
while ls
do
pwd
done
while ls
do
pwd
done
done

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 21:05:23 by ariard #+# #+# */ /* Created: 2017/02/21 21:05:23 by ariard #+# #+# */
/* Updated: 2017/02/21 21:12:13 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:22:08 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/02/21 19:20:20 by jhalford ### ########.fr */ /* Updated: 2017/02/24 16:24:19 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,10 @@ char *ft_putast(void *nodein)
{ {
t_astnode *node; t_astnode *node;
node = nodein; node = nodein;
if (node->type == TK_NAME)
return ("TK_NAME");
if (node->type == TK_FOR)
return ("TK_FOR");
if (node->type == TK_CASE) if (node->type == TK_CASE)
return ("TK_CASE"); return ("TK_CASE");
else if (node->type == TK_PAREN_OPEN) else if (node->type == TK_PAREN_OPEN)

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 22:44:41 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:17:54 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -55,7 +55,7 @@ int add_pattern(t_btree **ast, t_list **lst)
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
node->data.redir.word.word = ft_strdup(token->data); node->data.str = ft_strdup(token->data);
node->pattern = 1; node->pattern = 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/02/24 15:30:23 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:29:21 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,9 @@ int add_cmd(t_btree **ast, t_list **lst)
gen_node(ast); gen_node(ast);
else if (isdir(ast)) else if (isdir(ast))
return (add_file(ast, lst)); return (add_file(ast, lst));
else if (isloop(ast)) else if (isloop(ast, lst) == 3)
return (add_loop_condition(ast, lst));
else if (isloop(ast, lst))
return (add_loop_cmd(ast, lst)); return (add_loop_cmd(ast, lst));
else if (iscondition(ast, lst) == 1) else if (iscondition(ast, lst) == 1)
return (add_condition_cmd(ast, lst)); return (add_condition_cmd(ast, lst));

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 18:12:52 by ariard #+# #+# */ /* Created: 2017/02/19 18:12:52 by ariard #+# #+# */
/* Updated: 2017/02/24 14:53:49 by ariard ### ########.fr */ /* Updated: 2017/02/24 15:45:39 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,33 +6,33 @@
/* 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/02/24 15:08:08 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:28:27 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parser.h" #include "parser.h"
int isloop(t_btree **ast) int isloop(t_btree **ast, t_list **lst)
{ {
t_astnode *node; t_astnode *node;
t_token *token;
node = NULL; node = NULL;
token = (*lst)->content;
if (*ast) if (*ast)
{ {
node = (*ast)->item; node = (*ast)->item;
if (node->type == TK_FOR && token->type == TK_WORD && node->pattern == 0)
return (3);
if ((node->type == TK_NEWLINE || node->type == TK_SEMI if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && isloop(&(*ast)->right) == 1) || node->type == TK_AMP) && isloop(&(*ast)->right, lst) == 1)
return (1); return (1);
if ((node->type == TK_WHILE || node->type == TK_UNTIL) && node->full == 1) if ((node->type == TK_WHILE || node->type == TK_UNTIL
{ || node->type == TK_FOR) && node->full == 1)
DG("DON ENTER");
return (2); return (2);
} if ((node->type == TK_WHILE || node->type == TK_UNTIL
if ((node->type == TK_WHILE || node->type == TK_UNTIL) && node->full == 0) || node->type == TK_FOR) && node->full == 0)
{
DG(" NOFULL");
return (1); return (1);
}
} }
return (0); return (0);
} }
@ -45,27 +45,20 @@ int add_loop_cmd(t_btree **ast, t_list **lst)
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
DG("add loop cmd"); DG("add loop cmd");
if ((token->type == TK_WHILE || token->type == TK_UNTIL) if (token->type == TK_DO && node->type == TK_FOR)
&& (node->type == TK_WHILE || node->type == TK_UNTIL)) node->pattern = 1;
{ if ((token->type == TK_WHILE || token->type == TK_UNTIL || token->type == TK_FOR)
DG("nest one more"); && (node->type == TK_WHILE || node->type == TK_UNTIL || node->type == TK_FOR))
node->nest++; node->nest++;
} if (token->type == TK_DONE && (node->type == TK_WHILE
if (token->type == TK_DONE && (node->type == TK_WHILE || node->type == TK_UNTIL) || node->type == TK_UNTIL || node->type == TK_FOR) && node->nest > 0)
&& node->nest > 0)
{
node->nest--; node->nest--;
DG("nest one less");
}
else if (token->type == TK_DONE && (node->type == TK_WHILE else if (token->type == TK_DONE && (node->type == TK_WHILE
|| node->type == TK_UNTIL) && node->nest == 0) || node->type == TK_UNTIL || node->type == TK_FOR) && node->nest == 0)
{
DG("WHILE FULL");
return ((node->full = 1)); return ((node->full = 1));
}
if (token->type == TK_DO && node->nest == 0) if (token->type == TK_DO && node->nest == 0)
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
else if (!(*ast)->right && isloop(&(*ast)->left) != 2) else if (!(*ast)->right && isloop(&(*ast)->left, lst) != 2)
return (add_cmd(&(*ast)->left, lst)); return (add_cmd(&(*ast)->left, lst));
else else
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
@ -80,3 +73,15 @@ int add_loop_sep(t_btree **ast, t_list **lst)
return (add_sep(&(*ast)->right, lst)); return (add_sep(&(*ast)->right, lst));
return (0); return (0);
} }
int add_loop_condition(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
token = (*lst)->content;
node = (*ast)->item;
ft_lsteadd(&node->data.wordlist, ft_lstnew(ft_strdup(token->data),
ft_strlen(token->data)));
return (0);
}

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/02/24 15:15:33 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:08:47 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ int add_sep(t_btree **ast, t_list **lst)
t_btree *new_node; t_btree *new_node;
DG("add sep"); DG("add sep");
if (isloop(ast) == 1) if (isloop(ast, lst) == 1)
return (add_loop_sep(ast, lst)); return (add_loop_sep(ast, lst));
else if (iscondition(ast, lst) == 1) else if (iscondition(ast, lst) == 1)
return (add_condition_sep(ast, lst)); return (add_condition_sep(ast, lst));
@ -28,13 +28,11 @@ int add_sep(t_btree **ast, t_list **lst)
if (!*ast) if (!*ast)
gen_node(ast); gen_node(ast);
token = (*lst)->content; token = (*lst)->content;
// if (node->type != TK_DO) //watch != TK_DO
// { new_node = NULL;
new_node = NULL; gen_node(&new_node);
gen_node(&new_node); join_ast(ast, &new_node);
join_ast(ast, &new_node); node = (new_node)->item;
node = (new_node)->item;
// }
node->type = token->type; node->type = token->type;
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 18:32:59 by ariard #+# #+# */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/02/24 15:21:16 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:24:44 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,13 +35,15 @@ t_treematch g_treematch[] =
{TK_ESAC, &add_cmd}, {TK_ESAC, &add_cmd},
{TK_PAREN_OPEN, &add_cmd}, {TK_PAREN_OPEN, &add_cmd},
{TK_PAREN_CLOSE, &add_cmd}, {TK_PAREN_CLOSE, &add_cmd},
{TK_FOR, &add_cmd},
{0, NULL}, {0, NULL},
}; };
static int isseparator(int type, int cache) static int isseparator(int type, int cache)
{ {
if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO if (type == TK_NEWLINE && (cache == TK_WHILE || cache == TK_DO
|| cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN)) || cache == TK_NEWLINE || cache == TK_THEN || cache == TK_IN
|| cache == TK_WORD))
return (0); return (0);
return (1); return (1);
} }
@ -54,8 +56,8 @@ int build_tree(t_btree **ast, t_list **lst)
i = 0; i = 0;
token = (*lst)->content; token = (*lst)->content;
//check bug de cache //check bug de cache case ?
cache = token->type; // cache = token->type;
while (g_treematch[i].type) while (g_treematch[i].type)
{ {
if (g_treematch[i].type == token->type if (g_treematch[i].type == token->type
@ -63,7 +65,8 @@ int build_tree(t_btree **ast, t_list **lst)
{ {
DG("func TK : '%s' TK : '%s'", DG("func TK : '%s' TK : '%s'",
read_state(g_treematch[i].type) ,read_state(token->type)); read_state(g_treematch[i].type) ,read_state(token->type));
cache = token->type;
return (g_treematch[i].add(ast, lst)); return (g_treematch[i].add(ast, lst));
} }
i++; i++;

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/02/19 16:34:17 by ariard ### ########.fr */ /* Updated: 2017/02/24 16:00:40 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */