2017-06-16 19:19:21 -04:00
|
|
|
//! This example shows a simple read-evaluate-print-loop (REPL).
|
|
|
|
|
|
|
|
extern crate rlua;
|
2017-08-02 08:32:45 -04:00
|
|
|
extern crate rustyline;
|
2017-06-16 19:19:21 -04:00
|
|
|
|
2017-07-20 05:33:15 -04:00
|
|
|
use rlua::{Lua, MultiValue, Error};
|
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();
|
2017-08-02 08:32:45 -04:00
|
|
|
let mut editor = Editor::<()>::new();
|
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
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
match lua.eval::<MultiValue>(&line, None) {
|
2017-06-17 08:43:05 -04:00
|
|
|
Ok(values) => {
|
2017-08-02 08:32:45 -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-07-18 16:21:12 -04:00
|
|
|
Err(Error::IncompleteStatement(_)) => {
|
2017-06-17 08:43:05 -04:00
|
|
|
// continue reading input and append it to `line`
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|