Add optimize command to REPL.

This commit is contained in:
Stephen Chung 2022-01-24 15:50:25 +08:00
parent b76a2be7c3
commit 2b2685e387

View File

@ -47,6 +47,8 @@ fn print_help() {
println!("quit, exit => quit"); println!("quit, exit => quit");
println!("scope => print all variables in the scope"); println!("scope => print all variables in the scope");
println!("strict => toggle on/off Strict Variables Mode"); println!("strict => toggle on/off Strict Variables Mode");
#[cfg(not(feature = "no_optimize"))]
println!("optimize => toggle on/off script optimization");
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
println!("functions => print all functions defined"); println!("functions => print all functions defined");
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -85,7 +87,9 @@ fn main() {
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION")); let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
println!("{}", title); println!("{}", title);
println!("{0:=<1$}", "", title.len()); println!("{0:=<1$}", "", title.len());
print_help();
#[cfg(not(feature = "no_optimize"))]
let mut optimize_level = rhai::OptimizationLevel::Simple;
// Initialize scripting engine // Initialize scripting engine
let mut engine = Engine::new(); let mut engine = Engine::new();
@ -196,6 +200,8 @@ fn main() {
let mut ast_u = AST::empty(); let mut ast_u = AST::empty();
let mut ast = AST::empty(); let mut ast = AST::empty();
print_help();
'main_loop: loop { 'main_loop: loop {
print!("rhai-repl> "); print!("rhai-repl> ");
stdout().flush().expect("couldn't flush stdout"); stdout().flush().expect("couldn't flush stdout");
@ -247,6 +253,18 @@ fn main() {
println!("Strict Variables Mode turned ON."); println!("Strict Variables Mode turned ON.");
continue; continue;
} }
#[cfg(not(feature = "no_optimize"))]
"optimize" if optimize_level == rhai::OptimizationLevel::Simple => {
optimize_level = rhai::OptimizationLevel::None;
println!("Script optimization turned OFF.");
continue;
}
#[cfg(not(feature = "no_optimize"))]
"optimize" => {
optimize_level = rhai::OptimizationLevel::Simple;
println!("Script optimization turned ON.");
continue;
}
"scope" => { "scope" => {
print_scope(&scope); print_scope(&scope);
continue; continue;
@ -296,7 +314,7 @@ fn main() {
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
{ {
ast = engine.optimize_ast(&scope, r, rhai::OptimizationLevel::Simple); ast = engine.optimize_ast(&scope, r, optimize_level);
} }
#[cfg(feature = "no_optimize")] #[cfg(feature = "no_optimize")]