exp_rs/
expression_functions.rs1extern crate alloc;
7use crate::Real;
8use crate::context::EvalContext;
9use crate::error::Result;
10use crate::eval::eval_ast;
11use crate::types::AstExpr;
12use crate::types::TryIntoHeaplessString;
13use alloc::borrow::Cow;
14#[cfg(not(test))]
15use alloc::rc::Rc;
16use alloc::string::ToString;
17#[cfg(test)]
18use std::rc::Rc;
19
20pub fn eval_expression_function<'a, 'arena>(
24 ast: &'arena AstExpr<'arena>,
25 param_names: &[Cow<'a, str>],
26 arg_values: &[Real],
27 parent_ctx: Option<Rc<EvalContext>>,
28 arena: &'arena bumpalo::Bump,
29) -> Result<Real> {
30 let mut temp_ctx = EvalContext::new();
31 if let Some(parent) = parent_ctx {
32 temp_ctx.parent = Some(Rc::clone(&parent));
33 }
34 for (param_name, &arg_val) in param_names.iter().zip(arg_values.iter()) {
35 if let Ok(key) = param_name.to_string().try_into_heapless() {
36 let _ = temp_ctx.variables.insert(key, arg_val);
37 }
38 }
39 eval_ast(ast, Some(Rc::new(temp_ctx)), arena)
40}
41