parsenum.c 10.3 KB
Newer Older
1
/*
2
 * This file is part of the MicroPython project, http://micropython.org/
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

xbe's avatar
xbe committed
27
#include <stdbool.h>
28 29
#include <stdlib.h>

30
#include "py/runtime.h"
31
#include "py/parsenumbase.h"
32 33
#include "py/parsenum.h"
#include "py/smallint.h"
34

35
#if MICROPY_PY_BUILTINS_FLOAT
36 37 38
#include <math.h>
#endif

39
STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
40 41
    // if lex!=NULL then the parser called us and we need to convert the
    // exception's type from ValueError to SyntaxError and add traceback info
42
    if (lex != NULL) {
43
        ((mp_obj_base_t*)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError;
44 45 46 47 48
        mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
    }
    nlr_raise(exc);
}

49
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) {
Paul Sokolovsky's avatar
Paul Sokolovsky committed
50 51
    const byte *restrict str = (const byte *)str_;
    const byte *restrict top = str + len;
52
    bool neg = false;
53
    mp_obj_t ret_val;
54 55 56

    // check radix base
    if ((base != 0 && base < 2) || base > 36) {
57
        // this won't be reached if lex!=NULL
58
        mp_raise_ValueError("int() arg 2 must be >= 2 and <= 36");
59
    }
60 61 62

    // skip leading space
    for (; str < top && unichar_isspace(*str); str++) {
63 64
    }

65 66 67 68 69 70 71 72
    // parse optional sign
    if (str < top) {
        if (*str == '+') {
            str++;
        } else if (*str == '-') {
            str++;
            neg = true;
        }
73 74
    }

75
    // parse optional base prefix
Paul Sokolovsky's avatar
Paul Sokolovsky committed
76
    str += mp_parse_num_base((const char*)str, top - str, &base);
77 78

    // string should be an integer number
79
    mp_int_t int_val = 0;
Paul Sokolovsky's avatar
Paul Sokolovsky committed
80
    const byte *restrict str_val_start = str;
81
    for (; str < top; str++) {
82
        // get next digit as a value
83
        mp_uint_t dig = *str;
84 85 86 87 88 89
        if ('0' <= dig && dig <= '9') {
            dig -= '0';
        } else {
            dig |= 0x20; // make digit lower-case
            if ('a' <= dig && dig <= 'z') {
                dig -= 'a' - 10;
90 91 92 93
            } else {
                // unknown character
                break;
            }
94
        }
95
        if (dig >= (mp_uint_t)base) {
96 97
            break;
        }
98 99 100

        // add next digi and check for overflow
        if (mp_small_int_mul_overflow(int_val, base)) {
101
            goto overflow;
102 103 104
        }
        int_val = int_val * base + dig;
        if (!MP_SMALL_INT_FITS(int_val)) {
105
            goto overflow;
106 107 108
        }
    }

109 110 111 112
    // negate value if needed
    if (neg) {
        int_val = -int_val;
    }
113

114 115 116 117 118 119 120 121 122
    // create the small int
    ret_val = MP_OBJ_NEW_SMALL_INT(int_val);

have_ret_val:
    // check we parsed something
    if (str == str_val_start) {
        goto value_error;
    }

123 124 125
    // skip trailing space
    for (; str < top && unichar_isspace(*str); str++) {
    }
126

127 128
    // check we reached the end of the string
    if (str != top) {
129
        goto value_error;
130
    }
131

132
    // return the object
133
    return ret_val;
134

135
overflow:
136 137
    // reparse using long int
    {
Paul Sokolovsky's avatar
Paul Sokolovsky committed
138
        const char *s2 = (const char*)str_val_start;
139
        ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
Paul Sokolovsky's avatar
Paul Sokolovsky committed
140
        str = (const byte*)s2;
141 142 143
        goto have_ret_val;
    }

144 145
value_error:;
    #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
146
        mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
147
            "invalid syntax for integer");
148
        raise_exc(exc, lex);
149
    #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
150
        mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
151 152
            "invalid syntax for integer with base %d", base);
        raise_exc(exc, lex);
153
    #else
154 155 156 157 158 159 160
        vstr_t vstr;
        mp_print_t print;
        vstr_init_print(&vstr, 50, &print);
        mp_printf(&print, "invalid syntax for integer with base %d: ", base);
        mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
        mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
            mp_obj_new_str_from_vstr(&mp_type_str, &vstr));
161
        raise_exc(exc, lex);
162
    #endif
163
}
164

165 166 167 168 169
typedef enum {
    PARSE_DEC_IN_INTG,
    PARSE_DEC_IN_FRAC,
    PARSE_DEC_IN_EXP,
} parse_dec_in_t;
170

171
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
172
#if MICROPY_PY_BUILTINS_FLOAT
173 174

// DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing
175
// SMALL_NORMAL_VAL is the smallest power of 10 that is still a normal float
176 177
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#define DEC_VAL_MAX 1e20F
178 179
#define SMALL_NORMAL_VAL (1e-37F)
#define SMALL_NORMAL_EXP (-37)
180 181
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
#define DEC_VAL_MAX 1e200
182 183
#define SMALL_NORMAL_VAL (1e-307)
#define SMALL_NORMAL_EXP (-307)
184 185
#endif

186
    const char *top = str + len;
187
    mp_float_t dec_val = 0;
188
    bool dec_neg = false;
189
    bool imag = false;
190 191

    // skip leading space
192
    for (; str < top && unichar_isspace(*str); str++) {
193 194
    }

195
    // parse optional sign
196 197 198 199 200 201 202 203 204
    if (str < top) {
        if (*str == '+') {
            str++;
        } else if (*str == '-') {
            str++;
            dec_neg = true;
        }
    }

205 206
    const char *str_val_start = str;

207 208 209 210 211 212 213 214 215 216
    // determine what the string is
    if (str < top && (str[0] | 0x20) == 'i') {
        // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
        if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
            // inf
            str += 3;
            dec_val = INFINITY;
            if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
                // infinity
                str += 5;
217
            }
218 219 220 221 222 223 224 225 226
        }
    } else if (str < top && (str[0] | 0x20) == 'n') {
        // string starts with 'n', should be 'nan' (case insensitive)
        if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
            // NaN
            str += 3;
            dec_val = MICROPY_FLOAT_C_FUN(nan)("");
        }
    } else {
227
        // string should be a decimal number
228
        parse_dec_in_t in = PARSE_DEC_IN_INTG;
229
        bool exp_neg = false;
230
        mp_int_t exp_val = 0;
231
        mp_int_t exp_extra = 0;
232 233
        while (str < top) {
            mp_uint_t dig = *str++;
234 235 236 237 238
            if ('0' <= dig && dig <= '9') {
                dig -= '0';
                if (in == PARSE_DEC_IN_EXP) {
                    exp_val = 10 * exp_val + dig;
                } else {
239 240
                    if (dec_val < DEC_VAL_MAX) {
                        // dec_val won't overflow so keep accumulating
241
                        dec_val = 10 * dec_val + dig;
242 243 244 245 246 247 248 249 250
                        if (in == PARSE_DEC_IN_FRAC) {
                            --exp_extra;
                        }
                    } else {
                        // dec_val might overflow and we anyway can't represent more digits
                        // of precision, so ignore the digit and just adjust the exponent
                        if (in == PARSE_DEC_IN_INTG) {
                            ++exp_extra;
                        }
251 252 253 254 255 256
                    }
                }
            } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
                in = PARSE_DEC_IN_FRAC;
            } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
                in = PARSE_DEC_IN_EXP;
257 258 259 260 261 262 263 264 265 266
                if (str < top) {
                    if (str[0] == '+') {
                        str++;
                    } else if (str[0] == '-') {
                        str++;
                        exp_neg = true;
                    }
                }
                if (str == top) {
                    goto value_error;
267 268 269 270 271 272
                }
            } else if (allow_imag && (dig | 0x20) == 'j') {
                imag = true;
                break;
            } else {
                // unknown character
273
                str--;
274
                break;
275
            }
276 277 278 279 280 281 282
        }

        // work out the exponent
        if (exp_neg) {
            exp_val = -exp_val;
        }

283 284 285 286 287 288 289
        // apply the exponent, making sure it's not a subnormal value
        exp_val += exp_extra;
        if (exp_val < SMALL_NORMAL_EXP) {
            exp_val -= SMALL_NORMAL_EXP;
            dec_val *= SMALL_NORMAL_VAL;
        }
        dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
290
    }
291 292 293 294

    // negate value if needed
    if (dec_neg) {
        dec_val = -dec_val;
295
    }
296

297 298 299 300 301
    // check we parsed something
    if (str == str_val_start) {
        goto value_error;
    }

302
    // skip trailing space
303
    for (; str < top && unichar_isspace(*str); str++) {
304
    }
305 306 307

    // check we reached the end of the string
    if (str != top) {
308
        goto value_error;
309
    }
310 311

    // return the object
312
#if MICROPY_PY_BUILTINS_COMPLEX
313 314
    if (imag) {
        return mp_obj_new_complex(0, dec_val);
315 316
    } else if (force_complex) {
        return mp_obj_new_complex(dec_val, 0);
317 318
#else
    if (imag || force_complex) {
319
        raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
320
#endif
321 322 323
    } else {
        return mp_obj_new_float(dec_val);
    }
324

325
value_error:
326
    raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
327

328
#else
329
    raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
330 331
#endif
}