Add AST::combine and AST::combine_filtered.

This commit is contained in:
Stephen Chung
2020-10-07 12:11:25 +08:00
parent 3340760b35
commit 1523981e4e
5 changed files with 151 additions and 18 deletions

View File

@@ -10,8 +10,8 @@ Each Function is a Separate Compilation Unit
This means that individual functions can be separated, exported, re-grouped, imported,
and generally mix-'n-match-ed with other completely unrelated scripts.
For example, the `AST::merge` method allows merging all functions in one [`AST`] into another,
forming a new, combined, group of functions.
For example, the `AST::merge` and `AST::combine` methods (or the equivalent `+` and `+=` operators)
allow combining all functions in one [`AST`] into another, forming a new, unified, group of functions.
In general, there are two types of _namespaces_ where functions are looked up:
@@ -58,10 +58,10 @@ let ast1 = engine.compile(
// Compile another script with an overriding function
let ast2 = engine.compile(r#"fn get_message() { "Boo!" }"#)?;
// Merge the two AST's
let ast = ast1.merge(ast2); // 'message' will be overwritten
// Combine the two AST's
ast1 += ast2; // 'message' will be overwritten
engine.consume_ast(&ast)?; // prints 'Boo!'
engine.consume_ast(&ast1)?; // prints 'Boo!'
```
Therefore, care must be taken when _cross-calling_ functions to make sure that the correct

View File

@@ -29,8 +29,8 @@ Key Concepts
* The lowest layer script is compiled into a base [`AST`].
* Higher layer scripts are also compiled into [`AST`] and _merged_ into the base using `AST::merge`,
overriding any existing functions.
* Higher layer scripts are also compiled into [`AST`] and _combined_ into the base using `AST::combine`
(or the `+=` operator), overriding any existing functions.
Examples
@@ -83,7 +83,7 @@ fn baz() { print("hey!"); }
fn foo(x) { x + 42 }
```
Load and merge them sequentially:
Load and combine them sequentially:
```rust
let engine = Engine::new();
@@ -91,17 +91,17 @@ let engine = Engine::new();
// Compile the baseline default implementations.
let mut ast = engine.compile_file("default.rhai".into())?;
// Merge in the first layer.
// Combine the first layer.
let lowest = engine.compile_file("lowest.rhai".into())?;
ast = ast.merge(&lowest);
ast += lowest;
// Merge in the second layer.
// Combine the second layer.
let middle = engine.compile_file("middle.rhai".into())?;
ast = ast.merge(&middle);
ast += lowest;
// Merge in the third layer.
// Combine the third layer.
let highest = engine.compile_file("highest.rhai".into())?;
ast = ast.merge(&highest);
ast += lowest;
// Now, 'ast' contains the following functions:
//