Skip to content

Commit 2fcdbe8

Browse files
committed
Fix
1 parent d0999e7 commit 2fcdbe8

File tree

1 file changed

+19
-22
lines changed
  • src/elements/expr/arithmetic

1 file changed

+19
-22
lines changed

src/elements/expr/arithmetic/word.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,36 @@ pub fn str_to_num(name: &str, core: &mut ShellCore) -> Result<ArithElem, String>
6363
}
6464

6565
match single_str_to_num(&name, core) {
66-
Some(e) => return Ok(e),
67-
None => {},
66+
Some(e) => Ok(e),
67+
None => resolve_arithmetic_op(&name, core),
6868
}
69+
}
6970

70-
/* resolve the case where the name is an arithmetic operation */
71+
fn resolve_arithmetic_op(name: &str, core: &mut ShellCore) -> Result<ArithElem, String> {
7172
let mut f = Feeder::new(&name);
72-
if let Some(mut a) = ArithmeticExpr::parse(&mut f, core, false) {
73-
if a.elements.len() == 1 { // In this case, the element is not changed by the evaluation.
74-
return Err(error::syntax(&name));
75-
}
73+
let mut parsed = match ArithmeticExpr::parse(&mut f, core, false) {
74+
Some(p) => p,
75+
None => return Err(error::syntax(&name)),
76+
};
7677

77-
if let Some(s) = a.eval(core) {
78-
match single_str_to_num(&s, core) {
79-
Some(e) => return Ok(e),
80-
None => {},
81-
}
78+
if parsed.elements.len() == 1 { // In this case, the element is not changed by the evaluation.
79+
return Err(error::syntax(&name));
80+
}
81+
82+
if let Some(eval) = parsed.eval(core) {
83+
if let Some(e) = single_str_to_num(&eval, core) {
84+
return Ok(e);
8285
}
8386
}
8487

8588
Err(error::syntax(&name))
8689
}
8790

8891
fn single_str_to_num(name: &str, core: &mut ShellCore) -> Option<ArithElem> {
89-
if let Some(n) = int::parse(&name) {
90-
Some( ArithElem::Integer(n) )
91-
}else if is_name(&name, core) {
92-
Some( ArithElem::Integer(0) )
93-
}else if let Some(f) = float::parse(&name) {
94-
Some( ArithElem::Float(f) )
95-
}else{
96-
None
97-
}
92+
if let Some(n) = int::parse(&name) { Some( ArithElem::Integer(n) )
93+
}else if is_name(&name, core) { Some( ArithElem::Integer(0) )
94+
}else if let Some(f) = float::parse(&name) { Some( ArithElem::Float(f) )
95+
}else{ None }
9896
}
9997

10098
fn change_variable(name: &str, core: &mut ShellCore, inc: i64, pre: bool) -> Result<ArithElem, String> {
@@ -193,5 +191,4 @@ fn subs(op: &str, w: &Word, right_value: &ArithElem, core: &mut ShellCore)
193191
(ArithElem::Integer(cur), ArithElem::Float(right)) => float::substitute(op, &name, cur as f64, *right, core),
194192
_ => Err("support not yet".to_string()),
195193
}
196-
197194
}

0 commit comments

Comments
 (0)