grit_pattern_matcher/pattern/
call_built_in.rs

1use super::{
2    functions::GritCall,
3    patterns::{Pattern, PatternName},
4    Evaluator, FuncEvaluation, ResolvedPattern, State,
5};
6use crate::context::{ExecContext, QueryContext};
7use grit_util::{error::GritResult, AnalysisLogs};
8
9// todo we can probably use a macro to generate a function that takes a vec and
10// and calls the input function with the vec args unpacked.
11
12#[derive(Debug, Clone)]
13pub struct CallBuiltIn<Q: QueryContext> {
14    pub index: usize,
15    pub name: String,
16    pub args: Vec<Option<Pattern<Q>>>,
17}
18
19impl<Q: QueryContext> CallBuiltIn<Q> {
20    pub fn new(index: usize, name: &str, args: Vec<Option<Pattern<Q>>>) -> Self {
21        Self {
22            index,
23            name: name.to_string(),
24            args,
25        }
26    }
27}
28
29impl<Q: QueryContext> GritCall<Q> for CallBuiltIn<Q> {
30    fn call<'a>(
31        &'a self,
32        state: &mut State<'a, Q>,
33        context: &'a Q::ExecContext<'a>,
34        logs: &mut AnalysisLogs,
35    ) -> GritResult<Q::ResolvedPattern<'a>> {
36        context.call_built_in(self, context, state, logs)
37    }
38}
39
40impl<Q: QueryContext> Evaluator<Q> for CallBuiltIn<Q> {
41    fn execute_func<'a>(
42        &'a self,
43        state: &mut State<'a, Q>,
44        context: &'a Q::ExecContext<'a>,
45        logs: &mut AnalysisLogs,
46    ) -> GritResult<FuncEvaluation<Q>> {
47        let resolved = self.call(state, context, logs)?;
48        Ok(FuncEvaluation {
49            predicator: resolved.is_truthy(state, context.language())?,
50            ret_val: Some(resolved),
51        })
52    }
53}
54
55impl<Q: QueryContext> PatternName for CallBuiltIn<Q> {
56    fn name(&self) -> &'static str {
57        "CALL_BUILT_IN"
58    }
59}