blob: 477b8aa52bf578a745178edde0f4c67efb5fb21c [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"
9#include "mpqstr.h"
10#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 George5573f9f2014-01-15 22:58:39 +000023 if (MP_OBJ_IS_TYPE(args[0], &str_type)) {
24 // a string, parse it
25 return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), 0));
26 } else {
27 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
28 }
Damien George45b43c22014-01-05 01:50:45 +000029
xybc178ea42014-01-14 21:39:05 +080030 case 2:
Damien George5573f9f2014-01-15 22:58:39 +000031 // should be a string, parse it
32 // TODO proper error checking of argument types
Damien George20006db2014-01-18 14:10:48 +000033 return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), mp_obj_get_int(args[1])));
Damien George45b43c22014-01-05 01:50:45 +000034
35 default:
36 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));
37 }
38}
39
40const mp_obj_type_t int_type = {
41 { &mp_const_type },
42 "int",
Paul Sokolovsky48b35722014-01-12 17:30:48 +020043 .print = int_print,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020044 .make_new = int_make_new,
Paul Sokolovsky48b35722014-01-12 17:30:48 +020045 .binary_op = int_binary_op,
Damien George45b43c22014-01-05 01:50:45 +000046};
47
Paul Sokolovsky48b35722014-01-12 17:30:48 +020048#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
49// This is called only for non-SMALL_INT
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 George45b43c22014-01-05 01:50:45 +000051}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020052
53// This is called only for non-SMALL_INT
54mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
55 assert(0);
Damien George23005372014-01-13 19:39:01 +000056 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020057}
58
59// This is called only with strings whose value doesn't fit in SMALL_INT
60mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
61 assert(0);
Damien George23005372014-01-13 19:39:01 +000062 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020063}
64
65mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
66 // SMALL_INT accepts only signed numbers, of one bit less size
67 // then word size, which totals 2 bits less for unsigned numbers.
68 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
69 return MP_OBJ_NEW_SMALL_INT(value);
70 }
71 // TODO: Raise exception
72 assert(0);
Damien George23005372014-01-13 19:39:01 +000073 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020074}
75
76mp_obj_t mp_obj_new_int(machine_int_t value) {
77 if (MP_OBJ_FITS_SMALL_INT(value)) {
78 return MP_OBJ_NEW_SMALL_INT(value);
79 }
80 // TODO: Raise exception
81 assert(0);
Damien George23005372014-01-13 19:39:01 +000082 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020083}
84#endif