ablescript/src/repl.rs
Alex Bethel dc5ef63fef Better error handling in REPL
We have much better spanned expression support, so now we get to show
it off!
2021-06-07 20:08:38 -05:00

51 lines
1.5 KiB
Rust

use logos::Source;
use rustyline::Editor;
use crate::{interpret::ExecEnv, parser::Parser};
pub fn repl() {
let mut rl = Editor::<()>::new();
let mut env = ExecEnv::new();
loop {
let readline = rl.readline(":: ");
match readline {
Ok(line) => {
if &line == "exit" {
println!("bye");
break;
}
let mut parser = Parser::new(&line);
let value = parser.init().and_then(|ast| {
println!("{:?}", &ast);
env.eval_stmts(&ast)
});
if let Err(e) = value {
println!(
"Error `{:?}` occurred at span: {:?} = `{:?}`",
e.kind,
e.span.clone(),
line.slice(e.span.clone())
);
println!(" | {}", line);
println!(
" {}{}-- Here",
" ".repeat(e.span.start),
"^".repeat((e.span.end - e.span.start).max(1))
);
}
}
Err(rustyline::error::ReadlineError::Eof) => {
println!("bye");
break;
}
Err(rustyline::error::ReadlineError::Interrupted) => (),
Err(e) => {
println!("Error: {:?}", e);
break;
}
}
}
}