86 lines
2.9 KiB
C
86 lines
2.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* arith.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */
|
|
/* Updated: 2024/07/27 22:58:45 by rparodi ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "./arith.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);
|
|
if (op == ARITH_MOD)
|
|
return (*out = _binary_op_mod, 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)));
|
|
}
|
|
|
|
/// 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);
|
|
}
|