summaryrefslogtreecommitdiff
path: root/src/eval/capture.rs
blob: 182468f7e6e10e5c2ebff898949b257a8b48d219 (plain) (blame)
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
use std::rc::Rc;

use super::*;
use crate::syntax::visit::*;

/// A visitor that captures variable slots.
#[derive(Debug)]
pub struct CapturesVisitor<'a> {
    external: &'a Scopes<'a>,
    internal: Scopes<'a>,
    captures: Scope,
}

impl<'a> CapturesVisitor<'a> {
    /// Create a new visitor for the given external scopes.
    pub fn new(external: &'a Scopes) -> Self {
        Self {
            external,
            internal: Scopes::new(),
            captures: Scope::new(),
        }
    }

    /// Return the scope of captured variables.
    pub fn finish(self) -> Scope {
        self.captures
    }
}

impl<'ast> Visit<'ast> for CapturesVisitor<'_> {
    fn visit_expr(&mut self, node: &'ast Expr) {
        match node {
            Expr::Ident(ident) => {
                // Find out whether the identifier is not locally defined, but
                // captured, and if so, capture its value.
                if self.internal.get(ident).is_none() {
                    if let Some(slot) = self.external.get(ident) {
                        self.captures.def_slot(ident.as_str(), Rc::clone(slot));
                    }
                }
            }
            expr => visit_expr(self, expr),
        }
    }

    fn visit_binding(&mut self, id: &'ast Ident) {
        self.internal.def_mut(id.as_str(), Value::None);
    }

    fn visit_enter(&mut self) {
        self.internal.enter();
    }

    fn visit_exit(&mut self) {
        self.internal.exit();
    }
}