|
36 | 36 | #include "lexer.h" |
37 | 37 | #include "parsenumbase.h" |
38 | 38 | #include "parse.h" |
| 39 | +#include "smallint.h" |
39 | 40 |
|
40 | 41 | #define RULE_ACT_KIND_MASK (0xf0) |
41 | 42 | #define RULE_ACT_ARG_MASK (0x0f) |
@@ -311,31 +312,32 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) { |
311 | 312 | int i = mp_parse_num_base(str, len, &base); |
312 | 313 | bool overflow = false; |
313 | 314 | for (; i < len; i++) { |
314 | | - machine_int_t old_val = int_val; |
| 315 | + int dig; |
315 | 316 | if (unichar_isdigit(str[i]) && str[i] - '0' < base) { |
316 | | - int_val = base * int_val + str[i] - '0'; |
| 317 | + dig = str[i] - '0'; |
317 | 318 | } else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') { |
318 | | - int_val = base * int_val + str[i] - 'a' + 10; |
| 319 | + dig = str[i] - 'a' + 10; |
319 | 320 | } else if (base == 16 && 'A' <= str[i] && str[i] <= 'F') { |
320 | | - int_val = base * int_val + str[i] - 'A' + 10; |
| 321 | + dig = str[i] - 'A' + 10; |
321 | 322 | } else if (str[i] == '.' || str[i] == 'e' || str[i] == 'E' || str[i] == 'j' || str[i] == 'J') { |
322 | 323 | dec = true; |
323 | 324 | break; |
324 | 325 | } else { |
325 | 326 | small_int = false; |
326 | 327 | break; |
327 | 328 | } |
328 | | - if (int_val < old_val) { |
329 | | - // If new value became less than previous, it's overflow |
| 329 | + // add next digi and check for overflow |
| 330 | + if (mp_small_int_mul_overflow(int_val, base)) { |
330 | 331 | overflow = true; |
331 | | - } else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) { |
332 | | - // If signed number changed sign - it's overflow |
| 332 | + } |
| 333 | + int_val = int_val * base + dig; |
| 334 | + if (!MP_SMALL_INT_FITS(int_val)) { |
333 | 335 | overflow = true; |
334 | 336 | } |
335 | 337 | } |
336 | 338 | if (dec) { |
337 | 339 | pn = mp_parse_node_new_leaf(MP_PARSE_NODE_DECIMAL, qstr_from_strn(str, len)); |
338 | | - } else if (small_int && !overflow && MP_PARSE_FITS_SMALL_INT(int_val)) { |
| 340 | + } else if (small_int && !overflow && MP_SMALL_INT_FITS(int_val)) { |
339 | 341 | pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, int_val); |
340 | 342 | } else { |
341 | 343 | pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn(str, len)); |
|
0 commit comments