pub(crate) mod info;
pub(crate) mod logical;
pub(crate) mod lookup;
pub(crate) mod math;
pub(crate) mod stat;
pub(crate) mod text;
use crate::ast::Expr;
use crate::error::{ExcelError, FormulaError};
use crate::resolver::CellResolver;
use crate::value::Value;
use alloc::string::String;
use alloc::vec::Vec;
pub(crate) struct Ctx<'a> {
pub resolver: &'a dyn CellResolver,
pub now: f64,
pub depth: usize,
}
pub(crate) type R = Result<Value, FormulaError>;
#[must_use]
pub fn builtin_functions() -> &'static [&'static str] {
&[
"SUM",
"PRODUCT",
"ABS",
"SIGN",
"SQRT",
"POWER",
"EXP",
"LN",
"LOG",
"LOG10",
"MOD",
"INT",
"TRUNC",
"ROUND",
"ROUNDUP",
"ROUNDDOWN",
"CEILING",
"FLOOR",
"AVERAGE",
"COUNT",
"COUNTA",
"COUNTBLANK",
"MAX",
"MIN",
"MEDIAN",
"STDEV",
"VAR",
"IF",
"AND",
"OR",
"NOT",
"IFERROR",
"IFNA",
"TRUE",
"FALSE",
"CONCATENATE",
"LEFT",
"RIGHT",
"MID",
"LEN",
"LOWER",
"UPPER",
"PROPER",
"TRIM",
"SUBSTITUTE",
"FIND",
"SEARCH",
"REPT",
"TEXT",
"VLOOKUP",
"HLOOKUP",
"INDEX",
"MATCH",
"OFFSET",
"TODAY",
"NOW",
"ISBLANK",
"ISNUMBER",
"ISTEXT",
"ISLOGICAL",
"ISERROR",
"ISNA",
"ISREF",
]
}
pub(crate) fn dispatch(ctx: &mut Ctx<'_>, name: &str, args: &[Expr]) -> R {
let upper = if name.bytes().all(|b| b.is_ascii_uppercase()) {
None
} else {
Some(name.to_ascii_uppercase())
};
let name: &str = upper.as_deref().unwrap_or(name);
match name {
"SUM" => math::sum(ctx, args),
"PRODUCT" => math::product(ctx, args),
"ABS" => math::abs(ctx, args),
"SIGN" => math::sign(ctx, args),
"SQRT" => math::sqrt(ctx, args),
"POWER" => math::power(ctx, args),
"EXP" => math::exp(ctx, args),
"LN" => math::ln(ctx, args),
"LOG" => math::log(ctx, args),
"LOG10" => math::log10(ctx, args),
"MOD" => math::mod_(ctx, args),
"INT" => math::int(ctx, args),
"TRUNC" => math::trunc(ctx, args),
"ROUND" => math::round(ctx, args),
"ROUNDUP" => math::roundup(ctx, args),
"ROUNDDOWN" => math::rounddown(ctx, args),
"CEILING" => math::ceiling(ctx, args),
"FLOOR" => math::floor(ctx, args),
"AVERAGE" => stat::average(ctx, args),
"COUNT" => stat::count(ctx, args),
"COUNTA" => stat::counta(ctx, args),
"COUNTBLANK" => stat::countblank(ctx, args),
"MAX" => stat::max(ctx, args),
"MIN" => stat::min(ctx, args),
"MEDIAN" => stat::median(ctx, args),
"STDEV" => stat::stdev(ctx, args),
"VAR" => stat::var_(ctx, args),
"IF" => logical::if_(ctx, args),
"AND" => logical::and(ctx, args),
"OR" => logical::or(ctx, args),
"NOT" => logical::not(ctx, args),
"IFERROR" => logical::iferror(ctx, args),
"IFNA" => logical::ifna(ctx, args),
"TRUE" => logical::true_(args),
"FALSE" => logical::false_(args),
"CONCATENATE" => text::concatenate(ctx, args),
"LEFT" => text::left(ctx, args),
"RIGHT" => text::right(ctx, args),
"MID" => text::mid(ctx, args),
"LEN" => text::len(ctx, args),
"LOWER" => text::lower(ctx, args),
"UPPER" => text::upper(ctx, args),
"PROPER" => text::proper(ctx, args),
"TRIM" => text::trim(ctx, args),
"SUBSTITUTE" => text::substitute(ctx, args),
"FIND" => text::find(ctx, args),
"SEARCH" => text::search(ctx, args),
"REPT" => text::rept(ctx, args),
"TEXT" => text::text(ctx, args),
"VLOOKUP" => lookup::vlookup(ctx, args),
"HLOOKUP" => lookup::hlookup(ctx, args),
"INDEX" => lookup::index(ctx, args),
"MATCH" => lookup::match_(ctx, args),
"OFFSET" => lookup::offset(ctx, args),
"TODAY" => Ok(Value::Number(libm::floor(ctx.now))),
"NOW" => Ok(Value::Number(ctx.now)),
"ISBLANK" => info::isblank(ctx, args),
"ISNUMBER" => info::isnumber(ctx, args),
"ISTEXT" => info::istext(ctx, args),
"ISLOGICAL" => info::is_logical(ctx, args),
"ISERROR" => info::iserror(ctx, args),
"ISNA" => info::isna(ctx, args),
"ISREF" => info::isref(args),
_ => Err(FormulaError::UnknownFunction(String::from(name))),
}
}
pub(crate) fn arg_count(args: &[Expr], min: usize, max: usize) -> Result<(), FormulaError> {
if args.len() < min || args.len() > max {
Err(ExcelError::Value.into())
} else {
Ok(())
}
}
pub(crate) fn arg_count_exact(args: &[Expr], n: usize) -> Result<(), FormulaError> {
arg_count(args, n, n)
}
pub(crate) fn arg(args: &[Expr], i: usize) -> Result<&Expr, FormulaError> {
args.get(i).ok_or_else(|| ExcelError::Value.into())
}
pub(crate) fn eval1(ctx: &mut Ctx<'_>, e: &Expr) -> R {
crate::eval::eval(ctx, e)
}
pub(crate) fn eval_arg(ctx: &mut Ctx<'_>, args: &[Expr], i: usize) -> R {
let Some(e) = args.get(i) else {
return Err(ExcelError::Value.into());
};
if crate::ast::is_omitted_arg(e) {
return Err(ExcelError::Value.into());
}
eval1(ctx, e)
}
pub(crate) fn num_arg(ctx: &mut Ctx<'_>, args: &[Expr], i: usize) -> Result<f64, FormulaError> {
let v = eval_arg(ctx, args, i)?;
crate::value::coerce_number(&v).map_err(FormulaError::Eval)
}
pub(crate) fn int_arg(ctx: &mut Ctx<'_>, args: &[Expr], i: usize) -> Result<f64, FormulaError> {
Ok(libm::trunc(num_arg(ctx, args, i)?))
}
pub(crate) fn text_arg(ctx: &mut Ctx<'_>, args: &[Expr], i: usize) -> Result<String, FormulaError> {
let v = eval_arg(ctx, args, i)?;
crate::value::coerce_text(&v).map_err(FormulaError::Eval)
}
pub(crate) fn bool_arg(ctx: &mut Ctx<'_>, args: &[Expr], i: usize) -> Result<bool, FormulaError> {
let v = eval_arg(ctx, args, i)?;
crate::value::coerce_bool(&v).map_err(FormulaError::Eval)
}
pub(crate) fn collect_numbers(ctx: &mut Ctx<'_>, args: &[Expr]) -> Result<Vec<f64>, FormulaError> {
let mut out = Vec::new();
for a in args {
if crate::ast::is_omitted_arg(a) {
continue;
}
if crate::eval::is_ref_expr(a) {
if let Some(vals) = crate::eval::cell_values(ctx, a)? {
for v in vals {
match v {
Value::Number(n) => out.push(n),
Value::Error(e) => return Err(FormulaError::Eval(e)),
Value::Text(_) | Value::Boolean(_) | Value::Empty => {}
}
}
continue;
}
}
let v = eval1(ctx, a)?;
match v {
Value::Number(n) => out.push(n),
Value::Text(_) | Value::Boolean(_) => {
out.push(crate::value::coerce_number(&v).map_err(FormulaError::Eval)?);
}
Value::Empty => {}
Value::Error(e) => return Err(FormulaError::Eval(e)),
}
}
Ok(out)
}
pub(crate) fn collect_bools(ctx: &mut Ctx<'_>, args: &[Expr]) -> Result<Vec<bool>, FormulaError> {
let mut out = Vec::new();
for a in args {
if crate::ast::is_omitted_arg(a) {
continue;
}
if crate::eval::is_ref_expr(a) {
if let Some(vals) = crate::eval::cell_values(ctx, a)? {
for v in vals {
match v {
Value::Boolean(b) => out.push(b),
Value::Number(n) => out.push(n != 0.0),
Value::Error(e) => return Err(FormulaError::Eval(e)),
Value::Text(_) | Value::Empty => {}
}
}
continue;
}
}
let v = eval1(ctx, a)?;
match v {
Value::Error(e) => return Err(FormulaError::Eval(e)),
Value::Empty => {}
other => out.push(crate::value::coerce_bool(&other).map_err(FormulaError::Eval)?),
}
}
if out.is_empty() {
return Err(ExcelError::Value.into());
}
Ok(out)
}
pub(crate) fn wildcard_prefix_match(pattern: &str, text: &str) -> bool {
let p: Vec<char> = pattern.to_lowercase().chars().collect();
let t: Vec<char> = text.to_lowercase().chars().collect();
prefix_at(&p, 0, &t, 0)
}
fn prefix_at(p: &[char], pi: usize, t: &[char], ti: usize) -> bool {
let Some(&pc) = p.get(pi) else {
return true; };
let p_rest = p.get(pi + 1);
match pc {
'*' => {
let mut next = pi + 1;
while p.get(next) == Some(&'*') {
next += 1;
}
(ti..=t.len()).any(|k| prefix_at(p, next, t, k))
}
'~' if matches!(p_rest, Some('*') | Some('?') | Some('~')) => match (t.get(ti), p_rest) {
(Some(&tc), Some(&esc)) if tc == esc => prefix_at(p, pi + 2, t, ti + 1),
_ => false,
},
'?' => match t.get(ti) {
Some(_) => prefix_at(p, pi + 1, t, ti + 1),
None => false,
},
c => match t.get(ti) {
Some(&tc) if tc == c => prefix_at(p, pi + 1, t, ti + 1),
_ => false,
},
}
}
pub(crate) fn wildcard_match(pattern: &str, text: &str) -> bool {
let p: Vec<char> = pattern.to_lowercase().chars().collect();
let t: Vec<char> = text.to_lowercase().chars().collect();
wildcard_at(&p, 0, &t, 0)
}
fn wildcard_at(p: &[char], pi: usize, t: &[char], ti: usize) -> bool {
let (Some(&pc), p_rest) = (p.get(pi), p.get(pi + 1)) else {
return ti == t.len();
};
match pc {
'*' => {
let mut next = pi + 1;
while p.get(next) == Some(&'*') {
next += 1;
}
if next == p.len() {
return true;
}
(ti..=t.len()).any(|k| wildcard_at(p, next, t, k))
}
'~' if matches!(p_rest, Some('*') | Some('?') | Some('~')) => match (t.get(ti), p_rest) {
(Some(&tc), Some(&esc)) if tc == esc => wildcard_at(p, pi + 2, t, ti + 1),
_ => false,
},
'?' => match t.get(ti) {
Some(_) => wildcard_at(p, pi + 1, t, ti + 1),
None => false,
},
c => match t.get(ti) {
Some(&tc) if tc == c => wildcard_at(p, pi + 1, t, ti + 1),
_ => false,
},
}
}