blob: 6be042fe89f313ebea0d6c6840baeaa549ce6bea [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) {
22 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36"));
23 }
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;
44 for (; str < top; str++) {
45 machine_int_t old_val = int_val;
46 int dig = *str;
47 if (unichar_isdigit(dig) && dig - '0' < base) {
48 // 0-9 digit
49 int_val = base * int_val + dig - '0';
50 } else if (base == 16) {
51 dig |= 0x20;
52 if ('a' <= dig && dig <= 'f') {
53 // a-f hex digit
54 int_val = base * int_val + dig - 'a' + 10;
55 } else {
56 // unknown character
57 break;
58 }
59 } else {
60 // unknown character
61 break;
62 }
63 if (int_val < old_val) {
64 // If new value became less than previous, it's overflow
65 goto overflow;
66 } else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) {
67 // If signed number changed sign - it's overflow
68 goto overflow;
69 }
70 }
71
72 // negate value if needed
73 if (neg) {
74 int_val = -int_val;
75 }
76
77 // skip trailing space
78 for (; str < top && unichar_isspace(*str); str++) {
79 }
80
81 // check we reached the end of the string
82 if (str != top) {
83 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
84 }
85
86 // return the object
87 return MP_OBJ_NEW_SMALL_INT(int_val);
88
89overflow:
90 // TODO reparse using bignum
91 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer"));
Damien George20773972014-02-22 18:12:43 +000092}
93
Damien George20773972014-02-22 18:12:43 +000094#define PARSE_DEC_IN_INTG (1)
95#define PARSE_DEC_IN_FRAC (2)
96#define PARSE_DEC_IN_EXP (3)
97
Damien George6e48f7f2014-03-21 11:45:46 +000098mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
Damien George20773972014-02-22 18:12:43 +000099#if MICROPY_ENABLE_FLOAT
Damien George20773972014-02-22 18:12:43 +0000100 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000101 mp_float_t dec_val = 0;
102 bool dec_neg = false;
103 bool imag = false;
104
105 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000106 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000107 }
108
Damien Georgedfbafab2014-03-21 12:15:59 +0000109 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000110 if (str < top) {
111 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000112 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000113 } else if (*str == '-') {
114 str++;
115 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000116 }
117 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000118
119 // determine what the string is
120 if (str < top && (str[0] | 0x20) == 'i') {
121 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
122 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
123 // inf
124 str += 3;
125 dec_val = INFINITY;
126 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
127 // infinity
128 str += 5;
129 }
130 }
131 } else if (str < top && (str[0] | 0x20) == 'n') {
132 // string starts with 'n', should be 'nan' (case insensitive)
133 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
134 // NaN
135 str += 3;
136 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
137 }
138 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000139 // string should be a decimal number
Damien Georgec06ea7a2014-03-21 10:55:08 +0000140 int in = PARSE_DEC_IN_INTG;
141 bool exp_neg = false;
142 int exp_val = 0;
143 int exp_extra = 0;
144 for (; str < top; str++) {
145 int dig = *str;
146 if ('0' <= dig && dig <= '9') {
147 dig -= '0';
148 if (in == PARSE_DEC_IN_EXP) {
149 exp_val = 10 * exp_val + dig;
150 } else {
151 dec_val = 10 * dec_val + dig;
152 if (in == PARSE_DEC_IN_FRAC) {
153 exp_extra -= 1;
154 }
155 }
156 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
157 in = PARSE_DEC_IN_FRAC;
158 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
159 in = PARSE_DEC_IN_EXP;
160 if (str[1] == '+') {
161 str++;
162 } else if (str[1] == '-') {
163 str++;
164 exp_neg = true;
165 }
166 } else if (allow_imag && (dig | 0x20) == 'j') {
167 str++;
168 imag = true;
169 break;
170 } else {
171 // unknown character
172 break;
173 }
174 }
175
176 // work out the exponent
177 if (exp_neg) {
178 exp_val = -exp_val;
179 }
180 exp_val += exp_extra;
181
182 // apply the exponent
183 for (; exp_val > 0; exp_val--) {
184 dec_val *= 10;
185 }
186 for (; exp_val < 0; exp_val++) {
187 dec_val *= 0.1;
188 }
189 }
190
191 // negate value if needed
192 if (dec_neg) {
193 dec_val = -dec_val;
194 }
195
196 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000197 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000198 }
199
200 // check we reached the end of the string
201 if (str != top) {
Damien George20773972014-02-22 18:12:43 +0000202 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
203 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000204
205 // return the object
Damien George20773972014-02-22 18:12:43 +0000206 if (imag) {
207 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000208 } else if (force_complex) {
209 return mp_obj_new_complex(dec_val, 0);
Damien George20773972014-02-22 18:12:43 +0000210 } else {
211 return mp_obj_new_float(dec_val);
212 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000213
Damien George20773972014-02-22 18:12:43 +0000214#else
215 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
216#endif
217}