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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::collections::HashMap;

use var::Var;
use eval::Eval;


/// Def alias used for internal evaluation purposes
pub type Def = HashMap<String, DefBlock>;

#[derive(Debug,PartialEq,Clone)]
pub struct DefBlock {
    pub name: String,
    pub data: HashMap<String,Var>
}

impl DefBlock {
    pub fn new(name: &str) -> DefBlock {
        DefBlock {
            name: name.to_owned(),
            data: HashMap::new()
        }
    }
}

impl Eval for Def {
    fn get (&self, path: Option<Vec<&str>>, lookup: &str) -> Option<Var> {
        if let Some(path) = path {
            if let Some(ref def) = self.get(path[0]) {
                if let Some(v) = def.data.get(lookup) {
                    return Some(v.clone())
                }
            }
        }

        None
    }

    fn get_last (&self, lookup: &str) -> Option<(Var, bool)> {
        let mut lookup = lookup;
        let mut resolved = None;

        loop { // resolve symbol references
            let (path,sym) = self.as_path(lookup);
            if let Some(mut path) = path {
                // NOTE: for now we are using nested paths as actual names
                // so we need to rebuild it as a full name if necessary
                let mut p = String::new();
                if path.len() > 1 {
                    p.push_str(path.remove(0));
                    p.push('.');
                    p.push_str(path.remove(0));
                }

                let path_final = {
                    if p.is_empty() { path[0] }
                    else { &p }
                };
                
                if let Some(ref def) = self.get(path_final) {
                    if let Some(v) = def.data.get(sym) {
                        match v {
                            &Var::Sym(ref sym) => {
                                if lookup != sym {
                                    resolved = Some(v.clone()); // take note that we resolved atleast once
                                    lookup = sym;
                                    continue
                                }
                                else {
                                    return Some((v.clone(), false))
                                }
                            },
                            _ => {
                                return Some((v.clone(), true))
                            }
                        }
                    }
                    else { break }
                }
                else { break }
            }
            else { break }
        }

        if let Some(v) = resolved { return Some((v, false)) } 

        None
    }

    #[allow(unused_variables)]
    fn set (&mut self, path: Option<Vec<&str>>, lookup: &str, var: Var) {
        if let Some(mut path) = path {
            // NOTE: for now we are using nested paths as actual names
            // so we need to rebuild it as a full name if necessary
            let mut p = String::new();
            let mut block_name = String::new();
            if path.len() > 1 {
                p.push_str(path.remove(0));
                p.push('.');
                let n = path.remove(0);
                block_name.push_str(&n);
                p.push_str(n);
            }

            let path_final = {
                if p.is_empty() { path[0] }
                else { &p }
            };
            
            if let Some(ref mut def) = self.get_mut(path_final) {
                let set;
                if let Some(v) = def.data.get_mut(lookup) {
                    *v = var;
                    set = None;
                }
                else { set = Some(var); }
                
                if let Some(var) = set { // NOTE: we're building this from scratch, this should be considered explicit instead
                    def.data.insert(lookup.to_owned(), var);
                }

                return
            }
            
            // if we didn't successfully insert, let's build from scratch the new block path
            let mut map = HashMap::new();
            map.insert(lookup.to_owned(), var);
            
            let def = DefBlock {
                name: block_name,
                data: map,
            };
            
            self.insert(path_final.to_owned(), def);
        }
    }

    #[allow(unused_variables)]
    fn call (&mut self, var: Var, fun: &str, vars: &Vec<Var>) -> Option<Var> {
        None
    }
}