Assignments return () and no compound assignments.

This commit is contained in:
Stephen Chung 2020-07-05 23:07:02 +08:00
parent e390dd73e6
commit 4052ad3df1
2 changed files with 14 additions and 32 deletions

View File

@ -1663,14 +1663,20 @@ impl Engine {
Expr::Variable(_) => unreachable!(),
// idx_lhs[idx_expr] op= rhs
#[cfg(not(feature = "no_index"))]
Expr::Index(_) => self.eval_dot_index_chain(
scope, mods, state, lib, this_ptr, lhs_expr, level, new_val,
),
Expr::Index(_) => {
self.eval_dot_index_chain(
scope, mods, state, lib, this_ptr, lhs_expr, level, new_val,
)?;
Ok(Default::default())
}
// dot_lhs.dot_rhs op= rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(_) => self.eval_dot_index_chain(
scope, mods, state, lib, this_ptr, lhs_expr, level, new_val,
),
Expr::Dot(_) => {
self.eval_dot_index_chain(
scope, mods, state, lib, this_ptr, lhs_expr, level, new_val,
)?;
Ok(Default::default())
}
// Error assignment to constant
expr if expr.is_constant() => {
Err(Box::new(EvalAltResult::ErrorAssignmentToConstant(
@ -1965,15 +1971,7 @@ impl Engine {
Stmt::Noop(_) => Ok(Default::default()),
// Expression as statement
Stmt::Expr(expr) => {
let result = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
Ok(match expr.as_ref() {
// If it is a simple assignment, erase the result at the root
Expr::Assignment(_) => Default::default(),
_ => result,
})
}
Stmt::Expr(expr) => self.eval_expr(scope, mods, state, lib, this_ptr, expr, level),
// Block scope
Stmt::Block(x) => {

View File

@ -382,23 +382,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
stmt => Expr::Stmt(Box::new((stmt, x.1))),
},
// id op= expr
Expr::Assignment(x) => match x.2 {
//id = id2 op= rhs
Expr::Assignment(x2) if x.1.is_empty() => match (x.0, &x2.0) {
// var = var op= expr2 -> var op= expr2
(Expr::Variable(a), Expr::Variable(b))
if a.1.is_none() && b.1.is_none() && a.0 == b.0 && a.3 == b.3 =>
{
// Assignment to the same variable - fold
state.set_dirty();
Expr::Assignment(Box::new((Expr::Variable(a), x2.1, optimize_expr(x2.2, state), x.3)))
}
// expr1 = expr2 op= rhs
(expr1, _) => Expr::Assignment(Box::new((expr1, x.1, optimize_expr(Expr::Assignment(x2), state), x.3))),
},
// expr = rhs
expr => Expr::Assignment(Box::new((x.0, x.1, optimize_expr(expr, state), x.3))),
},
Expr::Assignment(x) => Expr::Assignment(Box::new((x.0, x.1, optimize_expr(x.2, state), x.3))),
// lhs.rhs
#[cfg(not(feature = "no_object"))]