pub mod display;
pub mod eval;
pub mod parser;
pub mod types;
pub use display::display_number;
pub use parser::{parse, validate, Expr};
pub use types::{ErrorKind, ParseError, Value};
pub use eval::functions::{FunctionMeta, Registry};
use std::collections::HashMap;
use eval::{evaluate_expr, Context, EvalCtx};
pub fn evaluate(formula: &str, variables: &HashMap<String, Value>) -> Value {
match parse(formula) {
Err(_) => Value::Error(ErrorKind::Value),
Ok(expr) => {
let ctx = Context::new(variables.clone());
let registry = Registry::new();
let mut eval_ctx = EvalCtx::new(ctx, ®istry);
first_of_array(evaluate_expr(&expr, &mut eval_ctx))
}
}
}
fn first_of_array(v: Value) -> Value {
match v {
Value::Array(elems) if !elems.is_empty() => {
first_of_array(elems.into_iter().next().unwrap())
}
other => other,
}
}