From 4849c899e4f6a238b259c9fccf8a25fbb25b3e17 Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Mon, 6 Mar 2017 01:10:54 +0100 Subject: [PATCH 1/2] execution for without expansion --- 42sh/sample/for/for.sh | 6 +++++- 42sh/src/exec/exec_for.c | 9 +++++---- 42sh/src/exec/ft_exec.c | 1 + 42sh/src/lexer/get_state_global.c | 1 - 42sh/src/parser/add_number.c | 15 ++++++++++++--- 42sh/src/parser/add_redir.c | 20 ++++++++++++++++---- 42sh/src/parser/aggregate_sym.c | 8 ++++---- 42sh/src/parser/eval_sym.c | 2 +- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/produce_sym.c | 6 +++--- 10 files changed, 48 insertions(+), 22 deletions(-) diff --git a/42sh/sample/for/for.sh b/42sh/sample/for/for.sh index 9d942c9e..d381c710 100644 --- a/42sh/sample/for/for.sh +++ b/42sh/sample/for/for.sh @@ -1,4 +1,8 @@ for i in hello bonjour salut comment do - ls + while cat efezf + do + echo INSIDE + done + ls | cat done diff --git a/42sh/src/exec/exec_for.c b/42sh/src/exec/exec_for.c index 270e12e2..91606fe6 100644 --- a/42sh/src/exec/exec_for.c +++ b/42sh/src/exec/exec_for.c @@ -22,11 +22,12 @@ int exec_for(t_btree **ast) node = (*ast)->item; temp = node->data.cmd.wordlist; var = temp->content; - builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var); + temp = temp->next; +// declare error bad identifier while (temp) - { - //process expansion av = token_to_char(temp->content) - builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var); + { + builtin_setenv("setenv", (char*[]){var, temp->content, 0}, + data_singleton()->local_var); ft_exec(&(*ast)->right); temp = temp->next; } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index ae3234c0..8f30eb66 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -25,6 +25,7 @@ t_execmap g_execmap[] = {TK_ELIF, &exec_elif}, {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_until}, + {TK_FOR, &exec_for}, /* {TK_SUBSHELL, &exec_}, */ {CMD, &exec_cmd}, {0, 0}, diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index d6af9653..b49264f9 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -17,7 +17,6 @@ t_lexstate get_state_global(t_lexer *lexer) char c; c = lexer->str[lexer->pos]; - DG("check, c=%c", lexer->str[lexer->pos]); if (ft_is_delim(c)) return (DELIM); else if (c == '#') diff --git a/42sh/src/parser/add_number.c b/42sh/src/parser/add_number.c index 84ffe8ca..f2c6d3e0 100644 --- a/42sh/src/parser/add_number.c +++ b/42sh/src/parser/add_number.c @@ -15,11 +15,20 @@ int isionumber(t_btree **ast, t_list **lst) { t_token *token; + t_astnode *node; - (void)ast; token = (*lst)->content; - if (token->type == TK_IO_NUMBER) - return (1); + if (*ast) + { + node = (*ast)->item; + if (node->type == CMD && token->type == TK_IO_NUMBER) + return (1); + } + if (!*ast) + { + if (token->type == TK_IO_NUMBER) + return (1); + } return (0); } diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 46d89b71..78cce0f4 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -15,13 +15,25 @@ int isdir_sep(t_btree **ast, t_list **list) { t_token *token; + t_astnode *node; - (void)ast; token = (*list)->content; - if (token->type == TK_LESS || token->type == TK_GREAT - || token->type == TK_GREATAND || token->type == TK_LESSAND - || token->type == TK_DLESS || token->type == TK_DGREAT) + if (*ast) + { + node = (*ast)->item; + if ((node->type == CMD || node->type == TK_IO_NUMBER) && + (token->type == TK_LESS || token->type == TK_GREAT + || token->type == TK_GREATAND || token->type == TK_LESSAND + || token->type == TK_DLESS || token->type == TK_DGREAT)) + return (1); + } + if (!*ast) + { + if (token->type == TK_LESS || token->type == TK_GREAT + || token->type == TK_GREATAND || token->type == TK_LESSAND + || token->type == TK_DLESS || token->type == TK_DGREAT) return (1); + } return (0); } diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index fb79eb04..a0fdff7a 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -315,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/eval_sym.c b/42sh/src/parser/eval_sym.c index 6a5f0724..9acbc37a 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 b959f9c5..cd00ce9a 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -37,7 +37,7 @@ 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)) return ((parser->state = ERROR)); else diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index c2302123..bea5760b 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -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++; From 2b5b23e98146277ec67c2cc17e701fd7ea3a0b1c Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Mon, 6 Mar 2017 02:11:03 +0100 Subject: [PATCH 2/2] execution case without extension --- 42sh/sample/case/case.sh | 5 +++-- 42sh/src/exec/exec_case.c | 18 ++++++++---------- 42sh/src/exec/exec_case_branch.c | 24 +++++++++++++----------- 42sh/src/exec/ft_exec.c | 2 ++ 42sh/src/main/ft_putast.c | 2 ++ 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_case.c | 1 + 42sh/src/parser/add_cmd.c | 6 ++++-- 42sh/src/parser/add_condition.c | 1 - 42sh/src/parser/aggregate_sym.c | 9 +++++---- 42sh/src/parser/eval_sym.c | 5 +++-- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/produce_sym.c | 6 +++--- 13 files changed, 46 insertions(+), 37 deletions(-) diff --git a/42sh/sample/case/case.sh b/42sh/sample/case/case.sh index 0f288357..f41470f9 100644 --- a/42sh/sample/case/case.sh +++ b/42sh/sample/case/case.sh @@ -1,4 +1,5 @@ -case $rental in +case van in ( "bus" ) echo Hello world ;; - ( "van" ) echo Comment va ? ;; + ( "velo" ) echo Comment va ;; + ( "van" ) ls ;; esac diff --git a/42sh/src/exec/exec_case.c b/42sh/src/exec/exec_case.c index b200ab15..b167b905 100644 --- a/42sh/src/exec/exec_case.c +++ b/42sh/src/exec/exec_case.c @@ -14,19 +14,17 @@ int exec_case(t_btree **ast) { -// t_astnode *node; - /* char **av; */ - t_exec *exec; + t_astnode *node; + char *av; + t_exec *exec; (void)ast; - return (0); exec = &data_singleton()->exec; - /* data_singleton()->exec.process.case_branch = 0; */ - exec->attrs |= EXEC_CASE_BRANCH; + exec->attrs &= ~EXEC_CASE_BRANCH; -// node = (*ast)->item; - /* av = token_to_argv(node); */ - /* data_singleton()->exec.process.case_pattern = av[0]; */ - /* exec->case_pattern = av[0]; */ + node = (*ast)->item; + av = node->data.str; +// av = token_to_argv(node); + exec->case_pattern = av; return (0); } diff --git a/42sh/src/exec/exec_case_branch.c b/42sh/src/exec/exec_case_branch.c index 32652b44..ae8308c2 100644 --- a/42sh/src/exec/exec_case_branch.c +++ b/42sh/src/exec/exec_case_branch.c @@ -14,22 +14,24 @@ int exec_case_branch(t_btree **ast) { -// t_astnode *node; - /* char **av; */ + t_astnode *node; + char *av; t_exec *exec; (void)ast; - return (0); exec = &data_singleton()->exec; - /* if (data_singleton()->exec.process.case_branch == 1) */ + + DG("case"); if (EXEC_IS_CASE_BRANCH(exec->attrs)) return (0); -// node = (*ast)->item; - /* av = token_to_argv(node); */ - /* if (ft_strcmp(av[0], data_singleton()->exec.process.case_pattern) == 1) */ - /* { */ - /* data_singleton()->exec.process.case_branch = 1; */ - /* ft_exec(&(*ast)->right); */ - /* } */ + DG("case2"); + node = (*ast)->item; + av = node->data.str; +// av = token_to_argv(node); + if (ft_strcmp(av, exec->case_pattern) == 0) + { + exec->attrs |= EXEC_CASE_BRANCH; + ft_exec(&(*ast)->right); + } return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 8f30eb66..3cd63335 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -26,6 +26,8 @@ t_execmap g_execmap[] = {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_until}, {TK_FOR, &exec_for}, + {TK_CASE, &exec_case}, + {TK_PAREN_OPEN, &exec_case_branch}, /* {TK_SUBSHELL, &exec_}, */ {CMD, &exec_cmd}, {0, 0}, diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 25fc6457..955f3da4 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -17,6 +17,8 @@ char *ft_putast(void *nodein) t_astnode *node; node = nodein; + if (node->type == TK_DSEMI) + return ("TK_DSEMI"); if (node->type == WORDLIST) return ("WORDLIST"); if (node->type == CMD) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 47fed25d..875a3cf7 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -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 74ab538e..077688d1 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -34,6 +34,7 @@ int iscase_pattern(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; + DG(" add pattern"); node = NULL; token = (*lst)->content; if (*ast) diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 4b42a3dc..f0bda953 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -39,7 +39,8 @@ int superflous_token(t_btree **ast, t_list **lst) if (*lst) { token = (*lst)->content; - if (token->type == TK_IN || token->type == TK_PAREN_OPEN) + if (token->type == TK_IN || token->type == TK_PAREN_CLOSE + || token->type == TK_DSEMI) return (1); } return (0); @@ -54,7 +55,7 @@ static int no_del_token(t_btree **ast, t_list **lst) if (*ast) { node = (*ast)->item; - if (node->type != TK_DO && node->type != TK_THEN && node->type != TK_PAREN_CLOSE + if (node->type != TK_DO && node->type != TK_THEN && node->type != CMD && node->type != REDIR) return (1); } @@ -91,6 +92,7 @@ int add_cmd(t_btree **ast, t_list **lst) node->type = CMD; if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) { + DG("add cmd default"); if ((my_tab = (char **)malloc(sizeof(char *) * 4))) { my_tab[0] = ft_strdup(token->data); diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index 6dd90ac6..44bf623b 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -19,7 +19,6 @@ int iscondition(t_btree **ast, t_list **lst) node = NULL; // token = (*lst)->content; - DG("iscondition"); if (*ast) { node = (*ast)->item; diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index a0fdff7a..93cfc4fe 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -18,6 +18,7 @@ t_aggrematch g_aggrematch[] = {TK_WORD, TK_IN, FOR_WORDLIST, TK_IN}, {TK_WORD, FOR_WORDLIST, FOR_WORDLIST, FOR_WORDLIST}, {TK_SEMI, FOR_WORDLIST, SEQUENTIAL_SEP, 0}, + {TK_DSEMI, CMD_SUPERIOR, PIPE_SEQUENCE, CMD_SUPERIOR}, {TK_DSEMI, PIPE_SEMI_SEQUENCE, PIPE_SEQUENCE, PIPE_SEMI_SEQUENCE}, {TK_PAREN_OPEN, TK_IN, PATTERN_CASE, 0}, {TK_PAREN_OPEN, CASE_LIST_NS, PATTERN_CASE, 0}, @@ -315,21 +316,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/eval_sym.c b/42sh/src/parser/eval_sym.c index 9acbc37a..af346da6 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -50,6 +50,7 @@ t_stackmatch g_stackmatch[] = {TK_DSEMI, COMPLETE_COMMANDS}, {TK_DSEMI, COMPOUND_LIST}, {TK_DSEMI, CASE_LIST_NS}, + {TK_DSEMI, CMD_SUPERIOR}, {TK_DSEMI, PIPE_SEMI_SEQUENCE}, {TK_DSEMI, SEQUENCE}, // watch! @@ -853,7 +854,7 @@ t_stackmatch g_stackmatch[] = {COMPOUND_LIST, TK_ELSE}, {COMPOUND_LIST, COMPOUND_LIST}, {COMPOUND_LIST, COMPLETE_CONDITION}, - {CLOSE_LIST, PATTERN_CASE}, + {CLOSE_LIST, PATTERN}, {CLOSE_LIST, FUNC_NAME}, {SUBSHELL, LINEBREAK}, {SUBSHELL, COMPLETE_COMMANDS}, @@ -1027,7 +1028,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 cd00ce9a..59c0d7b6 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -52,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/produce_sym.c b/42sh/src/parser/produce_sym.c index bea5760b..c2302123 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -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++;