blob: 58fc37b3bbb97b6cdf8f5886941d0775bcd67b96 [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;
14} mp_obj_int_t;
15
16static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
17 switch (n_args) {
18 case 0:
19 return MP_OBJ_NEW_SMALL_INT(0);
20
21 case 1:
22 // TODO allow string as arg and parse it
23 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
24
xybc178ea42014-01-14 21:39:05 +080025 case 2:
26 // TODO make args[0] and args[1] correct
27 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_base(args[0], args[1]));
Damien George45b43c22014-01-05 01:50:45 +000028
29 default:
30 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));
31 }
32}
33
34const mp_obj_type_t int_type = {
35 { &mp_const_type },
36 "int",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020037 .make_new = int_make_new,
Damien George45b43c22014-01-05 01:50:45 +000038};
39
40mp_obj_t mp_obj_new_int(machine_int_t value) {
41 return MP_OBJ_NEW_SMALL_INT(value);
42}