27 lines
846 B
Rust
27 lines
846 B
Rust
use rustyline::{error::ReadlineError, Editor};
|
|
|
|
use crate::{error::Error, parser::read};
|
|
|
|
pub fn repl() -> rustyline::Result<()> {
|
|
let prompt = "#=> ";
|
|
let mut rl = Editor::<()>::new()?;
|
|
loop {
|
|
match rl.readline(prompt) {
|
|
Ok(line) => match read(&line) {
|
|
Ok(values) => values.iter().for_each(|e| println!("{e}")),
|
|
Err(e) => e.into_iter().map(Error::Parse).for_each(|e| {
|
|
if let Err(e) = e.report(&line) {
|
|
eprintln!("Failed to generate error report\n{e:?}")
|
|
}
|
|
}),
|
|
},
|
|
Err(ReadlineError::Eof) => {
|
|
println!("Bye!");
|
|
break Ok(());
|
|
}
|
|
Err(ReadlineError::Interrupted) => (),
|
|
Err(e) => return Err(e),
|
|
}
|
|
}
|
|
}
|