2014-01-05 05:50:45 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-22 01:40:13 +04:00
|
|
|
#include "qstr.h"
|
2014-01-05 05:50:45 +04:00
|
|
|
#include "obj.h"
|
2014-02-22 22:12:43 +04:00
|
|
|
#include "parsenum.h"
|
2014-02-22 23:25:23 +04:00
|
|
|
#include "mpz.h"
|
2014-01-14 00:31:01 +04:00
|
|
|
#include "objint.h"
|
2014-01-12 19:30:48 +04:00
|
|
|
|
|
|
|
// This dispatcher function is expected to be independent of the implementation
|
|
|
|
// of long int
|
2014-02-12 20:15:40 +04:00
|
|
|
STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-01-18 18:10:48 +04:00
|
|
|
// TODO check n_kw == 0
|
|
|
|
|
2014-01-05 05:50:45 +04:00
|
|
|
switch (n_args) {
|
|
|
|
case 0:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
|
|
|
|
case 1:
|
2014-01-22 18:35:10 +04:00
|
|
|
if (MP_OBJ_IS_STR(args[0])) {
|
2014-01-16 02:58:39 +04:00
|
|
|
// a string, parse it
|
2014-01-22 18:35:10 +04:00
|
|
|
uint l;
|
2014-02-08 22:17:23 +04:00
|
|
|
const char *s = mp_obj_str_get_data(args[0], &l);
|
2014-02-22 22:12:43 +04:00
|
|
|
return mp_parse_num_integer(s, l, 0);
|
2014-01-16 02:58:39 +04:00
|
|
|
} else {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
|
|
|
|
}
|
2014-01-05 05:50:45 +04:00
|
|
|
|
2014-01-14 17:39:05 +04:00
|
|
|
case 2:
|
2014-01-22 18:35:10 +04:00
|
|
|
{
|
2014-01-16 02:58:39 +04:00
|
|
|
// should be a string, parse it
|
|
|
|
// TODO proper error checking of argument types
|
2014-01-22 18:35:10 +04:00
|
|
|
uint l;
|
2014-02-08 22:17:23 +04:00
|
|
|
const char *s = mp_obj_str_get_data(args[0], &l);
|
2014-02-22 22:12:43 +04:00
|
|
|
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
|
2014-01-22 18:35:10 +04:00
|
|
|
}
|
2014-01-05 05:50:45 +04:00
|
|
|
|
|
|
|
default:
|
2014-02-15 20:10:44 +04:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
|
2014-01-05 05:50:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-12 19:30:48 +04:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
2014-01-22 18:35:10 +04:00
|
|
|
|
2014-01-13 21:19:16 +04:00
|
|
|
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-22 18:35:10 +04:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
2014-02-22 21:34:09 +04:00
|
|
|
print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
|
2014-01-22 18:35:10 +04:00
|
|
|
}
|
2014-01-12 19:30:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 11:05:50 +04:00
|
|
|
// This is called only for non-SMALL_INT
|
|
|
|
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
|
|
|
|
assert(0);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-01-12 19:30:48 +04:00
|
|
|
// This is called only for non-SMALL_INT
|
|
|
|
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|
|
|
assert(0);
|
2014-01-13 23:39:01 +04:00
|
|
|
return mp_const_none;
|
2014-01-12 19:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is called only with strings whose value doesn't fit in SMALL_INT
|
|
|
|
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
|
2014-02-15 20:10:44 +04:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
|
2014-01-13 23:39:01 +04:00
|
|
|
return mp_const_none;
|
2014-01-12 19:30:48 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:38:15 +04:00
|
|
|
// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
|
|
|
|
mp_obj_t mp_obj_new_int_from_ll(long long val) {
|
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-01-12 19:30:48 +04:00
|
|
|
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
|
|
|
|
// SMALL_INT accepts only signed numbers, of one bit less size
|
|
|
|
// then word size, which totals 2 bits less for unsigned numbers.
|
|
|
|
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2014-02-15 20:10:44 +04:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
|
2014-01-13 23:39:01 +04:00
|
|
|
return mp_const_none;
|
2014-01-12 19:30:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-05 05:50:45 +04:00
|
|
|
mp_obj_t mp_obj_new_int(machine_int_t value) {
|
2014-01-12 19:30:48 +04:00
|
|
|
if (MP_OBJ_FITS_SMALL_INT(value)) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2014-02-15 20:10:44 +04:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
|
2014-01-13 23:39:01 +04:00
|
|
|
return mp_const_none;
|
2014-01-05 05:50:45 +04:00
|
|
|
}
|
2014-01-18 18:07:16 +04:00
|
|
|
|
|
|
|
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
|
2014-03-23 00:54:01 +04:00
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-22 18:35:10 +04:00
|
|
|
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
|
|
|
|
2014-03-29 17:43:38 +04:00
|
|
|
const mp_obj_type_t mp_type_int = {
|
2014-02-15 20:10:44 +04:00
|
|
|
{ &mp_type_type },
|
2014-02-15 15:34:50 +04:00
|
|
|
.name = MP_QSTR_int,
|
2014-01-22 18:35:10 +04:00
|
|
|
.print = int_print,
|
|
|
|
.make_new = int_make_new,
|
2014-01-27 11:05:50 +04:00
|
|
|
.unary_op = int_unary_op,
|
2014-01-22 18:35:10 +04:00
|
|
|
.binary_op = int_binary_op,
|
|
|
|
};
|