diff --git a/exec/src/arith/arith.c b/exec/src/arith/arith.c index 5da48e62..90a6447f 100644 --- a/exec/src/arith/arith.c +++ b/exec/src/arith/arith.c @@ -6,11 +6,12 @@ /* 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 "me/types.h" /// ADD OPERATOR STUFF 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))); } +// 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 /// the from node needs to change /// 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); } +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_state *state, t_i64 *out) { diff --git a/exec/src/arith/arith_operation.c b/exec/src/arith/arith_operation.c index 43a94a9c..2afcea9f 100644 --- a/exec/src/arith/arith_operation.c +++ b/exec/src/arith/arith_operation.c @@ -6,7 +6,7 @@ /* 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)) return (ERROR); *out = lhs + rhs; - if (*out - lhs != rhs || *out - rhs != lhs) - return (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)) return (ERROR); *out = lhs - rhs; - if (*out + lhs != rhs || *out + rhs != lhs) - return (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)) return (ERROR); *out = lhs * rhs; - if (*out / lhs != rhs || *out / rhs != lhs) - return (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); if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs)) return (ERROR); - *out = lhs / rhs; - if (*out * rhs != lhs) + if (rhs == 0) return (ERROR); + *out = lhs / rhs; return (NO_ERROR); } @@ -96,8 +90,8 @@ t_error _binary_op_mod(t_ast_node self, t_state *state, t_i64 *out) return (ERROR); if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs)) return (ERROR); - *out = lhs % rhs; - if (*out > rhs) + if (rhs == 0) return (ERROR); + *out = lhs % rhs; return (NO_ERROR); }