Simplify eval_dot_index_chain.

This commit is contained in:
Stephen Chung 2020-05-31 15:51:26 +08:00
parent d7d49a5196
commit 840afe74bb

View File

@ -441,7 +441,6 @@ fn search_scope<'s, 'a>(
Expr::Variable(x) => x.as_ref(), Expr::Variable(x) => x.as_ref(),
_ => unreachable!(), _ => unreachable!(),
}; };
let index = if state.always_search { None } else { *index };
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
@ -470,6 +469,8 @@ fn search_scope<'s, 'a>(
} }
} }
let index = if state.always_search { None } else { *index };
let index = if let Some(index) = index { let index = if let Some(index) = index {
scope.len() - index.get() scope.len() - index.get()
} else { } else {
@ -1102,7 +1103,7 @@ impl Engine {
} }
// Syntax error // Syntax error
_ => Err(Box::new(EvalAltResult::ErrorDotExpr( _ => Err(Box::new(EvalAltResult::ErrorDotExpr(
format!("{:?}", rhs), "".into(),
rhs.position(), rhs.position(),
))), ))),
} }
@ -1115,13 +1116,16 @@ impl Engine {
scope: &mut Scope, scope: &mut Scope,
state: &mut State, state: &mut State,
lib: &FunctionsLib, lib: &FunctionsLib,
dot_lhs: &Expr, expr: &Expr,
dot_rhs: &Expr,
is_index: bool,
op_pos: Position,
level: usize, level: usize,
new_val: Option<Dynamic>, new_val: Option<Dynamic>,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
let ((dot_lhs, dot_rhs, op_pos), is_index) = match expr {
Expr::Index(x) => (x.as_ref(), true),
Expr::Dot(x) => (x.as_ref(), false),
_ => unreachable!(),
};
let idx_values = &mut StaticVec::new(); let idx_values = &mut StaticVec::new();
self.eval_indexed_chain(scope, state, lib, dot_rhs, idx_values, 0, level)?; self.eval_indexed_chain(scope, state, lib, dot_rhs, idx_values, 0, level)?;
@ -1146,7 +1150,7 @@ impl Engine {
let this_ptr = &mut target.into(); let this_ptr = &mut target.into();
self.eval_dot_index_chain_helper( self.eval_dot_index_chain_helper(
state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
} }
@ -1161,7 +1165,7 @@ impl Engine {
let val = self.eval_expr(scope, state, lib, expr, level)?; let val = self.eval_expr(scope, state, lib, expr, level)?;
let this_ptr = &mut val.into(); let this_ptr = &mut val.into();
self.eval_dot_index_chain_helper( self.eval_dot_index_chain_helper(
state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
} }
@ -1477,14 +1481,14 @@ impl Engine {
Expr::Variable(_) => unreachable!(), Expr::Variable(_) => unreachable!(),
// idx_lhs[idx_expr] op= rhs // idx_lhs[idx_expr] op= rhs
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Index(x) => self.eval_dot_index_chain( Expr::Index(_) => {
scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, self.eval_dot_index_chain(scope, state, lib, lhs_expr, level, new_val)
), }
// dot_lhs.dot_rhs op= rhs // dot_lhs.dot_rhs op= rhs
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Expr::Dot(x) => self.eval_dot_index_chain( Expr::Dot(_) => {
scope, state, lib, &x.0, &x.1, false, *op_pos, level, new_val, self.eval_dot_index_chain(scope, state, lib, lhs_expr, level, new_val)
), }
// Error assignment to constant // Error assignment to constant
expr if expr.is_constant() => { expr if expr.is_constant() => {
Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( Err(Box::new(EvalAltResult::ErrorAssignmentToConstant(
@ -1501,15 +1505,11 @@ impl Engine {
// lhs[idx_expr] // lhs[idx_expr]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Index(x) => { Expr::Index(_) => self.eval_dot_index_chain(scope, state, lib, expr, level, None),
self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, true, x.2, level, None)
}
// lhs.dot_rhs // lhs.dot_rhs
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Expr::Dot(x) => { Expr::Dot(_) => self.eval_dot_index_chain(scope, state, lib, expr, level, None),
self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, false, x.2, level, None)
}
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new( Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new(