122 lines
4.3 KiB
C++
122 lines
4.3 KiB
C++
#include "engine/functions/helpers.hpp"
|
|||
|
|
#include "engine/functions/registry.hpp"
|
||
|
|
#include "engine/workbook.hpp"
|
||
|
|
|
||
|
|
#include <variant>
|
||
|
|
|
||
|
|
namespace htab {
|
||
|
|
|
||
|
|
using namespace htab::fn;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
std::optional<ErrorCode> collect_logicals(const std::vector<std::shared_ptr<Expr>>& args,
|
||
|
|
const EvalContext& ctx,
|
||
|
|
bool& any_found,
|
||
|
|
bool is_and,
|
||
|
|
bool& acc) {
|
||
|
|
any_found = false;
|
||
|
|
acc = is_and;
|
||
|
|
for (const auto& arg : args) {
|
||
|
|
EvalResult res = evaluate_ast(arg.get(), ctx);
|
||
|
|
if (res.is_ref) {
|
||
|
|
for (const auto& rng : res.ranges) {
|
||
|
|
for (int r = rng.r1; r <= rng.r2; ++r) {
|
||
|
|
for (int c = rng.c1; c <= rng.c2; ++c) {
|
||
|
|
Value v = ctx.wb->get_scalar_value(rng.sheet, r, c);
|
||
|
|
if (v.is_error()) return v.as_error();
|
||
|
|
if (v.is_bool()) {
|
||
|
|
any_found = true;
|
||
|
|
acc = is_and ? (acc && v.as_bool()) : (acc || v.as_bool());
|
||
|
|
} else if (v.is_number()) {
|
||
|
|
any_found = true;
|
||
|
|
bool b = (v.as_number() != 0.0);
|
||
|
|
acc = is_and ? (acc && b) : (acc || b);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (res.value.is_array()) {
|
||
|
|
const auto& arr = res.value.as_array();
|
||
|
|
if (arr) {
|
||
|
|
for (const auto& v : arr->data) {
|
||
|
|
if (v.is_error()) return v.as_error();
|
||
|
|
if (v.is_bool()) {
|
||
|
|
any_found = true;
|
||
|
|
acc = is_and ? (acc && v.as_bool()) : (acc || v.as_bool());
|
||
|
|
} else if (v.is_number()) {
|
||
|
|
any_found = true;
|
||
|
|
bool b = (v.as_number() != 0.0);
|
||
|
|
acc = is_and ? (acc && b) : (acc || b);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
const Value& v = res.value;
|
||
|
|
if (v.is_error()) return v.as_error();
|
||
|
|
if (v.is_empty()) continue;
|
||
|
|
auto b = to_bool(v);
|
||
|
|
if (std::holds_alternative<ErrorCode>(b)) return std::get<ErrorCode>(b);
|
||
|
|
any_found = true;
|
||
|
|
acc = is_and ? (acc && std::get<bool>(b)) : (acc || std::get<bool>(b));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_and(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||
|
|
if (args.empty()) return err_res(ErrorCode::Value);
|
||
|
|
bool any = false, acc = true;
|
||
|
|
if (auto err = collect_logicals(args, ctx, any, true, acc)) return err_res(*err);
|
||
|
|
if (!any) return err_res(ErrorCode::Value);
|
||
|
|
return EvalResult::from_value(Value::make_bool(acc));
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_or(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||
|
|
if (args.empty()) return err_res(ErrorCode::Value);
|
||
|
|
bool any = false, acc = false;
|
||
|
|
if (auto err = collect_logicals(args, ctx, any, false, acc)) return err_res(*err);
|
||
|
|
if (!any) return err_res(ErrorCode::Value);
|
||
|
|
return EvalResult::from_value(Value::make_bool(acc));
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_not(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
||
|
|
ErrorCode err = ErrorCode::Value;
|
||
|
|
auto b = eval_bool_arg(args[0], ctx, err);
|
||
|
|
if (!b) return err_res(err);
|
||
|
|
return EvalResult::from_value(Value::make_bool(!*b));
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_if(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||
|
|
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
||
|
|
ErrorCode err = ErrorCode::Value;
|
||
|
|
auto b = eval_bool_arg(args[0], ctx, err);
|
||
|
|
if (!b) return err_res(err);
|
||
|
|
if (*b) return evaluate_ast(args[1].get(), ctx);
|
||
|
|
if (args.size() == 3) return evaluate_ast(args[2].get(), ctx);
|
||
|
|
return EvalResult::from_value(Value::make_bool(false));
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_true(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext&) {
|
||
|
|
if (!args.empty()) return err_res(ErrorCode::Value);
|
||
|
|
return EvalResult::from_value(Value::make_bool(true));
|
||
|
|
}
|
||
|
|
|
||
|
|
EvalResult fn_false(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext&) {
|
||
|
|
if (!args.empty()) return err_res(ErrorCode::Value);
|
||
|
|
return EvalResult::from_value(Value::make_bool(false));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
void register_logical_functions(FunctionRegistry& reg) {
|
||
|
|
reg.register_fn("AND", fn_and);
|
||
|
|
reg.register_fn("FALSE", fn_false);
|
||
|
|
reg.register_fn("IF", fn_if);
|
||
|
|
reg.register_fn("NOT", fn_not);
|
||
|
|
reg.register_fn("OR", fn_or);
|
||
|
|
reg.register_fn("TRUE", fn_true);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace htab
|