diff --git a/42sh/Makefile b/42sh/Makefile index 83025081..cfce41bd 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -60,6 +60,7 @@ completion/c_terminal.c\ completion/completion.c\ exec/ast_free.c\ exec/bad_fd.c\ +exec/error_badidentifier.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_case.c\ @@ -77,6 +78,7 @@ exec/exec_semi.c\ exec/exec_until.c\ exec/exec_var.c\ exec/exec_while.c\ +exec/exec_math.c\ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ @@ -234,11 +236,12 @@ parser/add_cmd.c\ parser/add_condition.c\ parser/add_func.c\ parser/add_loop.c\ +parser/add_math.c\ +parser/add_number.c\ parser/add_redir.c\ parser/add_sep.c\ parser/add_subshell.c\ parser/add_var.c\ -parser/add_number.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/fi b/42sh/fi deleted file mode 100644 index 7caa40de..00000000 --- a/42sh/fi +++ /dev/null @@ -1,16 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -fi -file -includes -libft -objs -parser_init.c -pdf -sample -src -test_framework.sh -update_makefile.sh diff --git a/42sh/file b/42sh/file deleted file mode 100644 index 9bda8fd6..00000000 --- a/42sh/file +++ /dev/null @@ -1,15 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -file -includes -libft -objs -parser_init.c -pdf -sample -src -test_framework.sh -update_makefile.sh diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 7c9c54a8..1ae71afa 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:04:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:07:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ struct s_exec t_flag attrs; int fdin; t_list *op_stack; - char *case_pattern; + char **case_pattern; int control_count; }; @@ -115,6 +115,7 @@ int exec_var(t_btree **ast); int exec_for(t_btree **ast); int exec_case(t_btree **ast); int exec_case_branch(t_btree **ast); +int exec_math(t_btree **ast); int launch_process(t_process *p); int set_process(t_process *p, t_btree *ast); @@ -146,4 +147,6 @@ char **token_to_argv(t_ld *ld, int do_match); int add_new_job(t_job *job); +int error_badidentifier(char *name); + #endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 74fd80bc..d47b2e0f 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:29:09 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 18:28:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,13 @@ struct s_lexer t_list *heredoc_stack; }; +struct s_rvwords +{ + char *word; + int type; +}; + +extern t_rvwords g_rvwords[]; extern int (*g_lexer[])(t_list **alst, t_lexer *lexer); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 49eb2ddc..69bf088f 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -136,6 +136,7 @@ int iscondition(t_btree **ast, t_list **lst); int iscondition_branch(t_btree **ast, t_list **lst); int issubshell(t_btree **ast, t_list **lst); int isfunc(t_btree **ast, t_list **lst); +int isfunc_name(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); @@ -143,6 +144,8 @@ int isdir_word(t_btree **ast, t_list **list); int isvar(t_btree **ast, t_list **list); int isnull(t_btree **ast, t_list **list); int isionumber(t_btree **ast, t_list **lst); +int ismath(t_btree **ast, t_list **lst); +int ismath_expr(t_btree **ast, t_list **lst); int join_ast(t_btree **ast, t_btree **new_node); int gen_node(t_btree **ast); @@ -168,7 +171,7 @@ struct s_cmd { t_list *redir; t_ld *token; - t_list *wordlist; + t_ld *wordlist; }; union u_astdata diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 237c8902..792363f9 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:35:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:08:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ typedef enum e_mode t_mode; typedef struct s_lexer t_lexer; typedef enum e_lexstate t_lexstate; typedef struct s_token t_token; +typedef struct s_rvwords t_rvwords; typedef struct s_ld t_ld; typedef struct s_astnode t_astnode; @@ -159,6 +160,9 @@ enum e_sym PATTERN_CASE, FUNC_NAME, CLOSE_LIST, + MATH, + MATH_PLUS, + MATH_SUP, REDIR, CMD, ALL = 200, diff --git a/42sh/sample/func/func01.sh b/42sh/sample/func/func01.sh index d559c7d2..b60f425b 100644 --- a/42sh/sample/func/func01.sh +++ b/42sh/sample/func/func01.sh @@ -1,6 +1,3 @@ -hello() { - yolo() { - echo bonjour - } -} - +hello () ( + echo HELLO +) diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 62700b5a..f2e70ab1 100644 --- a/42sh/src/builtin/bt_read_get.c +++ b/42sh/src/builtin/bt_read_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:58:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 11:27:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 100cd236..9458300d 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:07:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 11:29:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 3c803bd5..d5d5f99f 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:18:25 by gwojda ### ########.fr */ +/* Updated: 2017/03/07 11:28:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 5a9577f8..7836b7aa 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:43:50 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:08:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ t_stof g_builtin[] = {"bg", &builtin_bg}, {"read", &builtin_read}, {"hash", &builtin_hash}, + {"history", &builtin_history}, {NULL, NULL}, }; diff --git a/42sh/src/exec/error_badidentifier.c b/42sh/src/exec/error_badidentifier.c new file mode 100644 index 00000000..9883f60d --- /dev/null +++ b/42sh/src/exec/error_badidentifier.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_badidentifier.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/06 18:03:51 by ariard #+# #+# */ +/* Updated: 2017/03/06 18:11:26 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int error_badidentifier(char *name) +{ + ft_putstr_fd("`", 2); + ft_putstr(name); + ft_putstr_fd("' not a valid identifier", 2); + return (1); +} diff --git a/42sh/src/exec/exec_case.c b/42sh/src/exec/exec_case.c index b167b905..ed982dac 100644 --- a/42sh/src/exec/exec_case.c +++ b/42sh/src/exec/exec_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/05 15:29:22 by wescande ### ########.fr */ +/* Updated: 2017/03/06 19:37:20 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ int exec_case(t_btree **ast) { t_astnode *node; - char *av; + char **av; t_exec *exec; (void)ast; @@ -23,8 +23,7 @@ int exec_case(t_btree **ast) exec->attrs &= ~EXEC_CASE_BRANCH; node = (*ast)->item; - av = node->data.str; -// av = token_to_argv(node); + av = token_to_argv(node->data.cmd.wordlist, 1); 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 ae8308c2..2d0db420 100644 --- a/42sh/src/exec/exec_case_branch.c +++ b/42sh/src/exec/exec_case_branch.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/05 15:29:46 by wescande ### ########.fr */ +/* Updated: 2017/03/06 19:36:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,23 +15,25 @@ int exec_case_branch(t_btree **ast) { t_astnode *node; - char *av; - t_exec *exec; + char **av; + t_exec *exec; + int i; - (void)ast; exec = &data_singleton()->exec; - - DG("case"); if (EXEC_IS_CASE_BRANCH(exec->attrs)) return (0); - 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); - } + av = token_to_argv(node->data.cmd.wordlist, 1); + i = 0; + while (av[i]) + { + if (ft_strcmp(av[i], ((char **)exec->case_pattern)[0]) == 0) + { + exec->attrs |= EXEC_CASE_BRANCH; + ft_exec(&(*ast)->right); + break ; + } + i++; + } return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 53bf4591..e3179079 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:04:00 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:09:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_for.c b/42sh/src/exec/exec_for.c index 91606fe6..632f5773 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/05 15:22:49 by ariard ### ########.fr */ +/* Updated: 2017/03/06 19:37:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,20 +15,28 @@ int exec_for(t_btree **ast) { t_astnode *node; - t_list *temp; -// char **av = NULL; + t_ld *temp; + char **av; char *var; + int i; node = (*ast)->item; temp = node->data.cmd.wordlist; - var = temp->content; + var = ((char **)(temp->content))[0]; + if (ft_isdigit(var[0])) + return (error_badidentifier(var)); temp = temp->next; -// declare error bad identifier while (temp) - { - builtin_setenv("setenv", (char*[]){var, temp->content, 0}, - data_singleton()->local_var); - ft_exec(&(*ast)->right); + { + i = 0; + av = token_to_argv(temp, 1); + while (av[i]) + { + builtin_setenv("setenv", (char*[]){var, av[i], 0}, + data_singleton()->local_var); + ft_exec(&(*ast)->right); + i++; + } temp = temp->next; } return (0); diff --git a/42sh/src/exec/exec_math.c b/42sh/src/exec/exec_math.c new file mode 100644 index 00000000..e157ef4c --- /dev/null +++ b/42sh/src/exec/exec_math.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_math.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ +/* Updated: 2017/03/07 13:42:11 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +static int get_math(char *stream, char **var, char **value, char **operator) +{ + char *temp; + + *var = ft_strduptr(stream, &ft_isalpha); + temp = ft_sstrstr(data_singleton()->env, *var); + if (temp) + { + temp += ft_strlenchr(temp, '=') + 1; + *value = ft_strdup(temp); + if (!(ft_stris(*value, &ft_isdigit))) + { + ft_strdel(value); + *value = ft_itoa(0); + } + } + else + *value = ft_itoa(0); + stream += ft_strlen(*var); + *operator = ft_strdup(stream); + return (0); +} + +static int do_math(char **value, char *operator) +{ + long ope1; + long ope2; + + ope1 = ft_atoi(*value); + if (operator[2]) + ope2 = ft_atoi(&operator[2]); + else + ope2 = 0; + if (operator[0] == '/' && ope2 == 0) + ope1 = 0; + else + { + ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; + ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1; + ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1; + ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1; + ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1; + } + ft_strdel(value); + *value = ft_itoa(ope1); + return (0); +} + +int exec_math(t_btree **ast) +{ + t_astnode *node; + char **av; + char *var; + char *value; + char *operator; + + node = (*ast)->item; + av = token_to_argv(node->data.cmd.wordlist, 1); + get_math(av[0], &var, &value, &operator); + do_math(&value, operator); + builtin_setenv("setenv", (char *[]){var, value, 0}, data_singleton()->local_var); + return (0); +} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 5f03aa23..c7848424 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/04 17:23:50 by ariard ### ########.fr */ +/* Updated: 2017/03/06 18:09:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 3952d5e6..b01c2f39 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -1,24 +1,35 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_while.c :+: :+: :+: */ +/* exec_var.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/03/03 20:32:27 by wescande ### ########.fr */ +/* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ +/* Updated: 2017/03/07 14:40:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" +static int set_var(char *stream, char **var, char **value) +{ + *var = ft_strdupchr(stream, '='); + stream += ft_strlenchr(stream, '=') + 1; + *value = ft_strdup(stream); + return (0); +} + int exec_var(t_btree **ast) { t_astnode *node; char **av; - + char *var; + char *value; + node = (*ast)->item; - av = token_to_argv(node->data.cmd.token, 0); - builtin_setenv("setenv", av, data_singleton()->local_var); + av = token_to_argv(node->data.cmd.token, 1); + set_var(av[0], &var, &value); + builtin_setenv("setenv", (char*[]){var, value, 0}, data_singleton()->local_var); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index fe167aa0..cc616e6c 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:54:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:08:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ t_itof g_execmap[] = {TK_FOR, &exec_for}, {TK_CASE, &exec_case}, {TK_PAREN_OPEN, &exec_case_branch}, + {TK_ASSIGNEMENT_WORD, &exec_var}, + {MATH, &exec_math}, /* {TK_SUBSHELL, &exec_}, */ {CMD, &exec_cmd}, {0, 0}, diff --git a/42sh/src/exec/set_process.c b/42sh/src/exec/set_process.c index 32a67674..a8e9c708 100644 --- a/42sh/src/exec/set_process.c +++ b/42sh/src/exec/set_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:02:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:09:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/expand_var.c b/42sh/src/glob/expand_var.c index 31453a98..ed163a76 100644 --- a/42sh/src/glob/expand_var.c +++ b/42sh/src/glob/expand_var.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 16:09:27 by wescande #+# #+# */ -/* Updated: 2017/03/03 20:38:36 by wescande ### ########.fr */ +/* Updated: 2017/03/07 12:07:53 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,7 +73,8 @@ void expand_var(t_glob *gl) { if (var && *var) { - content = ft_getenv(data_singleton()->env, var); + if (!(content = ft_getenv(data_singleton()->env, var))) + content = ft_getenv(data_singleton()->local_var, var); insert_var(gl, pat, var, content); pat = gl->pat; } diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 139d4c1f..4d14e54d 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,62 +6,77 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/02/21 21:06:16 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:59:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -int get_reserved_words(t_list **alst) + +t_rvwords g_rvwords[] = +{ + {"while", TK_WHILE}, + {"done", TK_DONE}, + {"do", TK_DO}, + {"if", TK_IF}, + {"then", TK_THEN}, + {"fi", TK_FI}, + {"elif", TK_ELIF}, + {"else", TK_ELSE}, + {"until", TK_UNTIL}, + {"case", TK_CASE}, + {"esac", TK_ESAC}, + {"for", TK_FOR}, + {"null", 0}, +}; + + +static int recognization_rvwords(t_token *pv_tk) +{ + if (!pv_tk || (pv_tk->type == TK_NEWLINE || pv_tk->type == TK_AMP + || pv_tk->type == TK_SEMI || pv_tk->type == TK_WHILE + || pv_tk->type == TK_DONE || pv_tk->type == TK_DO + || pv_tk->type == TK_IF || pv_tk->type == TK_FI || pv_tk->type == TK_THEN + || pv_tk->type == TK_ELIF || pv_tk->type == TK_ELSE)) + return (1); + return (0); +} + +int get_reserved_words(t_list **alst) { t_token *token; - t_token *previous_token; + t_token *pv_tk; t_token *ante_token; t_list *temp; + int i; temp = *alst; - previous_token = NULL; + pv_tk = NULL; ante_token = NULL; while (temp) { token = temp->content; - if (!previous_token || (previous_token->type & RW_SEP)) + //no more comp & + if (recognization_rvwords(pv_tk)) { if (token->type == TK_WORD) { - if (ft_strncmp(token->data, "while", 5) == 0) - token->type = TK_WHILE; - else if (ft_strncmp(token->data, "done", 4) == 0) - token->type = TK_DONE; - else if (ft_strncmp(token->data, "do" , 2) == 0) - token->type = TK_DO; - else if (ft_strncmp(token->data, "if", 2) == 0) - token->type = TK_IF; - else if (ft_strncmp(token->data, "then", 4) == 0) - token->type = TK_THEN; - else if(ft_strncmp(token->data, "fi", 4) == 0) - token->type = TK_FI; - else if (ft_strncmp(token->data, "elif", 4) == 0) - token->type = TK_ELIF; - else if (ft_strncmp(token->data, "else", 4) == 0) - token->type = TK_ELSE; - else if (ft_strncmp(token->data, "until", 5) == 0) - token->type = TK_UNTIL; - else if (ft_strncmp(token->data, "case", 4) == 0) - token->type = TK_CASE; - else if (ft_strncmp(token->data, "esac", 4) == 0) - token->type = TK_ESAC; - else if (ft_strncmp(token->data, "for", 3) == 0) - token->type = TK_FOR; + i = 0; + while (g_rvwords[i].type) + { + if (ft_strcmp(token->data, g_rvwords[i].word) == 0) + token->type = g_rvwords[i].type; + i++; + } } } if (ante_token && (ante_token->type == TK_CASE || ante_token->type == TK_FOR) && ft_strncmp(token->data, "in", 2) == 0) token->type = TK_IN; - if (previous_token && previous_token->type == TK_FOR && token->type == TK_WORD) + if (pv_tk && pv_tk->type == TK_FOR && token->type == TK_WORD) token->type = TK_NAME; - ante_token = previous_token; - previous_token = token; + ante_token = pv_tk; + pv_tk = token; temp = temp->next; } return (0); diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 770d2163..f9bf13df 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/04 16:50:05 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:20:27 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,8 @@ void ft_init_history(void) return ; while (get_next_line(fd, &str) > 0) { + if (!*str) + continue ; ft_push_back_history(&data_singleton()->line.list_beg, ft_create_history_list(str)); free(str); diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 955f3da4..405104b6 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 == MATH) + return ("MATH"); if (node->type == TK_DSEMI) return ("TK_DSEMI"); if (node->type == WORDLIST) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 0e3d9c1c..cf6c3dcc 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/07 14:40:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:09:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,9 +47,9 @@ int handle_instruction(int fd) if (get_lexer_stack(lexer) > 1) continue ; lexer.state = DEFAULT; - token_print(token); if (get_reserved_words(&token)) return (1); + token_print(token); if (insert_newline(&token)) return (1); if (ft_parse(&ast, &token, &parser)) diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index 1a87f15f..4b4d5415 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/06 15:11:04 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:52:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,9 @@ int iscase(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; @@ -80,7 +78,8 @@ int add_case_cmd(t_btree **ast, t_list **lst) else if (token->type == TK_DSEMI && node->type == TK_PAREN_OPEN && node->nest == 0) return ((node->full = 1)); - else if (token->type == TK_ESAC) + else if ((token->type == TK_ESAC || token->type == TK_PAREN_CLOSE) + && node->nest == 0) return (0); return (add_cmd(&(*ast)->right, lst)); } @@ -89,10 +88,19 @@ int add_pattern(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; + char **my_tab; + DG("add pattern"); token = (*lst)->content; node = (*ast)->item; - node->data.str = ft_strdup(token->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.wordlist, my_tab); node->pattern = 1; return (0); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 8a1f255a..0518acf6 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/06 15:57:48 by ariard ### ########.fr */ +/* Updated: 2017/03/06 19:36:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,13 @@ t_distrostree g_distrostree[] = {&iscase_pattern, &add_pattern}, {&iscase_branch, &add_branch}, {&iscase, &add_case_cmd}, + {&ismath, &add_null}, + {&ismath_expr, &add_pattern}, {&issubshell, &add_subshell_cmd}, + {&isfunc_name, &add_null}, {&isfunc, &add_func_cmd}, {&isionumber, &add_ionumber}, {&isnull, &add_null}, - }; int superflous_token(t_btree **ast, t_list **lst) @@ -40,8 +42,7 @@ int superflous_token(t_btree **ast, t_list **lst) if (*lst) { token = (*lst)->content; - if (token->type == TK_IN || token->type == TK_PAREN_CLOSE - || token->type == TK_DSEMI) + if (token->type == TK_IN || token->type == TK_DSEMI) return (1); } return (0); @@ -71,7 +72,6 @@ int add_cmd(t_btree **ast, t_list **lst) int i; i = 0; - DG("add cmd"); while (i < 14) { if (g_distrostree[i].test(ast, lst) == 1) diff --git a/42sh/src/parser/add_condition.c b/42sh/src/parser/add_condition.c index 44bf623b..bc62c84f 100644 --- a/42sh/src/parser/add_condition.c +++ b/42sh/src/parser/add_condition.c @@ -15,10 +15,9 @@ int iscondition(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; + (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index e29c7d8b..cc481f37 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -6,31 +6,47 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 23:43:07 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:27:25 by ariard ### ########.fr */ +/* Updated: 2017/03/07 10:49:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int isfunc(t_btree **ast, t_list **lst) +int isfunc_name(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; - node = NULL; token = (*lst)->content; if (*ast) { node = (*ast)->item; - if (node->type == TK_WORD && token->type == CLOSE_LIST) - { + if (node->type == CMD && token->type == TK_PAREN_OPEN) + { + DG("add func name"); node->type = FNAME; - add_one_func(ast, lst); - } + return (1); + } + if (node->type == FNAME && token->type == TK_PAREN_CLOSE && node->nest == 0) + return (1); + } + return (0); +} + +int isfunc(t_btree **ast, t_list **lst) +{ + t_astnode *node; + + (void)lst; + node = NULL; + if (*ast) + { + node = (*ast)->item; + if ((node->type == TK_NEWLINE || node->type == TK_SEMI + || node->type == TK_AMP) && isfunc(&(*ast)->right, lst) == 1) + return (1); if (node->type == FNAME && node->full == 0) return (1); - if (isfunc(&(*ast)->right, lst) == 1) - return (1); } return (0); } @@ -40,23 +56,25 @@ int add_func_cmd(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; + DG("add func cmd"); token = (*lst)->content; node = (*ast)->item; - if (token->type == CLOSE_LIST && node->nest == 0) - return (0); if ((token->type == TK_CASE || token->type == TK_WHILE || token->type == TK_IF || token->type == TK_UNTIL || token->type == TK_FOR || token->type == SUBSHELL || token->type == TK_LBRACE) && node->type == FNAME) node->nest++; if ((token->type == TK_DONE || token->type == TK_ESAC || token->type == TK_FI - || token->type == TK_RBRACE || token->type == TK_PAREN_OPEN) + || token->type == TK_RBRACE || token->type == TK_PAREN_CLOSE) && node->type == FNAME && node->nest > 0) node->nest--; if ((token->type == TK_DONE || token->type == TK_ESAC || token->type == TK_FI - || token->type == TK_RBRACE || token->type == TK_PAREN_OPEN) + || token->type == TK_RBRACE || token->type == TK_PAREN_CLOSE) && node->type == FNAME && node->nest == 0) + { node->full = 1; + add_one_func(ast, lst); + } return (add_cmd(&(*ast)->right, lst)); } @@ -67,7 +85,11 @@ int add_func_sep(t_btree **ast, t_list **lst) int add_one_func(t_btree **ast, t_list **lst) { + t_btree *func_ast; + (void)lst; - ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(ast, sizeof(*ast))); +// func_ast = btree_map(*ast, &id); + ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast))); + DG("arbre ajoute"); return (0); } diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index 63481be3..427b0741 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/05 16:46:50 by ariard ### ########.fr */ +/* Updated: 2017/03/06 19:06:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,9 @@ int isloop(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; + (void)lst; node = NULL; -// token = (*lst)->content; - DG("isloop"); if (*ast) { node = (*ast)->item; @@ -91,11 +89,18 @@ int add_loop_condition(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; + char **my_tab; token = (*lst)->content; node = (*ast)->item; DG("add word"); - ft_lsteadd(&node->data.cmd.wordlist, ft_lstnew(ft_strdup(token->data), - ft_strlen(token->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.wordlist, my_tab); return (0); } diff --git a/42sh/src/parser/add_math.c b/42sh/src/parser/add_math.c new file mode 100644 index 00000000..78a3f414 --- /dev/null +++ b/42sh/src/parser/add_math.c @@ -0,0 +1,39 @@ +#include "parser.h" + +int ismath(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + node = NULL; + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type == SUBSHELL && token->type == SUBSHELL) + { + node->type = MATH; + return (1); + } + if (node->type == MATH && token->type == TK_PAREN_CLOSE) + return (1); + } + return (0); +} + + +int ismath_expr(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + node = NULL; + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type == MATH && token->type == TK_WORD) + return (1); + } + return (0); +} diff --git a/42sh/src/parser/add_subshell.c b/42sh/src/parser/add_subshell.c index 02618324..80dea8e7 100644 --- a/42sh/src/parser/add_subshell.c +++ b/42sh/src/parser/add_subshell.c @@ -15,18 +15,14 @@ int issubshell(t_btree **ast, t_list **lst) { t_astnode *node; -// t_token *token; (void)lst; node = NULL; -// token = (*lst)->content; if (*ast) { node = (*ast)->item; if (node->type == SUBSHELL && node->full == 0) return (1); - if (node->type == TK_LBRACE && node->full == 0) - return (1); } return (0); } diff --git a/42sh/src/parser/add_var.c b/42sh/src/parser/add_var.c index ad11bf54..661446b4 100644 --- a/42sh/src/parser/add_var.c +++ b/42sh/src/parser/add_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 15:08:16 by ariard #+# #+# */ -/* Updated: 2017/03/03 16:17:27 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:40:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 93cfc4fe..25d6f683 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -17,12 +17,14 @@ t_aggrematch g_aggrematch[] = {TK_WORD, TK_PIPE, PATTERN, 0}, {TK_WORD, TK_IN, FOR_WORDLIST, TK_IN}, {TK_WORD, FOR_WORDLIST, FOR_WORDLIST, FOR_WORDLIST}, + {TK_WORD, MATH, MATH, MATH}, {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}, {TK_PAREN_OPEN, CMD_SUPERIOR, FUNC_NAME, CMD_SUPERIOR}, + {TK_PAREN_OPEN, TK_PAREN_OPEN, MATH, TK_PAREN_OPEN}, {TK_ASSIGNEMENT_WORD, CMD_PREFIX,CMD_PREFIX, 0}, {TK_PIPE, CMD_SUPERIOR, SEQUENCE, CMD_SUPERIOR}, {TK_PIPE, PIPE_SEMI_SEQUENCE, SEQUENCE, PIPE_SEMI_SEQUENCE}, @@ -38,7 +40,9 @@ t_aggrematch g_aggrematch[] = {TK_ESAC, TK_IN, CASE_CLAUSE, TK_CASE}, {TK_ESAC, CASE_LIST_NS, CASE_CLAUSE, TK_CASE}, {TK_RBRACE, COMPOUND_LIST, BRACE_GROUP, TK_LBRACE}, - {TK_PAREN_CLOSE, COMPOUND_LIST, SUBSHELL, TK_PAREN_OPEN}, + {TK_PAREN_CLOSE, MATH, MATH_PLUS, MATH}, + {TK_PAREN_CLOSE, MATH_PLUS, MATH_SUP, MATH_PLUS}, + {TK_PAREN_CLOSE, COMPOUND_LIST, SUBSHELL, TK_PAREN_OPEN}, {TK_PAREN_CLOSE, CMD_SUPERIOR, SUBSHELL, TK_PAREN_OPEN}, {TK_PAREN_CLOSE, PIPE_SEMI_SEQUENCE, SUBSHELL, TK_PAREN_OPEN}, @@ -48,30 +52,34 @@ t_aggrematch g_aggrematch[] = {TK_AND_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR}, {TK_OR_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR}, //watch this + {SEPARATOR_OP, MATH_SUP, CMD_SUPERIOR, MATH_SUP}, {SEPARATOR_OP, CMD_SUPERIOR, SEPARATOR, 0}, {SEPARATOR_OP, COMPOUND_LIST, SEPARATOR, 0}, {SEPARATOR_OP, CASE_LIST_NS, SEPARATOR, 0}, {SEPARATOR_OP, PIPE_SEMI_SEQUENCE, SEPARATOR, 0}, + {SEPARATOR, MATH_SUP, CMD_SUPERIOR, MATH_SUP}, {SEPARATOR, CMD_SUPERIOR, END_COMMAND, CMD_SUPERIOR}, {SEPARATOR, CASE_LIST_NS, CASE_LIST_NS, CASE_LIST_NS}, {SEPARATOR, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST}, {SEPARATOR, PIPE_SEMI_SEQUENCE, PIPE_SEQUENCE, PIPE_SEMI_SEQUENCE}, + {LINEBREAK, MATH_SUP, CMD_SUPERIOR, MATH_SUP}, {LINEBREAK, SEPARATOR_OP, SEPARATOR, SEPARATOR_OP}, {LINEBREAK, TK_SEMI, SEQUENTIAL_SEP, TK_SEMI}, //to abstract TK_ESAC {LINEBREAK, TK_PAREN_CLOSE, FUNC, FNAME}, -//paren open {LINEBREAK, COMPLETE_COMMANDS, PROGRAM, LINEBREAK}, {LINEBREAK, CMD_SUPERIOR, SEPARATOR_OP, 0}, {LINEBREAK, PIPE_SEMI_SEQUENCE, PIPE_SEQUENCE, PIPE_SEMI_SEQUENCE}, {LINEBREAK, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST}, {LINEBREAK, CASE_LIST_NS, CASE_LIST_NS, CASE_LIST_NS}, {LINEBREAK, COMPLETE_COMMANDS, PROGRAM, LINEBREAK}, + {NEWLINE_LIST, MATH_SUP, CMD_SUPERIOR, MATH_SUP}, {NEWLINE_LIST, CMD_NAME, CMD_SUPERIOR, CMD_NAME}, {NEWLINE_LIST, SEQUENTIAL_SEP, SEQUENTIAL_SEP, SEQUENTIAL_SEP}, {NEWLINE_LIST, TK_DO, TK_DO, TK_DO}, {NEWLINE_LIST, TK_PAREN_CLOSE, TK_PAREN_CLOSE, TK_PAREN_CLOSE}, + {NEWLINE_LIST, TK_PAREN_OPEN, TK_PAREN_OPEN, TK_PAREN_OPEN}, {NEWLINE_LIST, TK_IN, TK_IN, TK_IN}, {NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN}, {NEWLINE_LIST, TK_IF, TK_IF, TK_IF}, @@ -316,21 +324,22 @@ 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); + if (pop_stack(stack, g_aggrematch[i].erase_sym)) + return (1); 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 ff3a467b..e8b79714 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -70,12 +70,9 @@ int build_tree(t_btree **ast, t_list **lst) i = 0; token = (*lst)->content; //check bug de cache case ? -// cache = token->type; if (token->type == TK_PAREN_OPEN && cache != TK_IN && cache != TK_DSEMI && cache != TK_WORD) token->type = SUBSHELL; - if (token->type == TK_PAREN_CLOSE && cache == TK_PAREN_OPEN) - token->type = CLOSE_LIST; while (g_treematch[i].type) { if ((isseparator(token, cache) && g_treematch[i].type == token->type)) diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index 408d3afd..d48228c6 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/05 18:23:57 by ariard ### ########.fr */ +/* Updated: 2017/03/06 18:07:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 9dc520c7..dddb3261 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -23,6 +23,7 @@ t_stackmatch g_stackmatch[] = {TK_WORD, COMPLETE_COMMANDS}, {TK_WORD, TK_IN}, {TK_WORD, FOR_WORDLIST}, + {TK_WORD, MATH}, {TK_ASSIGNEMENT_WORD, CMD_PREFIX}, {TK_IO_NUMBER, REDIRECT_LIST}, {TK_IO_NUMBER, CMD_SUFFIX}, @@ -430,17 +431,20 @@ t_stackmatch g_stackmatch[] = {END_COMMAND, COMPOUND_LIST}, {END_COMMAND, CASE_LIST_NS}, {END_COMMAND, COMPLETE_CONDITION}, + {SEPARATOR, MATH_SUP}, {SEPARATOR, CMD_SUPERIOR}, {SEPARATOR, TERM}, {SEPARATOR, COMPOUND_LIST}, {SEPARATOR, CASE_LIST_NS}, {SEPARATOR, PIPE_SEMI_SEQUENCE}, + {SEPARATOR_OP, MATH_SUP}, {SEPARATOR_OP, CMD_SUPERIOR}, {SEPARATOR_OP, LIST}, {SEPARATOR_OP, TERM}, {SEPARATOR_OP, CASE_LIST_NS}, {SEPARATOR_OP, COMPOUND_LIST}, {SEPARATOR_OP, PIPE_SEMI_SEQUENCE}, + {LINEBREAK, MATH_SUP}, {LINEBREAK, TK_SEMI}, {LINEBREAK, END_COMMAND}, {LINEBREAK, SEPARATOR_OP}, @@ -459,6 +463,8 @@ t_stackmatch g_stackmatch[] = {LINEBREAK, PROGRAM}, {LINEBREAK, TK_PAREN_OPEN}, {LINEBREAK, TK_LBRACE}, + {TK_PAREN_CLOSE, MATH}, + {TK_PAREN_CLOSE, MATH_PLUS}, {TK_PAREN_CLOSE, TK_SEMI}, {TK_PAREN_CLOSE, END_COMMAND}, {TK_PAREN_CLOSE, SEPARATOR_OP}, @@ -478,6 +484,7 @@ t_stackmatch g_stackmatch[] = {TK_PAREN_CLOSE, PATTERN}, {TK_PAREN_CLOSE, COMPOUND_LIST}, {TK_PAREN_CLOSE, FUNC_NAME}, + {NEWLINE_LIST, MATH_SUP}, {NEWLINE_LIST, TK_DO}, {NEWLINE_LIST, CASE_LIST_NS}, {NEWLINE_LIST, TK_PAREN_CLOSE}, @@ -628,6 +635,19 @@ t_stackmatch g_stackmatch[] = // watch ! {CMD_SUPERIOR, AND_OR_MAJOR}, + {MATH, LINEBREAK}, + {MATH, NEWLINE_LIST}, + {MATH, SEPARATOR}, + {MATH, SEPARATOR_OP}, + {MATH_PLUS, LINEBREAK}, + {MATH_PLUS, NEWLINE_LIST}, + {MATH_PLUS, SEPARATOR}, + {MATH_PLUS, SEPARATOR_OP}, + {MATH_SUP, LINEBREAK}, + {MATH_SUP, NEWLINE_LIST}, + {MATH_SUP, SEPARATOR}, + {MATH_SUP, SEPARATOR_OP}, + {SIMPLE_COMMAND, TK_WHILE}, {SIMPLE_COMMAND, TK_UNTIL}, {SIMPLE_COMMAND, TK_DO}, @@ -1027,7 +1047,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..f87d0bcb 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -37,12 +37,13 @@ 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 { - aggregate_sym(&parser->stack, parser->new_sym, &parser->state); + if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state)) + return (1); push_stack(&parser->stack, *parser->new_sym); } // ft_read_stack(parser->stack); diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 9d1f40dc..1c884a72 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:00 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:04:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,7 @@ void parser_init(t_parser *parser) { parser->state = SUCCESS; parser->new_sym = ft_memalloc(sizeof(t_sym)); + parser->stack = NULL; push_stack(&parser->stack, TERMINUS); push_stack(&parser->stack, LINEBREAK); -// parser->stack = ft_memalloc(sizeof(t_sym) * 1000); -// push_stack(parser->stack, LINEBREAK); } diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c index b2b8c9d7..1552953c 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/05 16:28:52 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:09:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,19 @@ int pop_stack(t_list **stack, t_sym erase_sym) t_sym *head; t_list *temp; - head = (*stack)->content; - while ((*stack) && *head != erase_sym) + while ((*stack) && *(head = (*stack)->content) != erase_sym) + { + temp = *stack; + (*stack) = (*stack)->next; + ft_lstdelone(&temp, NULL); + } + if (*stack) { temp = *stack; (*stack) = (*stack)->next; ft_lstdelone(&temp, NULL); - head = (*stack)->content; } - temp = *stack; - (*stack) = (*stack)->next; - ft_lstdelone(&temp, NULL); + else + return (1); return (0); } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 4556574f..868c0117 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/07 14:36:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:09:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,6 +64,7 @@ t_prodmatch g_prodmatch[] = {TK_NAME, SEPARATOR_OP, FNAME}, {TK_NAME, NEWLINE_LIST, FNAME}, {TK_NAME, TK_FOR, NAME}, + {TK_NEWLINE, MATH_SUP, NEWLINE_LIST}, {TK_NEWLINE, CASE_LIST_NS, NEWLINE_LIST}, {TK_NEWLINE, TK_DO, NEWLINE_LIST}, {TK_NEWLINE, TK_PAREN_CLOSE, NEWLINE_LIST}, @@ -89,9 +90,11 @@ t_prodmatch g_prodmatch[] = {TK_NEWLINE, CONDITION, NEWLINE_LIST}, {TK_NEWLINE, FOR_WORDLIST, NEWLINE_LIST}, {TK_NEWLINE, SEQUENTIAL_SEP, NEWLINE_LIST}, + {TK_SEMI, MATH_SUP, SEPARATOR_OP}, {TK_SEMI, CMD_SUPERIOR, SEPARATOR_OP}, {TK_SEMI, LIST, SEPARATOR_OP}, {TK_SEMI, PIPE_SEMI_SEQUENCE, SEPARATOR_OP}, + {TK_AMP, MATH_SUP, SEPARATOR_OP}, {TK_AMP, CMD_SUPERIOR, SEPARATOR_OP}, {TK_AMP, LIST, SEPARATOR_OP}, {TK_AMP, PIPE_SEMI_SEQUENCE, SEPARATOR_OP}, @@ -107,8 +110,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 +119,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/read_stack.c b/42sh/src/parser/read_stack.c index d16bac78..0e7450af 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -14,6 +14,8 @@ char *read_state(t_sym current) { + if (current == TERMINUS) + return ("TERMINUS"); if (current == SEQUENCE) return ("SEQUENCE"); if (current == LINEBREAK) diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index ed89c93e..81415d2e 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/05 16:56:00 by ariard ### ########.fr */ +/* Updated: 2017/03/06 19:01:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */