summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/resolve/env.rs
blob: 43676ccba3fc6f65f9eeec613bccfdd71a5bf767 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
use std::collections::HashMap;

use crate::error::{Error, ImportError};
use crate::semantics::{AlphaVar, Import, TypedHir, VarEnv};
use crate::syntax::{Label, V};

/// Environment for resolving names.
#[derive(Debug, Clone)]
pub(crate) struct NameEnv {
    names: Vec<Label>,
}

pub(crate) type ImportCache = HashMap<Import, TypedHir>;
pub(crate) type ImportStack = Vec<Import>;

/// Environment for resolving imports
#[derive(Debug, Clone)]
pub(crate) struct ImportEnv {
    cache: ImportCache,
    stack: ImportStack,
}

impl NameEnv {
    pub fn new() -> Self {
        NameEnv { names: Vec::new() }
    }
    pub fn as_varenv(&self) -> VarEnv {
        VarEnv::from_size(self.names.len())
    }

    pub fn insert(&self, x: &Label) -> Self {
        let mut env = self.clone();
        env.insert_mut(x);
        env
    }
    pub fn insert_mut(&mut self, x: &Label) {
        self.names.push(x.clone())
    }
    pub fn remove_mut(&mut self) {
        self.names.pop();
    }

    pub fn unlabel_var(&self, var: &V) -> Option<AlphaVar> {
        let V(name, idx) = var;
        let (idx, _) = self
            .names
            .iter()
            .rev()
            .enumerate()
            .filter(|(_, n)| *n == name)
            .nth(*idx)?;
        Some(AlphaVar::new(idx))
    }
    pub fn label_var(&self, var: &AlphaVar) -> V {
        let name = &self.names[self.names.len() - 1 - var.idx()];
        let idx = self
            .names
            .iter()
            .rev()
            .take(var.idx())
            .filter(|n| *n == name)
            .count();
        V(name.clone(), idx)
    }
}

impl ImportEnv {
    pub fn new() -> Self {
        ImportEnv {
            cache: HashMap::new(),
            stack: Vec::new(),
        }
    }

    pub fn handle_import(
        &mut self,
        import: Import,
        mut do_resolve: impl FnMut(&mut Self, &Import) -> Result<TypedHir, Error>,
    ) -> Result<TypedHir, Error> {
        if self.stack.contains(&import) {
            return Err(
                ImportError::ImportCycle(self.stack.clone(), import).into()
            );
        }
        Ok(match self.cache.get(&import) {
            Some(expr) => expr.clone(),
            None => {
                // Push the current import on the stack
                self.stack.push(import.clone());

                // Resolve the import recursively
                let expr = do_resolve(self, &import)?;

                // Remove import from the stack.
                self.stack.pop();

                // Add the import to the cache
                self.cache.insert(import, expr.clone());

                expr
            }
        })
    }
}