mlua/examples/repl.rs

52 lines
1.4 KiB
Rust
Raw Normal View History

2017-06-16 18:19:21 -05:00
//! This example shows a simple read-evaluate-print-loop (REPL).
extern crate rlua;
extern crate rustyline;
2017-06-16 18:19:21 -05:00
2017-10-23 15:42:20 -05:00
use rlua::{Error, Lua, MultiValue};
use rustyline::Editor;
2017-06-16 18:19:21 -05:00
fn main() {
let lua = Lua::new();
let mut editor = Editor::<()>::new();
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,
}
match lua.eval::<MultiValue>(&line, None) {
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
}
}
}
}