From f8485932b5c01fdc051adc78689a0b0f65d02a99 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 25 Feb 2020 11:08:40 +0800 Subject: [PATCH] Add documentation on pre-compilation. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 31eafcf6..94c4afc2 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,24 @@ You can also evaluate a script file: if let Ok(result) = engine.eval_file::("hello_world.rhai") { ... } ``` +If you want to repeatedly evaluate a script, you can compile it first into an AST form: + +```rust +let ast = Engine::compile("40 + 2").unwrap(); // AST generated can be stored for later evaluations + +for _ in 0..42 { + if let Ok(result) = engine.eval_ast::(&ast) { + println!("Answer: {}", result); // prints 42 + } +} +``` + +Compiling a script file into AST is also supported: + +```rust +let ast = Engine::compile_file("hello_world.rhai").unwrap(); +``` + # Working with functions Rhai's scripting engine is very lightweight. It gets its ability from the functions in your program. To call these functions, you need to register them with the scripting engine.