blob: 70f20e3cdbf9248ffe617a365807de7db3eecfd4 [file] [log] [blame]
Damien George45b43c22014-01-05 01:50:45 +00001#include <stdint.h>
Damien George45b43c22014-01-05 01:50:45 +00002#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damien George45b43c22014-01-05 01:50:45 +00008#include "obj.h"
Damien George20773972014-02-22 18:12:43 +00009#include "parsenum.h"
Damien George438c88d2014-02-22 19:25:23 +000010#include "mpz.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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020015STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000016 // TODO check n_kw == 0
17
Damien George45b43c22014-01-05 01:50:45 +000018 switch (n_args) {
19 case 0:
20 return MP_OBJ_NEW_SMALL_INT(0);
21
22 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000023 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000024 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000025 uint l;
Damien George698ec212014-02-08 18:17:23 +000026 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000027 return mp_parse_num_integer(s, l, 0);
Damien George5573f9f2014-01-15 22:58:39 +000028 } else {
29 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
30 }
Damien George45b43c22014-01-05 01:50:45 +000031
xybc178ea42014-01-14 21:39:05 +080032 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000033 {
Damien George5573f9f2014-01-15 22:58:39 +000034 // should be a string, parse it
35 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000036 uint l;
Damien George698ec212014-02-08 18:17:23 +000037 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000038 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000039 }
Damien George45b43c22014-01-05 01:50:45 +000040
41 default:
Damien Georgec5966122014-02-15 16:10:44 +000042 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
Damien George45b43c22014-01-05 01:50:45 +000043 }
44}
45
Paul Sokolovsky48b35722014-01-12 17:30:48 +020046#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Damien George5fa93b62014-01-22 14:35:10 +000047
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 George5fa93b62014-01-22 14:35:10 +000049 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien George0379b552014-02-22 17:34:09 +000050 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
Damien George5fa93b62014-01-22 14:35:10 +000051 }
Damien George45b43c22014-01-05 01:50:45 +000052}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020053
54// This is called only for non-SMALL_INT
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020055mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
56 assert(0);
57 return mp_const_none;
58}
59
60// This is called only for non-SMALL_INT
Paul Sokolovsky48b35722014-01-12 17:30:48 +020061mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
62 assert(0);
Damien George23005372014-01-13 19:39:01 +000063 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020064}
65
66// This is called only with strings whose value doesn't fit in SMALL_INT
67mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgec5966122014-02-15 16:10:44 +000068 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +000069 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020070}
71
Damien George9d68e9c2014-03-12 15:38:15 +000072// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
73mp_obj_t mp_obj_new_int_from_ll(long long val) {
74 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
75 return mp_const_none;
76}
77
Paul Sokolovsky48b35722014-01-12 17:30:48 +020078mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
79 // SMALL_INT accepts only signed numbers, of one bit less size
80 // then word size, which totals 2 bits less for unsigned numbers.
81 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
82 return MP_OBJ_NEW_SMALL_INT(value);
83 }
Damien Georgec5966122014-02-15 16:10:44 +000084 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000085 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020086}
87
88mp_obj_t mp_obj_new_int(machine_int_t value) {
89 if (MP_OBJ_FITS_SMALL_INT(value)) {
90 return MP_OBJ_NEW_SMALL_INT(value);
91 }
Damien Georgec5966122014-02-15 16:10:44 +000092 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000093 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020094}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +020095
96machine_int_t mp_obj_int_get(mp_obj_t self_in) {
97 return MP_OBJ_SMALL_INT_VALUE(self_in);
98}
99
100machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
101 return MP_OBJ_SMALL_INT_VALUE(self_in);
102}
103
Damien Georgeeabdf672014-03-22 20:54:01 +0000104#if MICROPY_ENABLE_FLOAT
105mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
106 return MP_OBJ_SMALL_INT_VALUE(self_in);
107}
108#endif
109
Damien George5fa93b62014-01-22 14:35:10 +0000110#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
111
Damien George3e1a5c12014-03-29 13:43:38 +0000112const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000113 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000114 .name = MP_QSTR_int,
Damien George5fa93b62014-01-22 14:35:10 +0000115 .print = int_print,
116 .make_new = int_make_new,
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200117 .unary_op = int_unary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000118 .binary_op = int_binary_op,
119};