Skip to content

Commit 71c5181

Browse files
committed
Convert Python types to proper Python type hierarchy.
Now much more inline with how CPython does types.
1 parent e9906ac commit 71c5181

36 files changed

+285
-173
lines changed

py/builtin.c

Lines changed: 1 addition & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
8888
return mp_const_false;
8989
}
9090

91-
mp_obj_t mp_builtin_bool(int n_args, const mp_obj_t *args) {
92-
switch (n_args) {
93-
case 0: return mp_const_false;
94-
case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
95-
default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args));
96-
}
97-
}
98-
9991
mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
10092
if (mp_obj_is_callable(o_in)) {
10193
return mp_const_true;
@@ -104,42 +96,6 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
10496
}
10597
}
10698

107-
#if MICROPY_ENABLE_FLOAT
108-
mp_obj_t mp_builtin_complex(int n_args, const mp_obj_t *args) {
109-
assert(0 <= n_args && n_args <= 2);
110-
111-
if (n_args == 0) {
112-
return mp_obj_new_complex(0, 0);
113-
} else if (n_args == 1) {
114-
// TODO allow string as first arg and parse it
115-
if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
116-
return args[0];
117-
} else {
118-
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
119-
}
120-
} else {
121-
mp_float_t real, imag;
122-
if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
123-
mp_obj_get_complex(args[0], &real, &imag);
124-
} else {
125-
real = mp_obj_get_float(args[0]);
126-
imag = 0;
127-
}
128-
if (MP_OBJ_IS_TYPE(args[1], &complex_type)) {
129-
mp_float_t real2, imag2;
130-
mp_obj_get_complex(args[1], &real2, &imag2);
131-
real -= imag2;
132-
imag += real2;
133-
} else {
134-
imag += mp_obj_get_float(args[1]);
135-
}
136-
return mp_obj_new_complex(real, imag);
137-
}
138-
}
139-
140-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_complex_obj, 0, 2, mp_builtin_complex);
141-
#endif
142-
14399
mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
144100
int ord = mp_obj_get_int(o_in);
145101
if (0 <= ord && ord <= 0x10ffff) {
@@ -152,11 +108,6 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
152108
}
153109
}
154110

155-
mp_obj_t mp_builtin_dict(void) {
156-
// TODO create from an iterable!
157-
return rt_build_map(0);
158-
}
159-
160111
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
161112
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
162113
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
@@ -170,49 +121,13 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
170121
}
171122
}
172123

173-
#if MICROPY_ENABLE_FLOAT
174-
static mp_obj_t mp_builtin_float(int n_args, const mp_obj_t *args) {
175-
assert(0 <= n_args && n_args <= 1);
176-
177-
if (n_args == 0) {
178-
return mp_obj_new_float(0);
179-
} else {
180-
// TODO allow string as arg and parse it
181-
if (MP_OBJ_IS_TYPE(args[0], &float_type)) {
182-
return args[0];
183-
} else {
184-
return mp_obj_new_float(mp_obj_get_float(args[0]));
185-
}
186-
}
187-
}
188-
189-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_float_obj, 0, 1, mp_builtin_float);
190-
#endif
191-
192124
static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
193125
// TODO hash will generally overflow small integer; can we safely truncate it?
194126
return mp_obj_new_int(mp_obj_hash(o_in));
195127
}
196128

197129
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
198130

199-
static mp_obj_t mp_builtin_int(int n_args, const mp_obj_t *args) {
200-
assert(0 <= n_args && n_args <= 2);
201-
202-
if (n_args == 0) {
203-
return MP_OBJ_NEW_SMALL_INT(0);
204-
} else if (n_args == 1) {
205-
// TODO if arg is a string then parse it
206-
return mp_obj_new_int(mp_obj_get_int(args[0]));
207-
} else { // n_args == 2
208-
// TODO, parse with given base
209-
assert(0);
210-
return MP_OBJ_NEW_SMALL_INT(0);
211-
}
212-
}
213-
214-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_int_obj, 0, 2, mp_builtin_int);
215-
216131
static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
217132
return rt_getiter(o_in);
218133
}
@@ -241,24 +156,6 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
241156
return MP_OBJ_NEW_SMALL_INT(len);
242157
}
243158

