blob: 26d3c0e337185f99e20c2d9a82a388624203336c [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"
11
12typedef struct _mp_obj_int_t {
13 mp_obj_base_t base;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020014#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
15 mp_longint_impl_t val;
16#endif
Damien George45b43c22014-01-05 01:50:45 +000017} mp_obj_int_t;
18
Paul Sokolovsky48b35722014-01-12 17:30:48 +020019void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
20mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
21
22// This dispatcher function is expected to be independent of the implementation
23// of long int
Damien George45b43c22014-01-05 01:50:45 +000024static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
25 switch (n_args) {
26 case 0:
27 return MP_OBJ_NEW_SMALL_INT(0);
28
29 case 1:
30 // TODO allow string as arg and parse it
Paul Sokolovsky48b35722014-01-12 17:30:48 +020031 return mp_obj_new_int(mp_obj_get_int(args[0]));
Damien George45b43c22014-01-05 01:50:45 +000032
33 //case 2:
34 // TODO, parse with given base
35
36 default:
37 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));
38 }
39}
40
41const mp_obj_type_t int_type = {
42 { &mp_const_type },
43 "int",
Paul Sokolovsky48b35722014-01-12 17:30:48 +020044 .print = int_print,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020045 .make_new = int_make_new,
Paul Sokolovsky48b35722014-01-12 17:30:48 +020046 .binary_op = int_binary_op,
Damien George45b43c22014-01-05 01:50:45 +000047};
48
Paul Sokolovsky48b35722014-01-12 17:30:48 +020049#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
50// This is called only for non-SMALL_INT
51void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
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
55mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
56 assert(0);
57}
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);
62}
63
64mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
65 // SMALL_INT accepts only signed numbers, of one bit less size
66 // then word size, which totals 2 bits less for unsigned numbers.
67 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
68 return MP_OBJ_NEW_SMALL_INT(value);
69 }
70 // TODO: Raise exception
71 assert(0);
72}
73
74mp_obj_t mp_obj_new_int(machine_int_t value) {
75 if (MP_OBJ_FITS_SMALL_INT(value)) {
76 return MP_OBJ_NEW_SMALL_INT(value);
77 }
78 // TODO: Raise exception
79 assert(0);
80}
81#endif