Fix test.

This commit is contained in:
Stephen Chung 2022-10-18 16:54:26 +08:00
parent 4642895de8
commit d49dca8a29
2 changed files with 75 additions and 78 deletions

View File

@ -358,24 +358,14 @@ impl Engine {
});
match guard_val {
Ok(true) => {
if if_block.is_empty() {
Ok(Dynamic::UNIT)
} else {
self.eval_stmt_block(
scope, global, caches, lib, this_ptr, if_block, true, level,
)
}
}
Ok(false) => {
if else_block.is_empty() {
Ok(Dynamic::UNIT)
} else {
self.eval_stmt_block(
scope, global, caches, lib, this_ptr, else_block, true, level,
)
}
}
Ok(true) if if_block.is_empty() => Ok(Dynamic::UNIT),
Ok(true) => self.eval_stmt_block(
scope, global, caches, lib, this_ptr, if_block, true, level,
),
Ok(false) if else_block.is_empty() => Ok(Dynamic::UNIT),
Ok(false) => self.eval_stmt_block(
scope, global, caches, lib, this_ptr, else_block, true, level,
),
err => err.map(Into::into),
}
}
@ -490,41 +480,15 @@ impl Engine {
}
// Loop
Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..)) => loop {
Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..) | Expr::BoolConstant(true, ..)) => {
let (.., body) = &**x;
if body.is_empty() {
self.track_operation(global, body.position())?;
} else {
match self
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
{
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
_ => break Err(err),
},
loop {
self.track_operation(global, body.position())?;
}
}
},
// While loop
Stmt::While(x, ..) => loop {
let (expr, body) = &**x;
let condition = self
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
})
});
match condition {
Ok(false) => break Ok(Dynamic::UNIT),
Ok(true) if body.is_empty() => (),
Ok(true) => {
} else {
loop {
match self.eval_stmt_block(
scope, global, caches, lib, this_ptr, body, true, level,
) {
@ -536,42 +500,76 @@ impl Engine {
},
}
}
err => break err.map(|_| Dynamic::UNIT),
}
},
}
// While loop
Stmt::While(x, ..) => {
let (expr, body) = &**x;
loop {
let condition = self
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
})
});
match condition {
Ok(false) => break Ok(Dynamic::UNIT),
Ok(true) if body.is_empty() => (),
Ok(true) => {
match self.eval_stmt_block(
scope, global, caches, lib, this_ptr, body, true, level,
) {
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
_ => break Err(err),
},
}
}
err => break err.map(|_| Dynamic::UNIT),
}
}
}
// Do loop
Stmt::Do(x, options, ..) => loop {
Stmt::Do(x, options, ..) => {
let (expr, body) = &**x;
let is_while = !options.contains(ASTFlags::NEGATED);
if !body.is_empty() {
match self
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
{
loop {
if !body.is_empty() {
match self.eval_stmt_block(
scope, global, caches, lib, this_ptr, body, true, level,
) {
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => continue,
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
_ => break Err(err),
},
}
}
let condition = self
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
})
});
match condition {
Ok(condition) if condition ^ is_while => break Ok(Dynamic::UNIT),
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => continue,
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
_ => break Err(err),
},
err => break err.map(|_| Dynamic::UNIT),
}
}
let condition = self
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
})
});
match condition {
Ok(condition) if condition ^ is_while => break Ok(Dynamic::UNIT),
Ok(_) => (),
err => break err.map(|_| Dynamic::UNIT),
}
},
}
// For loop
Stmt::For(x, ..) => {

View File

@ -1,5 +1,4 @@
#![cfg(not(feature = "no_std"))]
#![cfg(not(target_family = "wasm"))]
#![cfg(not(feature = "no_time"))]
use rhai::{Engine, EvalAltResult};