procc_ll/
lib.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::cell::RefCell;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
use log::{debug};
use regex::Regex;
use crate::context::Context;
use crate::Errors::{FunctionNotFound, TokenNotMatched};
use crate::token::Token;

pub mod context;
pub mod token;
#[cfg(test)]
mod tests;

///measures the execution time and prints it on the screen,
/// example:
///```
/// use procc_ll::measure_time;
/// measure_time! ({println! ("hello ") });
/// ```
/// return the result of the code inside, only print the execution time if the RUST_LOG = debug
#[macro_export]
macro_rules! measure_time_debug {
    ($expression:expr) => {{
        use std::time::Instant;

        let start = Instant::now();
        let result = $expression;  // Ejecuta la expresión
        let duration = start.elapsed();

        debug!("Run Time: {:?}", duration);

        result  // Retorna el resultado de la expresión
    }};
}
#[macro_export]
macro_rules! measure_time {
    ($expression:expr) => {{
        use std::time::Instant;

        let start = Instant::now();
        let result = $expression;  // Ejecuta la expresión
        let duration = start.elapsed();

        println!("Run Time: {:?}", duration);

        result  // Retorna el resultado de la expresión
    }};
}


lazy_static::lazy_static! {
    static ref FUNC_REGEX: Regex = Regex::new(r"(.+)\((.+)\)").unwrap();
}
/// is a part of the program, contains the context, i is capable of executing
/// Use example:
/// ```
/// use procc_ll::Program;
/// let mut main = Program::new();
///
/// // now use the main ProgramBlock
/// ```

#[derive(Clone)]
pub struct Program {
    pub context: crate::context::Context,
}
impl Program {
    pub fn new() -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(Program { context: crate::context::Context::new() }))
    }
    pub fn new_from_context(context: crate::context::Context) -> Self {
        Program { context }
    }
    /// Executes a new token
    /// Use example:
    /// ```
    /// use procc_ll::Program;
    ///
    /// let mut main = Program::new();
    ///
    /// main.borrow_mut().push_internal_key("echo", |tok, prog| {
    ///     print!("{}" ,tok);
    ///     procc_ll::Values::Null
    /// });
    /// main.borrow_mut().exec("echo hello world!!");
    pub fn exec(&mut self, token: &str) -> Result<Values, Errors> {

        let token = token.trim();
        let token = token.replace("\n", "");

        // check keys
        debug!("[E] split token");
        let split: Vec<String> = measure_time_debug!({token.to_owned().split_whitespace() // Divide la cadena en espacios
            .map(|s| s.to_string()) // Convierte cada &str a String
            .collect()}); // Recolecta en un Vec<String>

        if self.context.keys.borrow().contains_key(&split[0]) {
            debug!("[V] token is has key");
            let content = token.replace(&split[0], "").trim().to_string();
            debug!("[E] getting key function");
            let func = measure_time_debug!({self.context.get_key(&split[0])});
            return Ok(func(content, self));
        }

        // checking import
        debug!("[E] checking references");
        if token.starts_with("$") {
            debug!("[E] token is reference");
            let name = token.replace("$", "").trim().to_string();
            debug!("[E] getting memory value: {}", &name);
            return Ok(self.context.get_memory(&name));
        }

        // check functions
        debug!("[E] checking function");
        debug!("[REGEX] Matching regex");
        if measure_time_debug!({FUNC_REGEX.is_match(&token)}) {
            debug!("[V] token is function");
            debug!("[E] getting function infos");
            let (name, args): (String, String) = if let Some(cap) = FUNC_REGEX.captures(&token) {
                debug!("[V] getting name");
                let name = measure_time_debug!({cap.get(1).map_or("", |m| m.as_str()).to_string()});
                debug!("[E] getting args");
                let args = measure_time_debug!({cap.get(2).map_or("", |m| m.as_str()).to_string()});
                (name, args)
            } else { ("".to_owned(), "".to_owned()) };
            debug!("[INF] function data {} {}", name, args);
            if !self.context.functions.borrow().contains_key(&name) { return Err(FunctionNotFound(format!("function call \"{}\" but function \"{}\" not found", token, &name))); }
            let func = { self.context.get_function(&name) };

            let mut pargs: Vec<Values> = Vec::new();

            for arg in args.split(",") {
                let res = self.exec(arg);
                if res.is_err() {
                    return Err(res.err().unwrap())
                } else {
                    pargs.push(res?);
                }
            }

            return Ok((func)(pargs, self));
        }

        // if is not key
        debug!("[E] other tokens definition");
        debug!("[E] getting token definition index");
        let index = measure_time_debug!({self.context.token_index(&token)});
        debug!("[E] getting token");
        if let None = index { return Err(Errors::TokenNotMatched(String::from(format!("token \"{}\" not matched with the registered token samples", token)))); }
        let def_tok = measure_time_debug!({self.context.get_token(index.unwrap())});

        debug!("[E] executing function");
        let val = measure_time_debug!({def_tok.borrow().exec(&token, self).unwrap()});

        Ok(val)
    }
    /// Push a new token in the context
    pub fn push_internal_token(&mut self, token: Box<dyn Token>) {
        self.context.push_token(token);
    }
    /// push a new key on the context
    pub fn push_internal_key(&mut self, key: &str, func: impl Fn(String, &mut Program) -> Values + 'static) {
        self.context.push_key(key.to_owned(), func);
    }
    /// push a new value on the context
    pub fn push_internal_memory(&mut self, key: &str, val: Values) {
        self.context.push_memory(key, val);
    }
    /// push a new function on the context
    pub fn push_internal_function(&mut self, name: &str, func: impl Fn(Vec<Values>, &mut Program) -> Values + 'static) {
        self.context.push_function(name.to_owned(), func);
    }
    pub fn new_depth_context(&mut self) -> Rc<RefCell<Program>> {
        let mut clone = self.clone();
        clone.context.sub_context = Some(Box::new(Context::new()));

        Rc::new(RefCell::new(clone))
    }
}
#[derive(Clone)]
pub enum Errors {
    Non,
    TokenNotMatched(String),
    FunctionNotFound(String),
}
impl Errors {
    pub fn to_str(&self) -> String {
        let (name, message) = match self {
            TokenNotMatched(msg) => ("ERRORS::TOKEN_NOT_FOUND", msg),
            Errors::FunctionNotFound(msg) => ("ERRORS::FUNCTION_NOT_FOUND", msg),
            _ => { ("ERRORS::UNKNOWN", &"UNKNOWN ERROR".to_owned()) }
        };
        format!("ERROR PROCESSING: {} : {}", name, message)
    }
}
impl Debug for Errors {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&*self.to_str())
    }
}
/// Values that retune the functions, tokens, keys i that also returns the exec of ProgramBlock
#[derive(PartialEq, Clone)]
pub enum Values {
    String(String),
    Number(f64),
    Boolean(bool),
    Array(Vec<Values>),
    Null
}
impl Debug for Values {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Values::String(v) => write!(f, "{}", v),
            Values::Number(v) => write!(f, "{}", v),
            Values::Boolean(v) => write!(f, "{}", v),
            Values::Array(v) => { write!(f, "[")?; v.fmt(f) },
            _ => { write!(f, "null") }
        }
    }
}