|
9 | 9 | #include "parsenumbase.h" |
10 | 10 | #include "parsenum.h" |
11 | 11 |
|
| 12 | +#if MICROPY_ENABLE_FLOAT |
| 13 | +#include <math.h> |
| 14 | +#endif |
| 15 | + |
12 | 16 | #if defined(UNIX) |
13 | 17 |
|
14 | 18 | #include <ctype.h> |
@@ -84,64 +88,120 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { |
84 | 88 | #define PARSE_DEC_IN_FRAC (2) |
85 | 89 | #define PARSE_DEC_IN_EXP (3) |
86 | 90 |
|
87 | | -mp_obj_t mp_parse_num_decimal(const char *str, uint len) { |
| 91 | +mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) { |
88 | 92 | #if MICROPY_ENABLE_FLOAT |
89 | | - int in = PARSE_DEC_IN_INTG; |
| 93 | + const char *top = str + len; |
90 | 94 | mp_float_t dec_val = 0; |
91 | | - bool exp_neg = false; |
92 | | - int exp_val = 0; |
93 | | - int exp_extra = 0; |
| 95 | + bool dec_neg = false; |
94 | 96 | bool imag = false; |
95 | | - const char *top = str + len; |
96 | | - for (; str < top; str++) { |
97 | | - int dig = *str; |
98 | | - if ('0' <= dig && dig <= '9') { |
99 | | - dig -= '0'; |
100 | | - if (in == PARSE_DEC_IN_EXP) { |
101 | | - exp_val = 10 * exp_val + dig; |
102 | | - } else { |
103 | | - dec_val = 10 * dec_val + dig; |
104 | | - if (in == PARSE_DEC_IN_FRAC) { |
105 | | - exp_extra -= 1; |
106 | | - } |
| 97 | + |
| 98 | + // skip leading space |
| 99 | + for (; str < top && isspace(*str); str++) { |
| 100 | + } |
| 101 | + |
| 102 | + // get optional sign |
| 103 | + if (str < top) { |
| 104 | + if (*str == '+') { |
| 105 | + str++; |
| 106 | + } else if (*str == '-') { |
| 107 | + str++; |
| 108 | + dec_neg = true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // determine what the string is |
| 113 | + if (str < top && (str[0] | 0x20) == 'i') { |
| 114 | + // string starts with 'i', should be 'inf' or 'infinity' (case insensitive) |
| 115 | + if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { |
| 116 | + // inf |
| 117 | + str += 3; |
| 118 | + dec_val = INFINITY; |
| 119 | + if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') { |
| 120 | + // infinity |
| 121 | + str += 5; |
107 | 122 | } |
108 | | - } else if (in == PARSE_DEC_IN_INTG && dig == '.') { |
109 | | - in = PARSE_DEC_IN_FRAC; |
110 | | - } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) { |
111 | | - in = PARSE_DEC_IN_EXP; |
112 | | - if (str[1] == '+') { |
113 | | - str++; |
114 | | - } else if (str[1] == '-') { |
| 123 | + } |
| 124 | + } else if (str < top && (str[0] | 0x20) == 'n') { |
| 125 | + // string starts with 'n', should be 'nan' (case insensitive) |
| 126 | + if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { |
| 127 | + // NaN |
| 128 | + str += 3; |
| 129 | + dec_val = MICROPY_FLOAT_C_FUN(nan)(""); |
| 130 | + } |
| 131 | + } else { |
| 132 | + // parse the digits |
| 133 | + int in = PARSE_DEC_IN_INTG; |
| 134 | + bool exp_neg = false; |
| 135 | + int exp_val = 0; |
| 136 | + int exp_extra = 0; |
| 137 | + for (; str < top; str++) { |
| 138 | + int dig = *str; |
| 139 | + if ('0' <= dig && dig <= '9') { |
| 140 | + dig -= '0'; |
| 141 | + if (in == PARSE_DEC_IN_EXP) { |
| 142 | + exp_val = 10 * exp_val + dig; |
| 143 | + } else { |
| 144 | + dec_val = 10 * dec_val + dig; |
| 145 | + if (in == PARSE_DEC_IN_FRAC) { |
| 146 | + exp_extra -= 1; |
| 147 | + } |
| 148 | + } |
| 149 | + } else if (in == PARSE_DEC_IN_INTG && dig == '.') { |
| 150 | + in = PARSE_DEC_IN_FRAC; |
| 151 | + } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) { |
| 152 | + in = PARSE_DEC_IN_EXP; |
| 153 | + if (str[1] == '+') { |
| 154 | + str++; |
| 155 | + } else if (str[1] == '-') { |
| 156 | + str++; |
| 157 | + exp_neg = true; |
| 158 | + } |
| 159 | + } else if (allow_imag && (dig | 0x20) == 'j') { |
115 | 160 | str++; |
116 | | - exp_neg = true; |
| 161 | + imag = true; |
| 162 | + break; |
| 163 | + } else { |
| 164 | + // unknown character |
| 165 | + break; |
117 | 166 | } |
118 | | - } else if (dig == 'J' || dig == 'j') { |
119 | | - str++; |
120 | | - imag = true; |
121 | | - break; |
122 | | - } else { |
123 | | - // unknown character |
124 | | - break; |
| 167 | + } |
| 168 | + |
| 169 | + // work out the exponent |
| 170 | + if (exp_neg) { |
| 171 | + exp_val = -exp_val; |
| 172 | + } |
| 173 | + exp_val += exp_extra; |
| 174 | + |
| 175 | + // apply the exponent |
| 176 | + for (; exp_val > 0; exp_val--) { |
| 177 | + dec_val *= 10; |
| 178 | + } |
| 179 | + for (; exp_val < 0; exp_val++) { |
| 180 | + dec_val *= 0.1; |
125 | 181 | } |
126 | 182 | } |
127 | | - if (*str != 0) { |
128 | | - nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
129 | | - } |
130 | | - if (exp_neg) { |
131 | | - exp_val = -exp_val; |
| 183 | + |
| 184 | + // negate value if needed |
| 185 | + if (dec_neg) { |
| 186 | + dec_val = -dec_val; |
132 | 187 | } |
133 | | - exp_val += exp_extra; |
134 | | - for (; exp_val > 0; exp_val--) { |
135 | | - dec_val *= 10; |
| 188 | + |
| 189 | + // skip trailing space |
| 190 | + for (; str < top && isspace(*str); str++) { |
136 | 191 | } |
137 | | - for (; exp_val < 0; exp_val++) { |
138 | | - dec_val *= 0.1; |
| 192 | + |
| 193 | + // check we reached the end of the string |
| 194 | + if (str != top) { |
| 195 | + nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
139 | 196 | } |
| 197 | + |
| 198 | + // return the object |
140 | 199 | if (imag) { |
141 | 200 | return mp_obj_new_complex(0, dec_val); |
142 | 201 | } else { |
143 | 202 | return mp_obj_new_float(dec_val); |
144 | 203 | } |
| 204 | + |
145 | 205 | #else |
146 | 206 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported")); |
147 | 207 | #endif |
|
0 commit comments