2021-05-02 08:43:25 -05:00
|
|
|
use logos::Source;
|
2021-05-01 06:44:58 -05:00
|
|
|
use rustyline::Editor;
|
|
|
|
|
2021-05-25 13:26:01 -05:00
|
|
|
use crate::{interpret::ExecEnv, parser::Parser};
|
2021-05-01 06:44:58 -05:00
|
|
|
|
|
|
|
pub fn repl() {
|
|
|
|
let mut rl = Editor::<()>::new();
|
2021-05-25 13:26:01 -05:00
|
|
|
let mut env = ExecEnv::new();
|
2021-05-01 06:44:58 -05:00
|
|
|
loop {
|
|
|
|
let readline = rl.readline(":: ");
|
|
|
|
match readline {
|
|
|
|
Ok(line) => {
|
|
|
|
if &line == "exit" {
|
|
|
|
println!("bye");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let mut parser = Parser::new(&line);
|
|
|
|
let ast = parser.init();
|
2021-05-02 08:43:25 -05:00
|
|
|
match ast {
|
2021-05-20 18:18:01 -05:00
|
|
|
Ok(ast) => {
|
|
|
|
println!("{:?}", ast);
|
2021-05-25 13:26:01 -05:00
|
|
|
println!("{:?}", env.eval_items(&ast));
|
2021-06-04 18:56:26 -05:00
|
|
|
}
|
2021-05-02 08:43:25 -05:00
|
|
|
Err(e) => {
|
|
|
|
println!(
|
2021-06-05 08:50:20 -05:00
|
|
|
"Error `{:?}` occurred at span: {:?} = `{:?}`",
|
2021-05-02 08:43:25 -05:00
|
|
|
e.kind,
|
2021-06-07 14:28:24 -05:00
|
|
|
e.span.clone(),
|
|
|
|
line.slice(e.span.clone())
|
2021-06-04 18:56:26 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
println!(" | {}", line);
|
|
|
|
println!(
|
|
|
|
" {}{}-- Here",
|
2021-06-07 14:28:24 -05:00
|
|
|
" ".repeat(e.span.start),
|
|
|
|
"^".repeat((e.span.end - e.span.start).max(1))
|
2021-05-02 08:43:25 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 06:44:58 -05:00
|
|
|
}
|
|
|
|
Err(rustyline::error::ReadlineError::Eof) => {
|
|
|
|
println!("bye");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(rustyline::error::ReadlineError::Interrupted) => (),
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error: {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|