objint.c 2.34 KB
Newer Older
Damien George's avatar
Damien George committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "mpqstr.h"
#include "obj.h"

typedef struct _mp_obj_int_t {
    mp_obj_base_t base;
14
15
16
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
    mp_longint_impl_t val;
#endif
Damien George's avatar
Damien George committed
17
18
} mp_obj_int_t;

19
20
21
22
23
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);

// This dispatcher function is expected to be independent of the implementation
// of long int
Damien George's avatar
Damien George committed
24
25
26
27
28
29
30
static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
    switch (n_args) {
        case 0:
            return MP_OBJ_NEW_SMALL_INT(0);

        case 1:
            // TODO allow string as arg and parse it
31
            return mp_obj_new_int(mp_obj_get_int(args[0]));
Damien George's avatar
Damien George committed
32
33
34
35
36
37
38
39
40
41
42
43

        //case 2:
            // TODO, parse with given base

        default:
            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));
    }
}

const mp_obj_type_t int_type = {
    { &mp_const_type },
    "int",
44
    .print = int_print,
45
    .make_new = int_make_new,
46
    .binary_op = int_binary_op,
Damien George's avatar
Damien George committed
47
48
};

49
50
51
52
53
54
55
56
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
// This is called only for non-SMALL_INT
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
}

// 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);
57
    return mp_const_none;
58
59
60
61
62
}

// 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) {
    assert(0);
63
    return mp_const_none;
64
65
66
67
68
69
70
71
72
73
}

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);
    }
    // TODO: Raise exception
    assert(0);
74
    return mp_const_none;
75
76
}

Damien George's avatar
Damien George committed
77
mp_obj_t mp_obj_new_int(machine_int_t value) {
78
79
80
81
82
    if (MP_OBJ_FITS_SMALL_INT(value)) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
    // TODO: Raise exception
    assert(0);
83
    return mp_const_none;
Damien George's avatar
Damien George committed
84
}
85
#endif