parsenum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdbool.h>
  27. #include <stdlib.h>
  28. #include "py/runtime.h"
  29. #include "py/parsenumbase.h"
  30. #include "py/parsenum.h"
  31. #include "py/smallint.h"
  32. #if MICROPY_PY_BUILTINS_FLOAT
  33. #include <math.h>
  34. #endif
  35. static NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
  36. // if lex!=NULL then the parser called us and we need to convert the
  37. // exception's type from ValueError to SyntaxError and add traceback info
  38. if (lex != NULL) {
  39. ((mp_obj_base_t *)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError;
  40. mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTRnull);
  41. }
  42. nlr_raise(exc);
  43. }
  44. mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) {
  45. const byte *restrict str = (const byte *)str_;
  46. const byte *restrict top = str + len;
  47. bool neg = false;
  48. mp_obj_t ret_val;
  49. // check radix base
  50. if ((base != 0 && base < 2) || base > 36) {
  51. // this won't be reached if lex!=NULL
  52. mp_raise_ValueError(MP_ERROR_TEXT("int() arg 2 must be >= 2 and <= 36"));
  53. }
  54. // skip leading space
  55. for (; str < top && unichar_isspace(*str); str++) {
  56. }
  57. // parse optional sign
  58. if (str < top) {
  59. if (*str == '+') {
  60. str++;
  61. } else if (*str == '-') {
  62. str++;
  63. neg = true;
  64. }
  65. }
  66. // parse optional base prefix
  67. str += mp_parse_num_base((const char *)str, top - str, &base);
  68. // string should be an integer number
  69. mp_int_t int_val = 0;
  70. const byte *restrict str_val_start = str;
  71. for (; str < top; str++) {
  72. // get next digit as a value
  73. mp_uint_t dig = *str;
  74. if ('0' <= dig && dig <= '9') {
  75. dig -= '0';
  76. } else if (dig == '_') {
  77. continue;
  78. } else {
  79. dig |= 0x20; // make digit lower-case
  80. if ('a' <= dig && dig <= 'z') {
  81. dig -= 'a' - 10;
  82. } else {
  83. // unknown character
  84. break;
  85. }
  86. }
  87. if (dig >= (mp_uint_t)base) {
  88. break;
  89. }
  90. // add next digi and check for overflow
  91. if (mp_small_int_mul_overflow(int_val, base)) {
  92. goto overflow;
  93. }
  94. int_val = int_val * base + dig;
  95. if (!MP_SMALL_INT_FITS(int_val)) {
  96. goto overflow;
  97. }
  98. }
  99. // negate value if needed
  100. if (neg) {
  101. int_val = -int_val;
  102. }
  103. // create the small int
  104. ret_val = MP_OBJ_NEW_SMALL_INT(int_val);
  105. have_ret_val:
  106. // check we parsed something
  107. if (str == str_val_start) {
  108. goto value_error;
  109. }
  110. // skip trailing space
  111. for (; str < top && unichar_isspace(*str); str++) {
  112. }
  113. // check we reached the end of the string
  114. if (str != top) {
  115. goto value_error;
  116. }
  117. // return the object
  118. return ret_val;
  119. overflow:
  120. // reparse using long int
  121. {
  122. const char *s2 = (const char *)str_val_start;
  123. ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
  124. str = (const byte *)s2;
  125. goto have_ret_val;
  126. }
  127. value_error:
  128. {
  129. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  130. mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
  131. MP_ERROR_TEXT("invalid syntax for integer"));
  132. raise_exc(exc, lex);
  133. #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
  134. mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  135. MP_ERROR_TEXT("invalid syntax for integer with base %d"), base);
  136. raise_exc(exc, lex);
  137. #else
  138. vstr_t vstr;
  139. mp_print_t print;
  140. vstr_init_print(&vstr, 50, &print);
  141. mp_printf(&print, "invalid syntax for integer with base %d: ", base);
  142. mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
  143. mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
  144. mp_obj_new_str_from_utf8_vstr(&vstr));
  145. raise_exc(exc, lex);
  146. #endif
  147. }
  148. }
  149. enum {
  150. REAL_IMAG_STATE_START = 0,
  151. REAL_IMAG_STATE_HAVE_REAL = 1,
  152. REAL_IMAG_STATE_HAVE_IMAG = 2,
  153. };
  154. typedef enum {
  155. PARSE_DEC_IN_INTG,
  156. PARSE_DEC_IN_FRAC,
  157. PARSE_DEC_IN_EXP,
  158. } parse_dec_in_t;
  159. #if MICROPY_PY_BUILTINS_FLOAT
  160. // DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing
  161. // SMALL_NORMAL_VAL is the smallest power of 10 that is still a normal float
  162. // EXACT_POWER_OF_10 is the largest value of x so that 10^x can be stored exactly in a float
  163. // Note: EXACT_POWER_OF_10 is at least floor(log_5(2^mantissa_length)). Indeed, 10^n = 2^n * 5^n
  164. // so we only have to store the 5^n part in the mantissa (the 2^n part will go into the float's
  165. // exponent).
  166. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  167. #define DEC_VAL_MAX 1e20F
  168. #define SMALL_NORMAL_VAL (1e-37F)
  169. #define SMALL_NORMAL_EXP (-37)
  170. #define EXACT_POWER_OF_10 (9)
  171. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  172. #define DEC_VAL_MAX 1e200
  173. #define SMALL_NORMAL_VAL (1e-307)
  174. #define SMALL_NORMAL_EXP (-307)
  175. #define EXACT_POWER_OF_10 (22)
  176. #endif
  177. // Break out inner digit accumulation routine to ease trailing zero deferral.
  178. static void accept_digit(mp_float_t *p_dec_val, int dig, int *p_exp_extra, int in) {
  179. // Core routine to ingest an additional digit.
  180. if (*p_dec_val < DEC_VAL_MAX) {
  181. // dec_val won't overflow so keep accumulating
  182. *p_dec_val = 10 * *p_dec_val + dig;
  183. if (in == PARSE_DEC_IN_FRAC) {
  184. --(*p_exp_extra);
  185. }
  186. } else {
  187. // dec_val might overflow and we anyway can't represent more digits
  188. // of precision, so ignore the digit and just adjust the exponent
  189. if (in == PARSE_DEC_IN_INTG) {
  190. ++(*p_exp_extra);
  191. }
  192. }
  193. }
  194. #endif // MICROPY_PY_BUILTINS_FLOAT
  195. #if MICROPY_PY_BUILTINS_COMPLEX
  196. mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
  197. #else
  198. mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lexer_t *lex)
  199. #endif
  200. {
  201. #if MICROPY_PY_BUILTINS_FLOAT
  202. const char *top = str + len;
  203. mp_float_t dec_val = 0;
  204. bool dec_neg = false;
  205. #if MICROPY_PY_BUILTINS_COMPLEX
  206. unsigned int real_imag_state = REAL_IMAG_STATE_START;
  207. mp_float_t dec_real = 0;
  208. parse_start:
  209. #endif
  210. // skip leading space
  211. for (; str < top && unichar_isspace(*str); str++) {
  212. }
  213. // parse optional sign
  214. if (str < top) {
  215. if (*str == '+') {
  216. str++;
  217. } else if (*str == '-') {
  218. str++;
  219. dec_neg = true;
  220. }
  221. }
  222. const char *str_val_start = str;
  223. // determine what the string is
  224. if (str < top && (str[0] | 0x20) == 'i') {
  225. // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
  226. if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
  227. // inf
  228. str += 3;
  229. dec_val = (mp_float_t)INFINITY;
  230. if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
  231. // infinity
  232. str += 5;
  233. }
  234. }
  235. } else if (str < top && (str[0] | 0x20) == 'n') {
  236. // string starts with 'n', should be 'nan' (case insensitive)
  237. if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
  238. // NaN
  239. str += 3;
  240. dec_val = MICROPY_FLOAT_C_FUN(nan)("");
  241. }
  242. } else {
  243. // string should be a decimal number
  244. parse_dec_in_t in = PARSE_DEC_IN_INTG;
  245. bool exp_neg = false;
  246. int exp_val = 0;
  247. int exp_extra = 0;
  248. int trailing_zeros_intg = 0, trailing_zeros_frac = 0;
  249. while (str < top) {
  250. unsigned int dig = *str++;
  251. if ('0' <= dig && dig <= '9') {
  252. dig -= '0';
  253. if (in == PARSE_DEC_IN_EXP) {
  254. // don't overflow exp_val when adding next digit, instead just truncate
  255. // it and the resulting float will still be correct, either inf or 0.0
  256. // (use INT_MAX/2 to allow adding exp_extra at the end without overflow)
  257. if (exp_val < (INT_MAX / 2 - 9) / 10) {
  258. exp_val = 10 * exp_val + dig;
  259. }
  260. } else {
  261. if (dig == 0 || dec_val >= DEC_VAL_MAX) {
  262. // Defer treatment of zeros in fractional part. If nothing comes afterwards, ignore them.
  263. // Also, once we reach DEC_VAL_MAX, treat every additional digit as a trailing zero.
  264. if (in == PARSE_DEC_IN_INTG) {
  265. ++trailing_zeros_intg;
  266. } else {
  267. ++trailing_zeros_frac;
  268. }
  269. } else {
  270. // Time to un-defer any trailing zeros. Intg zeros first.
  271. while (trailing_zeros_intg) {
  272. accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_INTG);
  273. --trailing_zeros_intg;
  274. }
  275. while (trailing_zeros_frac) {
  276. accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_FRAC);
  277. --trailing_zeros_frac;
  278. }
  279. accept_digit(&dec_val, dig, &exp_extra, in);
  280. }
  281. }
  282. } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
  283. in = PARSE_DEC_IN_FRAC;
  284. } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
  285. in = PARSE_DEC_IN_EXP;
  286. if (str < top) {
  287. if (str[0] == '+') {
  288. str++;
  289. } else if (str[0] == '-') {
  290. str++;
  291. exp_neg = true;
  292. }
  293. }
  294. if (str == top) {
  295. goto value_error;
  296. }
  297. } else if (dig == '_') {
  298. continue;
  299. } else {
  300. // unknown character
  301. str--;
  302. break;
  303. }
  304. }
  305. // work out the exponent
  306. if (exp_neg) {
  307. exp_val = -exp_val;
  308. }
  309. // apply the exponent, making sure it's not a subnormal value
  310. exp_val += exp_extra + trailing_zeros_intg;
  311. if (exp_val < SMALL_NORMAL_EXP) {
  312. exp_val -= SMALL_NORMAL_EXP;
  313. dec_val *= SMALL_NORMAL_VAL;
  314. }
  315. // At this point, we need to multiply the mantissa by its base 10 exponent. If possible,
  316. // we would rather manipulate numbers that have an exact representation in IEEE754. It
  317. // turns out small positive powers of 10 do, whereas small negative powers of 10 don't.
  318. // So in that case, we'll yield a division of exact values rather than a multiplication
  319. // of slightly erroneous values.
  320. if (exp_val < 0 && exp_val >= -EXACT_POWER_OF_10) {
  321. dec_val /= MICROPY_FLOAT_C_FUN(pow)(10, -exp_val);
  322. } else {
  323. dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
  324. }
  325. }
  326. if (allow_imag && str < top && (*str | 0x20) == 'j') {
  327. #if MICROPY_PY_BUILTINS_COMPLEX
  328. if (str == str_val_start) {
  329. // Convert "j" to "1j".
  330. dec_val = 1;
  331. }
  332. ++str;
  333. real_imag_state |= REAL_IMAG_STATE_HAVE_IMAG;
  334. #else
  335. raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("complex values not supported")), lex);
  336. #endif
  337. }
  338. // negate value if needed
  339. if (dec_neg) {
  340. dec_val = -dec_val;
  341. }
  342. // check we parsed something
  343. if (str == str_val_start) {
  344. goto value_error;
  345. }
  346. // skip trailing space
  347. for (; str < top && unichar_isspace(*str); str++) {
  348. }
  349. // check we reached the end of the string
  350. if (str != top) {
  351. #if MICROPY_PY_BUILTINS_COMPLEX
  352. if (force_complex && real_imag_state == REAL_IMAG_STATE_START) {
  353. // If we've only seen a real so far, keep parsing for the imaginary part.
  354. dec_real = dec_val;
  355. dec_val = 0;
  356. real_imag_state |= REAL_IMAG_STATE_HAVE_REAL;
  357. goto parse_start;
  358. }
  359. #endif
  360. goto value_error;
  361. }
  362. #if MICROPY_PY_BUILTINS_COMPLEX
  363. if (real_imag_state == REAL_IMAG_STATE_HAVE_REAL) {
  364. // We're on the second part, but didn't get the expected imaginary number.
  365. goto value_error;
  366. }
  367. #endif
  368. // return the object
  369. #if MICROPY_PY_BUILTINS_COMPLEX
  370. if (real_imag_state != REAL_IMAG_STATE_START) {
  371. return mp_obj_new_complex(dec_real, dec_val);
  372. } else if (force_complex) {
  373. return mp_obj_new_complex(dec_val, 0);
  374. }
  375. #endif
  376. return mp_obj_new_float(dec_val);
  377. value_error:
  378. raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("invalid syntax for number")), lex);
  379. #else
  380. raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("decimal numbers not supported")), lex);
  381. #endif
  382. }