blob: 0caaab649b69b62f2cf66c413b000b2b01ae588a [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"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020012#include "objint.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020013
14// This dispatcher function is expected to be independent of the implementation
15// of long int
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020016STATIC 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 +000017 // TODO check n_kw == 0
18
Damien George45b43c22014-01-05 01:50:45 +000019 switch (n_args) {
20 case 0:
21 return MP_OBJ_NEW_SMALL_INT(0);
22
23 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000024 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000025 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000026 uint l;
Damien George698ec212014-02-08 18:17:23 +000027 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000028 return mp_parse_num_integer(s, l, 0);
Damien George5573f9f2014-01-15 22:58:39 +000029 } else {
30 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
31 }
Damien George45b43c22014-01-05 01:50:45 +000032
xybc178ea42014-01-14 21:39:05 +080033 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000034 {
Damien George5573f9f2014-01-15 22:58:39 +000035 // should be a string, parse it
36 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000037 uint l;
Damien George698ec212014-02-08 18:17:23 +000038 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000039 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000040 }
Damien George45b43c22014-01-05 01:50:45 +000041
42 default:
Damien Georgec5966122014-02-15 16:10:44 +000043 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 +000044 }
45}
46
Paul Sokolovsky48b35722014-01-12 17:30:48 +020047#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Damien George5fa93b62014-01-22 14:35:10 +000048
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020049void 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 +000050 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien George0379b552014-02-22 17:34:09 +000051 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
Damien George5fa93b62014-01-22 14:35:10 +000052 }
Damien George45b43c22014-01-05 01:50:45 +000053}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020054
55// This is called only for non-SMALL_INT
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020056mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
57 assert(0);
58 return mp_const_none;
59}
60
61// This is called only for non-SMALL_INT
Paul Sokolovsky48b35722014-01-12 17:30:48 +020062mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
63 assert(0);
Damien George23005372014-01-13 19:39:01 +000064 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020065}
66
67// This is called only with strings whose value doesn't fit in SMALL_INT
68mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgec5966122014-02-15 16:10:44 +000069 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
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_from_uint(machine_uint_t value) {
74 // SMALL_INT accepts only signed numbers, of one bit less size
75 // then word size, which totals 2 bits less for unsigned numbers.
76 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
77 return MP_OBJ_NEW_SMALL_INT(value);
78 }
Damien Georgec5966122014-02-15 16:10:44 +000079 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000080 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020081}
82
83mp_obj_t mp_obj_new_int(machine_int_t value) {
84 if (MP_OBJ_FITS_SMALL_INT(value)) {
85 return MP_OBJ_NEW_SMALL_INT(value);
86 }
Damien Georgec5966122014-02-15 16:10:44 +000087 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000088 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020089}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +020090
91machine_int_t mp_obj_int_get(mp_obj_t self_in) {
92 return MP_OBJ_SMALL_INT_VALUE(self_in);
93}
94
95machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
96 return MP_OBJ_SMALL_INT_VALUE(self_in);
97}
98
Damien George5fa93b62014-01-22 14:35:10 +000099#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
100
101const mp_obj_type_t int_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000102 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000103 .name = MP_QSTR_int,
Damien George5fa93b62014-01-22 14:35:10 +0000104 .print = int_print,
105 .make_new = int_make_new,
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200106 .unary_op = int_unary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000107 .binary_op = int_binary_op,
108};