Use matches! macro.

This commit is contained in:
Stephen Chung 2020-03-14 23:41:15 +08:00
parent 6e076c409d
commit 01cf777961
3 changed files with 54 additions and 79 deletions

View File

@ -153,8 +153,8 @@ impl Engine<'_> {
);
// Evaluate
return match self.eval_stmt(&mut scope, &fn_def.body) {
// Convert return statement to return value
return match self.eval_stmt(&mut scope, &fn_def.body) {
Err(EvalAltResult::Return(x, _)) => Ok(x),
other => other,
};
@ -985,10 +985,11 @@ impl Engine<'_> {
Stmt::Expr(expr) => {
let result = self.eval_expr(scope, expr)?;
Ok(match expr.as_ref() {
Ok(if !matches!(expr.as_ref(), Expr::Assignment(_, _, _)) {
result
} else {
// If it is an assignment, erase the result at the root
Expr::Assignment(_, _, _) => ().into_dynamic(),
_ => result,
().into_dynamic()
})
}
@ -1032,9 +1033,9 @@ impl Engine<'_> {
Ok(guard_val) => {
if *guard_val {
match self.eval_stmt(scope, body) {
Ok(_) => (),
Err(EvalAltResult::LoopBreak) => return Ok(().into_dynamic()),
Err(x) => return Err(x),
_ => (),
}
} else {
return Ok(().into_dynamic());
@ -1047,9 +1048,9 @@ impl Engine<'_> {
// Loop statement
Stmt::Loop(body) => loop {
match self.eval_stmt(scope, body) {
Ok(_) => (),
Err(EvalAltResult::LoopBreak) => return Ok(().into_dynamic()),
Err(x) => return Err(x),
_ => (),
}
},
@ -1066,9 +1067,9 @@ impl Engine<'_> {
*scope.get_mut(name, idx) = a;
match self.eval_stmt(scope, body) {
Ok(_) => (),
Err(EvalAltResult::LoopBreak) => break,
Err(x) => return Err(x),
_ => (),
}
}
scope.pop();

View File

@ -50,9 +50,9 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
let pos = expr.position();
let expr = optimize_expr(*expr, state);
match expr {
Expr::False(_) | Expr::True(_) => Stmt::Noop(stmt1.position()),
expr => {
if matches!(expr, Expr::False(_) | Expr::True(_)) {
Stmt::Noop(stmt1.position())
} else {
let stmt = Stmt::Expr(Box::new(expr));
if preserve_result {
@ -62,7 +62,6 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
}
}
}
}
Stmt::IfElse(expr, stmt1, None) => match *expr {
Expr::False(pos) => {
@ -136,10 +135,7 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
// Remove all raw expression statements that are pure except for the very last statement
let last_stmt = if preserve_result { result.pop() } else { None };
result.retain(|stmt| match stmt {
Stmt::Expr(expr) if expr.is_pure() => false,
_ => true,
});
result.retain(|stmt| !matches!(stmt, Stmt::Expr(expr) if expr.is_pure()));
if let Some(stmt) = last_stmt {
result.push(stmt);
@ -154,7 +150,6 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
match expr {
Stmt::Let(_, None, _) => removed = true,
Stmt::Let(_, Some(val_expr), _) if val_expr.is_pure() => removed = true,
_ => {
result.push(expr);
break;
@ -402,10 +397,7 @@ pub(crate) fn optimize(statements: Vec<Stmt>, scope: &Scope) -> Vec<Stmt> {
let last_stmt = result.pop();
// Remove all pure statements at top level
result.retain(|stmt| match stmt {
Stmt::Expr(expr) if expr.is_pure() => false,
_ => true,
});
result.retain(|stmt| !matches!(stmt, Stmt::Expr(expr) if expr.is_pure()));
if let Some(stmt) = last_stmt {
result.push(stmt); // Add back the last statement

View File

@ -229,24 +229,15 @@ pub enum Stmt {
impl Stmt {
pub fn is_noop(&self) -> bool {
match self {
Stmt::Noop(_) => true,
_ => false,
}
matches!(self, Stmt::Noop(_))
}
pub fn is_op(&self) -> bool {
match self {
Stmt::Noop(_) => false,
_ => true,
}
!matches!(self, Stmt::Noop(_))
}
pub fn is_var(&self) -> bool {
match self {
Stmt::Let(_, _, _) => true,
_ => false,
}
matches!(self, Stmt::Let(_, _, _))
}
pub fn position(&self) -> Position {
@ -334,7 +325,7 @@ impl Expr {
match self {
Expr::Array(expressions, _) => expressions.iter().all(Expr::is_pure),
Expr::And(x, y) | Expr::Or(x, y) | Expr::Index(x, y, _) => x.is_pure() && y.is_pure(),
expr => expr.is_constant() || expr.is_variable(),
expr => expr.is_constant() || matches!(expr, Expr::Variable(_, _)),
}
}
@ -355,20 +346,6 @@ impl Expr {
_ => false,
}
}
pub fn is_variable(&self) -> bool {
match self {
Expr::Variable(_, _) => true,
_ => false,
}
}
pub fn is_property(&self) -> bool {
match self {
Expr::Property(_, _) => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Clone)]
@ -1629,13 +1606,13 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseEr
}
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, _, _) if idx_lhs.is_variable() => {
Expr::Index(idx_lhs, _, _) if matches!(idx_lhs.as_ref(), &Expr::Variable(_, _)) => {
assert!(is_top, "property expected but gets variable");
None
}
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, _, _) if idx_lhs.is_property() => {
Expr::Index(idx_lhs, _, _) if matches!(idx_lhs.as_ref(), &Expr::Property(_, _)) => {
assert!(!is_top, "variable expected but gets property");
None
}
@ -1655,7 +1632,13 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseEr
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, _, _)
if (idx_lhs.is_variable() && is_top) || (idx_lhs.is_property() && !is_top) =>
if matches!(idx_lhs.as_ref(), &Expr::Variable(_, _)) && is_top =>
{
valid_assignment_chain(dot_rhs, false)
}
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, _, _)
if matches!(idx_lhs.as_ref(), &Expr::Property(_, _)) && !is_top =>
{
valid_assignment_chain(dot_rhs, false)
}
@ -1865,9 +1848,10 @@ fn parse_if<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseEr
Some(&(Token::Else, _)) => {
input.next();
let else_body = match input.peek() {
Some(&(Token::If, _)) => parse_if(input)?,
_ => parse_block(input)?,
let else_body = if matches!(input.peek(), Some(&(Token::If, _))) {
parse_if(input)?
} else {
parse_block(input)?
};
Ok(Stmt::IfElse(
@ -1934,8 +1918,7 @@ fn parse_var<'a>(
None => return Err(ParseError::new(PERR::VarExpectsIdentifier, Position::eof())),
};
match input.peek() {
Some(&(Token::Equals, _)) => {
if matches!(input.peek(), Some(&(Token::Equals, _))) {
input.next();
let init_value = parse_expr(input)?;
@ -1951,8 +1934,8 @@ fn parse_var<'a>(
init_value.position(),
)),
}
}
_ => Ok(Stmt::Let(name, None, pos)),
} else {
Ok(Stmt::Let(name, None, pos))
}
}
@ -2072,11 +2055,10 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
let mut params = Vec::new();
match input.peek() {
Some(&(Token::RightParen, _)) => {
if matches!(input.peek(), Some(&(Token::RightParen, _))) {
input.next();
}
_ => loop {
} else {
loop {
match input.next() {
Some((Token::RightParen, _)) => break,
Some((Token::Comma, _)) => (),
@ -2100,7 +2082,7 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
))
}
}
},
}
}
let body = parse_block(input)?;