mlua/examples/repl.rs

48 lines
1.3 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;
use rlua::{Lua, MultiValue, Error};
2017-06-16 18:19:21 -05:00
use std::io::prelude::*;
use std::io::{stdin, stdout, stderr, BufReader};
fn main() {
let lua = Lua::new();
let mut stdout = stdout();
let mut stdin = BufReader::new(stdin());
loop {
write!(stdout, "> ").unwrap();
stdout.flush().unwrap();
let mut line = String::new();
loop {
stdin.read_line(&mut line).unwrap();
match lua.eval::<MultiValue>(&line, None) {
Ok(values) => {
println!(
"{}",
values
.iter()
.map(|value| format!("{:?}", value))
.collect::<Vec<_>>()
.join("\t")
);
break;
}
Err(Error::IncompleteStatement(_)) => {
// continue reading input and append it to `line`
write!(stdout, ">> ").unwrap();
stdout.flush().unwrap();
}
Err(e) => {
writeln!(stderr(), "error: {}", e).unwrap();
break;
}
2017-06-16 18:19:21 -05:00
}
}
}
}