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