blob: 64c86241098b466f800d23293e726fcf1a55555d [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien George20773972014-02-22 18:12:43 +00002#include <stdlib.h>
3
4#include "misc.h"
5#include "mpconfig.h"
6#include "qstr.h"
7#include "nlr.h"
8#include "obj.h"
Damien George06201ff2014-03-01 19:50:50 +00009#include "parsenumbase.h"
Damien George20773972014-02-22 18:12:43 +000010#include "parsenum.h"
11
Damien Georgec06ea7a2014-03-21 10:55:08 +000012#if MICROPY_ENABLE_FLOAT
13#include <math.h>
14#endif
15
Damien George20773972014-02-22 18:12:43 +000016mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
Damien Georgedfbafab2014-03-21 12:15:59 +000017 const char *restrict top = str + len;
18 bool neg = false;
Damien George20773972014-02-22 18:12:43 +000019
20 // check radix base
21 if ((base != 0 && base < 2) || base > 36) {
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010022 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 +000023 }
Damien Georgedfbafab2014-03-21 12:15:59 +000024
25 // skip leading space
26 for (; str < top && unichar_isspace(*str); str++) {
Damien George20773972014-02-22 18:12:43 +000027 }
28
Damien Georgedfbafab2014-03-21 12:15:59 +000029 // parse optional sign
30 if (str < top) {
31 if (*str == '+') {
32 str++;
33 } else if (*str == '-') {
34 str++;
35 neg = true;
Damien George20773972014-02-22 18:12:43 +000036 }
37 }
38
Damien Georgedfbafab2014-03-21 12:15:59 +000039 // parse optional base prefix
40 str += mp_parse_num_base(str, top - str, &base);
Damien George20773972014-02-22 18:12:43 +000041
Damien Georgedfbafab2014-03-21 12:15:59 +000042 // string should be an integer number
43 machine_int_t int_val = 0;
Damien George7b4b78b2014-03-21 20:46:38 +000044 const char *restrict str_val_start = str;
Damien Georgedfbafab2014-03-21 12:15:59 +000045 for (; str < top; str++) {
46 machine_int_t old_val = int_val;
47 int dig = *str;
48 if (unichar_isdigit(dig) && dig - '0' < base) {
49 // 0-9 digit
50 int_val = base * int_val + dig - '0';
51 } else if (base == 16) {
52 dig |= 0x20;
53 if ('a' <= dig && dig <= 'f') {
54 // a-f hex digit
55 int_val = base * int_val + dig - 'a' + 10;
56 } else {
57 // unknown character
58 break;
59 }
60 } else {
61 // unknown character
62 break;
63 }
64 if (int_val < old_val) {
65 // If new value became less than previous, it's overflow
66 goto overflow;
67 } else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) {
68 // If signed number changed sign - it's overflow
69 goto overflow;
70 }
71 }
72
Damien George7b4b78b2014-03-21 20:46:38 +000073 // check we parsed something
74 if (str == str_val_start) {
75 goto value_error;
76 }
77
Damien Georgedfbafab2014-03-21 12:15:59 +000078 // negate value if needed
79 if (neg) {
80 int_val = -int_val;
81 }
82
83 // skip trailing space
84 for (; str < top && unichar_isspace(*str); str++) {
85 }
86
87 // check we reached the end of the string
88 if (str != top) {
Damien George7b4b78b2014-03-21 20:46:38 +000089 goto value_error;
Damien Georgedfbafab2014-03-21 12:15:59 +000090 }
91
92 // return the object
93 return MP_OBJ_NEW_SMALL_INT(int_val);
94
Damien George7b4b78b2014-03-21 20:46:38 +000095value_error:
Damien Georgeea13f402014-04-05 18:32:08 +010096 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 +000097
Damien Georgedfbafab2014-03-21 12:15:59 +000098overflow:
99 // TODO reparse using bignum
Damien Georgeea13f402014-04-05 18:32:08 +0100100 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer"));
Damien George20773972014-02-22 18:12:43 +0000101}
102
Damien George20773972014-02-22 18:12:43 +0000103#define PARSE_DEC_IN_INTG (1)
104#define PARSE_DEC_IN_FRAC (2)
105#define PARSE_DEC_IN_EXP (3)
106
Damien George6e48f7f2014-03-21 11:45:46 +0000107mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
Damien George20773972014-02-22 18:12:43 +0000108#if MICROPY_ENABLE_FLOAT
Damien George20773972014-02-22 18:12:43 +0000109 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000110 mp_float_t dec_val = 0;
111 bool dec_neg = false;
112 bool imag = false;
113
114 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000115 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000116 }
117
Damien Georgedfbafab2014-03-21 12:15:59 +0000118 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000119 if (str < top) {
120 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000121 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000122 } else if (*str == '-') {
123 str++;
124 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000125 }
126 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000127
128 // determine what the string is
129 if (str < top && (str[0] | 0x20) == 'i') {
130 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
131 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
132 // inf
133 str += 3;
134 dec_val = INFINITY;
135 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
136 // infinity
137 str += 5;
138 }
139 }
140 } else if (str < top && (str[0] | 0x20) == 'n') {
141 // string starts with 'n', should be 'nan' (case insensitive)
142 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
143 // NaN
144 str += 3;
145 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
146 }
147 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000148 // string should be a decimal number
Damien Georgec06ea7a2014-03-21 10:55:08 +0000149 int in = PARSE_DEC_IN_INTG;
150 bool exp_neg = false;
151 int exp_val = 0;
152 int exp_extra = 0;
153 for (; str < top; str++) {
154 int dig = *str;
155 if ('0' <= dig && dig <= '9') {
156 dig -= '0';
157 if (in == PARSE_DEC_IN_EXP) {
158 exp_val = 10 * exp_val + dig;
159 } else {
160 dec_val = 10 * dec_val + dig;
161 if (in == PARSE_DEC_IN_FRAC) {
162 exp_extra -= 1;
163 }
164 }
165 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
166 in = PARSE_DEC_IN_FRAC;
167 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
168 in = PARSE_DEC_IN_EXP;
169 if (str[1] == '+') {
170 str++;
171 } else if (str[1] == '-') {
172 str++;
173 exp_neg = true;
174 }
175 } else if (allow_imag && (dig | 0x20) == 'j') {
176 str++;
177 imag = true;
178 break;
179 } else {
180 // unknown character
181 break;
182 }
183 }
184
185 // work out the exponent
186 if (exp_neg) {
187 exp_val = -exp_val;
188 }
189 exp_val += exp_extra;
190
191 // apply the exponent
192 for (; exp_val > 0; exp_val--) {
193 dec_val *= 10;
194 }
195 for (; exp_val < 0; exp_val++) {
196 dec_val *= 0.1;
197 }
198 }
199
200 // negate value if needed
201 if (dec_neg) {
202 dec_val = -dec_val;
203 }
204
205 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000206 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000207 }
208
209 // check we reached the end of the string
210 if (str != top) {
Damien Georgeea13f402014-04-05 18:32:08 +0100211 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Damien George20773972014-02-22 18:12:43 +0000212 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000213
214 // return the object
Damien George20773972014-02-22 18:12:43 +0000215 if (imag) {
216 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000217 } else if (force_complex) {
218 return mp_obj_new_complex(dec_val, 0);
Damien George20773972014-02-22 18:12:43 +0000219 } else {
220 return mp_obj_new_float(dec_val);
221 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000222
Damien George20773972014-02-22 18:12:43 +0000223#else
Damien Georgeea13f402014-04-05 18:32:08 +0100224 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
Damien George20773972014-02-22 18:12:43 +0000225#endif
226}