2020-11-02 00:11:12 +00:00
|
|
|
/* Serene --- Yet an other Lisp
|
2020-11-01 19:40:07 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-11-01 20:15:07 +00:00
|
|
|
*/
|
|
|
|
use crate::core::read_eval_print;
|
2020-11-10 20:14:30 +00:00
|
|
|
use crate::namespace;
|
|
|
|
use crate::runtime;
|
2020-11-01 19:40:07 +00:00
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
use rustyline::Editor;
|
|
|
|
|
2020-11-10 20:14:30 +00:00
|
|
|
pub fn repl(rt: &runtime::Runtime) {
|
2020-11-01 19:40:07 +00:00
|
|
|
let mut rl = Editor::<()>::new();
|
2020-11-10 20:14:30 +00:00
|
|
|
let history = runtime::history_file_path();
|
2020-11-01 19:40:07 +00:00
|
|
|
|
2020-11-10 20:14:30 +00:00
|
|
|
if rl.load_history(&history).is_err() && runtime::is_debug(&rt) {
|
2020-11-01 19:40:07 +00:00
|
|
|
println!("No previous history.");
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
2020-11-10 20:14:30 +00:00
|
|
|
let prompt = format!("{}> ", namespace::get_name(runtime::current_ns(rt)));
|
2020-11-01 19:40:07 +00:00
|
|
|
let readline = rl.readline(&prompt);
|
|
|
|
match readline {
|
|
|
|
Ok(line) => {
|
|
|
|
rl.add_history_entry(line.as_str());
|
2020-11-01 20:15:07 +00:00
|
|
|
read_eval_print(&rt, &line);
|
2020-11-01 19:40:07 +00:00
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) => {
|
|
|
|
println!("CTRL-C");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(ReadlineError::Eof) => {
|
|
|
|
println!("CTRL-D");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rl.save_history(&history).unwrap();
|
|
|
|
}
|