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 { match guard_val {
Ok(true) => { Ok(true) if if_block.is_empty() => Ok(Dynamic::UNIT),
if if_block.is_empty() { Ok(true) => self.eval_stmt_block(
Ok(Dynamic::UNIT) scope, global, caches, lib, this_ptr, if_block, true, level,
} else { ),
self.eval_stmt_block( Ok(false) if else_block.is_empty() => Ok(Dynamic::UNIT),
scope, global, caches, lib, this_ptr, if_block, true, level, Ok(false) => self.eval_stmt_block(
) scope, global, caches, lib, this_ptr, else_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,
)
}
}
err => err.map(Into::into), err => err.map(Into::into),
} }
} }
@ -490,41 +480,15 @@ impl Engine {
} }
// Loop // 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; let (.., body) = &**x;
if body.is_empty() { if body.is_empty() {
self.track_operation(global, body.position())?; loop {
} else { self.track_operation(global, body.position())?;
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),
},
} }
} } else {
}, loop {
// 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) => {
match self.eval_stmt_block( match self.eval_stmt_block(
scope, global, caches, lib, this_ptr, body, true, level, 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 // Do loop
Stmt::Do(x, options, ..) => loop { Stmt::Do(x, options, ..) => {
let (expr, body) = &**x; let (expr, body) = &**x;
let is_while = !options.contains(ASTFlags::NEGATED); let is_while = !options.contains(ASTFlags::NEGATED);
if !body.is_empty() { loop {
match self if !body.is_empty() {
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level) 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(_) => (), Ok(_) => (),
Err(err) => match *err { err => break err.map(|_| Dynamic::UNIT),
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 => break err.map(|_| Dynamic::UNIT),
}
},
// For loop // For loop
Stmt::For(x, ..) => { Stmt::For(x, ..) => {

View File

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