xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 1 | #include <stdbool.h> |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "misc.h" |
| 5 | #include "mpconfig.h" |
| 6 | #include "qstr.h" |
| 7 | #include "nlr.h" |
| 8 | #include "obj.h" |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 9 | #include "parsenumbase.h" |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 10 | #include "parsenum.h" |
| 11 | |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 12 | #if MICROPY_ENABLE_FLOAT |
| 13 | #include <math.h> |
| 14 | #endif |
| 15 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 16 | mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 17 | const char *restrict top = str + len; |
| 18 | bool neg = false; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 19 | |
| 20 | // check radix base |
| 21 | if ((base != 0 && base < 2) || base > 36) { |
| 22 | nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36")); |
| 23 | } |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 24 | |
| 25 | // skip leading space |
| 26 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 29 | // parse optional sign |
| 30 | if (str < top) { |
| 31 | if (*str == '+') { |
| 32 | str++; |
| 33 | } else if (*str == '-') { |
| 34 | str++; |
| 35 | neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 39 | // parse optional base prefix |
| 40 | str += mp_parse_num_base(str, top - str, &base); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 41 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 42 | // string should be an integer number |
| 43 | machine_int_t int_val = 0; |
| 44 | for (; str < top; str++) { |
| 45 | machine_int_t old_val = int_val; |
| 46 | int dig = *str; |
| 47 | if (unichar_isdigit(dig) && dig - '0' < base) { |
| 48 | // 0-9 digit |
| 49 | int_val = base * int_val + dig - '0'; |
| 50 | } else if (base == 16) { |
| 51 | dig |= 0x20; |
| 52 | if ('a' <= dig && dig <= 'f') { |
| 53 | // a-f hex digit |
| 54 | int_val = base * int_val + dig - 'a' + 10; |
| 55 | } else { |
| 56 | // unknown character |
| 57 | break; |
| 58 | } |
| 59 | } else { |
| 60 | // unknown character |
| 61 | break; |
| 62 | } |
| 63 | if (int_val < old_val) { |
| 64 | // If new value became less than previous, it's overflow |
| 65 | goto overflow; |
| 66 | } else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) { |
| 67 | // If signed number changed sign - it's overflow |
| 68 | goto overflow; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // negate value if needed |
| 73 | if (neg) { |
| 74 | int_val = -int_val; |
| 75 | } |
| 76 | |
| 77 | // skip trailing space |
| 78 | for (; str < top && unichar_isspace(*str); str++) { |
| 79 | } |
| 80 | |
| 81 | // check we reached the end of the string |
| 82 | if (str != top) { |
| 83 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
| 84 | } |
| 85 | |
| 86 | // return the object |
| 87 | return MP_OBJ_NEW_SMALL_INT(int_val); |
| 88 | |
| 89 | overflow: |
| 90 | // TODO reparse using bignum |
| 91 | nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer")); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 94 | #define PARSE_DEC_IN_INTG (1) |
| 95 | #define PARSE_DEC_IN_FRAC (2) |
| 96 | #define PARSE_DEC_IN_EXP (3) |
| 97 | |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 98 | mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 99 | #if MICROPY_ENABLE_FLOAT |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 100 | const char *top = str + len; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 101 | mp_float_t dec_val = 0; |
| 102 | bool dec_neg = false; |
| 103 | bool imag = false; |
| 104 | |
| 105 | // skip leading space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 106 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 109 | // parse optional sign |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 110 | if (str < top) { |
| 111 | if (*str == '+') { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 112 | str++; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 113 | } else if (*str == '-') { |
| 114 | str++; |
| 115 | dec_neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 118 | |
| 119 | // determine what the string is |
| 120 | if (str < top && (str[0] | 0x20) == 'i') { |
| 121 | // string starts with 'i', should be 'inf' or 'infinity' (case insensitive) |
| 122 | if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { |
| 123 | // inf |
| 124 | str += 3; |
| 125 | dec_val = INFINITY; |
| 126 | if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') { |
| 127 | // infinity |
| 128 | str += 5; |
| 129 | } |
| 130 | } |
| 131 | } else if (str < top && (str[0] | 0x20) == 'n') { |
| 132 | // string starts with 'n', should be 'nan' (case insensitive) |
| 133 | if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { |
| 134 | // NaN |
| 135 | str += 3; |
| 136 | dec_val = MICROPY_FLOAT_C_FUN(nan)(""); |
| 137 | } |
| 138 | } else { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 139 | // string should be a decimal number |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 140 | int in = PARSE_DEC_IN_INTG; |
| 141 | bool exp_neg = false; |
| 142 | int exp_val = 0; |
| 143 | int exp_extra = 0; |
| 144 | for (; str < top; str++) { |
| 145 | int dig = *str; |
| 146 | if ('0' <= dig && dig <= '9') { |
| 147 | dig -= '0'; |
| 148 | if (in == PARSE_DEC_IN_EXP) { |
| 149 | exp_val = 10 * exp_val + dig; |
| 150 | } else { |
| 151 | dec_val = 10 * dec_val + dig; |
| 152 | if (in == PARSE_DEC_IN_FRAC) { |
| 153 | exp_extra -= 1; |
| 154 | } |
| 155 | } |
| 156 | } else if (in == PARSE_DEC_IN_INTG && dig == '.') { |
| 157 | in = PARSE_DEC_IN_FRAC; |
| 158 | } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) { |
| 159 | in = PARSE_DEC_IN_EXP; |
| 160 | if (str[1] == '+') { |
| 161 | str++; |
| 162 | } else if (str[1] == '-') { |
| 163 | str++; |
| 164 | exp_neg = true; |
| 165 | } |
| 166 | } else if (allow_imag && (dig | 0x20) == 'j') { |
| 167 | str++; |
| 168 | imag = true; |
| 169 | break; |
| 170 | } else { |
| 171 | // unknown character |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // work out the exponent |
| 177 | if (exp_neg) { |
| 178 | exp_val = -exp_val; |
| 179 | } |
| 180 | exp_val += exp_extra; |
| 181 | |
| 182 | // apply the exponent |
| 183 | for (; exp_val > 0; exp_val--) { |
| 184 | dec_val *= 10; |
| 185 | } |
| 186 | for (; exp_val < 0; exp_val++) { |
| 187 | dec_val *= 0.1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // negate value if needed |
| 192 | if (dec_neg) { |
| 193 | dec_val = -dec_val; |
| 194 | } |
| 195 | |
| 196 | // skip trailing space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame^] | 197 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // check we reached the end of the string |
| 201 | if (str != top) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 202 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
| 203 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 204 | |
| 205 | // return the object |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 206 | if (imag) { |
| 207 | return mp_obj_new_complex(0, dec_val); |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 208 | } else if (force_complex) { |
| 209 | return mp_obj_new_complex(dec_val, 0); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 210 | } else { |
| 211 | return mp_obj_new_float(dec_val); |
| 212 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 213 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 214 | #else |
| 215 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported")); |
| 216 | #endif |
| 217 | } |