Implement variable resolver.

This commit is contained in:
Stephen Chung
2020-10-11 21:58:11 +08:00
parent 9d93dac8e7
commit fd5a932611
18 changed files with 511 additions and 237 deletions

View File

@@ -22,21 +22,28 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|engine: &Engine, context: &mut EvalContext, scope: &mut Scope, inputs: &[Expression]| {
|scope: &mut Scope, context: &mut EvalContext, inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let expr = inputs.get(2).unwrap();
let condition = inputs.get(2).unwrap();
scope.push(var_name, 0 as INT);
loop {
engine.eval_expression_tree(context, scope, stmt)?;
context.eval_expression_tree(scope, stmt)?;
if !engine
.eval_expression_tree(context, scope, expr)?
let stop = !context
.eval_expression_tree(scope, condition)?
.as_bool()
.map_err(|err| engine.make_type_mismatch_err::<bool>(err, expr.position()))?
{
.map_err(|err| {
Box::new(EvalAltResult::ErrorMismatchDataType(
"bool".to_string(),
err.to_string(),
condition.position(),
))
})?;
if stop {
break;
}
}
@@ -61,7 +68,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
// The first symbol must be an identifier
assert_eq!(
*engine
.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into()))
.register_custom_syntax(&["!"], 0, |_, _, _| Ok(().into()))
.expect_err("should error")
.0,
ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())

View File

@@ -9,7 +9,7 @@ fn test_unary_minus() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_function"))]
assert_eq!(engine.eval::<INT>("fn neg(x) { -x } neg(5)")?, -5);
assert_eq!(engine.eval::<INT>("5 - -+++--+-5")?, 0);
assert_eq!(engine.eval::<INT>("5 - -+ + + - -+-5")?, 0);
Ok(())
}

View File

@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, Scope, INT};
use rhai::{Engine, EvalAltResult, Position, Scope, INT};
#[test]
fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
@@ -52,3 +52,41 @@ fn test_scope_eval() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_var_resolver() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let mut scope = Scope::new();
scope.push("innocent", 1 as INT);
scope.push("chameleon", 123 as INT);
scope.push("DO_NOT_USE", 999 as INT);
engine.on_var(|name, _, scope, _| {
match name {
"MYSTIC_NUMBER" => Ok(Some((42 as INT).into())),
// Override a variable - make it not found even if it exists!
"DO_NOT_USE" => {
Err(EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::none()).into())
}
// Silently maps 'chameleon' into 'innocent'.
"chameleon" => scope.get_value("innocent").map(Some).ok_or_else(|| {
EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::none()).into()
}),
// Return Ok(None) to continue with the normal variable resolution process.
_ => Ok(None),
}
});
assert_eq!(
engine.eval_with_scope::<INT>(&mut scope, "MYSTIC_NUMBER")?,
42
);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "chameleon")?, 1);
assert!(
matches!(*engine.eval_with_scope::<INT>(&mut scope, "DO_NOT_USE").expect_err("should error"),
EvalAltResult::ErrorVariableNotFound(n, _) if n == "DO_NOT_USE")
);
Ok(())
}