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
//! binks is an interpreted dynamically typed embededd scripting language
//! trying to be as simple as python and as clean looking as possible, written 100% in rust
//! 
//! ```no_run
//! # use std::boxed::Box;
//! # use std::error::Error;
//! use binks::preload::*;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!     let x = binks::run(r#"
//!         let x = 10
//!         x = x + 1
//!     "#)?.vars["x"].clone();
//!
//!     assert_eq!(x, Int(11));
//!     println!("{:?}", x);
//!    
//!     Ok(())
//! }
//! ```
//! 
//! note that to use binks stdlib when you use it as a crate you will need the [binks github repo](https://github.com/omrihhh/binks) cloned and in your path (hopefuly this wont be the case soon)

/// function struct and its behavier
pub mod functions;
/// imports of binks files and dynamic librarys
pub mod import;
/// the interpreter the Program struct and the Data enum
pub mod interpreter;
/// the tokenizer and Token enum
pub mod lexer;
/// oop items
pub mod oop;
/// Token form expresion to data for every one of the primitives and a whoel expretion 
pub mod vars;

mod utils;

use crossbeam_channel::unbounded;
use std::thread;

/// run the code T where T: Into\<String\> and reurtns the Program that results in it
pub fn run<T>(code: T) -> anyhow::Result<interpreter::line_handeler::Program>
where
    T: Into<String>,
    T: Send,
{
    // initalize the main program
    let mut program = interpreter::line_handeler::Program::new();

    // initalize an unbounded chanle to send the expretions token represenation to the interpreter multythreaddedly 
    let (s, r) = unbounded();

    let code = code.into();

    let tokenizer = thread::spawn(move || {
        lexer::tokenizer::make_tokens(code.replace("\r", "").trim(), s);
    });

    // crate the thread the main program will run in
    let builder = thread::Builder::new()
        .name("binks main".into())
        .stack_size((2.0 * 1024.0 * 1024.0) as usize);

    // build the handeler from the builder above
    let handler = builder
        .spawn(move || {
            for line in r.iter() {
                let line = line;
                match program.handel_line(line.clone()) {
                    Ok(_) => {}
                    Err(err) => {
                        eprintln!("\nerror at line {}:\n{}", program.line, err);
                        break;
                    }
                }
                program.line += line
                    .iter()
                    .filter(|tok| **tok == lexer::token::Token::Semicolon)
                    .count() as i64
                    + 1
                    - line
                        .iter()
                        .filter(|tok| **tok == lexer::token::Token::MinusLineMark)
                        .count() as i64;
            }
            program
        })
        .unwrap();

    // join the program to the main thread and save te program the returned from the handeler thread
    let program = handler.join().unwrap();
    tokenizer.join().unwrap();

    Ok(program)
}

/// contain the mod usful features of the crate for you to not have to import them manualy 
pub mod preload {
    pub use super::interpreter::line_handeler::{Data::*, Program};
    pub use super::oop::oop_items;
}

#[cfg(test)]
mod test {
    use super::run;
    use super::preload::*;
    #[test]
    pub fn simple_math() -> anyhow::Result<()> {
        let p = run("let x = 10 + 2")?;
        assert_eq!(p.vars["x"], Int(12));
        Ok(())
    }

    #[test]
    pub fn objects() -> anyhow::Result<()> {
        let p = run(r#"
            use "binks:IO.binks" as Io

            class Object {
                fn Object(self) {
                    self.data = "Hello World!" 
                }

                fn print_data(self) {
                    Io.print(self.data)
                }
            }

            let o = new Object()
            o.print_data()
        "#)?;

        let mut o = oop_items::Object::new("Object");
        o.fields
            .insert(String::from("data"), Str(String::from("Hello World!")));
        assert_eq!(p.vars["o"], Object(o));
        Ok(())
    }
}