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
use std::{collections::HashMap, fmt::Debug, hash::Hash};

use crate::immix::{mark::Mark, ptr::TypedPtr};

use super::{Object, ObjectTrait, ObjectType};

#[derive(Debug, Clone, PartialEq)]
pub struct Environment<T: Hash + Eq + PartialEq + Debug>
where
    T: Clone,
{
    binding: HashMap<T, TypedPtr<Object>>,
    outer: Option<Box<Environment<T>>>,
    // 0 for global, the bigger is the outter
    level: usize,
}

impl<T: Hash + Eq + PartialEq + Debug + Clone> Default for Environment<T> {
    fn default() -> Self {
        Self {
            binding: Default::default(),
            outer: Default::default(),
            level: Default::default(),
        }
    }
}

impl<T: Hash + Eq + PartialEq + Debug + Clone> Environment<T> {
    // get object clone from environment
    pub fn get_clone(&self, key: &T) -> Option<TypedPtr<Object>> {
        // first, try get object from current scope
        let rst = self.binding.get(key);

        if rst.is_none() && self.outer.is_some() {
            // if not found, try get object from outer scope
            return self.outer.as_ref().unwrap().get_clone(key);
        }
        rst.cloned()
    }

    // set object to environment
    pub fn set(
        &mut self,
        key: T,
        obj: TypedPtr<Object>,
    ) -> Option<TypedPtr<Object>> {
        self.binding.insert(key, obj)
    }

    pub fn new() -> Self {
        Environment {
            binding: HashMap::new(),
            outer: None,
            level: 0,
        }
    }

    pub fn capture(tgt: &Environment<T>) -> Self {
        let level = tgt.level + 1;

        let outer = (*tgt).clone();

        Environment {
            binding: HashMap::new(),
            level,
            outer: Some(Box::new(outer)),
        }
    }

    pub fn mark_all(&mut self) {
        for (_, ptr) in self.binding.iter_mut() {
            ptr.set_mark(&Mark::Marked);

            let obj = unsafe { &mut (*(ptr.as_ptr())) };
            if obj.get_type() == ObjectType::Function {
                let Object::Function(fun) = obj else {
                    unreachable!()
                };
                fun.env.mark_all();
            }
        }
        if self.outer.is_some() {
            self.outer.as_mut().unwrap().mark_all();
        }
    }
}