expr/functions/
mod.rs

1pub mod string;
2pub mod array;
3
4use crate::Result;
5
6use crate::ast::program::Program;
7use crate::{bail, Context, Environment, Value};
8
9pub type Function<'a> = Box<dyn Fn(ExprCall) -> Result<Value> + 'a + Sync + Send>;
10
11pub struct ExprCall<'a, 'b> {
12    pub ident: String,
13    pub args: Vec<Value>,
14    pub predicate: Option<Program>,
15    pub ctx: &'a Context,
16    pub env: &'a Environment<'b>,
17}
18
19impl Environment<'_> {
20    pub fn eval_func(&self, ctx: &Context, ident: String, args: Vec<Value>, predicate: Option<Program>) -> Result<Value> {
21        let call = ExprCall {
22            ident,
23            args,
24            predicate,
25            ctx,
26            env: self,
27        };
28        if let Some(f) = self.functions.get(&call.ident) {
29            f(call)
30        } else {
31            bail!("Unknown function: {}", call.ident)
32        }
33    }
34}