summaryrefslogtreecommitdiff
path: root/examples/repl.rs
blob: 6691f0bd6d6fce79c536a8b86e76244f82dd66b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! This example shows a simple read-evaluate-print-loop (REPL).

extern crate rlua;

use rlua::*;
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();
        stdin.read_line(&mut line).unwrap();

        match lua.eval::<LuaMultiValue>(&line) {
            Ok(values) => {
                println!("{}", values.iter().map(|value| format!("{:?}", value)).collect::<Vec<_>>().join("\t"));
            }
            Err(e) => {
                writeln!(stderr(), "error: {}", e).unwrap();
            }
        }
    }
}