Change Stmt Variant names.

This commit is contained in:
Stephen Chung 2020-11-14 22:55:23 +08:00
parent fce2c62f02
commit 0182117759
4 changed files with 31 additions and 43 deletions

View File

@ -582,7 +582,7 @@ pub enum Stmt {
/// No-op. /// No-op.
Noop(Position), Noop(Position),
/// if expr { stmt } else { stmt } /// if expr { stmt } else { stmt }
IfThenElse(Expr, Box<(Stmt, Option<Stmt>)>, Position), If(Expr, Box<(Stmt, Option<Stmt>)>, Position),
/// while expr { stmt } /// while expr { stmt }
While(Expr, Box<Stmt>, Position), While(Expr, Box<Stmt>, Position),
/// loop { stmt } /// loop { stmt }
@ -606,7 +606,7 @@ pub enum Stmt {
/// break /// break
Break(Position), Break(Position),
/// return/throw /// return/throw
ReturnWithVal((ReturnType, Position), Option<Expr>, Position), Return((ReturnType, Position), Option<Expr>, Position),
/// import expr as var /// import expr as var
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Import(Expr, Option<Box<IdentX>>, Position), Import(Expr, Option<Box<IdentX>>, Position),
@ -641,11 +641,11 @@ impl Stmt {
| Self::Break(pos) | Self::Break(pos)
| Self::Block(_, pos) | Self::Block(_, pos)
| Self::Assignment(_, pos) | Self::Assignment(_, pos)
| Self::IfThenElse(_, _, pos) | Self::If(_, _, pos)
| Self::While(_, _, pos) | Self::While(_, _, pos)
| Self::Loop(_, pos) | Self::Loop(_, pos)
| Self::For(_, _, pos) | Self::For(_, _, pos)
| Self::ReturnWithVal((_, pos), _, _) | Self::Return((_, pos), _, _)
| Self::Let(_, _, _, pos) | Self::Let(_, _, _, pos)
| Self::Const(_, _, _, pos) | Self::Const(_, _, _, pos)
| Self::TryCatch(_, pos, _) => *pos, | Self::TryCatch(_, pos, _) => *pos,
@ -669,11 +669,11 @@ impl Stmt {
| Self::Break(pos) | Self::Break(pos)
| Self::Block(_, pos) | Self::Block(_, pos)
| Self::Assignment(_, pos) | Self::Assignment(_, pos)
| Self::IfThenElse(_, _, pos) | Self::If(_, _, pos)
| Self::While(_, _, pos) | Self::While(_, _, pos)
| Self::Loop(_, pos) | Self::Loop(_, pos)
| Self::For(_, _, pos) | Self::For(_, _, pos)
| Self::ReturnWithVal((_, pos), _, _) | Self::Return((_, pos), _, _)
| Self::Let(_, _, _, pos) | Self::Let(_, _, _, pos)
| Self::Const(_, _, _, pos) | Self::Const(_, _, _, pos)
| Self::TryCatch(_, pos, _) => *pos = new_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)? /// Is this statement self-terminated (i.e. no need for a semicolon terminator)?
pub fn is_self_terminated(&self) -> bool { pub fn is_self_terminated(&self) -> bool {
match self { match self {
Self::IfThenElse(_, _, _) Self::If(_, _, _)
| Self::While(_, _, _) | Self::While(_, _, _)
| Self::Loop(_, _) | Self::Loop(_, _)
| Self::For(_, _, _) | Self::For(_, _, _)
@ -712,7 +712,7 @@ impl Stmt {
| Self::Expr(_) | Self::Expr(_)
| Self::Continue(_) | Self::Continue(_)
| Self::Break(_) | Self::Break(_)
| Self::ReturnWithVal(_, _, _) => false, | Self::Return(_, _, _) => false,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Self::Import(_, _, _) | Self::Export(_, _) => false, Self::Import(_, _, _) | Self::Export(_, _) => false,
@ -726,16 +726,16 @@ impl Stmt {
match self { match self {
Self::Noop(_) => true, Self::Noop(_) => true,
Self::Expr(expr) => expr.is_pure(), 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() 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::While(condition, block, _) => condition.is_pure() && block.is_pure(),
Self::Loop(block, _) => block.is_pure(), Self::Loop(block, _) => block.is_pure(),
Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(), Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(),
Self::Let(_, _, _, _) | Self::Const(_, _, _, _) | Self::Assignment(_, _) => false, Self::Let(_, _, _, _) | Self::Const(_, _, _, _) | Self::Assignment(_, _) => false,
Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), 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(), Self::TryCatch(x, _, _) => x.0.is_pure() && x.2.is_pure(),
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]

View File

@ -2064,7 +2064,7 @@ impl Engine {
} }
// If-else statement // If-else statement
Stmt::IfThenElse(expr, x, _) => { Stmt::If(expr, x, _) => {
let (if_block, else_block) = x.as_ref(); let (if_block, else_block) = x.as_ref();
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)? self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?
.as_bool() .as_bool()
@ -2231,25 +2231,25 @@ impl Engine {
} }
// Return value // 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)?, self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?,
*pos, *pos,
) )
.into(), .into(),
// Empty return // Empty return
Stmt::ReturnWithVal((ReturnType::Return, pos), None, _) => { Stmt::Return((ReturnType::Return, pos), None, _) => {
EvalAltResult::Return(Default::default(), *pos).into() EvalAltResult::Return(Default::default(), *pos).into()
} }
// Throw value // 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)?; let val = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
EvalAltResult::ErrorRuntime(val, *pos).into() EvalAltResult::ErrorRuntime(val, *pos).into()
} }
// Empty throw // Empty throw
Stmt::ReturnWithVal((ReturnType::Exception, pos), None, _) => { Stmt::Return((ReturnType::Exception, pos), None, _) => {
EvalAltResult::ErrorRuntime(().into(), *pos).into() EvalAltResult::ErrorRuntime(().into(), *pos).into()
} }

View File

@ -243,7 +243,7 @@ fn optimize_stmt_block(
} }
match stmt { 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 // 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(); state.set_dirty();
*stmt = Stmt::Noop(*pos); *stmt = Stmt::Noop(*pos);
} }
// if true { if_block } -> if_block // 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); *stmt = mem::take(&mut x.0);
optimize_stmt(stmt, state, true); optimize_stmt(stmt, state, true);
} }
// if expr { Noop } // if expr { Noop }
Stmt::IfThenElse(ref mut condition, x, _) Stmt::If(ref mut condition, x, _) if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) => {
if x.1.is_none() && matches!(x.0, Stmt::Noop(_)) =>
{
state.set_dirty(); state.set_dirty();
let pos = condition.position(); let pos = condition.position();
@ -323,22 +321,22 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) {
}; };
} }
// if expr { if_block } // 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_expr(condition, state);
optimize_stmt(&mut x.0, state, true); optimize_stmt(&mut x.0, state, true);
} }
// if false { if_block } else { else_block } -> else_block // 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()); *stmt = mem::take(x.1.as_mut().unwrap());
optimize_stmt(stmt, state, true); optimize_stmt(stmt, state, true);
} }
// if true { if_block } else { else_block } -> if_block // 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); *stmt = mem::take(&mut x.0);
optimize_stmt(stmt, state, true); optimize_stmt(stmt, state, true);
} }
// if expr { if_block } else { else_block } // 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_expr(condition, state);
optimize_stmt(&mut x.0, state, true); optimize_stmt(&mut x.0, state, true);
if let Some(else_block) = x.1.as_mut() { 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; // expr;
Stmt::Expr(ref mut expr) => optimize_expr(expr, state), Stmt::Expr(ref mut expr) => optimize_expr(expr, state),
// return expr; // 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 // All other statements - skip
_ => (), _ => (),
@ -908,11 +906,9 @@ pub fn optimize_into_ast(
// {} -> Noop // {} -> Noop
fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) {
// { return val; } -> val // { return val; } -> val
Stmt::ReturnWithVal((ReturnType::Return, _), Some(expr), _) => { Stmt::Return((ReturnType::Return, _), Some(expr), _) => Stmt::Expr(expr),
Stmt::Expr(expr)
}
// { return; } -> () // { return; } -> ()
Stmt::ReturnWithVal((ReturnType::Return, pos), None, _) => { Stmt::Return((ReturnType::Return, pos), None, _) => {
Stmt::Expr(Expr::Unit(pos)) Stmt::Expr(Expr::Unit(pos))
} }
// All others // All others

View File

@ -1970,11 +1970,7 @@ fn parse_if(
None None
}; };
Ok(Stmt::IfThenElse( Ok(Stmt::If(guard, Box::new((if_body, else_body)), token_pos))
guard,
Box::new((if_body, else_body)),
token_pos,
))
} }
/// Parse a while loop. /// Parse a while loop.
@ -2451,13 +2447,9 @@ fn parse_stmt(
match input.peek().unwrap() { match input.peek().unwrap() {
// `return`/`throw` at <EOF> // `return`/`throw` at <EOF>
(Token::EOF, pos) => Ok(Some(Stmt::ReturnWithVal( (Token::EOF, pos) => Ok(Some(Stmt::Return((return_type, token_pos), None, *pos))),
(return_type, token_pos),
None,
*pos,
))),
// `return;` or `throw;` // `return;` or `throw;`
(Token::SemiColon, _) => Ok(Some(Stmt::ReturnWithVal( (Token::SemiColon, _) => Ok(Some(Stmt::Return(
(return_type, token_pos), (return_type, token_pos),
None, None,
settings.pos, settings.pos,
@ -2466,7 +2458,7 @@ fn parse_stmt(
(_, _) => { (_, _) => {
let expr = parse_expr(input, state, lib, settings.level_up())?; let expr = parse_expr(input, state, lib, settings.level_up())?;
let pos = expr.position(); let pos = expr.position();
Ok(Some(Stmt::ReturnWithVal( Ok(Some(Stmt::Return(
(return_type, token_pos), (return_type, token_pos),
Some(expr), Some(expr),
pos, pos,