rust_lisp/
lib.rs

1#![forbid(unsafe_code)]
2
3pub mod interpreter;
4pub mod model;
5pub mod parser;
6pub mod utils;
7
8mod default_environment;
9pub use default_environment::default_env;
10
11#[macro_use]
12mod macros;
13
14use model::Env;
15use std::io::{self, prelude::*};
16use std::{cell::RefCell, rc::Rc};
17
18// 🦀 I am all over this project!
19/// Starts a REPL prompt at stdin/stdout. **This will block the current thread.**
20pub fn start_repl(env: Option<Env>) {
21    let env_rc = Rc::new(RefCell::new(env.unwrap_or_else(default_env)));
22
23    print!("> ");
24    io::stdout().flush().unwrap();
25    for line in io::stdin().lock().lines() {
26        match interpreter::eval_block(
27            env_rc.clone(),
28            parser::parse(&line.unwrap()).filter_map(|a| a.ok()),
29        ) {
30            Ok(val) => println!("{}", val),
31            Err(e) => println!("{}", e),
32        };
33
34        print!("> ");
35        io::stdout().flush().unwrap();
36    }
37
38    // Properly go to the next line after quitting
39    println!();
40}