summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/obj.h2
-rw-r--r--py/objfun.c12
-rw-r--r--py/objlist.c72
3 files changed, 86 insertions, 0 deletions
diff --git a/py/obj.h b/py/obj.h
index 16c7c36dd1..b39b84239f 100644
--- a/py/obj.h
+++ b/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_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_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_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_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_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_var_t)(int n, const mp_obj_t *);
diff --git a/py/objfun.c b/py/objfun.c
index e998bd28d2..9601622d89 100644
--- a/py/objfun.c
+++ b/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:
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:
assert(0);
return mp_const_none;
@@ -106,6 +109,15 @@ mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
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_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
diff --git a/py/objlist.c b/py/objlist.c
index 28b373b782..f5299ddc86 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -134,8 +134,76 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
return mp_const_none;
}
+static mp_obj_t list_copy(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &list_type));
+ mp_obj_list_t *self = self_in;
+ return mp_obj_new_list(self->len, self->items);
+}
+
+static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
+ assert(MP_OBJ_IS_TYPE(self_in, &list_type));
+ mp_obj_list_t *self = self_in;
+ int count = 0;
+ for (int i = 0; i < self->len; i++) {
+ if (mp_obj_equal(self->items[i], value)) {
+ count++;
+ }
+ }
+
+ return mp_obj_new_int(count);
+}
+
+static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
+ assert(2 <= n_args && n_args <= 4);
+ assert(MP_OBJ_IS_TYPE(args[0], &list_type));
+ mp_obj_list_t *self = args[0];
+ mp_obj_t *value = args[1];
+
+ uint start = mp_get_index(self->base.type, self->len,
+ n_args >= 3 ? args[2] : mp_obj_new_int(0));
+ uint stop = mp_get_index(self->base.type, self->len,
+ n_args >= 4 ? args[3] : mp_obj_new_int(-1));
+
+ for (uint i = start; i <= stop; i++) {
+ if (mp_obj_equal(self->items[i], value)) {
+ return mp_obj_new_int(i);
+ }
+ }
+
+ 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_1(list_clear_obj, list_clear);
+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_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_2(list_sort_obj, list_sort);
@@ -151,6 +219,10 @@ const mp_obj_type_t list_type = {
{ // method list
{ "append", &list_append_obj },
{ "clear", &list_clear_obj },
+ { "copy", &list_copy_obj },
+ { "count", &list_count_obj },
+ { "index", &list_index_obj },
+ { "insert", &list_insert_obj },
{ "pop", &list_pop_obj },
{ "sort", &list_sort_obj },
{ NULL, NULL }, // end-of-list sentinel