diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index da36d5d1..d654f75b 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -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; }; diff --git a/42sh/sample/for/for.sh b/42sh/sample/for/for.sh index 90cfffe6..9d942c9e 100644 --- a/42sh/sample/for/for.sh +++ b/42sh/sample/for/for.sh @@ -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 diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index efee8bea..c4b1a6f8 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/42sh/src/exec/exec_for.c b/42sh/src/exec/exec_for.c index eba6c713..270e12e2 100644 --- a/42sh/src/exec/exec_for.c +++ b/42sh/src/exec/exec_for.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index a9d2b823..cb734fb5 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_paren.c b/42sh/src/lexer/lexer_paren.c index e80b6a1a..bb517adf 100644 --- a/42sh/src/lexer/lexer_paren.c +++ b/42sh/src/lexer/lexer_paren.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index d2f3fd6e..25fc6457 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 256f42f2..0dbee69a 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index a6fb19a5..8955d666 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 691cfd9b..4b42a3dc 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/42sh/src/parser/add_cmd2.c b/42sh/src/parser/add_cmd2.c deleted file mode 100644 index 5f99ca94..00000000 --- a/42sh/src/parser/add_cmd2.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* add_cmd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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); -} diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index 5c39e299..fc775e97 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 6e7eb442..947bd8d5 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 03ae7a56..fb79eb04 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -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)); diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index 1848f810..0ebefed7 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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}, diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 9acbc37a..6a5f0724 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -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) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index c0d40cd6..b959f9c5 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -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 diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c index eea77345..b2b8c9d7 100644 --- a/42sh/src/parser/pop_stack.c +++ b/42sh/src/parser/pop_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 717c15ca..c2302123 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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++; diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index 07be0e9c..d85ce201 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */