objint.c 3.22 KB
Newer Older
Damien George's avatar
Damien George committed
1
2
3
4
5
6
7
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include "nlr.h"
#include "misc.h"
8
#include "strtonum.h"
Damien George's avatar
Damien George committed
9
#include "mpconfig.h"
10
#include "qstr.h"
Damien George's avatar
Damien George committed
11
#include "obj.h"
12
#include "objint.h"
13
14
15

// This dispatcher function is expected to be independent of the implementation
// of long int
16
STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
17
18
    // TODO check n_kw == 0

Damien George's avatar
Damien George committed
19
20
21
22
23
    switch (n_args) {
        case 0:
            return MP_OBJ_NEW_SMALL_INT(0);

        case 1:
24
            if (MP_OBJ_IS_STR(args[0])) {
25
                // a string, parse it
26
                uint l;
27
                const char *s = mp_obj_str_get_data(args[0], &l);
28
                return MP_OBJ_NEW_SMALL_INT(mp_strtonum(s, 0));
29
30
31
            } else {
                return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
            }
Damien George's avatar
Damien George committed
32

xyb's avatar
xyb committed
33
        case 2:
34
        {
35
36
            // should be a string, parse it
            // TODO proper error checking of argument types
37
            uint l;
38
            const char *s = mp_obj_str_get_data(args[0], &l);
39
            return MP_OBJ_NEW_SMALL_INT(mp_strtonum(s, mp_obj_get_int(args[1])));
40
        }
Damien George's avatar
Damien George committed
41
42

        default:
43
            nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
Damien George's avatar
Damien George committed
44
45
46
    }
}

47
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
48

49
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
50
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
51
        print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
52
    }
53
54
}

55
56
57
58
59
60
// This is called only for non-SMALL_INT
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
    assert(0);
    return mp_const_none;
}

61
62
63
// This is called only for non-SMALL_INT
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    assert(0);
64
    return mp_const_none;
65
66
67
68
}

// This is called only with strings whose value doesn't fit in SMALL_INT
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
69
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
70
    return mp_const_none;
71
72
73
74
75
76
77
78
}

mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
    // SMALL_INT accepts only signed numbers, of one bit less size
    // then word size, which totals 2 bits less for unsigned numbers.
    if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
79
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
80
    return mp_const_none;
81
82
}

Damien George's avatar
Damien George committed
83
mp_obj_t mp_obj_new_int(machine_int_t value) {
84
85
86
    if (MP_OBJ_FITS_SMALL_INT(value)) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
87
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
88
    return mp_const_none;
Damien George's avatar
Damien George committed
89
}
90
91
92
93
94
95
96
97
98

machine_int_t mp_obj_int_get(mp_obj_t self_in) {
    return MP_OBJ_SMALL_INT_VALUE(self_in);
}

machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
    return MP_OBJ_SMALL_INT_VALUE(self_in);
}

99
100
101
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE

const mp_obj_type_t int_type = {
102
    { &mp_type_type },
103
    .name = MP_QSTR_int,
104
105
    .print = int_print,
    .make_new = int_make_new,
106
    .unary_op = int_unary_op,
107
108
    .binary_op = int_binary_op,
};