From 14be331b4eef1a7277f5de92bd5af806cab5e969 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 7 Mar 2017 12:22:05 +0100 Subject: [PATCH] expr math doing --- 42sh/Makefile | 1 + 42sh/includes/exec.h | 3 +- 42sh/libft | 2 +- 42sh/src/builtin/bt_read_get.c | 2 +- 42sh/src/builtin/builtin_env.c | 2 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/exec/exec_math.c | 81 +++++++++++++++++++++++++++++++ 42sh/src/exec/exec_var.c | 6 +-- 42sh/src/exec/ft_exec.c | 3 +- 42sh/src/parser/add_case.c | 3 +- 42sh/src/parser/add_func.c | 4 +- 42sh/src/parser/add_var.c | 2 +- 12 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 42sh/src/exec/exec_math.c diff --git a/42sh/Makefile b/42sh/Makefile index fe951535..ce3e375c 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -77,6 +77,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\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index d43904a2..0aacdb69 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/06 19:16:30 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:47:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,6 +116,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); diff --git a/42sh/libft b/42sh/libft index dc155bf5..318efc7c 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit dc155bf51cc9de83df073669a1d2a9a915f16121 +Subproject commit 318efc7cfb7b7cc9d3714fa19fd2be7382b6adec 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/exec/exec_math.c b/42sh/src/exec/exec_math.c new file mode 100644 index 00000000..9bf8a3c7 --- /dev/null +++ b/42sh/src/exec/exec_math.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_math.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ +/* Updated: 2017/03/07 12:19:25 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); + DG("var : %s", var); + DG("value : %s", value); + DG("operator : %s", operator); + do_math(&value, operator); + DG("value : %s", value); + builtin_setenv("setenv", (char *[]){var, value, 0}, data_singleton()->local_var); + return (0); +} diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 3bfc50ce..ac98e0cb 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 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 12:17:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index b24b7612..2a8cc5c3 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/06 18:08:06 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:50:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ t_execmap g_execmap[] = {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/parser/add_case.c b/42sh/src/parser/add_case.c index f5862cef..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 19:36:37 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:52:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,6 +90,7 @@ int add_pattern(t_btree **ast, t_list **lst) t_token *token; char **my_tab; + DG("add pattern"); token = (*lst)->content; node = (*ast)->item; if ((my_tab = (char **)malloc(sizeof(char *) * 4))) diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index ab3e0ae2..cc481f37 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ int add_one_func(t_btree **ast, t_list **lst) t_btree *func_ast; (void)lst; - func_ast = btree_map(*ast, &id); +// 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_var.c b/42sh/src/parser/add_var.c index ad11bf54..db9ce5c2 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 10:47:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */