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