Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python 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 | |
xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/nlr.h" |
| 31 | #include "py/parsenum.h" |
| 32 | #include "py/smallint.h" |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 33 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 34 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 35 | #include <math.h> |
| 36 | #endif |
| 37 | |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 38 | mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base) { |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 39 | const byte *restrict str = (const byte *)str_; |
| 40 | const byte *restrict top = str + len; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 41 | bool neg = false; |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 42 | mp_obj_t ret_val; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 43 | |
| 44 | // check radix base |
| 45 | if ((base != 0 && base < 2) || base > 36) { |
Andrew Scheller | f78cfaf | 2014-04-09 19:56:38 +0100 | [diff] [blame] | 46 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "int() arg 2 must be >= 2 and <= 36")); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 47 | } |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 48 | |
| 49 | // skip leading space |
| 50 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 53 | // parse optional sign |
| 54 | if (str < top) { |
| 55 | if (*str == '+') { |
| 56 | str++; |
| 57 | } else if (*str == '-') { |
| 58 | str++; |
| 59 | neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 63 | // parse optional base prefix |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 64 | str += mp_parse_num_base((const char*)str, top - str, &base); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 65 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 66 | // string should be an integer number |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 67 | mp_int_t int_val = 0; |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 68 | const byte *restrict str_val_start = str; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 69 | for (; str < top; str++) { |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 70 | // get next digit as a value |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 71 | mp_uint_t dig = *str; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 72 | if (unichar_isdigit(dig) && dig - '0' < base) { |
| 73 | // 0-9 digit |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 74 | dig = dig - '0'; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 75 | } else if (base == 16) { |
| 76 | dig |= 0x20; |
| 77 | if ('a' <= dig && dig <= 'f') { |
| 78 | // a-f hex digit |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 79 | dig = dig - 'a' + 10; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 80 | } else { |
| 81 | // unknown character |
| 82 | break; |
| 83 | } |
| 84 | } else { |
| 85 | // unknown character |
| 86 | break; |
| 87 | } |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 88 | |
| 89 | // add next digi and check for overflow |
| 90 | if (mp_small_int_mul_overflow(int_val, base)) { |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 91 | goto overflow; |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 92 | } |
| 93 | int_val = int_val * base + dig; |
| 94 | if (!MP_SMALL_INT_FITS(int_val)) { |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 95 | goto overflow; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // negate value if needed |
| 100 | if (neg) { |
| 101 | int_val = -int_val; |
| 102 | } |
| 103 | |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 104 | // create the small int |
| 105 | ret_val = MP_OBJ_NEW_SMALL_INT(int_val); |
| 106 | |
| 107 | have_ret_val: |
| 108 | // check we parsed something |
| 109 | if (str == str_val_start) { |
| 110 | goto value_error; |
| 111 | } |
| 112 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 113 | // skip trailing space |
| 114 | for (; str < top && unichar_isspace(*str); str++) { |
| 115 | } |
| 116 | |
| 117 | // check we reached the end of the string |
| 118 | if (str != top) { |
Damien George | 7b4b78b | 2014-03-21 20:46:38 +0000 | [diff] [blame] | 119 | goto value_error; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // return the object |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 123 | return ret_val; |
Damien George | 7b4b78b | 2014-03-21 20:46:38 +0000 | [diff] [blame] | 124 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 125 | overflow: |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 126 | // reparse using long int |
| 127 | { |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 128 | const char *s2 = (const char*)str_val_start; |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 129 | ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base); |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 130 | str = (const byte*)s2; |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 131 | goto have_ret_val; |
| 132 | } |
| 133 | |
| 134 | value_error: |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 135 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 136 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, |
| 137 | "invalid syntax for integer")); |
| 138 | } else { |
| 139 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 140 | "invalid syntax for integer with base %d: '%s'", base, str)); |
| 141 | } |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 144 | typedef enum { |
| 145 | PARSE_DEC_IN_INTG, |
| 146 | PARSE_DEC_IN_FRAC, |
| 147 | PARSE_DEC_IN_EXP, |
| 148 | } parse_dec_in_t; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 149 | |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 150 | mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex) { |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 151 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 152 | const char *top = str + len; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 153 | mp_float_t dec_val = 0; |
| 154 | bool dec_neg = false; |
| 155 | bool imag = false; |
| 156 | |
| 157 | // skip leading space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 158 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 161 | // parse optional sign |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 162 | if (str < top) { |
| 163 | if (*str == '+') { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 164 | str++; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 165 | } else if (*str == '-') { |
| 166 | str++; |
| 167 | dec_neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 170 | |
| 171 | // determine what the string is |
| 172 | if (str < top && (str[0] | 0x20) == 'i') { |
| 173 | // string starts with 'i', should be 'inf' or 'infinity' (case insensitive) |
| 174 | if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { |
| 175 | // inf |
| 176 | str += 3; |
| 177 | dec_val = INFINITY; |
| 178 | if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') { |
| 179 | // infinity |
| 180 | str += 5; |
| 181 | } |
| 182 | } |
| 183 | } else if (str < top && (str[0] | 0x20) == 'n') { |
| 184 | // string starts with 'n', should be 'nan' (case insensitive) |
| 185 | if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { |
| 186 | // NaN |
| 187 | str += 3; |
| 188 | dec_val = MICROPY_FLOAT_C_FUN(nan)(""); |
| 189 | } |
| 190 | } else { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 191 | // string should be a decimal number |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 192 | parse_dec_in_t in = PARSE_DEC_IN_INTG; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 193 | bool exp_neg = false; |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 194 | mp_int_t exp_val = 0; |
| 195 | mp_int_t exp_extra = 0; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 196 | for (; str < top; str++) { |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 197 | mp_uint_t dig = *str; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 198 | if ('0' <= dig && dig <= '9') { |
| 199 | dig -= '0'; |
| 200 | if (in == PARSE_DEC_IN_EXP) { |
| 201 | exp_val = 10 * exp_val + dig; |
| 202 | } else { |
| 203 | dec_val = 10 * dec_val + dig; |
| 204 | if (in == PARSE_DEC_IN_FRAC) { |
| 205 | exp_extra -= 1; |
| 206 | } |
| 207 | } |
| 208 | } else if (in == PARSE_DEC_IN_INTG && dig == '.') { |
| 209 | in = PARSE_DEC_IN_FRAC; |
| 210 | } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) { |
| 211 | in = PARSE_DEC_IN_EXP; |
| 212 | if (str[1] == '+') { |
| 213 | str++; |
| 214 | } else if (str[1] == '-') { |
| 215 | str++; |
| 216 | exp_neg = true; |
| 217 | } |
| 218 | } else if (allow_imag && (dig | 0x20) == 'j') { |
| 219 | str++; |
| 220 | imag = true; |
| 221 | break; |
| 222 | } else { |
| 223 | // unknown character |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // work out the exponent |
| 229 | if (exp_neg) { |
| 230 | exp_val = -exp_val; |
| 231 | } |
| 232 | exp_val += exp_extra; |
| 233 | |
| 234 | // apply the exponent |
| 235 | for (; exp_val > 0; exp_val--) { |
| 236 | dec_val *= 10; |
| 237 | } |
| 238 | for (; exp_val < 0; exp_val++) { |
| 239 | dec_val *= 0.1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // negate value if needed |
| 244 | if (dec_neg) { |
| 245 | dec_val = -dec_val; |
| 246 | } |
| 247 | |
| 248 | // skip trailing space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 249 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // check we reached the end of the string |
| 253 | if (str != top) { |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 254 | nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 255 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 256 | |
| 257 | // return the object |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 258 | #if MICROPY_PY_BUILTINS_COMPLEX |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 259 | if (imag) { |
| 260 | return mp_obj_new_complex(0, dec_val); |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 261 | } else if (force_complex) { |
| 262 | return mp_obj_new_complex(dec_val, 0); |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 263 | #else |
| 264 | if (imag || force_complex) { |
| 265 | mp_not_implemented("complex values not supported"); |
| 266 | #endif |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 267 | } else { |
| 268 | return mp_obj_new_float(dec_val); |
| 269 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 270 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 271 | #else |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 272 | nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported")); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 273 | #endif |
| 274 | } |