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
/// A [`Context`] is an associative list which assigns each element with type information.
#[derive(Clone, Debug)]
pub struct Context<K, T>(Vec<(K, T)>);

impl<K: Eq + Clone, T: Clone> Context<K, T> {
    pub fn empty() -> Context<K, T> {
        Context(vec![])
    }

    pub fn from(ctx: Vec<(K, T)>) -> Context<K, T> {
        Context(ctx)
    }

    pub fn lookup(&self, v: &K) -> Option<T> {
        for (v0, t) in self.0.iter().rev() {
            if v0 == v {
                return Some(t.clone());
            }
        }
        None
    }

    pub fn extend(&self, v: K, t: T) -> Context<K, T> {
        let mut result = self.clone();
        result.0.push((v, t));
        result
    }

    pub fn extend_from(&self, another: &Context<K, T>) -> Context<K, T> {
        let mut result = self.clone();
        for (v, t) in another.0.iter() {
            result.0.push((v.clone(), t.clone()));
        }
        result
    }

    pub fn into_inner(self) -> Vec<(K, T)> {
        self.0
    }
}

impl<K, T> std::ops::Deref for Context<K, T> {
    type Target = Vec<(K, T)>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<K: std::fmt::Display, T: std::fmt::Display> std::fmt::Display for Context<K, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "[")?;
        for (i, (name, v)) in self.0.iter().enumerate() {
            write!(f, "{name} : {v}")?;
            if i + 1 < self.0.len() {
                write!(f, ", ")?;
            }
        }
        write!(f, "]")?;
        Ok(())
    }
}