Optimize variable shadowing.
This commit is contained in:
@@ -84,7 +84,7 @@ fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
assert_eq!(
|
||||
format!("{:?}", ast),
|
||||
r#"AST { body: [Var(("DECISION" @ 1:7, false @ 1:18), (Constant), 1:1), Expr(123 @ 1:51)] }"#
|
||||
r#"AST { body: [Var(("DECISION" @ 1:7, false @ 1:18, None), (Constant), 1:1), Expr(123 @ 1:51)] }"#
|
||||
);
|
||||
|
||||
let ast = engine.compile("if 1 == 2 { 42 }")?;
|
||||
|
@@ -25,11 +25,15 @@ fn test_options_allow() -> Result<(), Box<EvalAltResult>> {
|
||||
assert!(engine.compile("let x = || 42;").is_err());
|
||||
}
|
||||
|
||||
engine.compile("while x > y { foo(z); }")?;
|
||||
let ast = engine.compile("let x = 0; while x < 10 { x += 1; }")?;
|
||||
|
||||
engine.set_allow_looping(false);
|
||||
|
||||
assert!(engine.compile("while x > y { foo(z); }").is_err());
|
||||
engine.run_ast(&ast)?;
|
||||
|
||||
assert!(engine
|
||||
.compile("let x = 0; while x < 10 { x += 1; }")
|
||||
.is_err());
|
||||
|
||||
engine.compile("let x = 42; let x = 123;")?;
|
||||
|
||||
|
@@ -5,17 +5,67 @@ fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
|
||||
engine.run_with_scope(&mut scope, "let x = 4 + 5")?;
|
||||
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 9);
|
||||
engine.eval_with_scope::<()>(&mut scope, "x += 1; x += 2;")?;
|
||||
engine.run_with_scope(&mut scope, "x += 1; x += 2;")?;
|
||||
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 12);
|
||||
|
||||
scope.set_value("x", 42 as INT);
|
||||
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
|
||||
|
||||
engine.eval_with_scope::<()>(&mut scope, "{let x = 3}")?;
|
||||
engine.run_with_scope(&mut scope, "{ let x = 3 }")?;
|
||||
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
|
||||
|
||||
#[cfg(not(feature = "no_optimize"))]
|
||||
if engine.optimization_level() != rhai::OptimizationLevel::None {
|
||||
scope.clear();
|
||||
engine.run_with_scope(&mut scope, "let x = 3; let x = 42; let x = 123;")?;
|
||||
assert_eq!(scope.len(), 1);
|
||||
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
|
||||
|
||||
scope.clear();
|
||||
engine.run_with_scope(
|
||||
&mut scope,
|
||||
"let x = 3; let y = 0; let x = 42; let y = 999; let x = 123;",
|
||||
)?;
|
||||
assert_eq!(scope.len(), 2);
|
||||
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
|
||||
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
|
||||
|
||||
scope.clear();
|
||||
engine.run_with_scope(
|
||||
&mut scope,
|
||||
"const x = 3; let y = 0; let x = 42; let y = 999; const x = 123;",
|
||||
)?;
|
||||
assert_eq!(scope.len(), 3);
|
||||
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
|
||||
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
|
||||
|
||||
scope.clear();
|
||||
engine.run_with_scope(
|
||||
&mut scope,
|
||||
"let x = 3; let y = 0; { let x = 42; let y = 999; } let x = 123;",
|
||||
)?;
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
let sum = 0;
|
||||
for x in 0..10 {
|
||||
let x = 42;
|
||||
sum += x;
|
||||
}
|
||||
sum
|
||||
",
|
||||
)?,
|
||||
420
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(scope.len(), 2);
|
||||
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
|
||||
assert_eq!(scope.get_value::<INT>("y").unwrap(), 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -60,7 +110,7 @@ fn test_scope_eval() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
// First invocation
|
||||
engine
|
||||
.eval_with_scope::<()>(&mut scope, " let x = 4 + 5 - y + z; y = 1;")
|
||||
.run_with_scope(&mut scope, " let x = 4 + 5 - y + z; y = 1;")
|
||||
.expect("variables y and z should exist");
|
||||
|
||||
// Second invocation using the same state
|
||||
|
Reference in New Issue
Block a user