rhai/examples/repl.rs

176 lines
4.7 KiB
Rust
Raw Normal View History

use rhai::{Dynamic, Engine, EvalAltResult, Scope, AST};
2020-03-16 05:41:19 +01:00
#[cfg(not(feature = "no_optimize"))]
use rhai::OptimizationLevel;
use std::io::{stdin, stdout, Write};
2017-10-15 17:50:39 +02:00
fn print_error(input: &str, err: EvalAltResult) {
2020-03-24 10:30:04 +01:00
let lines: Vec<_> = input.trim().split('\n').collect();
let pos = err.position();
let line_no = if lines.len() > 1 {
if pos.is_none() {
"".to_string()
} else {
format!("{}: ", pos.line().unwrap())
}
} else {
"".to_string()
};
// Print error
2020-03-25 04:51:13 +01:00
let pos_text = format!(" ({})", pos);
2020-03-22 14:03:58 +01:00
if pos.is_none() {
// No position
println!("{}", err);
} else {
// Specific position
println!("{}{}", line_no, lines[pos.line().unwrap() - 1]);
println!(
"{0:>1$} {2}",
"^",
line_no.len() + pos.position().unwrap(),
err.to_string().replace(&pos_text, "")
);
}
}
fn print_help() {
println!("help => print this help");
println!("quit, exit => quit");
2020-06-24 16:45:34 +02:00
println!("scope => print all variables in the scope");
println!("ast => print the last AST");
println!("astu => print the last raw, un-optimized AST");
println!(r"end a line with '\' to continue to the next line.");
println!();
}
fn main() {
2017-12-20 12:16:14 +01:00
let mut engine = Engine::new();
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(OptimizationLevel::None);
2017-12-20 12:16:14 +01:00
let mut scope = Scope::new();
2017-10-15 17:50:39 +02:00
let mut input = String::new();
2020-05-05 09:00:10 +02:00
let mut main_ast: AST = Default::default();
let mut ast_u: AST = Default::default();
let mut ast: AST = Default::default();
2017-10-15 17:50:39 +02:00
println!("Rhai REPL tool");
println!("==============");
print_help();
'main_loop: loop {
print!("rhai> ");
2017-12-20 12:16:14 +01:00
stdout().flush().expect("couldn't flush stdout");
2020-03-02 08:19:59 +01:00
input.clear();
loop {
match stdin().read_line(&mut input) {
Ok(0) => break 'main_loop,
Ok(_) => (),
Err(err) => panic!("input error: {}", err),
}
let line = input.as_str().trim_end();
// Allow line continuation
if line.ends_with('\\') {
let len = line.len();
input.truncate(len - 1);
input.push('\n');
} else {
break;
}
print!("> ");
stdout().flush().expect("couldn't flush stdout");
2017-12-20 12:16:14 +01:00
}
2017-10-15 17:50:39 +02:00
let script = input.trim();
if script.is_empty() {
continue;
}
// Implement standard commands
match script {
"help" => {
print_help();
continue;
}
"exit" | "quit" => break, // quit
2020-06-24 16:45:34 +02:00
"scope" => {
2020-10-09 07:22:40 +02:00
scope
.iter_raw()
.enumerate()
.for_each(|(i, (name, constant, value))| {
println!(
"[{}] {}{}{} = {:?}",
i + 1,
if constant { "const " } else { "" },
name,
if value.is_shared() { " (shared)" } else { "" },
*value.read_lock::<Dynamic>().unwrap(),
)
});
2020-06-24 16:45:34 +02:00
continue;
}
"astu" => {
// print the last un-optimized AST
println!("{:#?}", &ast_u);
continue;
}
"ast" => {
// print the last AST
println!("{:#?}", &ast);
continue;
}
_ => (),
}
2020-04-13 04:27:08 +02:00
match engine
.compile_with_scope(&scope, &script)
2020-06-16 03:34:30 +02:00
.map_err(Into::into)
.and_then(|r| {
2020-04-05 11:44:48 +02:00
ast_u = r.clone();
2020-03-22 14:03:58 +01:00
#[cfg(not(feature = "no_optimize"))]
{
ast = engine.optimize_ast(&scope, r, OptimizationLevel::Simple);
2020-03-22 14:03:58 +01:00
}
#[cfg(feature = "no_optimize")]
{
2020-04-05 11:44:48 +02:00
ast = r;
2020-03-22 14:03:58 +01:00
}
// Merge the AST into the main
main_ast += ast.clone();
// Evaluate
2020-04-13 04:27:08 +02:00
engine.eval_ast_with_scope::<Dynamic>(&mut scope, &main_ast)
}) {
2020-04-16 05:57:08 +02:00
Ok(result) if !result.is::<()>() => {
2020-04-13 04:27:08 +02:00
println!("=> {:?}", result);
println!();
}
2020-04-16 05:57:08 +02:00
Ok(_) => (),
2020-04-13 04:27:08 +02:00
Err(err) => {
println!();
print_error(&input, *err);
2020-04-13 04:27:08 +02:00
println!();
}
2017-12-20 12:16:14 +01:00
}
2020-04-13 04:27:08 +02:00
// Throw away all the statements, leaving only the functions
main_ast.clear_statements();
2017-12-20 12:16:14 +01:00
}
2017-10-15 17:50:39 +02:00
}