blob: 02628b7ef9dad8bf6da1cc66505535910053d7c1 [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 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) {
Paul Sokolovsky166bb402014-01-18 12:46:43 +020061 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "long int not supported in this build"));
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 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020071 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000072 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020073}
74
75mp_obj_t mp_obj_new_int(machine_int_t value) {
76 if (MP_OBJ_FITS_SMALL_INT(value)) {
77 return MP_OBJ_NEW_SMALL_INT(value);
78 }
Paul Sokolovsky166bb402014-01-18 12:46:43 +020079 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000080 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020081}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +020082
83machine_int_t mp_obj_int_get(mp_obj_t self_in) {
84 return MP_OBJ_SMALL_INT_VALUE(self_in);
85}
86
87machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
88 return MP_OBJ_SMALL_INT_VALUE(self_in);
89}
90
Paul Sokolovsky48b35722014-01-12 17:30:48 +020091#endif