blob: 3a853eab800c1be874b7bb8d69286bac12fee47e [file] [log] [blame]
Damien George660aef62014-04-02 12:22:07 +01001#include <stdlib.h>
Damien George45b43c22014-01-05 01:50:45 +00002#include <stdint.h>
Damien George45b43c22014-01-05 01:50:45 +00003#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien George45b43c22014-01-05 01:50:45 +00009#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000010#include "parsenum.h"
Damien George438c88d2014-02-22 19:25:23 +000011#include "mpz.h"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020012#include "objint.h"
Damien George660aef62014-04-02 12:22:07 +010013#include "runtime0.h"
14#include "runtime.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020015
Damien George6433bd92014-03-30 23:13:16 +010016#if MICROPY_ENABLE_FLOAT
17#include <math.h>
18#endif
19
Paul Sokolovsky48b35722014-01-12 17:30:48 +020020// This dispatcher function is expected to be independent of the implementation
21// of long int
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020022STATIC 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 +000023 // TODO check n_kw == 0
24
Damien George45b43c22014-01-05 01:50:45 +000025 switch (n_args) {
26 case 0:
27 return MP_OBJ_NEW_SMALL_INT(0);
28
29 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000030 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000031 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000032 uint l;
Damien George698ec212014-02-08 18:17:23 +000033 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000034 return mp_parse_num_integer(s, l, 0);
Damien George6433bd92014-03-30 23:13:16 +010035#if MICROPY_ENABLE_FLOAT
36 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
37 return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
38#endif
Damien George5573f9f2014-01-15 22:58:39 +000039 } else {
40 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
41 }
Damien George45b43c22014-01-05 01:50:45 +000042
xybc178ea42014-01-14 21:39:05 +080043 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000044 {
Damien George5573f9f2014-01-15 22:58:39 +000045 // should be a string, parse it
46 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000047 uint l;
Damien George698ec212014-02-08 18:17:23 +000048 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000049 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000050 }
Damien George45b43c22014-01-05 01:50:45 +000051
52 default:
Damien Georgec5966122014-02-15 16:10:44 +000053 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 +000054 }
55}
56
Paul Sokolovsky48b35722014-01-12 17:30:48 +020057#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Damien George5fa93b62014-01-22 14:35:10 +000058
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020059void 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 +000060 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien George0379b552014-02-22 17:34:09 +000061 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
Damien George5fa93b62014-01-22 14:35:10 +000062 }
Damien George45b43c22014-01-05 01:50:45 +000063}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020064
Damien George660aef62014-04-02 12:22:07 +010065// This is called for operations on SMALL_INT that are not handled by mp_unary_op
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020066mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
Damien George660aef62014-04-02 12:22:07 +010067 return MP_OBJ_NULL;
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020068}
69
Damien George660aef62014-04-02 12:22:07 +010070// This is called for operations on SMALL_INT that are not handled by mp_binary_op
Paul Sokolovsky48b35722014-01-12 17:30:48 +020071mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George660aef62014-04-02 12:22:07 +010072 if (op == MP_BINARY_OP_MULTIPLY) {
73 if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
74 // multiply is commutative for these types, so delegate to them
75 return mp_binary_op(op, rhs_in, lhs_in);
76 }
77 }
78 return MP_OBJ_NULL;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020079}
80
81// This is called only with strings whose value doesn't fit in SMALL_INT
82mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgec5966122014-02-15 16:10:44 +000083 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +000084 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020085}
86
Damien George9d68e9c2014-03-12 15:38:15 +000087// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
88mp_obj_t mp_obj_new_int_from_ll(long long val) {
89 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
90 return mp_const_none;
91}
92
Paul Sokolovsky48b35722014-01-12 17:30:48 +020093mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
94 // SMALL_INT accepts only signed numbers, of one bit less size
95 // then word size, which totals 2 bits less for unsigned numbers.
96 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
97 return MP_OBJ_NEW_SMALL_INT(value);
98 }
Damien Georgec5966122014-02-15 16:10:44 +000099 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +0000100 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200101}
102
103mp_obj_t mp_obj_new_int(machine_int_t value) {
104 if (MP_OBJ_FITS_SMALL_INT(value)) {
105 return MP_OBJ_NEW_SMALL_INT(value);
106 }
Damien Georgec5966122014-02-15 16:10:44 +0000107 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +0000108 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200109}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200110
111machine_int_t mp_obj_int_get(mp_obj_t self_in) {
112 return MP_OBJ_SMALL_INT_VALUE(self_in);
113}
114
115machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
116 return MP_OBJ_SMALL_INT_VALUE(self_in);
117}
118
Damien Georgeeabdf672014-03-22 20:54:01 +0000119#if MICROPY_ENABLE_FLOAT
120mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
121 return MP_OBJ_SMALL_INT_VALUE(self_in);
122}
123#endif
124
Damien George5fa93b62014-01-22 14:35:10 +0000125#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
126
Damien George3e1a5c12014-03-29 13:43:38 +0000127const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000128 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000129 .name = MP_QSTR_int,
Damien George5fa93b62014-01-22 14:35:10 +0000130 .print = int_print,
131 .make_new = int_make_new,
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200132 .unary_op = int_unary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000133 .binary_op = int_binary_op,
134};