Corrected some stuff on arith
This commit is contained in:
parent
19c35d8c9f
commit
cb11be6f61
2 changed files with 41 additions and 12 deletions
|
|
@ -6,11 +6,12 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */
|
/* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/07/27 22:58:45 by rparodi ### ########.fr */
|
/* Updated: 2024/07/28 14:44:24 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "./arith.h"
|
#include "./arith.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
|
||||||
/// ADD OPERATOR STUFF
|
/// ADD OPERATOR STUFF
|
||||||
t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out)
|
t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out)
|
||||||
|
|
@ -54,6 +55,26 @@ t_ast_node _arith_binary_to_ast_node(t_ast_arithmetic_binary *self)
|
||||||
struct s_ast_node, data.arithmetic_binary)));
|
struct s_ast_node, data.arithmetic_binary)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is black magic don't worry
|
||||||
|
t_ast_node _arith_literal_to_ast_node(t_ast_arithmetic_literal *self)
|
||||||
|
{
|
||||||
|
t_u8 *ptr;
|
||||||
|
|
||||||
|
ptr = (void *)(self);
|
||||||
|
return ((void *)(ptr - offsetof(\
|
||||||
|
struct s_ast_node, data.arithmetic_literal)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is black magic don't worry
|
||||||
|
t_ast_node _arith_expansion_to_ast_node(t_ast_arithmetic_expansion *self)
|
||||||
|
{
|
||||||
|
t_u8 *ptr;
|
||||||
|
|
||||||
|
ptr = (void *)(self);
|
||||||
|
return ((void *)(ptr \
|
||||||
|
- offsetof(struct s_ast_node, data.arithmetic_expansion)));
|
||||||
|
}
|
||||||
|
|
||||||
/// AFTER 65
|
/// AFTER 65
|
||||||
/// the from node needs to change
|
/// the from node needs to change
|
||||||
/// if they find a raw_string, it is a variable expansion,
|
/// if they find a raw_string, it is a variable expansion,
|
||||||
|
|
@ -71,6 +92,20 @@ t_error run_arithmetic_literal(t_ast_arithmetic_literal *arithmetic_literal, \
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, \
|
||||||
|
t_state *state, t_i64 *out)
|
||||||
|
{
|
||||||
|
t_arith_op_func func;
|
||||||
|
t_i64 ret;
|
||||||
|
|
||||||
|
if (arithmetic_expansion == NULL || state == NULL || out == NULL)
|
||||||
|
return (ERROR);
|
||||||
|
if (_get_node_number(_arith_expansion_to_ast_node(arithmetic_expansion), state, &ret))
|
||||||
|
return (ERROR);
|
||||||
|
*out = ret;
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, \
|
t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, \
|
||||||
t_state *state, t_i64 *out)
|
t_state *state, t_i64 *out)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/27 21:23:07 by rparodi #+# #+# */
|
/* Created: 2024/07/27 21:23:07 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/27 22:54:43 by rparodi ### ########.fr */
|
/* Updated: 2024/07/28 14:37:34 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -25,8 +25,6 @@ t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out)
|
||||||
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = lhs + rhs;
|
*out = lhs + rhs;
|
||||||
if (*out - lhs != rhs || *out - rhs != lhs)
|
|
||||||
return (ERROR);
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,8 +41,6 @@ t_error _binary_op_sub(t_ast_node self, t_state *state, t_i64 *out)
|
||||||
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = lhs - rhs;
|
*out = lhs - rhs;
|
||||||
if (*out + lhs != rhs || *out + rhs != lhs)
|
|
||||||
return (ERROR);
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,8 +57,6 @@ t_error _binary_op_mul(t_ast_node self, t_state *state, t_i64 *out)
|
||||||
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = lhs * rhs;
|
*out = lhs * rhs;
|
||||||
if (*out / lhs != rhs || *out / rhs != lhs)
|
|
||||||
return (ERROR);
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,9 +72,9 @@ t_error _binary_op_div(t_ast_node self, t_state *state, t_i64 *out)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = lhs / rhs;
|
if (rhs == 0)
|
||||||
if (*out * rhs != lhs)
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
|
*out = lhs / rhs;
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,8 +90,8 @@ t_error _binary_op_mod(t_ast_node self, t_state *state, t_i64 *out)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = lhs % rhs;
|
if (rhs == 0)
|
||||||
if (*out > rhs)
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
|
*out = lhs % rhs;
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue