Implements list.insert. Fixes issue #61.
This commit is contained in:
parent
5d4a821339
commit
45a8744617
2
py/obj.h
2
py/obj.h
|
@ -46,6 +46,7 @@ struct _mp_obj_base_t {
|
||||||
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name}
|
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name}
|
||||||
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name}
|
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name}
|
||||||
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name}
|
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name}
|
||||||
|
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, fun_name}
|
||||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name}
|
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name}
|
||||||
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name}
|
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name}
|
||||||
|
|
||||||
|
@ -54,6 +55,7 @@ struct _mp_obj_base_t {
|
||||||
typedef mp_obj_t (*mp_fun_0_t)(void);
|
typedef mp_obj_t (*mp_fun_0_t)(void);
|
||||||
typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
|
typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
|
||||||
typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
|
typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
|
||||||
|
typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
|
||||||
typedef mp_obj_t (*mp_fun_t)(void);
|
typedef mp_obj_t (*mp_fun_t)(void);
|
||||||
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
|
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
|
||||||
|
|
||||||
|
|
12
py/objfun.c
12
py/objfun.c
|
@ -38,6 +38,9 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
||||||
case 2:
|
case 2:
|
||||||
return ((mp_fun_2_t)self->fun)(args[1], args[0]);
|
return ((mp_fun_2_t)self->fun)(args[1], args[0]);
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
return ((mp_fun_3_t)self->fun)(args[2], args[1], args[0]);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
|
@ -106,6 +109,15 @@ mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
|
||||||
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
||||||
|
o->base.type = &fun_native_type;
|
||||||
|
o->n_args_min = 3;
|
||||||
|
o->n_args_max = 3;
|
||||||
|
o->fun = fun;
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
|
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
|
||||||
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
||||||
o->base.type = &fun_native_type;
|
o->base.type = &fun_native_type;
|
||||||
|
|
27
py/objlist.c
27
py/objlist.c
|
@ -173,11 +173,37 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
|
||||||
nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list."));
|
nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
|
||||||
|
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
|
||||||
|
mp_obj_list_t *self = self_in;
|
||||||
|
// insert has its own strange index logic
|
||||||
|
int index = MP_OBJ_SMALL_INT_VALUE(idx);
|
||||||
|
if (index < 0) {
|
||||||
|
index += self->len;
|
||||||
|
}
|
||||||
|
if (index < 0) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
if (index > self->len) {
|
||||||
|
index = self->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
mp_obj_list_append(self_in, mp_const_none);
|
||||||
|
|
||||||
|
for (int i = self->len-1; i > index; i--) {
|
||||||
|
self->items[i] = self->items[i-1];
|
||||||
|
}
|
||||||
|
self->items[index] = obj;
|
||||||
|
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
|
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
|
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
|
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
|
||||||
|
static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
|
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
|
||||||
|
|
||||||
|
@ -196,6 +222,7 @@ const mp_obj_type_t list_type = {
|
||||||
{ "copy", &list_copy_obj },
|
{ "copy", &list_copy_obj },
|
||||||
{ "count", &list_count_obj },
|
{ "count", &list_count_obj },
|
||||||
{ "index", &list_index_obj },
|
{ "index", &list_index_obj },
|
||||||
|
{ "insert", &list_insert_obj },
|
||||||
{ "pop", &list_pop_obj },
|
{ "pop", &list_pop_obj },
|
||||||
{ "sort", &list_sort_obj },
|
{ "sort", &list_sort_obj },
|
||||||
{ NULL, NULL }, // end-of-list sentinel
|
{ NULL, NULL }, // end-of-list sentinel
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
a = [1, 2, 3]
|
||||||
|
a.insert(1, 42)
|
||||||
|
print(a)
|
||||||
|
a.insert(-1, -1)
|
||||||
|
print(a)
|
||||||
|
a.insert(99, 99)
|
||||||
|
print(a)
|
||||||
|
a.insert(-99, -99)
|
||||||
|
print(a)
|
Loading…
Reference in New Issue