Skip to content

Commit

Permalink
evaluation: rpn: remove function argument size checks
Browse files Browse the repository at this point in the history
it's enforced at compile time now
  • Loading branch information
AdelKS committed Oct 9, 2023
1 parent f457982 commit 974893c
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions include/zecalculator/evaluation/rpn/impl/evaluation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ template <size_t input_size>
template <size_t args_num>
inline void Evaluator<input_size>::operator()(const zc::parsing::node::rpn::Function<args_num>& node)
{
if (max_recursion_depth < current_recursion_depth) [[unlikely]]
expected_eval_stack = tl::unexpected(Error::recursion_depth_overflow());
if (not bool(node.f)) [[unlikely]]
expected_eval_stack = tl::unexpected(Error::calling_invalid_function(node));
else
Expand All @@ -73,26 +71,21 @@ template <size_t input_size>
template <size_t args_num>
inline void Evaluator<input_size>::operator()(const zc::parsing::node::rpn::CppFunction<args_num>& node)
{
if (expected_eval_stack->size() < args_num) [[unlikely]]
expected_eval_stack = tl::unexpected(Error::mismatched_fun_args(node));
else
// points on the before last value on the stack
const auto it = expected_eval_stack->end() - args_num;

auto compute_overwrite_val = [&]<size_t... i>(std::integer_sequence<size_t, i...>)
{
// points on the before last value on the stack
const auto it = expected_eval_stack->end() - args_num;

auto compute_overwrite_val = [&]<size_t... i>(std::integer_sequence<size_t, i...>)
{
// since the function pops two elements, then pushes back one
// we can overwrite directly the value that will get replaced
*it = node.f(*(it+i)...);
};
compute_overwrite_val(std::make_index_sequence<args_num>());

// remove args_num-1 values from the stack,
// why the minus one: one value got overwritten with the computation result, as an optim
if constexpr (args_num >= 2)
expected_eval_stack->resize(expected_eval_stack->size() - (args_num - 1));
}
// since the function pops two elements, then pushes back one
// we can overwrite directly the value that will get replaced
*it = node.f(*(it+i)...);
};
compute_overwrite_val(std::make_index_sequence<args_num>());

// remove args_num-1 values from the stack,
// why the minus one: one value got overwritten with the computation result, as an optim
if constexpr (args_num >= 2)
expected_eval_stack->resize(expected_eval_stack->size() - (args_num - 1));
}

template <size_t input_size>
Expand All @@ -101,8 +94,6 @@ inline void Evaluator<input_size>::operator()(const zc::parsing::node::rpn::Sequ
// std::cout << "Evaluating zc function: " << node.name << std::endl;
if (not bool(node.u))
expected_eval_stack = tl::unexpected(Error::calling_invalid_function(node));
else if (expected_eval_stack->empty())
expected_eval_stack = tl::unexpected(Error::mismatched_fun_args(node));
else
{
// sequence handles only one argument
Expand Down

0 comments on commit 974893c

Please sign in to comment.