rectification cleanage code

This commit is contained in:
Antoine Riard 2017-03-05 16:40:22 +01:00
parent 56b106ee0f
commit 2b456eac9c
20 changed files with 41 additions and 129 deletions

View file

@ -26,8 +26,6 @@ enum e_parstate
SUCCESS,
};
# define TK_REDIR(x) (TK_LESS <= x && x <= TK_GREATAND)
# define MATCH_STACK(x, y) (x == y || y == ALL)
struct s_parser
@ -174,12 +172,12 @@ struct s_cmd
{
t_list *redir;
t_ld *token;
t_list *wordlist;
};
union u_astdata
{
t_cmd cmd;
t_list *wordlist;
char **sstr;
char *str;
};

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2017/03/04 17:09:11 by ariard ### ########.fr */
/* Updated: 2017/03/05 16:24:52 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,7 +29,7 @@ void ast_free(void *data, size_t content_size)
if (node->type == CMD)
{
ft_ld_clear(&node->data.cmd.token, &ft_tabdel);
ft_lstdel(&node->data.cmd.redir, &redir_free);
// ft_lstdel(&node->data.cmd.redir, &redir_free);
}
// if (node->type == WORDLIST)
// do clear

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 20:42:20 by ariard #+# #+# */
/* Updated: 2017/03/04 18:17:05 by ariard ### ########.fr */
/* Updated: 2017/03/05 15:22:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ int exec_for(t_btree **ast)
char *var;
node = (*ast)->item;
temp = node->data.wordlist;
temp = node->data.cmd.wordlist;
var = temp->content;
builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var);
while (temp)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */
/* Updated: 2017/03/04 20:51:21 by ariard ### ########.fr */
/* Updated: 2017/03/05 14:52:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/03 17:37:15 by jhalford #+# #+# */
/* Updated: 2017/03/03 17:48:28 by jhalford ### ########.fr */
/* Updated: 2017/03/05 14:47:09 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/03/03 20:05:54 by ariard ### ########.fr */
/* Updated: 2017/03/05 14:57:19 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,8 @@ char *ft_putast(void *nodein)
t_astnode *node;
node = nodein;
if (node->type == WORDLIST)
return ("WORDLIST");
if (node->type == CMD)
return ("CMD");
if (node->type == REDIR)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/04 19:29:28 by ariard ### ########.fr */
/* Updated: 2017/03/05 16:38:23 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -62,7 +62,7 @@ int handle_instruction(int fd)
error_syntax(&token, &parser, &ast);
}
DG("Before execution:");
// btree_print(STDBUG, ast, &ft_putast);
btree_print(STDBUG, ast, &ft_putast);
if (ft_exec(&ast))
return (1);
instruction_free(&token, &parser, &ast);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/04 20:42:13 by ariard #+# #+# */
/* Updated: 2017/03/04 21:54:21 by ariard ### ########.fr */
/* Updated: 2017/03/05 14:47:19 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/04 22:05:47 by ariard ### ########.fr */
/* Updated: 2017/03/05 16:34:17 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,7 +36,6 @@ int superflous_token(t_btree **ast, t_list **lst)
t_token *token;
(void)ast;
DG("superflous token");
if (*lst)
{
token = (*lst)->content;
@ -73,7 +72,6 @@ int add_cmd(t_btree **ast, t_list **lst)
DG("add cmd");
while (i < 14)
{
DG("test");
if (g_distrostree[i].test(ast, lst) == 1)
{
DG("add : %d", i);

View file

@ -1,71 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/04 21:31:18 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int add_cmd(t_btree **ast, t_list **lst)
{
t_token *token;
t_astnode *node;
char **my_tab;
if ((token = (*lst)->content)->type == TK_IN || token->type == TK_PAREN_OPEN)
return (0);
else if (isdir_sep(ast, lst))
return (add_redir_type(ast, lst));
else if (!*ast)
gen_node(ast);
else if (isdir_word(ast, lst))
return (add_redir_word(ast, lst));
else if (isvar(ast, lst))
return (add_var(ast, lst));
else if (isloop(ast, lst) == 3)
return (add_loop_condition(ast, lst));
else if (isloop(ast, lst))
return (add_loop_cmd(ast, lst));
else if (iscondition(ast, lst) == 1)
return (add_condition_cmd(ast, lst));
else if (iscondition(ast, lst) == 2)
return (add_branch(ast, lst));
else if (iscase(ast, lst) == 1)
return (add_pattern(ast, lst));
else if (iscase(ast, lst) == 2)
return (add_case_cmd(ast, lst));
else if (iscase(ast, lst) == 3)
return (add_branch(ast, lst));
else if (issubshell(ast, lst))
return (add_subshell_cmd(ast, lst));
else if (isfunc(ast, lst))
return (add_func_cmd(ast, lst));
else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN
&& node->type != TK_PAREN_CLOSE && node->type != CMD
&& node->type != REDIR)
return (add_cmd(&(*ast)->right, lst));
node = (*ast)->item;
if (token->type != TK_WORD)
node->type = token->type;
else
node->type = CMD;
if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD)
{
DG("add data");
if ((my_tab = (char **)malloc(sizeof(char *) * 4)))
{
my_tab[0] = ft_strdup(token->data);
my_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3);
my_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3);
my_tab[3] = NULL;
}
ft_ld_pushback(&node->data.cmd.token, my_tab);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 22:17:14 by ariard #+# #+# */
/* Updated: 2017/03/04 22:05:26 by ariard ### ########.fr */
/* Updated: 2017/03/05 15:51:04 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,7 +43,7 @@ int isloop_condition(t_btree **ast, t_list **lst)
if (*ast)
{
node = (*ast)->item;
if (node->type == TK_FOR && token->type == TK_WORD
if (node->type == TK_FOR && (token->type == TK_WORD || token->type == TK_NAME)
&& node->pattern == 0)
return (1);
}
@ -94,7 +94,8 @@ int add_loop_condition(t_btree **ast, t_list **lst)
token = (*lst)->content;
node = (*ast)->item;
ft_lsteadd(&node->data.wordlist, ft_lstnew(ft_strdup(token->data),
DG("add word");
ft_lsteadd(&node->data.cmd.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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/03/04 20:00:46 by ariard ### ########.fr */
/* Updated: 2017/03/05 16:39:29 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,10 @@ int isdir_word(t_btree **ast, t_list **list)
{
node = (*ast)->item;
if (token->type == TK_WORD && node->type == REDIR)
return ((node->type = CMD));
{
node->type = CMD;
return (1);
}
}
return (0);
}
@ -80,7 +83,6 @@ int add_redir_type(t_btree **ast, t_list **lst)
t_token *token;
t_redir redir;
DG("add redir");
if (!*ast)
gen_node(ast);
token = (*lst)->content;

View file

@ -1,5 +1,4 @@
/* ************************************************************************** */
/* */
/* ************************************************************************** */ /* */
/* ::: :::::::: */
/* aggregate_sym.c :+: :+: :+: */
/* +:+ +:+ +:+ */
@ -316,21 +315,21 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state)
i = 0;
head = (*stack)->content;
DG("aggregate head %s && sym %s",
read_state(*head), read_state(*new_sym));
// DG("aggregate head %s && sym %s",
// read_state(*head), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(*head, g_aggrematch[i].under))
{
DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
head = (*stack)->content;
DG("stack after pop: %s", read_state(*head));
// DG("stack after pop: %s", read_state(*head));
}
if (eval_sym(stack, *new_sym))
return ((*state = ERROR));

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/03/04 20:56:42 by ariard ### ########.fr */
/* Updated: 2017/03/05 15:04:14 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,6 +41,7 @@ t_treematch g_treematch[] =
{TK_PAREN_OPEN, &add_cmd},
{TK_PAREN_CLOSE, &add_cmd},
{TK_FOR, &add_cmd},
{TK_NAME, &add_cmd},
{TK_ASSIGNEMENT_WORD, &add_cmd},
{SUBSHELL, &add_cmd},
{TK_LBRACE, &add_cmd},

View file

@ -1027,7 +1027,7 @@ int eval_sym(t_list **stack, t_sym new_sym)
int i;
head = (*stack)->content;
DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{

View file

@ -37,12 +37,9 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
while (*token)
{
produce_sym(&parser->stack, parser->new_sym, token);
DG("new sym %s", read_state(*parser->new_sym));
// DG("new sym %s", read_state(*parser->new_sym));
if (eval_sym(&parser->stack, *parser->new_sym))
{
DG("ERRROR");
return ((parser->state = ERROR));
}
else
{
aggregate_sym(&parser->stack, parser->new_sym, &parser->state);
@ -55,7 +52,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
else
parser->state = UNDEFINED;
build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast);
btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token);
else

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 19:12:44 by ariard #+# #+# */
/* Updated: 2017/03/03 14:28:02 by ariard ### ########.fr */
/* Updated: 2017/03/05 16:28:52 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,6 @@ int pop_stack(t_list **stack, t_sym erase_sym)
t_sym *head;
t_list *temp;
DG("pop until :%s", read_state(erase_sym));
head = (*stack)->content;
while ((*stack) && *head != erase_sym)
{
@ -29,12 +28,5 @@ int pop_stack(t_list **stack, t_sym erase_sym)
temp = *stack;
(*stack) = (*stack)->next;
ft_lstdelone(&temp, NULL);
/*
DG("pop until :%s", read_state(erase_sym));
temp = *stack;
while (*temp != erase_sym)
*temp-- = 0;
*temp-- = 0;
*stack = temp;
*/ return (0);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/03/03 18:43:13 by ariard ### ########.fr */
/* Updated: 2017/03/05 15:12:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -107,8 +107,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
token = (*lst)->content;
head = (*stack)->content;
DG("produce stack : %s && token : %s", read_state(*head),
read_state(token->type));
// DG("produce stack : %s && token : %s", read_state(*head),
// read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)
@ -116,7 +116,7 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
if (token->type == g_prodmatch[i].token
&& *head == 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;
}
i++;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:57:44 by ariard #+# #+# */
/* Updated: 2017/03/04 21:20:16 by ariard ### ########.fr */
/* Updated: 2017/03/05 15:19:25 by ariard ### ########.fr */
/* */
/* ************************************************************************** */