grit_pattern_matcher/pattern/
call.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use super::{
    function_definition::FunctionDefinition,
    functions::{Evaluator, FuncEvaluation},
    patterns::Matcher,
    patterns::{Pattern, PatternName},
    State,
};
use crate::{context::ExecContext, context::QueryContext};
use grit_util::{
    error::{GritPatternError, GritResult},
    AnalysisLogs,
};

#[derive(Clone, Debug)]
pub struct Call<Q: QueryContext> {
    pub index: usize,
    pub args: Vec<Option<Pattern<Q>>>,
}

impl<Q: QueryContext> Call<Q> {
    pub fn new(index: usize, args: Vec<Option<Pattern<Q>>>) -> Self {
        Self { index, args }
    }
}

impl<Q: QueryContext> PatternName for Call<Q> {
    fn name(&self) -> &'static str {
        "CALL"
    }
}

// todo parameters, and name should both be usize references
// argument should throw an error if its not a parameter at compile time
impl<Q: QueryContext> Matcher<Q> for Call<Q> {
    fn execute<'a>(
        &'a self,
        binding: &Q::ResolvedPattern<'a>,
        state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        logs: &mut AnalysisLogs,
    ) -> GritResult<bool> {
        let pattern_definition = &context.pattern_definitions()[self.index];

        pattern_definition.call(state, binding, context, logs, &self.args)
    }
}

#[derive(Debug, Clone)]
pub struct PrCall<Q: QueryContext> {
    pub(crate) index: usize,
    pub args: Vec<Option<Pattern<Q>>>,
}

impl<Q: QueryContext> PrCall<Q> {
    pub fn new(index: usize, args: Vec<Option<Pattern<Q>>>) -> Self {
        Self { index, args }
    }
}

impl<Q: QueryContext> PatternName for PrCall<Q> {
    fn name(&self) -> &'static str {
        "PREDICATE_CALL"
    }
}

impl<Q: QueryContext> Evaluator<Q> for PrCall<Q> {
    fn execute_func<'a>(
        &'a self,
        state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        logs: &mut AnalysisLogs,
    ) -> GritResult<FuncEvaluation<Q>> {
        let predicate_definition = &context.predicate_definitions().get(self.index);
        if let Some(predicate_definition) = predicate_definition {
            let predicator = predicate_definition.call(state, context, &self.args, logs)?;
            Ok(FuncEvaluation {
                predicator,
                ret_val: None,
            })
        } else {
            let function_definition = &context.function_definitions().get(self.index);
            if let Some(function_definition) = function_definition {
                let res = function_definition.call(state, context, &self.args, logs)?;
                Ok(res)
            } else {
                Err(GritPatternError::new(format!(
                    "predicate or function definition not found: {}. Try running grit init.",
                    self.index
                )))
            }
        }
    }
}