while one nest ok

This commit is contained in:
ariard@student.42.fr 2017-02-18 20:10:42 +01:00
parent 96e439b652
commit 6609e3b8d9
16 changed files with 96 additions and 44 deletions

BIN
42sh/includes/.parser.h.swn Normal file

Binary file not shown.

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/17 23:18:23 by ariard ### ########.fr */ /* Updated: 2017/02/18 20:06:02 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -249,11 +249,13 @@ union u_astdata
t_ld *token; t_ld *token;
char **sstr; char **sstr;
char *str; char *str;
int loop;
}; };
struct s_astnode struct s_astnode
{ {
int nest;
int full;
t_type type; t_type type;
t_astdata data; t_astdata data;
}; };

View file

@ -1,4 +1,8 @@
while ls | cat ; while ls
do do
pwd | cat ; while ls
do
pwd
done
pwd ; ls
done done

Binary file not shown.

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/17 23:40:13 by ariard ### ########.fr */ /* Updated: 2017/02/18 18:56:15 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,9 +25,8 @@ int add_cmd(t_btree **ast, t_list **lst)
return (add_file(ast, lst)); return (add_file(ast, lst));
else if (isloop(ast)) else if (isloop(ast))
return (add_loop_cmd(ast, lst)); return (add_loop_cmd(ast, lst));
else else if ((node = (*ast)->item)->type != TK_DO)
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
DG("insert data cmd");
my_tab = NULL; my_tab = NULL;
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
@ -37,7 +36,6 @@ int add_cmd(t_btree **ast, t_list **lst)
my_tab = ft_sstradd(my_tab, token->data); my_tab = ft_sstradd(my_tab, token->data);
my_tab = ft_sstradd(my_tab, (char *)token->esc); my_tab = ft_sstradd(my_tab, (char *)token->esc);
// ft_ld_pushback(&node->data.token, my_tab); // ft_ld_pushback(&node->data.token, my_tab);
DG("still alive");
} }
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/17 16:39:05 by ariard #+# #+# */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/02/17 23:24:01 by ariard ### ########.fr */ /* Updated: 2017/02/18 16:43:28 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,7 +32,6 @@ int add_file(t_btree **ast, t_list **lst)
t_astnode *node; t_astnode *node;
t_token *token; t_token *token;
DG("add file");
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
node->data.redir.n = ft_atoi(token->data); node->data.redir.n = ft_atoi(token->data);

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/02/17 23:38:25 by ariard ### ########.fr */ /* Updated: 2017/02/18 20:10:08 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,12 +19,18 @@ int isloop(t_btree **ast)
node = NULL; node = NULL;
if (*ast) if (*ast)
{ {
DG("end isloop");
node = (*ast)->item; node = (*ast)->item;
if (node->data.loop == 1) DG("TEST LOOP");
if (node->type == TK_WHILE && node->full == 1)
{
DG("DON ENTER");
return (2); return (2);
if (node->type == TK_WHILE) }
if (node->type == TK_WHILE && node->full == 0)
{
DG(" NOFULL");
return (1); return (1);
}
} }
return (0); return (0);
} }
@ -34,12 +40,26 @@ int add_loop_cmd(t_btree **ast, t_list **lst)
t_token *token; t_token *token;
t_astnode *node; t_astnode *node;
DG("add loop cmd");
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
if (token->type == TK_DONE) DG("add loop cmd");
return ((node->data.loop = 1)); if (token->type == TK_WHILE && node->type == TK_WHILE)
else if (token->type == TK_DO) {
DG("nest one more");
node->nest++;
}
if (token->type == TK_DONE && node->type == TK_WHILE && node->nest > 0)
{
node->nest--;
DG("nest one less");
}
else if (token->type == TK_DONE && node->type == TK_WHILE
&& node->nest == 0)
{
DG("WHILE FULL");
return ((node->full = 1));
}
if (token->type == TK_DO)
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) != 2)
return (add_cmd(&(*ast)->left, lst)); return (add_cmd(&(*ast)->left, lst));
@ -50,7 +70,6 @@ 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)
{ {
DG("add loop sep");
if (!(*ast)->right) if (!(*ast)->right)
return (add_sep(&(*ast)->left, lst)); return (add_sep(&(*ast)->left, lst));
else else

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/17 23:34:43 by ariard ### ########.fr */ /* Updated: 2017/02/18 20:06:47 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,13 +19,18 @@ 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)->right) == 1 || isloop(ast)) if (isloop(ast) == 1)
return (add_loop_sep(ast, lst)); return (add_loop_sep(ast, lst));
new_node = NULL; node = (*ast)->item;
gen_node(&new_node);
join_ast(ast, &new_node);
token = (*lst)->content; token = (*lst)->content;
node = (new_node)->item; // if (node->type != TK_DO)
// {
new_node = NULL;
gen_node(&new_node);
join_ast(ast, &new_node);
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/09 17:39:18 by ariard #+# #+# */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */
/* Updated: 2017/02/17 21:35:09 by ariard ### ########.fr */ /* Updated: 2017/02/18 16:58:05 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -151,6 +151,7 @@ t_aggrematch g_aggrematch[] =
{END_COMMAND, TK_WHILE, PIPE_SEQUENCE, 0}, {END_COMMAND, TK_WHILE, PIPE_SEQUENCE, 0},
{END_COMMAND, LINEBREAK, PIPE_SEQUENCE, 0}, {END_COMMAND, LINEBREAK, PIPE_SEQUENCE, 0},
{END_COMMAND, TK_DO, PIPE_SEQUENCE, 0}, {END_COMMAND, TK_DO, PIPE_SEQUENCE, 0},
{END_COMMAND, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST},
{PIPE_SEQUENCE, TK_WHILE, PIPELINE, 0}, {PIPE_SEQUENCE, TK_WHILE, PIPELINE, 0},
{PIPE_SEQUENCE, TK_BANG, PIPELINE, TK_BANG}, {PIPE_SEQUENCE, TK_BANG, PIPELINE, TK_BANG},
{PIPE_SEQUENCE, SEPARATOR_OP, PIPELINE, 0}, {PIPE_SEQUENCE, SEPARATOR_OP, PIPELINE, 0},
@ -187,8 +188,8 @@ 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
@ -200,7 +201,7 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
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

@ -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/17 23:34:15 by ariard ### ########.fr */ /* Updated: 2017/02/18 18:43:50 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,24 +24,36 @@ t_treematch g_treematch[] =
{TK_WHILE, &add_cmd}, {TK_WHILE, &add_cmd},
{TK_DO, &add_cmd}, {TK_DO, &add_cmd},
{TK_DONE, &add_cmd}, {TK_DONE, &add_cmd},
{TK_NEWLINE, &add_sep},
{0, NULL}, {0, NULL},
}; };
int build_tree(t_btree **ast, t_list **lst) static int isseparator(int type, int cache)
{ {
int i; if (type == TK_NEWLINE && (cache == TK_WHILE
|| cache == TK_DO))
return (0);
return (1);
}
int build_tree(t_btree **ast, t_list **lst)
{
int i;
static int cache;
t_token *token; t_token *token;
i = 0; i = 0;
token = (*lst)->content; token = (*lst)->content;
while (g_treematch[i].type) while (g_treematch[i].type)
{ {
if (g_treematch[i].type == token->type) if (g_treematch[i].type == token->type
&& isseparator(token->type, cache))
{ {
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));
return (g_treematch[i].add(ast, lst)); cache = token->type;
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/09 16:26:30 by ariard #+# #+# */ /* Created: 2017/02/09 16:26:30 by ariard #+# #+# */
/* Updated: 2017/02/17 21:35:11 by ariard ### ########.fr */ /* Updated: 2017/02/18 18:50:18 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -195,6 +195,7 @@ t_stackmatch g_stackmatch[] =
{TK_WHILE, SEPARATOR_OP}, {TK_WHILE, SEPARATOR_OP},
{TK_WHILE, NEWLINE_LIST}, {TK_WHILE, NEWLINE_LIST},
{TK_WHILE, PIPE_SEMI_SEQUENCE}, {TK_WHILE, PIPE_SEMI_SEQUENCE},
{TK_WHILE, TK_DO},
{TK_UNTIL, LINEBREAK}, {TK_UNTIL, LINEBREAK},
{TK_UNTIL, TK_BANG}, {TK_UNTIL, TK_BANG},
{TK_UNTIL, SEPARATOR_OP}, {TK_UNTIL, SEPARATOR_OP},
@ -243,6 +244,7 @@ t_stackmatch g_stackmatch[] =
{END_COMMAND, TK_WHILE}, {END_COMMAND, TK_WHILE},
{END_COMMAND, TK_DO}, {END_COMMAND, TK_DO},
{END_COMMAND, LINEBREAK}, {END_COMMAND, LINEBREAK},
{END_COMMAND, COMPOUND_LIST},
{SEPARATOR, CMD_SUPERIOR}, {SEPARATOR, CMD_SUPERIOR},
{SEPARATOR, TERM}, {SEPARATOR, TERM},
{SEPARATOR, COMPOUND_LIST}, {SEPARATOR, COMPOUND_LIST},
@ -387,6 +389,7 @@ t_stackmatch g_stackmatch[] =
{WHILE_CLAUSE, SEPARATOR_OP}, {WHILE_CLAUSE, SEPARATOR_OP},
{WHILE_CLAUSE, NEWLINE_LIST}, {WHILE_CLAUSE, NEWLINE_LIST},
{WHILE_CLAUSE, PIPE_SEMI_SEQUENCE}, {WHILE_CLAUSE, PIPE_SEMI_SEQUENCE},
{WHILE_CLAUSE, TK_DO},
{ELSE_PART, COMPOUND_LIST}, {ELSE_PART, COMPOUND_LIST},
{IF_CLAUSE, LINEBREAK}, {IF_CLAUSE, LINEBREAK},
{IF_CLAUSE, TK_BANG}, {IF_CLAUSE, TK_BANG},
@ -435,6 +438,7 @@ t_stackmatch g_stackmatch[] =
{COMPOUND_COMMAND, NEWLINE_LIST}, {COMPOUND_COMMAND, NEWLINE_LIST},
{COMPOUND_COMMAND, PIPE_SEMI_SEQUENCE}, {COMPOUND_COMMAND, PIPE_SEMI_SEQUENCE},
{COMPOUND_COMMAND, FUNC}, {COMPOUND_COMMAND, FUNC},
{COMPOUND_COMMAND, TK_DO},
{COMMAND, TK_WHILE}, {COMMAND, TK_WHILE},
{COMMAND, LINEBREAK}, {COMMAND, LINEBREAK},
{COMMAND, TK_DO}, {COMMAND, TK_DO},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 14:30:22 by ariard #+# #+# */ /* Created: 2017/02/09 14:30:22 by ariard #+# #+# */
/* Updated: 2017/02/17 23:39:18 by ariard ### ########.fr */ /* Updated: 2017/02/18 18:06:45 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/17 21:29:20 by ariard ### ########.fr */ /* Updated: 2017/02/18 18:42:55 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,8 +72,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)
@ -81,7 +81,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/17 23:40:50 by ariard ### ########.fr */ /* Updated: 2017/02/18 20:03:24 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,12 @@
char *read_state(t_sym current) char *read_state(t_sym current)
{ {
if (current == COMPOUND_COMMAND)
return ("COMPOUND_COMMAND");
if (current == WHILE_CLAUSE)
return ("WHILE_CLAUSE");
if (current == LOOP)
return ("LOOP");
if (current == TK_DONE) if (current == TK_DONE)
return ("DONE"); return ("DONE");
if (current == TK_DO) if (current == TK_DO)

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/17 23:38:47 by ariard ### ########.fr */ /* Updated: 2017/02/18 20:06:49 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,10 +25,13 @@ int gen_node(t_btree **ast)
if (!*ast) if (!*ast)
{ {
DG("node create");
*ast = btree_create_node(&item, sizeof(item)); *ast = btree_create_node(&item, sizeof(item));
((t_astnode *)(*ast)->item)->data.token = NULL; ((t_astnode *)(*ast)->item)->data.token = NULL;
((t_astnode *)(*ast)->item)->data.redir.word.word = NULL; ((t_astnode *)(*ast)->item)->data.redir.word.word = NULL;
((t_astnode *)(*ast)->item)->data.loop = 0; ((t_astnode *)(*ast)->item)->nest = 0;
((t_astnode *)(*ast)->item)->full = 0;
((t_astnode *)(*ast)->item)->type = 0;
} }
return (0); return (0);
} }

View file

@ -1 +0,0 @@
ls |