Adding arithmetic and normed this shit

This commit is contained in:
Raphaël 2024-07-27 21:24:31 +02:00
parent 70958ee4c1
commit 16113d5f51
8 changed files with 189 additions and 86 deletions

View file

@ -6,63 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/26 15:14:50 by maiboyer #+# #+# */
/* Updated: 2024/07/27 14:43:31 by maiboyer ### ########.fr */
/* Updated: 2024/07/27 21:24:11 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "ast/ast.h"
#include "exec/run.h"
#include "me/convert/numbers_to_str.h"
#include "me/convert/str_to_numbers.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/os/pipe.h"
#include "me/os/process.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
#include "me/vec/vec_estr.h"
#include "me/vec/vec_str.h"
#include <stddef.h>
#include <stdio.h>
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-variable"
typedef t_error (*t_arith_op_func)(t_ast_node self, t_state *state, t_i64 *out);
t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, t_state *state, t_i64 *out);
t_error run_arithmetic_literal(t_ast_arithmetic_literal *arithmetic_literal, t_state *state, t_i64 *out);
t_error run_arithmetic_postfix(t_ast_arithmetic_postfix *arithmetic_postfix, t_state *state, t_i64 *out);
t_error run_arithmetic_ternary(t_ast_arithmetic_ternary *arithmetic_ternary, t_state *state, t_i64 *out);
t_error run_arithmetic_unary(t_ast_arithmetic_unary *arithmetic_unary, t_state *state, t_i64 *out);
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out);
t_error _get_node_number(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_sub(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_mul(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_div(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_mod(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
return (ERROR);
// do check for invalid values here (like rhs == 0 for div/mod)
*out = lhs + rhs;
return (NO_ERROR);
}
#include "./arith.h"
t_error _binary_get_op(t_ast_arithmetic_operator op, t_arith_op_func *out)
{
@ -105,7 +53,6 @@ t_ast_node _arith_binary_to_ast_node(t_ast_arithmetic_binary *self)
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);
/// the from node needs to change

58
exec/src/arith/arith.h Normal file
View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arith.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/27 21:13:15 by rparodi #+# #+# */
/* Updated: 2024/07/27 21:21:17 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARITH_H
# define ARITH_H
# include <stddef.h>
# include <stdio.h>
# include "app/state.h"
# include "ast/ast.h"
# include "exec/run.h"
# include "me/convert/numbers_to_str.h"
# include "me/convert/str_to_numbers.h"
# include "me/hashmap/hashmap_env.h"
# include "me/mem/mem.h"
# include "me/os/pipe.h"
# include "me/os/process.h"
# include "me/str/str.h"
# include "me/string/string.h"
# include "me/types.h"
# include "me/vec/vec_estr.h"
# include "me/vec/vec_str.h"
typedef t_error (*t_arith_op_func)(t_ast_node self, t_state *state, t_i64 *out);
t_error run_arithmetic_binary(t_ast_arithmetic_binary *arithmetic_binary, \
t_state *state, t_i64 *out);
t_error run_arithmetic_literal(t_ast_arithmetic_literal *arithmetic_literal, \
t_state *state, t_i64 *out);
t_error run_arithmetic_postfix(t_ast_arithmetic_postfix *arithmetic_postfix, \
t_state *state, t_i64 *out);
t_error run_arithmetic_ternary(t_ast_arithmetic_ternary *arithmetic_ternary, \
t_state *state, t_i64 *out);
t_error run_arithmetic_unary(t_ast_arithmetic_unary *arithmetic_unary, \
t_state *state, t_i64 *out);
t_error run_arithmetic_expansion( \
t_ast_arithmetic_expansion *arithmetic_expansion, \
t_state *state, t_i64 *out);
t_error _get_node_number(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_sub(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_mul(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_div(t_ast_node self, t_state *state, t_i64 *out);
t_error _binary_op_mod(t_ast_node self, t_state *state, t_i64 *out);
#endif

View file

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arith_operation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/27 21:23:07 by rparodi #+# #+# */
/* Updated: 2024/07/27 21:24:04 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "./arith.h"
t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
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);
}
t_error _binary_op_sub(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
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);
}
t_error _binary_op_mul(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
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);
}
t_error _binary_op_div(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
return (ERROR);
*out = lhs / rhs;
if (*out * rhs != lhs)
return (ERROR);
return (NO_ERROR);
}
t_error _binary_op_mod(t_ast_node self, t_state *state, t_i64 *out)
{
t_i64 lhs;
t_i64 rhs;
if (self == NULL || state == NULL || out == NULL || self->kind == AST_ARITHMETIC_BINARY)
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.lhs, state, &lhs))
return (ERROR);
if (_get_node_number(self->data.arithmetic_binary.rhs, state, &rhs))
return (ERROR);
*out = lhs % rhs;
if (*out > rhs)
return (ERROR);
return (NO_ERROR);
}