blob: 490b4340bb271a60c39c77170c509ac0fc8dd53d [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"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien George45b43c22014-01-05 01:50:45 +000010#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000011#include "parsenum.h"
Damien George438c88d2014-02-22 19:25:23 +000012#include "mpz.h"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020013#include "objint.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020014
15// This dispatcher function is expected to be independent of the implementation
16// of long int
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020017STATIC 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 +000018 // TODO check n_kw == 0
19
Damien George45b43c22014-01-05 01:50:45 +000020 switch (n_args) {
21 case 0:
22 return MP_OBJ_NEW_SMALL_INT(0);
23
24 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000025 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000026 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000027 uint l;
Damien George698ec212014-02-08 18:17:23 +000028 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000029 return mp_parse_num_integer(s, l, 0);
Damien George5573f9f2014-01-15 22:58:39 +000030 } else {
31 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
32 }
Damien George45b43c22014-01-05 01:50:45 +000033
xybc178ea42014-01-14 21:39:05 +080034 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000035 {
Damien George5573f9f2014-01-15 22:58:39 +000036 // should be a string, parse it
37 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000038 uint l;
Damien George698ec212014-02-08 18:17:23 +000039 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000040 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000041 }
Damien George45b43c22014-01-05 01:50:45 +000042
43 default:
Damien Georgec5966122014-02-15 16:10:44 +000044 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 +000045 }
46}
47
Paul Sokolovsky48b35722014-01-12 17:30:48 +020048#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Damien George5fa93b62014-01-22 14:35:10 +000049
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020050void 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 +000051 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien George0379b552014-02-22 17:34:09 +000052 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
Damien George5fa93b62014-01-22 14:35:10 +000053 }
Damien George45b43c22014-01-05 01:50:45 +000054}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020055
56// This is called only for non-SMALL_INT
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020057mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
58 assert(0);
59 return mp_const_none;
60}
61
62// This is called only for non-SMALL_INT
Paul Sokolovsky48b35722014-01-12 17:30:48 +020063mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
64 assert(0);
Damien George23005372014-01-13 19:39:01 +000065 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020066}
67
68// This is called only with strings whose value doesn't fit in SMALL_INT
69mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgec5966122014-02-15 16:10:44 +000070 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +000071 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020072}
73
74mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
75 // SMALL_INT accepts only signed numbers, of one bit less size
76 // then word size, which totals 2 bits less for unsigned numbers.
77 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
78 return MP_OBJ_NEW_SMALL_INT(value);
79 }
Damien Georgec5966122014-02-15 16:10:44 +000080 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000081 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020082}
83
84mp_obj_t mp_obj_new_int(machine_int_t value) {
85 if (MP_OBJ_FITS_SMALL_INT(value)) {
86 return MP_OBJ_NEW_SMALL_INT(value);
87 }
Damien Georgec5966122014-02-15 16:10:44 +000088 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000089 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020090}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +020091
92machine_int_t mp_obj_int_get(mp_obj_t self_in) {
93 return MP_OBJ_SMALL_INT_VALUE(self_in);
94}
95
96machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
97 return MP_OBJ_SMALL_INT_VALUE(self_in);
98}
99
Damien George5fa93b62014-01-22 14:35:10 +0000100#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
101
102const mp_obj_type_t int_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000103 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000104 .name = MP_QSTR_int,
Damien George5fa93b62014-01-22 14:35:10 +0000105 .print = int_print,
106 .make_new = int_make_new,
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200107 .unary_op = int_unary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000108 .binary_op = int_binary_op,
109};