@@ -62,35 +62,38 @@ pub fn str_to_num(name: &str, core: &mut ShellCore) -> Result<ArithElem, String>
62
62
}
63
63
}
64
64
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 {
67
74
return Err ( error:: syntax ( & name) ) ;
68
75
}
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
+ }
69
83
}
70
84
85
+ Err ( error:: syntax ( & name) )
86
+ }
87
+
88
+ fn single_str_to_num ( name : & str , core : & mut ShellCore ) -> Option < ArithElem > {
71
89
if let Some ( n) = int:: parse ( & name) {
72
- Ok ( ArithElem :: Integer ( n) )
90
+ Some ( ArithElem :: Integer ( n) )
73
91
} else if is_name ( & name, core) {
74
- Ok ( ArithElem :: Integer ( 0 ) )
92
+ Some ( ArithElem :: Integer ( 0 ) )
75
93
} 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
94
97
}
95
98
}
96
99
0 commit comments