Skip to content

Commit 45b43c2

Browse files
committed
Oops: add objint.c
1 parent 8b2688f commit 45b43c2

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

py/objint.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#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+
12+
typedef struct _mp_obj_int_t {
13+
mp_obj_base_t base;
14+
} mp_obj_int_t;
15+
16+
static 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+
25+
//case 2:
26+
// TODO, parse with given base
27+
28+
default:
29+
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));
30+
}
31+
}
32+
33+
const mp_obj_type_t int_type = {
34+
{ &mp_const_type },
35+
"int",
36+
NULL,
37+
int_make_new, // make_new
38+
NULL, // call_n
39+
NULL, // unary_op
40+
NULL, // binary_op
41+
NULL, // getiter
42+
NULL, // iternext
43+
{ { NULL, NULL }, }, // method list
44+
};
45+
46+
mp_obj_t mp_obj_new_int(machine_int_t value) {
47+
return MP_OBJ_NEW_SMALL_INT(value);
48+
}

0 commit comments

Comments
 (0)