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