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
use crate::Repl;
use std::{collections::HashMap, fs};
/// the interpreter struct that contains all logic and code for interpreter creation
pub struct Interpreter {
    pub(crate) functions: HashMap<String, fn(Vec<String>)>,
}

impl Interpreter {
    /// create a new blank interpreter
    pub fn new() -> Interpreter {
        Interpreter {
            functions: HashMap::new(),
        }
    }
    /// create an interpreter from a repl
    pub fn from_repl(repl: Repl) -> Interpreter {
        Interpreter {
            functions: repl.functions,
        }
    }
    /// add a function to an interpreter
    pub fn add_function(&mut self, name: String, function: fn(Vec<String>)) {
        self.functions.insert(name, function);
    }
    /// run the interpreter
    pub fn run(&self, filename: &str) {
        let mut arg: Vec<String>;
        for line in fs::read_to_string(filename)
            .expect("could not read file")
            .split("\n")
        {
            arg = line
                .trim_end_matches("\r")
                .split(" ")
                .map(|x| x.to_string())
                .collect();
            if self.functions.contains_key(&arg[0]) {
                self.functions[&arg[0]](arg[1..arg.len()].to_vec());
            }
        }
    }
    // runs the interpreter in debug mode
    pub fn run_debug(&self, filename: &str) {
        let mut arg: Vec<String>;
        for line in fs::read_to_string(filename)
            .expect("could not read file")
            .split("\n")
        {
            arg = line
                .trim_end_matches("\r")
                .split(" ")
                .map(|x| x.to_string())
                .collect();
            if self.functions.contains_key(&arg[0]) {
                dbg!(&self.functions, &arg, &arg.len());
                arg.iter().for_each(|x| {
                    println!("{}", dbg!(x).len());
                });
                dbg!("{}", arg[1..arg.len()].to_vec());
                self.functions[&arg[0]](arg[1..arg.len()].to_vec());
            }
        }
    }
}