blob: 1c1868ae0a4984c0a0e46500db974a1897e2cc2b [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien George20773972014-02-22 18:12:43 +000028#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 George06201ff2014-03-01 19:50:50 +000035#include "parsenumbase.h"
Damien George20773972014-02-22 18:12:43 +000036#include "parsenum.h"
Damien Georged1e355e2014-05-28 14:51:12 +010037#include "smallint.h"
Damien George20773972014-02-22 18:12:43 +000038
Damien Georgefb510b32014-06-01 13:32:54 +010039#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgec06ea7a2014-03-21 10:55:08 +000040#include <math.h>
41#endif
42
Paul Sokolovsky02946612014-06-14 17:56:44 +030043mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
44 const byte *restrict str = (const byte *)str_;
45 const byte *restrict top = str + len;
Damien Georgedfbafab2014-03-21 12:15:59 +000046 bool neg = false;
Damien George503d6112014-05-28 14:07:21 +010047 mp_obj_t ret_val;
Damien George20773972014-02-22 18:12:43 +000048
49 // check radix base
50 if ((base != 0 && base < 2) || base > 36) {
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010051 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "int() arg 2 must be >= 2 and <= 36"));
Damien George20773972014-02-22 18:12:43 +000052 }
Damien Georgedfbafab2014-03-21 12:15:59 +000053
54 // skip leading space
55 for (; str < top && unichar_isspace(*str); str++) {
Damien George20773972014-02-22 18:12:43 +000056 }
57
Damien Georgedfbafab2014-03-21 12:15:59 +000058 // parse optional sign
59 if (str < top) {
60 if (*str == '+') {
61 str++;
62 } else if (*str == '-') {
63 str++;
64 neg = true;
Damien George20773972014-02-22 18:12:43 +000065 }
66 }
67
Damien Georgedfbafab2014-03-21 12:15:59 +000068 // parse optional base prefix
Paul Sokolovsky02946612014-06-14 17:56:44 +030069 str += mp_parse_num_base((const char*)str, top - str, &base);
Damien George20773972014-02-22 18:12:43 +000070
Damien Georgedfbafab2014-03-21 12:15:59 +000071 // string should be an integer number
72 machine_int_t int_val = 0;
Paul Sokolovsky02946612014-06-14 17:56:44 +030073 const byte *restrict str_val_start = str;
Damien Georgedfbafab2014-03-21 12:15:59 +000074 for (; str < top; str++) {
Damien Georged1e355e2014-05-28 14:51:12 +010075 // get next digit as a value
Damien Georgedfbafab2014-03-21 12:15:59 +000076 int dig = *str;
77 if (unichar_isdigit(dig) && dig - '0' < base) {
78 // 0-9 digit
Damien Georged1e355e2014-05-28 14:51:12 +010079 dig = dig - '0';
Damien Georgedfbafab2014-03-21 12:15:59 +000080 } else if (base == 16) {
81 dig |= 0x20;
82 if ('a' <= dig && dig <= 'f') {
83 // a-f hex digit
Damien Georged1e355e2014-05-28 14:51:12 +010084 dig = dig - 'a' + 10;
Damien Georgedfbafab2014-03-21 12:15:59 +000085 } else {
86 // unknown character
87 break;
88 }
89 } else {
90 // unknown character
91 break;
92 }
Damien Georged1e355e2014-05-28 14:51:12 +010093
94 // add next digi and check for overflow
95 if (mp_small_int_mul_overflow(int_val, base)) {
Damien Georgedfbafab2014-03-21 12:15:59 +000096 goto overflow;
Damien Georged1e355e2014-05-28 14:51:12 +010097 }
98 int_val = int_val * base + dig;
99 if (!MP_SMALL_INT_FITS(int_val)) {
Damien Georgedfbafab2014-03-21 12:15:59 +0000100 goto overflow;
101 }
102 }
103
104 // negate value if needed
105 if (neg) {
106 int_val = -int_val;
107 }
108
Damien George503d6112014-05-28 14:07:21 +0100109 // create the small int
110 ret_val = MP_OBJ_NEW_SMALL_INT(int_val);
111
112have_ret_val:
113 // check we parsed something
114 if (str == str_val_start) {
115 goto value_error;
116 }
117
Damien Georgedfbafab2014-03-21 12:15:59 +0000118 // skip trailing space
119 for (; str < top && unichar_isspace(*str); str++) {
120 }
121
122 // check we reached the end of the string
123 if (str != top) {
Damien George7b4b78b2014-03-21 20:46:38 +0000124 goto value_error;
Damien Georgedfbafab2014-03-21 12:15:59 +0000125 }
126
127 // return the object
Damien George503d6112014-05-28 14:07:21 +0100128 return ret_val;
Damien George7b4b78b2014-03-21 20:46:38 +0000129
Damien Georgedfbafab2014-03-21 12:15:59 +0000130overflow:
Damien George503d6112014-05-28 14:07:21 +0100131 // reparse using long int
132 {
Paul Sokolovsky02946612014-06-14 17:56:44 +0300133 const char *s2 = (const char*)str_val_start;
Damien George503d6112014-05-28 14:07:21 +0100134 ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
Paul Sokolovsky02946612014-06-14 17:56:44 +0300135 str = (const byte*)s2;
Damien George503d6112014-05-28 14:07:21 +0100136 goto have_ret_val;
137 }
138
139value_error:
140 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid syntax for integer with base %d: '%s'", base, str));
Damien George20773972014-02-22 18:12:43 +0000141}
142
Damien George20773972014-02-22 18:12:43 +0000143#define PARSE_DEC_IN_INTG (1)
144#define PARSE_DEC_IN_FRAC (2)
145#define PARSE_DEC_IN_EXP (3)
146
Damien George6e48f7f2014-03-21 11:45:46 +0000147mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
Damien Georgefb510b32014-06-01 13:32:54 +0100148#if MICROPY_PY_BUILTINS_FLOAT
Damien George20773972014-02-22 18:12:43 +0000149 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000150 mp_float_t dec_val = 0;
151 bool dec_neg = false;
152 bool imag = false;
153
154 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000155 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000156 }
157
Damien Georgedfbafab2014-03-21 12:15:59 +0000158 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000159 if (str < top) {
160 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000161 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000162 } else if (*str == '-') {
163 str++;
164 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000165 }
166 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000167
168 // determine what the string is
169 if (str < top && (str[0] | 0x20) == 'i') {
170 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
171 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
172 // inf
173 str += 3;
174 dec_val = INFINITY;
175 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
176 // infinity
177 str += 5;
178 }
179 }
180 } else if (str < top && (str[0] | 0x20) == 'n') {
181 // string starts with 'n', should be 'nan' (case insensitive)
182 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
183 // NaN
184 str += 3;
185 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
186 }
187 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000188 // string should be a decimal number
Damien Georgec06ea7a2014-03-21 10:55:08 +0000189 int in = PARSE_DEC_IN_INTG;
190 bool exp_neg = false;
191 int exp_val = 0;
192 int exp_extra = 0;
193 for (; str < top; str++) {
194 int dig = *str;
195 if ('0' <= dig && dig <= '9') {
196 dig -= '0';
197 if (in == PARSE_DEC_IN_EXP) {
198 exp_val = 10 * exp_val + dig;
199 } else {
200 dec_val = 10 * dec_val + dig;
201 if (in == PARSE_DEC_IN_FRAC) {
202 exp_extra -= 1;
203 }
204 }
205 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
206 in = PARSE_DEC_IN_FRAC;
207 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
208 in = PARSE_DEC_IN_EXP;
209 if (str[1] == '+') {
210 str++;
211 } else if (str[1] == '-') {
212 str++;
213 exp_neg = true;
214 }
215 } else if (allow_imag && (dig | 0x20) == 'j') {
216 str++;
217 imag = true;
218 break;
219 } else {
220 // unknown character
221 break;
222 }
223 }
224
225 // work out the exponent
226 if (exp_neg) {
227 exp_val = -exp_val;
228 }
229 exp_val += exp_extra;
230
231 // apply the exponent
232 for (; exp_val > 0; exp_val--) {
233 dec_val *= 10;
234 }
235 for (; exp_val < 0; exp_val++) {
236 dec_val *= 0.1;
237 }
238 }
239
240 // negate value if needed
241 if (dec_neg) {
242 dec_val = -dec_val;
243 }
244
245 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000246 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000247 }
248
249 // check we reached the end of the string
250 if (str != top) {
Damien Georgeea13f402014-04-05 18:32:08 +0100251 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Damien George20773972014-02-22 18:12:43 +0000252 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000253
254 // return the object
Damien George20773972014-02-22 18:12:43 +0000255 if (imag) {
256 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000257 } else if (force_complex) {
258 return mp_obj_new_complex(dec_val, 0);
Damien George20773972014-02-22 18:12:43 +0000259 } else {
260 return mp_obj_new_float(dec_val);
261 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000262
Damien George20773972014-02-22 18:12:43 +0000263#else
Damien Georgeea13f402014-04-05 18:32:08 +0100264 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
Damien George20773972014-02-22 18:12:43 +0000265#endif
266}