mlua/examples/repl.rs
2022-07-25 14:14:01 +01:00

49 lines
1.4 KiB
Rust

//! This example shows a simple read-evaluate-print-loop (REPL).
use mlua::{Error, Lua, MultiValue};
use rustyline::Editor;
fn main() {
let lua = Lua::new();
let mut editor = Editor::<()>::new().expect("Failed to make rustyline editor");
loop {
let mut prompt = "> ";
let mut line = String::new();
loop {
match editor.readline(prompt) {
Ok(input) => line.push_str(&input),
Err(_) => return,
}
match lua.load(&line).eval::<MultiValue>() {
Ok(values) => {
editor.add_history_entry(line);
println!(
"{}",
values
.iter()
.map(|value| format!("{:?}", value))
.collect::<Vec<_>>()
.join("\t")
);
break;
}
Err(Error::SyntaxError {
incomplete_input: true,
..
}) => {
// continue reading input and append it to `line`
line.push_str("\n"); // separate input lines
prompt = ">> ";
}
Err(e) => {
eprintln!("error: {}", e);
break;
}
}
}
}
}