From d49dca8a297b723d3355cecf4e70e0452057d569 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 Oct 2022 16:54:26 +0800 Subject: [PATCH] Fix test. --- src/eval/stmt.rs | 150 +++++++++++++++++++++++------------------------ tests/time.rs | 3 +- 2 files changed, 75 insertions(+), 78 deletions(-) diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 387b434e..c3588754 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -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::(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::(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::(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::(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, ..) => { diff --git a/tests/time.rs b/tests/time.rs index 7a35da15..5434faa5 100644 --- a/tests/time.rs +++ b/tests/time.rs @@ -1,5 +1,4 @@ -#![cfg(not(feature = "no_std"))] -#![cfg(not(target_family = "wasm"))] +#![cfg(not(feature = "no_time"))] use rhai::{Engine, EvalAltResult};