mlua/examples/repl.rs

49 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2017-06-16 18:19:21 -05:00
//! This example shows a simple read-evaluate-print-loop (REPL).
2019-10-17 10:59:33 -05:00
use mlua::{Error, Lua, MultiValue};
use rustyline::Editor;
2017-06-16 18:19:21 -05:00
fn main() {
let lua = Lua::new();
2022-07-25 08:14:01 -05:00
let mut editor = Editor::<()>::new().expect("Failed to make rustyline editor");
2017-06-16 18:19:21 -05:00
loop {
let mut prompt = "> ";
2017-06-16 18:19:21 -05:00
let mut line = String::new();
loop {
match editor.readline(prompt) {
Ok(input) => line.push_str(&input),
Err(_) => return,
}
2019-10-17 10:59:33 -05:00
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;
}
2017-10-23 15:42:20 -05:00
Err(Error::SyntaxError {
incomplete_input: true,
..
}) => {
// continue reading input and append it to `line`
2017-08-03 01:03:28 -05:00
line.push_str("\n"); // separate input lines
prompt = ">> ";
}
Err(e) => {
eprintln!("error: {}", e);
break;
}
2017-06-16 18:19:21 -05:00
}
}
}
}