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 | #if defined(UNIX) |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | |
| 21 | mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { |
| 22 | // TODO at the moment we ignore len; we should honour it! |
| 23 | // TODO detect integer overflow and return bignum |
| 24 | |
| 25 | int c, neg = 0; |
| 26 | const char *p = str; |
| 27 | char *num; |
| 28 | long found; |
| 29 | |
| 30 | // check radix base |
| 31 | if ((base != 0 && base < 2) || base > 36) { |
| 32 | nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36")); |
| 33 | } |
| 34 | // skip surrounded whitespace |
| 35 | while (isspace((c = *(p++)))); |
| 36 | if (c == 0) { |
| 37 | goto value_error; |
| 38 | } |
| 39 | // preced sign |
| 40 | if (c == '+' || c == '-') { |
| 41 | neg = - (c == '-'); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 42 | } else { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 43 | p--; |
| 44 | } |
| 45 | |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 46 | len -= p - str; |
| 47 | int skip = mp_parse_num_base(p, len, &base); |
| 48 | p += skip; |
| 49 | len -= skip; |
| 50 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 51 | errno = 0; |
| 52 | found = strtol(p, &num, base); |
| 53 | if (errno) { |
| 54 | goto value_error; |
| 55 | } else if (found && *(num) == 0) { |
| 56 | goto done; |
| 57 | } else if (found || num != p) { |
| 58 | goto check_tail_space; |
| 59 | } else { |
| 60 | goto value_error; |
| 61 | } |
| 62 | |
| 63 | check_tail_space: |
| 64 | if (*(num) != 0) { |
| 65 | while (isspace((c = *(num++)))); |
| 66 | if (c != 0) { |
| 67 | goto value_error; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | done: |
| 72 | return MP_OBJ_NEW_SMALL_INT((found ^ neg) - neg); |
| 73 | |
| 74 | value_error: |
| 75 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str)); |
| 76 | } |
| 77 | |
| 78 | #else /* defined(UNIX) */ |
| 79 | |
| 80 | mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { |
| 81 | // TODO port strtol to stm |
| 82 | return MP_OBJ_NEW_SMALL_INT(0); |
| 83 | } |
| 84 | |
| 85 | #endif /* defined(UNIX) */ |
| 86 | |
| 87 | #define PARSE_DEC_IN_INTG (1) |
| 88 | #define PARSE_DEC_IN_FRAC (2) |
| 89 | #define PARSE_DEC_IN_EXP (3) |
| 90 | |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame^] | 91 | 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] | 92 | #if MICROPY_ENABLE_FLOAT |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 93 | const char *top = str + len; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 94 | mp_float_t dec_val = 0; |
| 95 | bool dec_neg = false; |
| 96 | bool imag = false; |
| 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 == '+') { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 105 | str++; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 106 | } else if (*str == '-') { |
| 107 | str++; |
| 108 | dec_neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 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; |
| 122 | } |
| 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 { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame^] | 132 | // string should be a decimal number |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 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') { |
| 160 | str++; |
| 161 | imag = true; |
| 162 | break; |
| 163 | } else { |
| 164 | // unknown character |
| 165 | break; |
| 166 | } |
| 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; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // negate value if needed |
| 185 | if (dec_neg) { |
| 186 | dec_val = -dec_val; |
| 187 | } |
| 188 | |
| 189 | // skip trailing space |
| 190 | for (; str < top && isspace(*str); str++) { |
| 191 | } |
| 192 | |
| 193 | // check we reached the end of the string |
| 194 | if (str != top) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 195 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
| 196 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 197 | |
| 198 | // return the object |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 199 | if (imag) { |
| 200 | return mp_obj_new_complex(0, dec_val); |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame^] | 201 | } else if (force_complex) { |
| 202 | return mp_obj_new_complex(dec_val, 0); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 203 | } else { |
| 204 | return mp_obj_new_float(dec_val); |
| 205 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 206 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 207 | #else |
| 208 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported")); |
| 209 | #endif |
| 210 | } |