expr math doing

This commit is contained in:
Antoine Riard 2017-03-07 12:22:05 +01:00
parent 6c8c3ea792
commit 14be331b4e
12 changed files with 98 additions and 13 deletions

View file

@ -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\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

@ -1 +1 @@
Subproject commit dc155bf51cc9de83df073669a1d2a9a915f16121
Subproject commit 318efc7cfb7b7cc9d3714fa19fd2be7382b6adec

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

81
42sh/src/exec/exec_math.c Normal file
View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_math.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_while.c :+: :+: :+: */
/* exec_var.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)))

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */