Skip to content

Commit 6349206

Browse files
committed
Fix
1 parent 3ea5253 commit 6349206

File tree

1 file changed

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

1 file changed

+25
-22
lines changed

src/elements/expr/arithmetic/word.rs

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

65-
for ch in name.chars() {
66-
if ch.len_utf8() > 1 {
65+
match single_str_to_num(&name, core) {
66+
Some(e) => return Ok(e),
67+
None => {},
68+
}
69+
70+
/* resolve the case where the name is an arithmetic operation */
71+
let mut f = Feeder::new(&name);
72+
if let Some(mut a) = ArithmeticExpr::parse(&mut f, core, false) {
73+
if a.elements.len() == 1 {
6774
return Err(error::syntax(&name));
6875
}
76+
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+
}
82+
}
6983
}
7084

85+
Err(error::syntax(&name))
86+
}
87+
88+
fn single_str_to_num(name: &str, core: &mut ShellCore) -> Option<ArithElem> {
7189
if let Some(n) = int::parse(&name) {
72-
Ok( ArithElem::Integer(n) )
90+
Some( ArithElem::Integer(n) )
7391
}else if is_name(&name, core) {
74-
Ok( ArithElem::Integer(0) )
92+
Some( ArithElem::Integer(0) )
7593
}else if let Some(f) = float::parse(&name) {
76-
Ok( ArithElem::Float(f) )
77-
}else {
78-
let mut f = Feeder::new(&name);
79-
if let Some(mut a) = ArithmeticExpr::parse(&mut f, core, false) {
80-
if a.elements.len() == 1 {
81-
if a.text.contains('#') || a.text.contains('x') || a.text.contains('o') {
82-
return Err(error::syntax(&name));
83-
}
84-
}
85-
86-
if let Some(s) = a.eval(core) {
87-
if let Some(n) = int::parse(&s) {
88-
return Ok( ArithElem::Integer(n) );
89-
}
90-
}
91-
}
92-
93-
Err(error::syntax(&name))
94+
Some( ArithElem::Float(f) )
95+
}else{
96+
None
9497
}
9598
}
9699

0 commit comments

Comments
 (0)