From 7114085b8210b74bdcbf8dd191b2c26e53f7b510 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 30 Aug 2024 18:03:08 +0200 Subject: [PATCH] Normed the arithmetic part --- exec/src/run_arithmetic/_get_op.c | 75 +++++++++++ exec/src/run_arithmetic/_run_arith.c | 82 +++++++++++ exec/src/run_arithmetic/_to_ast_node.c | 62 +++++++++ exec/src/run_arithmetic/arithmetic.c | 179 +------------------------ 4 files changed, 221 insertions(+), 177 deletions(-) create mode 100644 exec/src/run_arithmetic/_get_op.c create mode 100644 exec/src/run_arithmetic/_run_arith.c create mode 100644 exec/src/run_arithmetic/_to_ast_node.c diff --git a/exec/src/run_arithmetic/_get_op.c b/exec/src/run_arithmetic/_get_op.c new file mode 100644 index 00000000..542e3e47 --- /dev/null +++ b/exec/src/run_arithmetic/_get_op.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* _get_op.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/30 17:56:29 by rparodi #+# #+# */ +/* Updated: 2024/08/30 17:58:54 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec/_run_arith.h" +#include "me/types.h" +#include "me/convert/str_to_numbers.h" + +/// ADD OPERATOR STUFF +t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) +{ + if (out == NULL) + return (ERROR); + if (op == ARITH_PLUS) + return (*out = _binary_op_add, NO_ERROR); + if (op == ARITH_MINUS) + return (*out = _binary_op_sub, NO_ERROR); + if (op == ARITH_MULT) + return (*out = _binary_op_mul, NO_ERROR); + if (op == ARITH_DIVIDE) + return (*out = _binary_op_div, NO_ERROR); + return (ERROR); +} + +// t_ast_node _postfix_op_decrement(t_ast_node ) + +t_error _postfix_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) +{ + if (op == ARITH_INCREMENT) + return (*out = _postfix_op_inc, NO_ERROR); + if (op == ARITH_DECREMENT) + return (*out = _postfix_op_dec, NO_ERROR); + return (ERROR); +} + +t_error _unary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) +{ + if (op == ARITH_INCREMENT) + return (*out = _unary_op_plus, NO_ERROR); + if (op == ARITH_DECREMENT) + return (*out = _unary_op_minus, NO_ERROR); + return (ERROR); +} + +t_error _get_node_number(t_ast_node self, t_state *state, t_i64 *out) +{ + if (self == NULL || state == NULL || out == NULL) + return (ERROR); + if (self->kind == AST_ARITHMETIC_LITTERAL) + return (run_arithmetic_literal(\ + &self->data.arithmetic_literal, state, out)); + if (self->kind == AST_ARITHMETIC_BINARY) + return (run_arithmetic_binary(\ + &self->data.arithmetic_binary, state, out)); + return (ERROR); +} + +//NOT FINISH +// this is black magic don't worry +t_ast_node _arith_binary_to_ast_node(t_ast_arithmetic_binary *self) +{ + t_u8 *ptr; + + ptr = (void *)(self); + return ((void *)(ptr - offsetof(\ + struct s_ast_node, data.arithmetic_binary))); +} diff --git a/exec/src/run_arithmetic/_run_arith.c b/exec/src/run_arithmetic/_run_arith.c new file mode 100644 index 00000000..1bc6bcac --- /dev/null +++ b/exec/src/run_arithmetic/_run_arith.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* _run_arith.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/30 18:01:31 by rparodi #+# #+# */ +/* Updated: 2024/08/30 18:02:13 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec/_run_arith.h" +#include "me/types.h" +#include "me/convert/str_to_numbers.h" + +/// AFTER 65 +/// the from node needs to change +/// if they find a raw_string, it is a variable expansion, +/// so create a AST_EXPANSION +/// AFTER 67 +/// probably an variable expansion i guess +t_error run_arithmetic_literal(t_ast_arithmetic_literal *arithmetic_literal, \ + t_state *state, t_i64 *out) +{ + if (arithmetic_literal == NULL || state == NULL || out == NULL) + return (ERROR); + if (arithmetic_literal->value->kind == AST_RAW_STRING) + return (str_to_i64(\ + arithmetic_literal->value->data.raw_string.str, 10, out)); + return (ERROR); +} + +t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, \ + t_state *state, t_i64 *out) +{ + t_arith_op_func func; + + if (arithmetic_binary == NULL || state == NULL || out == NULL) + return (ERROR); + if (_binary_get_op(arithmetic_binary->op, &func)) + return (ERROR); + if (func(_arith_binary_to_ast_node(arithmetic_binary), state, out)) + return (ERROR); + return (NO_ERROR); +} + +t_error run_arithmetic_ternary(t_ast_arithmetic_ternary *arithmetic_ternary, \ + t_state *state, t_i64 *out) +{ + t_i64 cond; + + if (arithmetic_ternary == NULL || state == NULL || out == NULL) + return (ERROR); + if (_get_node_number(arithmetic_ternary->condition, state, &cond)) + return (ERROR); + if (cond != 0) + { + if (_get_node_number(arithmetic_ternary->then, state, out)) + return (ERROR); + } + else + { + if (_get_node_number(arithmetic_ternary->else_, state, out)) + return (ERROR); + } + return (NO_ERROR); +} + +t_error run_arithmetic_postfix( \ +t_ast_arithmetic_postfix *arithmetic_postfix, t_state *state, t_i64 *out) +{ + t_arith_op_func func; + + if (arithmetic_postfix == NULL || state == NULL || out == NULL) + return (ERROR); + if (_postfix_get_op(arithmetic_postfix->op, &func)) + return (ERROR); + if (func(_arith_postfix_to_ast_node(arithmetic_postfix), state, out)) + return (ERROR); + return (NO_ERROR); +} diff --git a/exec/src/run_arithmetic/_to_ast_node.c b/exec/src/run_arithmetic/_to_ast_node.c new file mode 100644 index 00000000..94f94520 --- /dev/null +++ b/exec/src/run_arithmetic/_to_ast_node.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* _to_ast_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/30 17:59:59 by rparodi #+# #+# */ +/* Updated: 2024/08/30 18:01:09 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec/_run_arith.h" +#include "me/types.h" +#include "me/convert/str_to_numbers.h" + +t_ast_node _arith_ternary_to_ast_node(t_ast_arithmetic_ternary *self) +{ + t_u8 *ptr; + + ptr = (void *)(self); + return ((void *)(ptr - offsetof(\ + struct s_ast_node, data.arithmetic_ternary))); +} + +t_ast_node _arith_unary_to_ast_node(t_ast_arithmetic_unary *self) +{ + t_u8 *ptr; + + ptr = (void *)(self); + return ((void *)(ptr - offsetof(\ + struct s_ast_node, data.arithmetic_unary))); +} + +t_ast_node _arith_postfix_to_ast_node(t_ast_arithmetic_postfix *self) +{ + t_u8 *ptr; + + ptr = (void *)(self); + return ((void *)(ptr - offsetof(\ + struct s_ast_node, data.arithmetic_postfix))); +} + +// 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))); +} diff --git a/exec/src/run_arithmetic/arithmetic.c b/exec/src/run_arithmetic/arithmetic.c index b308346f..2a10ddd3 100644 --- a/exec/src/run_arithmetic/arithmetic.c +++ b/exec/src/run_arithmetic/arithmetic.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* arith.c :+: :+: :+: */ +/* arithmetic.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */ -/* Updated: 2024/07/30 17:15:00 by rparodi ### ########.fr */ +/* Updated: 2024/08/30 18:02:38 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,130 +14,6 @@ #include "me/types.h" #include "me/convert/str_to_numbers.h" -/// ADD OPERATOR STUFF -t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) -{ - if (out == NULL) - return (ERROR); - if (op == ARITH_PLUS) - return (*out = _binary_op_add, NO_ERROR); - if (op == ARITH_MINUS) - return (*out = _binary_op_sub, NO_ERROR); - if (op == ARITH_MULT) - return (*out = _binary_op_mul, NO_ERROR); - if (op == ARITH_DIVIDE) - return (*out = _binary_op_div, NO_ERROR); - return (ERROR); -} - -// t_ast_node _postfix_op_decrement(t_ast_node ) - -t_error _postfix_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) -{ - if (op == ARITH_INCREMENT) - return (*out = _postfix_op_inc, NO_ERROR); - if (op == ARITH_DECREMENT) - return (*out = _postfix_op_dec, NO_ERROR); - return (ERROR); -} - -t_error _unary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out) -{ - if (op == ARITH_INCREMENT) - return (*out = _unary_op_plus, NO_ERROR); - if (op == ARITH_DECREMENT) - return (*out = _unary_op_minus, NO_ERROR); - return (ERROR); -} - -//NOT FINISH -t_error _get_node_number(t_ast_node self, t_state *state, t_i64 *out) -{ - if (self == NULL || state == NULL || out == NULL) - return (ERROR); - if (self->kind == AST_ARITHMETIC_LITTERAL) - return (run_arithmetic_literal(\ - &self->data.arithmetic_literal, state, out)); - if (self->kind == AST_ARITHMETIC_BINARY) - return (run_arithmetic_binary(\ - &self->data.arithmetic_binary, state, out)); - return (ERROR); -} - -// this is black magic don't worry -t_ast_node _arith_binary_to_ast_node(t_ast_arithmetic_binary *self) -{ - t_u8 *ptr; - - ptr = (void *)(self); - return ((void *)(ptr - offsetof(\ - struct s_ast_node, data.arithmetic_binary))); -} - -t_ast_node _arith_ternary_to_ast_node(t_ast_arithmetic_ternary *self) -{ - t_u8 *ptr; - - ptr = (void *)(self); - return ((void *)(ptr - offsetof(\ - struct s_ast_node, data.arithmetic_ternary))); -} - -t_ast_node _arith_unary_to_ast_node(t_ast_arithmetic_unary *self) -{ - t_u8 *ptr; - - ptr = (void *)(self); - return ((void *)(ptr - offsetof(\ - struct s_ast_node, data.arithmetic_unary))); -} - -t_ast_node _arith_postfix_to_ast_node(t_ast_arithmetic_postfix *self) -{ - t_u8 *ptr; - - ptr = (void *)(self); - return ((void *)(ptr - offsetof(\ - struct s_ast_node, data.arithmetic_postfix))); -} - -// 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, -/// so create a AST_EXPANSION -/// AFTER 67 -/// probably an variable expansion i guess -t_error run_arithmetic_literal(t_ast_arithmetic_literal *arithmetic_literal, \ - t_state *state, t_i64 *out) -{ - if (arithmetic_literal == NULL || state == NULL || out == NULL) - return (ERROR); - if (arithmetic_literal->value->kind == AST_RAW_STRING) - return (str_to_i64(\ - arithmetic_literal->value->data.raw_string.str, 10, out)); - return (ERROR); -} - /* t_error run_arithmetic_expansion( \ t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out) @@ -154,57 +30,6 @@ t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out) return (NO_ERROR); } */ - -t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, \ - t_state *state, t_i64 *out) -{ - t_arith_op_func func; - - if (arithmetic_binary == NULL || state == NULL || out == NULL) - return (ERROR); - if (_binary_get_op(arithmetic_binary->op, &func)) - return (ERROR); - if (func(_arith_binary_to_ast_node(arithmetic_binary), state, out)) - return (ERROR); - return (NO_ERROR); -} - -t_error run_arithmetic_ternary(t_ast_arithmetic_ternary *arithmetic_ternary, \ - t_state *state, t_i64 *out) -{ - t_i64 cond; - - if (arithmetic_ternary == NULL || state == NULL || out == NULL) - return (ERROR); - if (_get_node_number(arithmetic_ternary->condition, state, &cond)) - return (ERROR); - if (cond != 0) - { - if (_get_node_number(arithmetic_ternary->then, state, out)) - return (ERROR); - } - else - { - if (_get_node_number(arithmetic_ternary->else_, state, out)) - return (ERROR); - } - return (NO_ERROR); -} - -t_error run_arithmetic_postfix( \ -t_ast_arithmetic_postfix *arithmetic_postfix, t_state *state, t_i64 *out) -{ - t_arith_op_func func; - - if (arithmetic_postfix == NULL || state == NULL || out == NULL) - return (ERROR); - if (_postfix_get_op(arithmetic_postfix->op, &func)) - return (ERROR); - if (func(_arith_postfix_to_ast_node(arithmetic_postfix), state, out)) - return (ERROR); - return (NO_ERROR); -} - t_error run_arithmetic_unary( \ t_ast_arithmetic_unary *arithmetic_unary, t_state *state, t_i64 *out) {