From 01821177592db8d04f8b8bc3715c6c433075a0d0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 14 Nov 2020 22:55:23 +0800 Subject: [PATCH] Change Stmt Variant names. --- src/ast.rs | 22 +++++++++++----------- src/engine.rs | 10 +++++----- src/optimize.rs | 26 +++++++++++--------------- src/parser.rs | 16 ++++------------ 4 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 6626d595..5fcb2438 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -582,7 +582,7 @@ pub enum Stmt { /// No-op. Noop(Position), /// if expr { stmt } else { stmt } - IfThenElse(Expr, Box<(Stmt, Option)>, Position), + If(Expr, Box<(Stmt, Option)>, Position), /// while expr { stmt } While(Expr, Box, Position), /// loop { stmt } @@ -606,7 +606,7 @@ pub enum Stmt { /// break Break(Position), /// return/throw - ReturnWithVal((ReturnType, Position), Option, Position), + Return((ReturnType, Position), Option, Position), /// import expr as var #[cfg(not(feature = "no_module"))] Import(Expr, Option>, Position), @@ -641,11 +641,11 @@ impl Stmt { | Self::Break(pos) | Self::Block(_, pos) | Self::Assignment(_, pos) - | Self::IfThenElse(_, _, pos) + | Self::If(_, _, pos) | Self::While(_, _, pos) | Self::Loop(_, pos) | Self::For(_, _, pos) - | Self::ReturnWithVal((_, pos), _, _) + | Self::Return((_, pos), _, _) | Self::Let(_, _, _, pos) | Self::Const(_, _, _, pos) | Self::TryCatch(_, pos, _) => *pos, @@ -669,11 +669,11 @@ impl Stmt { | Self::Break(pos) | Self::Block(_, pos) | Self::Assignment(_, pos) - | Self::IfThenElse(_, _, pos) + | Self::If(_, _, pos) | Self::While(_, _, pos) | Self::Loop(_, pos) | Self::For(_, _, pos) - | Self::ReturnWithVal((_, pos), _, _) + | Self::Return((_, pos), _, _) | Self::Let(_, _, _, pos) | Self::Const(_, _, _, pos) | Self::TryCatch(_, pos, _) => *pos = new_pos, @@ -696,7 +696,7 @@ impl Stmt { /// Is this statement self-terminated (i.e. no need for a semicolon terminator)? pub fn is_self_terminated(&self) -> bool { match self { - Self::IfThenElse(_, _, _) + Self::If(_, _, _) | Self::While(_, _, _) | Self::Loop(_, _) | Self::For(_, _, _) @@ -712,7 +712,7 @@ impl Stmt { | Self::Expr(_) | Self::Continue(_) | Self::Break(_) - | Self::ReturnWithVal(_, _, _) => false, + | Self::Return(_, _, _) => false, #[cfg(not(feature = "no_module"))] Self::Import(_, _, _) | Self::Export(_, _) => false, @@ -726,16 +726,16 @@ impl Stmt { match self { Self::Noop(_) => true, Self::Expr(expr) => expr.is_pure(), - Self::IfThenElse(condition, x, _) if x.1.is_some() => { + Self::If(condition, x, _) if x.1.is_some() => { condition.is_pure() && x.0.is_pure() && x.1.as_ref().unwrap().is_pure() } - Self::IfThenElse(condition, x, _) => condition.is_pure() && x.0.is_pure(), + Self::If(condition, x, _) => condition.is_pure() && x.0.is_pure(), Self::While(condition, block, _) => condition.is_pure() && block.is_pure(), Self::Loop(block, _) => block.is_pure(), Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(), Self::Let(_, _, _, _) | Self::Const(_, _, _, _) | Self::Assignment(_, _) => false, Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), - Self::Continue(_) | Self::Break(_) | Self::ReturnWithVal(_, _, _) => false, + Self::Continue(_) | Self::Break(_) | Self::Return(_, _, _) => false, Self::TryCatch(x, _, _) => x.0.is_pure() && x.2.is_pure(), #[cfg(not(feature = "no_module"))] diff --git a/src/engine.rs b/src/engine.rs index d2a5d684..40383a12 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2064,7 +2064,7 @@ impl Engine { } // If-else statement - Stmt::IfThenElse(expr, x, _) => { + Stmt::If(expr, x, _) => { let (if_block, else_block) = x.as_ref(); self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)? .as_bool() @@ -2231,25 +2231,25 @@ impl Engine { } // Return value - Stmt::ReturnWithVal((ReturnType::Return, pos), Some(expr), _) => EvalAltResult::Return( + Stmt::Return((ReturnType::Return, pos), Some(expr), _) => EvalAltResult::Return( self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?, *pos, ) .into(), // Empty return - Stmt::ReturnWithVal((ReturnType::Return, pos), None, _) => { + Stmt::Return((ReturnType::Return, pos), None, _) => { EvalAltResult::Return(Default::default(), *pos).into() } // Throw value - Stmt::ReturnWithVal((ReturnType::Exception, pos), Some(expr), _) => { + Stmt::Return((ReturnType::Exception, pos), Some(expr), _) => { let val = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?; EvalAltResult::ErrorRuntime(val, *pos).into() } // Empty throw - Stmt::ReturnWithVal((ReturnType::Exception, pos), None, _) => { + Stmt::Return((ReturnType::Exception, pos), None, _) => { EvalAltResult::ErrorRuntime(().into(), *pos).into() } diff --git a/src/optimize.rs b/src/optimize.rs index 292d3fc1..a90dc7fd 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -243,7 +243,7 @@ fn optimize_stmt_block( } match stmt { - Stmt::ReturnWithVal(_, _, _) | Stmt::Break(_) => dead_code = true, + Stmt::Return(_, _, _) | Stmt::Break(_) => dead_code = true, _ => (), } @@ -292,19 +292,17 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } }, // if false { if_block } -> Noop - Stmt::IfThenElse(Expr::False(pos), x, _) if x.1.is_none() => { + Stmt::If(Expr::False(pos), x, _) if x.1.is_none() => { state.set_dirty(); *stmt = Stmt::Noop(*pos); } // if true { if_block } -> if_block - Stmt::IfThenElse(Expr::True(_), x, _) if x.1.is_none() => { + Stmt::If(Expr::True(_), x, _) if x.1.is_none() => { *stmt = mem::take(&mut x.0); optimize_stmt(stmt, state, true); } // if expr { Noop } - Stmt::IfThenElse(ref mut condition, x, _) - if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) => - { + Stmt::If(ref mut condition, x, _) if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) => { state.set_dirty(); let pos = condition.position(); @@ -323,22 +321,22 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { }; } // if expr { if_block } - Stmt::IfThenElse(ref mut condition, ref mut x, _) if x.1.is_none() => { + Stmt::If(ref mut condition, ref mut x, _) if x.1.is_none() => { optimize_expr(condition, state); optimize_stmt(&mut x.0, state, true); } // if false { if_block } else { else_block } -> else_block - Stmt::IfThenElse(Expr::False(_), x, _) if x.1.is_some() => { + Stmt::If(Expr::False(_), x, _) if x.1.is_some() => { *stmt = mem::take(x.1.as_mut().unwrap()); optimize_stmt(stmt, state, true); } // if true { if_block } else { else_block } -> if_block - Stmt::IfThenElse(Expr::True(_), x, _) => { + Stmt::If(Expr::True(_), x, _) => { *stmt = mem::take(&mut x.0); optimize_stmt(stmt, state, true); } // if expr { if_block } else { else_block } - Stmt::IfThenElse(ref mut condition, ref mut x, _) => { + Stmt::If(ref mut condition, ref mut x, _) => { optimize_expr(condition, state); optimize_stmt(&mut x.0, state, true); if let Some(else_block) = x.1.as_mut() { @@ -441,7 +439,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { // expr; Stmt::Expr(ref mut expr) => optimize_expr(expr, state), // return expr; - Stmt::ReturnWithVal(_, Some(ref mut expr), _) => optimize_expr(expr, state), + Stmt::Return(_, Some(ref mut expr), _) => optimize_expr(expr, state), // All other statements - skip _ => (), @@ -908,11 +906,9 @@ pub fn optimize_into_ast( // {} -> Noop fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { // { return val; } -> val - Stmt::ReturnWithVal((ReturnType::Return, _), Some(expr), _) => { - Stmt::Expr(expr) - } + Stmt::Return((ReturnType::Return, _), Some(expr), _) => Stmt::Expr(expr), // { return; } -> () - Stmt::ReturnWithVal((ReturnType::Return, pos), None, _) => { + Stmt::Return((ReturnType::Return, pos), None, _) => { Stmt::Expr(Expr::Unit(pos)) } // All others diff --git a/src/parser.rs b/src/parser.rs index 93d78a89..d37bf0e1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1970,11 +1970,7 @@ fn parse_if( None }; - Ok(Stmt::IfThenElse( - guard, - Box::new((if_body, else_body)), - token_pos, - )) + Ok(Stmt::If(guard, Box::new((if_body, else_body)), token_pos)) } /// Parse a while loop. @@ -2451,13 +2447,9 @@ fn parse_stmt( match input.peek().unwrap() { // `return`/`throw` at - (Token::EOF, pos) => Ok(Some(Stmt::ReturnWithVal( - (return_type, token_pos), - None, - *pos, - ))), + (Token::EOF, pos) => Ok(Some(Stmt::Return((return_type, token_pos), None, *pos))), // `return;` or `throw;` - (Token::SemiColon, _) => Ok(Some(Stmt::ReturnWithVal( + (Token::SemiColon, _) => Ok(Some(Stmt::Return( (return_type, token_pos), None, settings.pos, @@ -2466,7 +2458,7 @@ fn parse_stmt( (_, _) => { let expr = parse_expr(input, state, lib, settings.level_up())?; let pos = expr.position(); - Ok(Some(Stmt::ReturnWithVal( + Ok(Some(Stmt::Return( (return_type, token_pos), Some(expr), pos,