blob: 59181eaf6de4ee3464d6000202f4e5385913a9e8 [file] [log] [blame]
Damien George45b43c22014-01-05 01:50:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
9#include "mpqstr.h"
10#include "obj.h"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020011#include "objint.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020012
13// This dispatcher function is expected to be independent of the implementation
14// of long int
Damien George45b43c22014-01-05 01:50:45 +000015static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
16 switch (n_args) {
17 case 0:
18 return MP_OBJ_NEW_SMALL_INT(0);
19
20 case 1:
Damien George5573f9f2014-01-15 22:58:39 +000021 if (MP_OBJ_IS_TYPE(args[0], &str_type)) {
22 // a string, parse it
23 return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), 0));
24 } else {
25 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
26 }
Damien George45b43c22014-01-05 01:50:45 +000027
xybc178ea42014-01-14 21:39:05 +080028 case 2:
Damien George5573f9f2014-01-15 22:58:39 +000029 // should be a string, parse it
30 // TODO proper error checking of argument types
31 return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[1])), mp_obj_get_int(args[0])));
Damien George45b43c22014-01-05 01:50:45 +000032
33 default:
34 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));
35 }
36}
37
38const mp_obj_type_t int_type = {
39 { &mp_const_type },
40 "int",
Paul Sokolovsky48b35722014-01-12 17:30:48 +020041 .print = int_print,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020042 .make_new = int_make_new,
Paul Sokolovsky48b35722014-01-12 17:30:48 +020043 .binary_op = int_binary_op,
Damien George45b43c22014-01-05 01:50:45 +000044};
45
Paul Sokolovsky48b35722014-01-12 17:30:48 +020046#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
47// This is called only for non-SMALL_INT
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020048void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George45b43c22014-01-05 01:50:45 +000049}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020050
51// This is called only for non-SMALL_INT
52mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
53 assert(0);
Damien George23005372014-01-13 19:39:01 +000054 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020055}
56
57// This is called only with strings whose value doesn't fit in SMALL_INT
58mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Paul Sokolovsky166bb402014-01-18 12:46:43 +020059 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +000060 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020061}
62
63mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
64 // SMALL_INT accepts only signed numbers, of one bit less size
65 // then word size, which totals 2 bits less for unsigned numbers.
66 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
67 return MP_OBJ_NEW_SMALL_INT(value);
68 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020069 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000070 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020071}
72
73mp_obj_t mp_obj_new_int(machine_int_t value) {
74 if (MP_OBJ_FITS_SMALL_INT(value)) {
75 return MP_OBJ_NEW_SMALL_INT(value);
76 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020077 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000078 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020079}
80#endif