Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 1 | #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" |
| 11 | |
| 12 | typedef struct _mp_obj_int_t { |
| 13 | mp_obj_base_t base; |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 14 | #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 15 | mp_longint_impl_t val; |
| 16 | #endif |
Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 17 | } mp_obj_int_t; |
| 18 | |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 19 | void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in); |
| 20 | mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in); |
| 21 | |
| 22 | // This dispatcher function is expected to be independent of the implementation |
| 23 | // of long int |
Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 24 | static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { |
| 25 | switch (n_args) { |
| 26 | case 0: |
| 27 | return MP_OBJ_NEW_SMALL_INT(0); |
| 28 | |
| 29 | case 1: |
| 30 | // TODO allow string as arg and parse it |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 31 | return mp_obj_new_int(mp_obj_get_int(args[0])); |
Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 32 | |
| 33 | //case 2: |
| 34 | // TODO, parse with given base |
| 35 | |
| 36 | default: |
| 37 | 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)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const mp_obj_type_t int_type = { |
| 42 | { &mp_const_type }, |
| 43 | "int", |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 44 | .print = int_print, |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 45 | .make_new = int_make_new, |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 46 | .binary_op = int_binary_op, |
Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 49 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE |
| 50 | // This is called only for non-SMALL_INT |
| 51 | void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { |
Damien George | 45b43c2 | 2014-01-05 01:50:45 +0000 | [diff] [blame] | 52 | } |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 53 | |
| 54 | // This is called only for non-SMALL_INT |
| 55 | mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| 56 | assert(0); |
Damien George | 2300537 | 2014-01-13 19:39:01 +0000 | [diff] [blame^] | 57 | return mp_const_none; |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // This is called only with strings whose value doesn't fit in SMALL_INT |
| 61 | mp_obj_t mp_obj_new_int_from_long_str(const char *s) { |
| 62 | assert(0); |
Damien George | 2300537 | 2014-01-13 19:39:01 +0000 | [diff] [blame^] | 63 | return mp_const_none; |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { |
| 67 | // SMALL_INT accepts only signed numbers, of one bit less size |
| 68 | // then word size, which totals 2 bits less for unsigned numbers. |
| 69 | if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { |
| 70 | return MP_OBJ_NEW_SMALL_INT(value); |
| 71 | } |
| 72 | // TODO: Raise exception |
| 73 | assert(0); |
Damien George | 2300537 | 2014-01-13 19:39:01 +0000 | [diff] [blame^] | 74 | return mp_const_none; |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | mp_obj_t mp_obj_new_int(machine_int_t value) { |
| 78 | if (MP_OBJ_FITS_SMALL_INT(value)) { |
| 79 | return MP_OBJ_NEW_SMALL_INT(value); |
| 80 | } |
| 81 | // TODO: Raise exception |
| 82 | assert(0); |
Damien George | 2300537 | 2014-01-13 19:39:01 +0000 | [diff] [blame^] | 83 | return mp_const_none; |
Paul Sokolovsky | 48b3572 | 2014-01-12 17:30:48 +0200 | [diff] [blame] | 84 | } |
| 85 | #endif |