blob: 77f00957c6c66d97b8ae9a6397036d2aa3867aa5 [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 +000016#if defined(UNIX)
17
18#include <ctype.h>
19#include <errno.h>
20
21mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
22 // TODO at the moment we ignore len; we should honour it!
23 // TODO detect integer overflow and return bignum
24
25 int c, neg = 0;
26 const char *p = str;
27 char *num;
28 long found;
29
30 // check radix base
31 if ((base != 0 && base < 2) || base > 36) {
32 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36"));
33 }
34 // skip surrounded whitespace
35 while (isspace((c = *(p++))));
36 if (c == 0) {
37 goto value_error;
38 }
39 // preced sign
40 if (c == '+' || c == '-') {
41 neg = - (c == '-');
Damien George20773972014-02-22 18:12:43 +000042 } else {
Damien George20773972014-02-22 18:12:43 +000043 p--;
44 }
45
Damien George06201ff2014-03-01 19:50:50 +000046 len -= p - str;
47 int skip = mp_parse_num_base(p, len, &base);
48 p += skip;
49 len -= skip;
50
Damien George20773972014-02-22 18:12:43 +000051 errno = 0;
52 found = strtol(p, &num, base);
53 if (errno) {
54 goto value_error;
55 } else if (found && *(num) == 0) {
56 goto done;
57 } else if (found || num != p) {
58 goto check_tail_space;
59 } else {
60 goto value_error;
61 }
62
63check_tail_space:
64 if (*(num) != 0) {
65 while (isspace((c = *(num++))));
66 if (c != 0) {
67 goto value_error;
68 }
69 }
70
71done:
72 return MP_OBJ_NEW_SMALL_INT((found ^ neg) - neg);
73
74value_error:
75 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str));
76}
77
78#else /* defined(UNIX) */
79
80mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
81 // TODO port strtol to stm
82 return MP_OBJ_NEW_SMALL_INT(0);
83}
84
85#endif /* defined(UNIX) */
86
87#define PARSE_DEC_IN_INTG (1)
88#define PARSE_DEC_IN_FRAC (2)
89#define PARSE_DEC_IN_EXP (3)
90
Damien George6e48f7f2014-03-21 11:45:46 +000091mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
Damien George20773972014-02-22 18:12:43 +000092#if MICROPY_ENABLE_FLOAT
Damien George20773972014-02-22 18:12:43 +000093 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +000094 mp_float_t dec_val = 0;
95 bool dec_neg = false;
96 bool imag = false;
97
98 // skip leading space
99 for (; str < top && isspace(*str); str++) {
100 }
101
102 // get optional sign
103 if (str < top) {
104 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000105 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000106 } else if (*str == '-') {
107 str++;
108 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000109 }
110 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000111
112 // determine what the string is
113 if (str < top && (str[0] | 0x20) == 'i') {
114 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
115 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
116 // inf
117 str += 3;
118 dec_val = INFINITY;
119 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
120 // infinity
121 str += 5;
122 }
123 }
124 } else if (str < top && (str[0] | 0x20) == 'n') {
125 // string starts with 'n', should be 'nan' (case insensitive)
126 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
127 // NaN
128 str += 3;
129 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
130 }
131 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000132 // string should be a decimal number
Damien Georgec06ea7a2014-03-21 10:55:08 +0000133 int in = PARSE_DEC_IN_INTG;
134 bool exp_neg = false;
135 int exp_val = 0;
136 int exp_extra = 0;
137 for (; str < top; str++) {
138 int dig = *str;
139 if ('0' <= dig && dig <= '9') {
140 dig -= '0';
141 if (in == PARSE_DEC_IN_EXP) {
142 exp_val = 10 * exp_val + dig;
143 } else {
144 dec_val = 10 * dec_val + dig;
145 if (in == PARSE_DEC_IN_FRAC) {
146 exp_extra -= 1;
147 }
148 }
149 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
150 in = PARSE_DEC_IN_FRAC;
151 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
152 in = PARSE_DEC_IN_EXP;
153 if (str[1] == '+') {
154 str++;
155 } else if (str[1] == '-') {
156 str++;
157 exp_neg = true;
158 }
159 } else if (allow_imag && (dig | 0x20) == 'j') {
160 str++;
161 imag = true;
162 break;
163 } else {
164 // unknown character
165 break;
166 }
167 }
168
169 // work out the exponent
170 if (exp_neg) {
171 exp_val = -exp_val;
172 }
173 exp_val += exp_extra;
174
175 // apply the exponent
176 for (; exp_val > 0; exp_val--) {
177 dec_val *= 10;
178 }
179 for (; exp_val < 0; exp_val++) {
180 dec_val *= 0.1;
181 }
182 }
183
184 // negate value if needed
185 if (dec_neg) {
186 dec_val = -dec_val;
187 }
188
189 // skip trailing space
190 for (; str < top && isspace(*str); str++) {
191 }
192
193 // check we reached the end of the string
194 if (str != top) {
Damien George20773972014-02-22 18:12:43 +0000195 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
196 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000197
198 // return the object
Damien George20773972014-02-22 18:12:43 +0000199 if (imag) {
200 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000201 } else if (force_complex) {
202 return mp_obj_new_complex(dec_val, 0);
Damien George20773972014-02-22 18:12:43 +0000203 } else {
204 return mp_obj_new_float(dec_val);
205 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000206
Damien George20773972014-02-22 18:12:43 +0000207#else
208 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
209#endif
210}