2014-01-04 20:50:45 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "mpqstr.h"
|
|
|
|
#include "obj.h"
|
2014-01-13 15:31:01 -05:00
|
|
|
#include "objint.h"
|
2014-01-12 10:30:48 -05:00
|
|
|
|
|
|
|
// This dispatcher function is expected to be independent of the implementation
|
|
|
|
// of long int
|
2014-01-18 09:10:48 -05:00
|
|
|
static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
|
|
|
// TODO check n_kw == 0
|
|
|
|
|
2014-01-04 20:50:45 -05:00
|
|
|
switch (n_args) {
|
|
|
|
case 0:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
|
|
|
|
case 1:
|
2014-01-15 17:58:39 -05:00
|
|
|
if (MP_OBJ_IS_TYPE(args[0], &str_type)) {
|
|
|
|
// a string, parse it
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), 0));
|
|
|
|
} else {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
|
|
|
|
}
|
2014-01-04 20:50:45 -05:00
|
|
|
|
2014-01-14 08:39:05 -05:00
|
|
|
case 2:
|
2014-01-15 17:58:39 -05:00
|
|
|
// should be a string, parse it
|
|
|
|
// TODO proper error checking of argument types
|
2014-01-18 09:10:48 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), mp_obj_get_int(args[1])));
|
2014-01-04 20:50:45 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "int takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t int_type = {
|
|
|
|
{ &mp_const_type },
|
|
|
|
"int",
|
2014-01-12 10:30:48 -05:00
|
|
|
.print = int_print,
|
2014-01-05 15:34:09 -05:00
|
|
|
.make_new = int_make_new,
|
2014-01-12 10:30:48 -05:00
|
|
|
.binary_op = int_binary_op,
|
2014-01-04 20:50:45 -05:00
|
|
|
};
|
|
|
|
|
2014-01-12 10:30:48 -05:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
|
|
|
// This is called only for non-SMALL_INT
|
2014-01-13 12:19:16 -05: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-12 10:30:48 -05: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 14:39:01 -05:00
|
|
|
return mp_const_none;
|
2014-01-12 10:30:48 -05: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-01-18 05:46:43 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "long int not supported in this build"));
|
2014-01-13 14:39:01 -05:00
|
|
|
return mp_const_none;
|
2014-01-12 10:30:48 -05: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-01-18 05:46:43 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
|
2014-01-13 14:39:01 -05:00
|
|
|
return mp_const_none;
|
2014-01-12 10:30:48 -05:00
|
|
|
}
|
|
|
|
|
2014-01-04 20:50:45 -05:00
|
|
|
mp_obj_t mp_obj_new_int(machine_int_t value) {
|
2014-01-12 10:30:48 -05:00
|
|
|
if (MP_OBJ_FITS_SMALL_INT(value)) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2014-01-18 05:46:43 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
|
2014-01-13 14:39:01 -05:00
|
|
|
return mp_const_none;
|
2014-01-04 20:50:45 -05:00
|
|
|
}
|
2014-01-18 09:07:16 -05: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-01-12 10:30:48 -05:00
|
|
|
#endif
|