244-
mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args) {
245-
switch (n_args) {
246-
case 0: return rt_build_list(0, NULL);
247-
case 1:
248-
{
249-
// make list from iterable
250-
mp_obj_t iterable = rt_getiter(args[0]);
251-
mp_obj_t list = rt_build_list(0, NULL);
252-
mp_obj_t item;
253-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
254-
rt_list_append(list, item);
255-
}
256-
return list;
257-
}
258-
default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list() takes at most 1 argument (%d given)", (void*)(machine_int_t)n_args));
259-
}
260-
}
261-
262159
mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
263160
if (n_args == 1) {
264161
// given an iterable
@@ -367,26 +264,6 @@ mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
367264
}
368265
}
369266

370-
static mp_obj_t mp_builtin_set(int n_args, const mp_obj_t *args) {
371-
assert(0 <= n_args && n_args <= 1);
372-
373-
if (n_args == 0) {
374-
// return a new, empty set
375-
return mp_obj_new_set(0, NULL);
376-
} else {
377-
// 1 argument, an iterable from which we make a new set
378-
mp_obj_t set = mp_obj_new_set(0, NULL);
379-
mp_obj_t iterable = rt_getiter(args[0]);
380-
mp_obj_t item;
381-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
382-
mp_obj_set_store(set, item);
383-
}
384-
return set;
385-
}
386-
}
387-
388-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_set_obj, 0, 1, mp_builtin_set);
389-
390267
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
391268
mp_obj_t value;
392269
switch (n_args) {
@@ -405,8 +282,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
405282
static mp_obj_t mp_builtin_type(mp_obj_t o_in) {
406283
// TODO implement the 3 argument version of type()
407284
if (MP_OBJ_IS_SMALL_INT(o_in)) {
408-
// TODO implement int-type
409-
return mp_const_none;
285+
return (mp_obj_t)&int_type;
410286
} else {
411287
mp_obj_base_t *o = o_in;
412288
return (mp_obj_t)o->type;

py/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13161316
void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13171317
int l_end = comp_next_label(comp);
13181318
c_if_cond(comp, pns->nodes[0], true, l_end);
1319-
EMIT(load_id, MP_QSTR_assertion_error);
1319+
EMIT(load_id, MP_QSTR_AssertionError);
13201320
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
13211321
// assertion message
13221322
compile_node(comp, pns->nodes[1]);

py/emitpass1.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10+
#include "mpqstr.h"
1011
#include "lexer.h"
1112
#include "parse.h"
1213
#include "scope.h"
@@ -44,9 +45,9 @@ static void emit_pass1_load_id(emit_t *emit, qstr qstr) {
4445
bool added;
4546
id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
4647
if (added) {
47-
if (strcmp(qstr_str(qstr), "AssertionError") == 0) {
48-
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
48+
if (qstr == MP_QSTR_AssertionError) {
4949
// TODO how much of a hack is this?
50+
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
5051
} else if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) {
5152
// special case, super is a global, and also counts as use of __class__
5253
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;

py/mpqstrraw.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Q(__next__)
1313
Q(__qualname__)
1414
Q(__repl_print__)
1515

16-
Q(assertion_error)
1716
Q(micropython)
1817
Q(byte_code)
1918
Q(native)
@@ -23,12 +22,13 @@ Q(asm_thumb)
2322
Q(Ellipsis)
2423
Q(StopIteration)
2524

25+
Q(AssertionError)
2626
Q(AttributeError)
2727
Q(IndexError)
2828
Q(KeyError)
2929
Q(NameError)
30-
Q(TypeError)
3130
Q(SyntaxError)
31+
Q(TypeError)
3232
Q(ValueError)
3333

3434
Q(abs)
@@ -55,6 +55,7 @@ Q(print)
5555
Q(range)
5656
Q(set)
5757
Q(sum)
58+
Q(tuple)
5859
Q(type)
5960

6061
Q(append)

py/obj.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#include "runtime.h"
1414
#include "map.h"
1515

16-
mp_obj_t mp_obj_new_int(machine_int_t value) {
17-
return MP_OBJ_NEW_SMALL_INT(value);
18-
}
19-
2016
const char *mp_obj_get_type_str(mp_obj_t o_in) {
2117
if (MP_OBJ_IS_SMALL_INT(o_in)) {
2218
return "int";
@@ -128,9 +124,13 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) {
128124
return 1;
129125
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
130126
return MP_OBJ_SMALL_INT_VALUE(arg);
127+
#if MICROPY_ENABLE_FLOAT
128+
} else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
129+
// TODO work out if this should be floor, ceil or trunc
130+
return (machine_int_t)mp_obj_float_get(arg);
131+
#endif
131132
} else {
132-
assert(0);
133-
return 0;
133+
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
134134
}
135135
}
136136

py/obj.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef mp_obj_t (*mp_fun_t)(void);
5858
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
5959

6060
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
61+
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
6162
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
6263
typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
6364
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
@@ -71,6 +72,7 @@ struct _mp_obj_type_t {
7172
mp_obj_base_t base;
7273
const char *name;
7374
mp_print_fun_t print;
75+
mp_make_new_fun_t make_new; // to make an instance of the type
7476

7577
mp_call_n_fun_t call_n;
7678
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
@@ -110,6 +112,7 @@ extern const mp_obj_type_t mp_const_type;
110112
extern const mp_obj_t mp_const_none;
111113
extern const mp_obj_t mp_const_false;
112114
extern const mp_obj_t mp_const_true;
115+
extern const mp_obj_t mp_const_empty_tuple;
113116
extern const mp_obj_t mp_const_ellipsis;
114117
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
115118

@@ -180,6 +183,9 @@ extern const mp_obj_type_t bool_type;
180183
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
181184
void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
182185

186+
// int
187+
extern const mp_obj_type_t int_type;
188+
183189
// exception
184190
extern const mp_obj_type_t exception_type;
185191
qstr mp_obj_exception_get_type(mp_obj_t self_in);
@@ -214,6 +220,7 @@ uint mp_obj_dict_len(mp_obj_t self_in);
214220
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
215221

216222
// set
223+
extern const mp_obj_type_t set_type;
217224
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
218225

219226
// slice

py/objbool.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
#include "nlr.h"
55
#include "misc.h"
66
#include "mpconfig.h"
7+
#include "mpqstr.h"
78
#include "obj.h"
9+
#include "runtime.h"
810

911
typedef struct _mp_obj_bool_t {
1012
mp_obj_base_t base;
1113
bool value;
1214
} mp_obj_bool_t;
1315

14-
void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
16+
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
1517
mp_obj_bool_t *self = self_in;
1618
if (self->value) {
1719
print(env, "True");
@@ -20,10 +22,20 @@ void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ob
2022
}
2123
}
2224

25+
// args are reverse in the array
26+
static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
27+
switch (n_args) {
28+
case 0: return mp_const_false;
29+
case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
30+
default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
31+
}
32+
}
33+
2334
const mp_obj_type_t bool_type = {
2435
{ &mp_const_type },
2536
"bool",
2637
bool_print, // print
38+
bool_make_new, // make_new
2739
NULL, // call_n
2840
NULL, // unary_op
2941
NULL, // binary_op

py/objboundmeth.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const mp_obj_type_t bound_meth_type = {
3737
{ &mp_const_type },
3838
"bound_method",
3939
NULL, // print
40+
NULL, // make_new
4041
bound_meth_call_n, // call_n
4142
NULL, // unary_op
4243
NULL, // binary_op

py/objcell.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const mp_obj_type_t cell_type = {
2727
{ &mp_const_type },
2828
"cell",
2929
NULL, // print
30+
NULL, // make_new
3031
NULL, // call_n
3132
NULL, // unary_op
3233
NULL, // binary_op

py/objclass.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const mp_obj_type_t class_type = {
6464
{ &mp_const_type },
6565
"class",
6666
NULL, // print
67+
NULL, // make_new
6768
class_call_n, // call_n
6869
NULL, // unary_op
6970
NULL, // binary_op

0 commit comments

Comments
 (0)