rhai/examples/repl.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

2017-10-15 17:50:39 +02:00
extern crate rhai;
use std::fmt::Display;
use std::process::exit;
use std::io::{stdin, stdout, Write};
2017-12-20 21:09:53 +01:00
use rhai::{Engine, RegisterFn, Scope};
2017-10-15 17:50:39 +02:00
fn showit<T: Display>(x: &mut T) -> () {
println!("{}", x)
}
2017-12-20 12:16:14 +01:00
fn quit() {
exit(0);
}
2017-10-15 17:50:39 +02:00
pub fn main() {
2017-12-20 12:16:14 +01:00
let mut engine = Engine::new();
let mut scope = Scope::new();
2017-10-15 17:50:39 +02:00
2017-12-20 12:16:14 +01:00
engine.register_fn("print", showit as fn(x: &mut i32) -> ());
engine.register_fn("print", showit as fn(x: &mut i64) -> ());
engine.register_fn("print", showit as fn(x: &mut u32) -> ());
engine.register_fn("print", showit as fn(x: &mut u64) -> ());
engine.register_fn("print", showit as fn(x: &mut f32) -> ());
engine.register_fn("print", showit as fn(x: &mut f64) -> ());
engine.register_fn("print", showit as fn(x: &mut bool) -> ());
engine.register_fn("print", showit as fn(x: &mut String) -> ());
2017-10-15 17:50:39 +02:00
engine.register_fn("exit", quit);
2017-12-20 12:16:14 +01:00
loop {
print!("> ");
let mut input = String::new();
stdout().flush().expect("couldn't flush stdout");
if let Err(e) = stdin().read_line(&mut input) {
println!("input error: {}", e);
}
2017-10-15 17:50:39 +02:00
2017-12-20 12:16:14 +01:00
if let Err(e) = engine.consume_with_scope(&mut scope, &input) {
println!("error: {}", e);
}
}
2017-10-15 17:50:39 +02:00
}