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
use super::abc::*;
use ::incrust::Incrust;


pub enum ParentScope<'a> {
    Env(&'a Incrust),
    Context(&'a Context<'a>),
}

impl <'a> ParentScope<'a> {
    pub fn get(&self, id: &str) -> Option<&BType> {
        match *self {
            ParentScope::Env(ref env) => env.top_context().get(id),
            ParentScope::Context(ref context) => context.get(id)
        }
    }

    pub fn env(&self) -> &'a Incrust {
        match *self {
            ParentScope::Env(ref env) => env,
            ParentScope::Context(ref context) => context.env()
        }
    }
}

impl <'a> From<&'a Incrust> for ParentScope<'a> {
    fn from(x: &'a Incrust) -> Self {
        ParentScope::Env(x)
    }
}


pub struct Context<'a> {
    parent_scope: ParentScope<'a>,
    local_scope: &'a Args<'a>,
}


impl <'a> Context<'a> {
    pub fn new<PS: Into<ParentScope<'a>>>(parent_scope: PS, local_scope: &'a Args<'a>) -> Self {
        Context { parent_scope: parent_scope.into(), local_scope: local_scope }
    }

    pub fn nest(&'a self, local_scope: &'a Args<'a>) -> Self {
        Context { parent_scope: ParentScope::Context(self), local_scope: local_scope }
    }

    pub fn env(&self) -> &'a Incrust {
        self.parent_scope.env()
    }

    pub fn get(&self, id: &str) -> Option<&BType> {
        self.local_scope.get(id)
            .or_else(|| self.parent_scope.get(id))
    }
}