@@ -63,38 +63,36 @@ pub fn str_to_num(name: &str, core: &mut ShellCore) -> Result<ArithElem, String>
63
63
}
64
64
65
65
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 ) ,
68
68
}
69
+ }
69
70
70
- /* resolve the case where the name is an arithmetic operation */
71
+ fn resolve_arithmetic_op ( name : & str , core : & mut ShellCore ) -> Result < ArithElem , String > {
71
72
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
+ } ;
76
77
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) ;
82
85
}
83
86
}
84
87
85
88
Err ( error:: syntax ( & name) )
86
89
}
87
90
88
91
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 }
98
96
}
99
97
100
98
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)
193
191
( ArithElem :: Integer ( cur) , ArithElem :: Float ( right) ) => float:: substitute ( op, & name, cur as f64 , * right, core) ,
194
192
_ => Err ( "support not yet" . to_string ( ) ) ,
195
193
}
196
-
197
194
}
0 commit comments