blob: d3cb821a13f080c172c0fb3135c5cd5d723c5039 [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"
37
Damien Georgec06ea7a2014-03-21 10:55:08 +000038#if MICROPY_ENABLE_FLOAT
39#include <math.h>
40#endif
41
Damien George20773972014-02-22 18:12:43 +000042mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
Damien Georgedfbafab2014-03-21 12:15:59 +000043 const char *restrict top = str + len;
44 bool neg = false;
Damien George20773972014-02-22 18:12:43 +000045
46 // check radix base
47 if ((base != 0 && base < 2) || base > 36) {
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010048 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 +000049 }
Damien Georgedfbafab2014-03-21 12:15:59 +000050
51 // skip leading space
52 for (; str < top && unichar_isspace(*str); str++) {
Damien George20773972014-02-22 18:12:43 +000053 }
54
Damien Georgedfbafab2014-03-21 12:15:59 +000055 // parse optional sign
56 if (str < top) {
57 if (*str == '+') {
58 str++;
59 } else if (*str == '-') {
60 str++;
61 neg = true;
Damien George20773972014-02-22 18:12:43 +000062 }
63 }
64
Damien Georgedfbafab2014-03-21 12:15:59 +000065 // parse optional base prefix
66 str += mp_parse_num_base(str, top - str, &base);
Damien George20773972014-02-22 18:12:43 +000067
Damien Georgedfbafab2014-03-21 12:15:59 +000068 // string should be an integer number
69 machine_int_t int_val = 0;
Damien George7b4b78b2014-03-21 20:46:38 +000070 const char *restrict str_val_start = str;
Damien Georgedfbafab2014-03-21 12:15:59 +000071 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 George7b4b78b2014-03-21 20:46:38 +000099 // check we parsed something
100 if (str == str_val_start) {
101 goto value_error;
102 }
103
Damien Georgedfbafab2014-03-21 12:15:59 +0000104 // 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 George7b4b78b2014-03-21 20:46:38 +0000115 goto value_error;
Damien Georgedfbafab2014-03-21 12:15:59 +0000116 }
117
118 // return the object
119 return MP_OBJ_NEW_SMALL_INT(int_val);
120
Damien George7b4b78b2014-03-21 20:46:38 +0000121value_error:
Damien Georgeea13f402014-04-05 18:32:08 +0100122 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str));
Damien George7b4b78b2014-03-21 20:46:38 +0000123
Damien Georgedfbafab2014-03-21 12:15:59 +0000124overflow:
125 // TODO reparse using bignum
Damien Georgeea13f402014-04-05 18:32:08 +0100126 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer"));
Damien George20773972014-02-22 18:12:43 +0000127}
128
Damien George20773972014-02-22 18:12:43 +0000129#define PARSE_DEC_IN_INTG (1)
130#define PARSE_DEC_IN_FRAC (2)
131#define PARSE_DEC_IN_EXP (3)
132
Damien George6e48f7f2014-03-21 11:45:46 +0000133mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
Damien George20773972014-02-22 18:12:43 +0000134#if MICROPY_ENABLE_FLOAT
Damien George20773972014-02-22 18:12:43 +0000135 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000136 mp_float_t dec_val = 0;
137 bool dec_neg = false;
138 bool imag = false;
139
140 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000141 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000142 }
143
Damien Georgedfbafab2014-03-21 12:15:59 +0000144 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000145 if (str < top) {
146 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000147 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000148 } else if (*str == '-') {
149 str++;
150 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000151 }
152 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000153
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 George6e48f7f2014-03-21 11:45:46 +0000174 // string should be a decimal number
Damien Georgec06ea7a2014-03-21 10:55:08 +0000175 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 Georgedfbafab2014-03-21 12:15:59 +0000232 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000233 }
234
235 // check we reached the end of the string
236 if (str != top) {
Damien Georgeea13f402014-04-05 18:32:08 +0100237 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Damien George20773972014-02-22 18:12:43 +0000238 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000239
240 // return the object
Damien George20773972014-02-22 18:12:43 +0000241 if (imag) {
242 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000243 } else if (force_complex) {
244 return mp_obj_new_complex(dec_val, 0);
Damien George20773972014-02-22 18:12:43 +0000245 } else {
246 return mp_obj_new_float(dec_val);
247 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000248
Damien George20773972014-02-22 18:12:43 +0000249#else
Damien Georgeea13f402014-04-05 18:32:08 +0100250 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
Damien George20773972014-02-22 18:12:43 +0000251#endif
252}