2017-06-16 19:19:21 -04:00
|
|
|
//! This example shows a simple read-evaluate-print-loop (REPL).
|
|
|
|
|
2019-10-17 11:59:33 -04:00
|
|
|
use mlua::{Error, Lua, MultiValue};
|
2017-08-02 08:32:45 -04:00
|
|
|
use rustyline::Editor;
|
2017-06-16 19:19:21 -04:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let lua = Lua::new();
|
2022-07-25 09:14:01 -04:00
|
|
|
let mut editor = Editor::<()>::new().expect("Failed to make rustyline editor");
|
2017-06-16 19:19:21 -04:00
|
|
|
|
|
|
|
loop {
|
2017-08-02 08:32:45 -04:00
|
|
|
let mut prompt = "> ";
|
2017-06-16 19:19:21 -04:00
|
|
|
let mut line = String::new();
|
|
|
|
|
2017-06-17 08:43:05 -04:00
|
|
|
loop {
|
2017-08-02 08:32:45 -04:00
|
|
|
match editor.readline(prompt) {
|
|
|
|
Ok(input) => line.push_str(&input),
|
|
|
|
Err(_) => return,
|
|
|
|
}
|
2017-06-17 08:43:05 -04:00
|
|
|
|
2019-10-17 11:59:33 -04:00
|
|
|
match lua.load(&line).eval::<MultiValue>() {
|
2017-06-17 08:43:05 -04:00
|
|
|
Ok(values) => {
|
2018-09-24 21:27:29 -04:00
|
|
|
editor.add_history_entry(line);
|
2017-06-20 19:04:25 -04:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
values
|
|
|
|
.iter()
|
|
|
|
.map(|value| format!("{:?}", value))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\t")
|
|
|
|
);
|
2017-06-17 08:43:05 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-10-23 16:42:20 -04:00
|
|
|
Err(Error::SyntaxError {
|
|
|
|
incomplete_input: true,
|
|
|
|
..
|
|
|
|
}) => {
|
2017-06-17 08:43:05 -04:00
|
|
|
// continue reading input and append it to `line`
|
2017-08-03 02:03:28 -04:00
|
|
|
line.push_str("\n"); // separate input lines
|
2017-08-02 08:32:45 -04:00
|
|
|
prompt = ">> ";
|
2017-06-17 08:43:05 -04:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2017-08-02 08:32:45 -04:00
|
|
|
eprintln!("error: {}", e);
|
2017-06-17 08:43:05 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-06-16 19:19:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|