Convert Python types to proper Python type hierarchy.

Now much more inline with how CPython does types.
This commit is contained in:
Damien George
2014-01-04 20:21:15 +00:00
parent e9906ac3d7
commit 71c5181a8d
36 changed files with 285 additions and 173 deletions

View File

@@ -36,6 +36,29 @@ static void list_print(void (*print)(void *env, const char *fmt, ...), void *env
print(env, "]");
}
static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
switch (n_args) {
case 0:
// return a new, empty list
return rt_build_list(0, NULL);
case 1:
{
// make list from iterable
mp_obj_t iterable = rt_getiter(args[0]);
mp_obj_t list = rt_build_list(0, NULL);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
rt_list_append(list, item);
}
return list;
}
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));
}
}
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_list_t *o = lhs;
switch (op) {
@@ -165,6 +188,7 @@ const mp_obj_type_t list_type = {
{ &mp_const_type },
"list",
list_print, // print
list_make_new, // make_new
NULL, // call_n
NULL, // unary_op
list_binary_op, // binary_op
@@ -242,6 +266,7 @@ static const mp_obj_type_t list_it_type = {
{ &mp_const_type },
"list_iterator",
NULL, // print
NULL, // make_new
NULL, // call_n
NULL, // unary_op
NULL, // binary_op