Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | 7d0d721 | 2016-10-17 12:17:37 +1100 | [diff] [blame] | 30 | #include "py/runtime.h" |
Damien George | dddb98d | 2016-03-14 22:34:03 +0000 | [diff] [blame] | 31 | #include "py/parsenumbase.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/parsenum.h" |
| 33 | #include "py/smallint.h" |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 34 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 35 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 36 | #include <math.h> |
| 37 | #endif |
| 38 | |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 39 | STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) { |
Damien George | 2a1090a | 2015-06-23 16:08:51 +0000 | [diff] [blame] | 40 | // if lex!=NULL then the parser called us and we need to convert the |
| 41 | // exception's type from ValueError to SyntaxError and add traceback info |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 42 | if (lex != NULL) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 43 | ((mp_obj_base_t*)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError; |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 44 | mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL); |
| 45 | } |
| 46 | nlr_raise(exc); |
| 47 | } |
| 48 | |
Damien George | dddb98d | 2016-03-14 22:34:03 +0000 | [diff] [blame] | 49 | mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) { |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 50 | const byte *restrict str = (const byte *)str_; |
| 51 | const byte *restrict top = str + len; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 52 | bool neg = false; |
Damien George | 503d611 | 2014-05-28 14:07:21 +0100 | [diff] [blame] | 53 | mp_obj_t ret_val; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 54 | |
| 55 | // check radix base |
| 56 | if ((base != 0 && base < 2) || base > 36) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 57 | // this won't be reached if lex!=NULL |
Damien George | 94c41bb | 2017-03-28 22:37:26 +1100 | [diff] [blame] | 58 | mp_raise_ValueError("int() arg 2 must be >= 2 and <= 36"); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 59 | } |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 60 | |
| 61 | // skip leading space |
| 62 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 65 | // parse optional sign |
| 66 | if (str < top) { |
| 67 | if (*str == '+') { |
| 68 | str++; |
| 69 | } else if (*str == '-') { |
| 70 | str++; |
| 71 | neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 75 | // parse optional base prefix |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 76 | str += mp_parse_num_base((const char*)str, top - str, &base); |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 77 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 78 | // string should be an integer number |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 79 | mp_int_t int_val = 0; |
Paul Sokolovsky | 0294661 | 2014-06-14 17:56:44 +0300 | [diff] [blame] | 80 | const byte *restrict str_val_start = str; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 81 | for (; str < top; str++) { |
Damien George | d1e355e | 2014-05-28 14:51:12 +0100 | [diff] [blame] | 82 | // get next digit as a value |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 83 | mp_uint_t dig = *str; |
Damien George | c2dd494 | 2016-12-28 12:02:49 +1100 | [diff] [blame] | 84 | if ('0' <= dig && dig <= '9') { |
| 85 | dig -= '0'; |
| 86 | } else { |
| 87 | dig |= 0x20; // make digit lower-case |
| 88 | if ('a' <= dig && dig <= 'z') { |
| 89 | dig -= 'a' - 10; |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 90 | } else { |
| 91 | // unknown character |
| 92 | break; |
| 93 | } |
Damien George | c2dd494 | 2016-12-28 12:02:49 +1100 | [diff] [blame] | 94 | } |
Damien George | ca7af9a | 2016-12-28 12:25:00 +1100 | [diff] [blame] | 95 | if (dig >= (mp_uint_t)base) { |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 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: |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 145 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Daniel Campora | 228c68a | 2015-06-23 15:30:49 +0200 | [diff] [blame] | 146 | mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError, |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 147 | "invalid syntax for integer"); |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 148 | raise_exc(exc, lex); |
Damien George | 5f3c3ec | 2015-10-01 17:06:13 +0100 | [diff] [blame] | 149 | } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 150 | mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
Damien George | 5f3c3ec | 2015-10-01 17:06:13 +0100 | [diff] [blame] | 151 | "invalid syntax for integer with base %d", base); |
| 152 | raise_exc(exc, lex); |
| 153 | } else { |
| 154 | vstr_t vstr; |
| 155 | mp_print_t print; |
| 156 | vstr_init_print(&vstr, 50, &print); |
| 157 | mp_printf(&print, "invalid syntax for integer with base %d: ", base); |
| 158 | mp_str_print_quoted(&print, str_val_start, top - str_val_start, true); |
| 159 | mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError, |
| 160 | mp_obj_new_str_from_vstr(&mp_type_str, &vstr)); |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 161 | raise_exc(exc, lex); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 162 | } |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 165 | typedef enum { |
| 166 | PARSE_DEC_IN_INTG, |
| 167 | PARSE_DEC_IN_FRAC, |
| 168 | PARSE_DEC_IN_EXP, |
| 169 | } parse_dec_in_t; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 170 | |
Damien George | dddb98d | 2016-03-14 22:34:03 +0000 | [diff] [blame] | 171 | mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) { |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 172 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 84895f1 | 2017-11-27 12:51:52 +1100 | [diff] [blame] | 173 | |
| 174 | // DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing |
| 175 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT |
| 176 | #define DEC_VAL_MAX 1e20F |
| 177 | #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE |
| 178 | #define DEC_VAL_MAX 1e200 |
| 179 | #endif |
| 180 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 181 | const char *top = str + len; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 182 | mp_float_t dec_val = 0; |
| 183 | bool dec_neg = false; |
| 184 | bool imag = false; |
| 185 | |
| 186 | // skip leading space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 187 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 190 | // parse optional sign |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 191 | if (str < top) { |
| 192 | if (*str == '+') { |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 193 | str++; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 194 | } else if (*str == '-') { |
| 195 | str++; |
| 196 | dec_neg = true; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 199 | |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 200 | const char *str_val_start = str; |
| 201 | |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 202 | // determine what the string is |
| 203 | if (str < top && (str[0] | 0x20) == 'i') { |
| 204 | // string starts with 'i', should be 'inf' or 'infinity' (case insensitive) |
| 205 | if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { |
| 206 | // inf |
| 207 | str += 3; |
| 208 | dec_val = INFINITY; |
| 209 | if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') { |
| 210 | // infinity |
| 211 | str += 5; |
| 212 | } |
| 213 | } |
| 214 | } else if (str < top && (str[0] | 0x20) == 'n') { |
| 215 | // string starts with 'n', should be 'nan' (case insensitive) |
| 216 | if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { |
| 217 | // NaN |
| 218 | str += 3; |
| 219 | dec_val = MICROPY_FLOAT_C_FUN(nan)(""); |
| 220 | } |
| 221 | } else { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 222 | // string should be a decimal number |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 223 | parse_dec_in_t in = PARSE_DEC_IN_INTG; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 224 | bool exp_neg = false; |
Damien George | 3816182 | 2014-07-03 14:13:33 +0100 | [diff] [blame] | 225 | mp_int_t exp_val = 0; |
Damien George | 84895f1 | 2017-11-27 12:51:52 +1100 | [diff] [blame] | 226 | mp_int_t exp_extra = 0; |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 227 | while (str < top) { |
| 228 | mp_uint_t dig = *str++; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 229 | if ('0' <= dig && dig <= '9') { |
| 230 | dig -= '0'; |
| 231 | if (in == PARSE_DEC_IN_EXP) { |
| 232 | exp_val = 10 * exp_val + dig; |
| 233 | } else { |
Damien George | 84895f1 | 2017-11-27 12:51:52 +1100 | [diff] [blame] | 234 | if (dec_val < DEC_VAL_MAX) { |
| 235 | // dec_val won't overflow so keep accumulating |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 236 | dec_val = 10 * dec_val + dig; |
Damien George | 84895f1 | 2017-11-27 12:51:52 +1100 | [diff] [blame] | 237 | if (in == PARSE_DEC_IN_FRAC) { |
| 238 | --exp_extra; |
| 239 | } |
| 240 | } else { |
| 241 | // dec_val might overflow and we anyway can't represent more digits |
| 242 | // of precision, so ignore the digit and just adjust the exponent |
| 243 | if (in == PARSE_DEC_IN_INTG) { |
| 244 | ++exp_extra; |
| 245 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } else if (in == PARSE_DEC_IN_INTG && dig == '.') { |
| 249 | in = PARSE_DEC_IN_FRAC; |
| 250 | } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) { |
| 251 | in = PARSE_DEC_IN_EXP; |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 252 | if (str < top) { |
| 253 | if (str[0] == '+') { |
| 254 | str++; |
| 255 | } else if (str[0] == '-') { |
| 256 | str++; |
| 257 | exp_neg = true; |
| 258 | } |
| 259 | } |
| 260 | if (str == top) { |
| 261 | goto value_error; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 262 | } |
| 263 | } else if (allow_imag && (dig | 0x20) == 'j') { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 264 | imag = true; |
| 265 | break; |
| 266 | } else { |
| 267 | // unknown character |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 268 | str--; |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // work out the exponent |
| 274 | if (exp_neg) { |
| 275 | exp_val = -exp_val; |
| 276 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 277 | |
| 278 | // apply the exponent |
Damien George | 84895f1 | 2017-11-27 12:51:52 +1100 | [diff] [blame] | 279 | dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val + exp_extra); |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // negate value if needed |
| 283 | if (dec_neg) { |
| 284 | dec_val = -dec_val; |
| 285 | } |
| 286 | |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 287 | // check we parsed something |
| 288 | if (str == str_val_start) { |
| 289 | goto value_error; |
| 290 | } |
| 291 | |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 292 | // skip trailing space |
Damien George | dfbafab | 2014-03-21 12:15:59 +0000 | [diff] [blame] | 293 | for (; str < top && unichar_isspace(*str); str++) { |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // check we reached the end of the string |
| 297 | if (str != top) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 298 | goto value_error; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 299 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 300 | |
| 301 | // return the object |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 302 | #if MICROPY_PY_BUILTINS_COMPLEX |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 303 | if (imag) { |
| 304 | return mp_obj_new_complex(0, dec_val); |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 305 | } else if (force_complex) { |
| 306 | return mp_obj_new_complex(dec_val, 0); |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 307 | #else |
| 308 | if (imag || force_complex) { |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 309 | 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] | 310 | #endif |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 311 | } else { |
| 312 | return mp_obj_new_float(dec_val); |
| 313 | } |
Damien George | c06ea7a | 2014-03-21 10:55:08 +0000 | [diff] [blame] | 314 | |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 315 | value_error: |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 316 | 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] | 317 | |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 318 | #else |
Damien George | 0ec8cf8 | 2015-05-30 23:13:16 +0100 | [diff] [blame] | 319 | 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] | 320 | #endif |
| 321 | } |