Change Stmt Variant names.
This commit is contained in:
parent
fce2c62f02
commit
0182117759
22
src/ast.rs
22
src/ast.rs
@ -582,7 +582,7 @@ pub enum Stmt {
|
||||
/// No-op.
|
||||
Noop(Position),
|
||||
/// if expr { stmt } else { stmt }
|
||||
IfThenElse(Expr, Box<(Stmt, Option<Stmt>)>, Position),
|
||||
If(Expr, Box<(Stmt, Option<Stmt>)>, Position),
|
||||
/// while expr { stmt }
|
||||
While(Expr, Box<Stmt>, Position),
|
||||
/// loop { stmt }
|
||||
@ -606,7 +606,7 @@ pub enum Stmt {
|
||||
/// break
|
||||
Break(Position),
|
||||
/// return/throw
|
||||
ReturnWithVal((ReturnType, Position), Option<Expr>, Position),
|
||||
Return((ReturnType, Position), Option<Expr>, Position),
|
||||
/// import expr as var
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
Import(Expr, Option<Box<IdentX>>, 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"))]
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 <EOF>
|
||||
(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,
|
||||
|
Loading…
Reference in New Issue
Block a user