blob: 1a04408afdfbcdd80a5908787d829c4b3ec39a0b [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"
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
Damien George20006db2014-01-18 14:10:48 +000015static mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
16 // 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;
26 const byte *s = mp_obj_str_get_data(args[0], &l);
27 return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, 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;
37 const byte *s = mp_obj_str_get_data(args[0], &l);
38 return MP_OBJ_NEW_SMALL_INT(strtonum((const char*)s, mp_obj_get_int(args[1])));
39 }
Damien George45b43c22014-01-05 01:50:45 +000040
41 default:
42 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));
43 }
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)) {
50 print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(self_in));
51 }
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) {
Paul Sokolovsky166bb402014-01-18 12:46:43 +020068 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_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
72mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
73 // SMALL_INT accepts only signed numbers, of one bit less size
74 // then word size, which totals 2 bits less for unsigned numbers.
75 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
76 return MP_OBJ_NEW_SMALL_INT(value);
77 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020078 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000079 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020080}
81
82mp_obj_t mp_obj_new_int(machine_int_t value) {
83 if (MP_OBJ_FITS_SMALL_INT(value)) {
84 return MP_OBJ_NEW_SMALL_INT(value);
85 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020086 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000087 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020088}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +020089
90machine_int_t mp_obj_int_get(mp_obj_t self_in) {
91 return MP_OBJ_SMALL_INT_VALUE(self_in);
92}
93
94machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
95 return MP_OBJ_SMALL_INT_VALUE(self_in);
96}
97
Damien George5fa93b62014-01-22 14:35:10 +000098#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
99
100const mp_obj_type_t int_type = {
101 { &mp_const_type },
102 "int",
103 .print = int_print,
104 .make_new = int_make_new,
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200105 .unary_op = int_unary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000106 .binary_op = int_binary_op,
107};