Initial commit: HTab spreadsheet engine and GemUI X11 frontend
This commit is contained in:
commit
395f458fee
55 files changed
+12682
No files matched your search
@@ -0,0 +1,353 @@
|
||||
#include "engine/functions/helpers.hpp"
|
||||
#include "engine/functions/registry.hpp"
|
||||
#include "engine/workbook.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <variant>
|
||||
|
||||
namespace htab {
|
||||
|
||||
using namespace htab::fn;
|
||||
|
||||
namespace {
|
||||
|
||||
bool values_equal_for_lookup(const Value& candidate, const Value& key) {
|
||||
if (candidate.is_error() || key.is_error() || candidate.is_empty() || key.is_empty()) {
|
||||
return false;
|
||||
}
|
||||
if (key.is_string() && candidate.is_string()) {
|
||||
return wildcard_match_ci(key.as_string(), candidate.as_string());
|
||||
}
|
||||
if (key.is_number() && candidate.is_number()) {
|
||||
return candidate.as_number() == key.as_number();
|
||||
}
|
||||
if (key.is_bool() && candidate.is_bool()) {
|
||||
return candidate.as_bool() == key.as_bool();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool same_lookup_type(const Value& a, const Value& b) {
|
||||
return (a.is_number() && b.is_number()) ||
|
||||
(a.is_string() && b.is_string()) ||
|
||||
(a.is_bool() && b.is_bool());
|
||||
}
|
||||
|
||||
EvalResult fn_choose(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() < 2) return err_res(ErrorCode::Value);
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto idx_d = eval_number_arg(args[0], ctx, err);
|
||||
if (!idx_d) return err_res(err);
|
||||
int idx = static_cast<int>(std::trunc(*idx_d));
|
||||
if (idx < 1 || static_cast<size_t>(idx) >= args.size()) return err_res(ErrorCode::Value);
|
||||
return evaluate_ast(args[static_cast<size_t>(idx)].get(), ctx);
|
||||
}
|
||||
|
||||
EvalResult fn_rows(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() != 1) return err_res(ErrorCode::Value);
|
||||
EvalResult res = evaluate_ast(args[0].get(), ctx);
|
||||
if (res.is_ref) {
|
||||
if (res.ranges.size() != 1) return err_res(ErrorCode::Value);
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(res.ranges[0].row_count())));
|
||||
}
|
||||
if (res.value.is_error()) return EvalResult::from_value(res.value);
|
||||
if (res.value.is_array()) {
|
||||
const auto& arr = res.value.as_array();
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(arr ? arr->rows : 0)));
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(1.0));
|
||||
}
|
||||
|
||||
EvalResult fn_columns(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() != 1) return err_res(ErrorCode::Value);
|
||||
EvalResult res = evaluate_ast(args[0].get(), ctx);
|
||||
if (res.is_ref) {
|
||||
if (res.ranges.size() != 1) return err_res(ErrorCode::Value);
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(res.ranges[0].col_count())));
|
||||
}
|
||||
if (res.value.is_error()) return EvalResult::from_value(res.value);
|
||||
if (res.value.is_array()) {
|
||||
const auto& arr = res.value.as_array();
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(arr ? arr->cols : 0)));
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(1.0));
|
||||
}
|
||||
|
||||
EvalResult fn_filter(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
||||
auto arr_var = result_to_array2d(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
const Array2D& arr = std::get<Array2D>(arr_var);
|
||||
|
||||
auto inc_var = result_to_array2d(evaluate_ast(args[1].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(inc_var)) return err_res(std::get<ErrorCode>(inc_var));
|
||||
const Array2D& inc = std::get<Array2D>(inc_var);
|
||||
|
||||
auto eval_include_flag = [](const Value& v, ErrorCode& err_out) -> std::optional<bool> {
|
||||
if (v.is_error()) { err_out = v.as_error(); return std::nullopt; }
|
||||
if (v.is_empty()) return false;
|
||||
if (v.is_bool()) return v.as_bool();
|
||||
if (v.is_number()) return v.as_number() != 0.0;
|
||||
err_out = ErrorCode::Value;
|
||||
return std::nullopt;
|
||||
};
|
||||
|
||||
// Row filtering: include is (rows x 1)
|
||||
if (inc.cols == 1 && inc.rows == arr.rows) {
|
||||
std::vector<int> kept_rows;
|
||||
for (int r = 0; r < arr.rows; ++r) {
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto flag = eval_include_flag(inc.at(r, 0), err);
|
||||
if (!flag) return err_res(err);
|
||||
if (*flag) kept_rows.push_back(r);
|
||||
}
|
||||
if (kept_rows.empty()) {
|
||||
if (args.size() == 3) return evaluate_ast(args[2].get(), ctx);
|
||||
return err_res(ErrorCode::NA);
|
||||
}
|
||||
auto out = std::make_shared<Array2D>(static_cast<int>(kept_rows.size()), arr.cols);
|
||||
for (size_t i = 0; i < kept_rows.size(); ++i) {
|
||||
for (int c = 0; c < arr.cols; ++c) {
|
||||
out->at(static_cast<int>(i), c) = arr.at(kept_rows[i], c);
|
||||
}
|
||||
}
|
||||
if (out->rows == 1 && out->cols == 1) return EvalResult::from_value(out->at(0, 0));
|
||||
return EvalResult::from_value(Value::make_array(std::move(out)));
|
||||
}
|
||||
|
||||
// Column filtering: include is (1 x cols)
|
||||
if (inc.rows == 1 && inc.cols == arr.cols) {
|
||||
std::vector<int> kept_cols;
|
||||
for (int c = 0; c < arr.cols; ++c) {
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto flag = eval_include_flag(inc.at(0, c), err);
|
||||
if (!flag) return err_res(err);
|
||||
if (*flag) kept_cols.push_back(c);
|
||||
}
|
||||
if (kept_cols.empty()) {
|
||||
if (args.size() == 3) return evaluate_ast(args[2].get(), ctx);
|
||||
return err_res(ErrorCode::NA);
|
||||
}
|
||||
auto out = std::make_shared<Array2D>(arr.rows, static_cast<int>(kept_cols.size()));
|
||||
for (int r = 0; r < arr.rows; ++r) {
|
||||
for (size_t j = 0; j < kept_cols.size(); ++j) {
|
||||
out->at(r, static_cast<int>(j)) = arr.at(r, kept_cols[j]);
|
||||
}
|
||||
}
|
||||
if (out->rows == 1 && out->cols == 1) return EvalResult::from_value(out->at(0, 0));
|
||||
return EvalResult::from_value(Value::make_array(std::move(out)));
|
||||
}
|
||||
|
||||
return err_res(ErrorCode::Value);
|
||||
}
|
||||
|
||||
EvalResult fn_index(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
||||
EvalResult arr_res = evaluate_ast(args[0].get(), ctx);
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto r_d = eval_number_arg(args[1], ctx, err);
|
||||
if (!r_d) return err_res(err);
|
||||
int row_num = static_cast<int>(std::trunc(*r_d));
|
||||
int col_num = -1;
|
||||
if (args.size() == 3) {
|
||||
auto c_d = eval_number_arg(args[2], ctx, err);
|
||||
if (!c_d) return err_res(err);
|
||||
col_num = static_cast<int>(std::trunc(*c_d));
|
||||
}
|
||||
|
||||
if (arr_res.is_ref) {
|
||||
if (arr_res.ranges.size() != 1) return err_res(ErrorCode::Ref);
|
||||
const CellRange& rng = arr_res.ranges[0];
|
||||
int rc = rng.row_count();
|
||||
int cc = rng.col_count();
|
||||
if (col_num < 0) {
|
||||
if (rc == 1) {
|
||||
col_num = row_num;
|
||||
row_num = 1;
|
||||
} else if (cc == 1) {
|
||||
col_num = 1;
|
||||
} else {
|
||||
col_num = 0;
|
||||
}
|
||||
}
|
||||
if (row_num < 0 || col_num < 0 || row_num > rc || col_num > cc) {
|
||||
return err_res(ErrorCode::Ref);
|
||||
}
|
||||
if (row_num == 0 && col_num == 0) return arr_res;
|
||||
if (row_num == 0) {
|
||||
return EvalResult::from_range(
|
||||
CellRange{rng.sheet, rng.r1, rng.c1 + col_num - 1, rng.r2, rng.c1 + col_num - 1});
|
||||
}
|
||||
if (col_num == 0) {
|
||||
return EvalResult::from_range(
|
||||
CellRange{rng.sheet, rng.r1 + row_num - 1, rng.c1, rng.r1 + row_num - 1, rng.c2});
|
||||
}
|
||||
return EvalResult::from_range(CellRange{
|
||||
rng.sheet, rng.r1 + row_num - 1, rng.c1 + col_num - 1,
|
||||
rng.r1 + row_num - 1, rng.c1 + col_num - 1});
|
||||
}
|
||||
|
||||
auto arr_var = result_to_array2d(arr_res, *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
const Array2D& arr = std::get<Array2D>(arr_var);
|
||||
if (col_num < 0) {
|
||||
if (arr.rows == 1) {
|
||||
col_num = row_num;
|
||||
row_num = 1;
|
||||
} else if (arr.cols == 1) {
|
||||
col_num = 1;
|
||||
} else {
|
||||
col_num = 0;
|
||||
}
|
||||
}
|
||||
if (row_num < 0 || col_num < 0 || row_num > arr.rows || col_num > arr.cols) {
|
||||
return err_res(ErrorCode::Ref);
|
||||
}
|
||||
if (row_num == 0 && col_num == 0) {
|
||||
return EvalResult::from_value(Value::make_array(std::make_shared<Array2D>(arr)));
|
||||
}
|
||||
if (row_num == 0) {
|
||||
auto out = std::make_shared<Array2D>(arr.rows, 1);
|
||||
for (int r = 0; r < arr.rows; ++r) out->at(r, 0) = arr.at(r, col_num - 1);
|
||||
return EvalResult::from_value(out->rows == 1 ? out->at(0, 0) : Value::make_array(std::move(out)));
|
||||
}
|
||||
if (col_num == 0) {
|
||||
auto out = std::make_shared<Array2D>(1, arr.cols);
|
||||
for (int c = 0; c < arr.cols; ++c) out->at(0, c) = arr.at(row_num - 1, c);
|
||||
return EvalResult::from_value(out->cols == 1 ? out->at(0, 0) : Value::make_array(std::move(out)));
|
||||
}
|
||||
return EvalResult::from_value(arr.at(row_num - 1, col_num - 1));
|
||||
}
|
||||
|
||||
EvalResult fn_match(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
||||
Value key = deref_to_value(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
||||
if (key.is_error()) return EvalResult::from_value(key);
|
||||
|
||||
auto arr_var = result_to_array2d(evaluate_ast(args[1].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
const Array2D& arr = std::get<Array2D>(arr_var);
|
||||
if (arr.rows != 1 && arr.cols != 1) return err_res(ErrorCode::NA);
|
||||
|
||||
int match_type = 1;
|
||||
if (args.size() == 3) {
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto mt = eval_number_arg(args[2], ctx, err);
|
||||
if (!mt) return err_res(err);
|
||||
match_type = static_cast<int>(std::trunc(*mt));
|
||||
}
|
||||
|
||||
int n = static_cast<int>(arr.data.size());
|
||||
if (match_type == 0) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (values_equal_for_lookup(arr.data[static_cast<size_t>(i)], key)) {
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(i + 1)));
|
||||
}
|
||||
}
|
||||
return err_res(ErrorCode::NA);
|
||||
}
|
||||
|
||||
if (match_type > 0) {
|
||||
int best = -1;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const Value& elem = arr.data[static_cast<size_t>(i)];
|
||||
if (!same_lookup_type(elem, key)) continue;
|
||||
auto cmp = compare_values(elem, key);
|
||||
if (!std::holds_alternative<int>(cmp)) continue;
|
||||
int c = std::get<int>(cmp);
|
||||
if (c <= 0) best = i;
|
||||
else break;
|
||||
}
|
||||
if (best < 0) return err_res(ErrorCode::NA);
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(best + 1)));
|
||||
}
|
||||
|
||||
// match_type < 0: descending, smallest >= key
|
||||
int best = -1;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const Value& elem = arr.data[static_cast<size_t>(i)];
|
||||
if (!same_lookup_type(elem, key)) continue;
|
||||
auto cmp = compare_values(elem, key);
|
||||
if (!std::holds_alternative<int>(cmp)) continue;
|
||||
int c = std::get<int>(cmp);
|
||||
if (c >= 0) best = i;
|
||||
else break;
|
||||
}
|
||||
if (best < 0) return err_res(ErrorCode::NA);
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(best + 1)));
|
||||
}
|
||||
|
||||
EvalResult fn_vh_lookup(const std::vector<std::shared_ptr<Expr>>& args,
|
||||
const EvalContext& ctx,
|
||||
bool vertical) {
|
||||
if (args.size() < 3 || args.size() > 4) return err_res(ErrorCode::Value);
|
||||
Value key = deref_to_value(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
||||
if (key.is_error()) return EvalResult::from_value(key);
|
||||
|
||||
auto tbl_var = result_to_array2d(evaluate_ast(args[1].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(tbl_var)) return err_res(std::get<ErrorCode>(tbl_var));
|
||||
const Array2D& tbl = std::get<Array2D>(tbl_var);
|
||||
|
||||
ErrorCode err = ErrorCode::Value;
|
||||
auto idx_d = eval_number_arg(args[2], ctx, err);
|
||||
if (!idx_d) return err_res(err);
|
||||
int idx = static_cast<int>(std::trunc(*idx_d));
|
||||
if (idx < 1) return err_res(ErrorCode::Value);
|
||||
int max_idx = vertical ? tbl.cols : tbl.rows;
|
||||
if (idx > max_idx) return err_res(ErrorCode::Ref);
|
||||
|
||||
bool range_lookup = true;
|
||||
if (args.size() == 4) {
|
||||
auto rl = eval_bool_arg(args[3], ctx, err);
|
||||
if (!rl) return err_res(err);
|
||||
range_lookup = *rl;
|
||||
}
|
||||
|
||||
int len = vertical ? tbl.rows : tbl.cols;
|
||||
int found = -1;
|
||||
if (!range_lookup) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
const Value& elem = vertical ? tbl.at(i, 0) : tbl.at(0, i);
|
||||
if (values_equal_for_lookup(elem, key)) {
|
||||
found = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
const Value& elem = vertical ? tbl.at(i, 0) : tbl.at(0, i);
|
||||
if (!same_lookup_type(elem, key)) continue;
|
||||
auto cmp = compare_values(elem, key);
|
||||
if (!std::holds_alternative<int>(cmp)) continue;
|
||||
int c = std::get<int>(cmp);
|
||||
if (c <= 0) found = i;
|
||||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found < 0) return err_res(ErrorCode::NA);
|
||||
Value out = vertical ? tbl.at(found, idx - 1) : tbl.at(idx - 1, found);
|
||||
return EvalResult::from_value(out.is_empty() ? Value::make_number(0.0) : out);
|
||||
}
|
||||
|
||||
EvalResult fn_vlookup(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_vh_lookup(a, c, true);
|
||||
}
|
||||
EvalResult fn_hlookup(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_vh_lookup(a, c, false);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_lookup_functions(FunctionRegistry& reg) {
|
||||
reg.register_fn("CHOOSE", fn_choose);
|
||||
reg.register_fn("COLUMNS", fn_columns);
|
||||
reg.register_fn("FILTER", fn_filter);
|
||||
reg.register_fn("HLOOKUP", fn_hlookup);
|
||||
reg.register_fn("INDEX", fn_index);
|
||||
reg.register_fn("MATCH", fn_match);
|
||||
reg.register_fn("ROWS", fn_rows);
|
||||
reg.register_fn("VLOOKUP", fn_vlookup);
|
||||
}
|
||||
|
||||
} // namespace htab
|
||||
Reference in new issue
Block a user