procc_ll/
context.rs

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use crate::token::Token;
use crate::Program;
use crate::values::Values;
/// structure that contains all the data at the count level, saves the variables, tokens, functions and keys
/// Use example :
/// ```
///  use procc_ll::context::Context;
///  let mut context = Context::new();
///  context.push_key("key".to_owned(), |token, prog| {
///     prog.exec(&token);
///     procc_ll::values::Values::Null
///  });
/// ```
#[derive(Clone)]
pub struct Context {
    pub(crate) tokens: Rc<RefCell<Vec<Rc<RefCell<Box<dyn Token + 'static>>>>>>,
    pub(crate) functions: Rc<RefCell<HashMap<String, Arc<dyn Fn(Vec<Values>, &mut Program) -> Values>>>>,
    pub(crate) memory: Rc<RefCell<HashMap<String , Values>>>,
    pub(crate) keys: Rc<RefCell<HashMap<String, Arc<dyn Fn(String, &mut Program) -> Values>>>>,
    pub(crate) sub_context: Option<Box<Context>>
}

impl Context {
    /// Create a new instance of Context
    pub fn new<'b>() -> Context {
        Context {
            tokens: Rc::new(RefCell::new(Vec::new())),
            memory: Rc::new(RefCell::new(HashMap::new())),
            keys: Rc::new(RefCell::new(HashMap::new())),
            functions: Rc::new(RefCell::new(HashMap::new())),
            sub_context: None
        }
    }
    /// Localize the index in the list of tokens of the token
    ///
    /// # PANICS
    /// Return a panic if the token is not registered on the context
    pub fn token_index(&self, tok: &str) -> usize {
        for ix in 0..self.tokens.borrow().len() {
            let def_tok = &self.tokens.borrow()[ix];
            if def_tok.borrow().is_token(&tok) { return ix; }
        }
        panic!("Token \"{}\" no registered", tok);
    }
    /// Register a new token in te context
    pub fn push_token(&mut self, tok: Box<dyn Token>) {
        self.tokens.borrow_mut().push(Rc::new(RefCell::new(tok)));
    }
    /// Push a new data on the memory
    pub fn push_memory(&mut self, key: &str, tok: Values) {
        if !self.memory.borrow().contains_key(key) {
            if let Some(subc) = &mut self.sub_context {
                subc.push_memory(key, tok);
                return;
            }
        }
        self.memory.borrow_mut().insert(key.to_owned(),tok);
    }
    /// Register a new key
    pub fn push_key(&mut self, key: String, tok: impl Fn(String, &mut Program) -> Values + 'static) {
        self.keys.borrow_mut().insert(key, Arc::new(tok));
    }
    /// Add a new function
    pub fn push_function(&mut self, name: String, func: impl Fn(Vec<Values>, &mut Program) -> Values + 'static) {
        if let Some(subc) = &mut self.sub_context {
            if !self.functions.borrow().contains_key(&name) {
                subc.push_function(name, func);
                return;
            }
        }
        self.functions.borrow_mut().insert(name, Arc::new(func));
    }
    pub fn get_token(&self, idx: usize) -> Rc<RefCell<Box<dyn Token + 'static>>> {
        self.tokens.borrow().get(idx).unwrap().clone()
    }
    pub fn get_memory(&self, key: &str) -> Values {
        if !self.memory.borrow().contains_key(key) {
            if let Some(subc) = &self.sub_context {
                if subc.memory.borrow().contains_key(key) {
                    return subc.get_memory(key);
                } else {
                    panic!("Memory \"{}\" no longer exists", key);
                }
            } else {
                panic!("Memory {} not pushed", key);
            }
        }
        self.memory.borrow().get(key).unwrap().clone()
    }
    pub fn get_key(&self, key: &str) -> Arc<dyn Fn(String, &mut Program) -> Values> {
        self.keys.borrow().get(key).unwrap().clone()
    }
    pub fn get_function(&self, key: &str) -> Arc<dyn Fn(Vec<Values>, &mut Program) -> Values> {
        if !self.functions.borrow().contains_key(key) {
            if let Some(subc) = &self.sub_context {
                if subc.functions.borrow().contains_key(key) {
                    return subc.get_function(key);
                } else {
                    panic!("Function \"{}\" no longer exists", key);
                }
            } else {
                panic!("Function {} not pushed", key);
            }
        }
        self.functions.borrow().get(key).unwrap().clone()
    }


    pub fn has_token(&self, idx: usize) -> bool {
        self.tokens.borrow().len() > idx
    }
    pub fn has_memory(&self, key: &str) -> bool {
        let has_sub_context = {
            if let Some(subc) = &self.sub_context {
                subc.has_memory(key)
            } else {
                false
            }
        };
        self.memory.borrow().contains_key(key) || has_sub_context
    }
    pub fn has_key(&self, key: &str) -> bool {
        self.keys.borrow().contains_key(key)
    }
    pub fn has_function(&self, key: &str) -> bool {
        let has_sub_context = {
            if let Some(subc) = &self.sub_context {
                subc.has_function(key)
            } else {
                false
            }
        };
        self.functions.borrow().contains_key(key) || has_sub_context
    }

    pub fn gifs_token(&self, idx: usize) -> Option<Rc<RefCell<Box<dyn Token + 'static>>>> {
        if self.has_token(idx) {
           Some(self.get_token(idx))
        } else { None }
    }
    pub fn gifs_memory(&self, key: &str) -> Option<Values> {
        if self.has_memory(key) {
            Some(self.get_memory(key))
        } else { None }
    }
    pub fn gifs_key(&self, key: &str) -> Option<Arc<dyn Fn(String, &mut Program) -> Values>> {
        if self.has_memory(key) {
            Some(self.get_key(key))
        } else { None }
    }
    pub fn gifs_function(&self, key: &str) -> Option<Arc<dyn Fn(String, &mut Program) -> Values>> {
        if self.has_memory(key) {
            Some(self.get_key(key))
        } else { None }
    }
}