From 6e1e98f8648d327098a03ce8d175c9854dd06cc8 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Sat, 4 Jan 2014 02:10:29 +0000 Subject: [PATCH 1/3] Implements list.reverse; fixes issue #66 --- py/objlist.c | 16 ++++++++++++++++ tests/basics/tests/list_reverse.py | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 tests/basics/tests/list_reverse.py diff --git a/py/objlist.c b/py/objlist.c index 0a795a2b08..9b3993d95c 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -207,6 +207,20 @@ static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) { return mp_const_none; } +static mp_obj_t list_reverse(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &list_type)); + mp_obj_list_t *self = self_in; + + int len = self->len; + for (int i = 0; i < len/2; i++) { + mp_obj_t *a = self->items[i]; + self->items[i] = self->items[len-i-1]; + self->items[len-i-1] = a; + } + + 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); @@ -215,6 +229,7 @@ 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_remove_obj, list_remove); +static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse); static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); const mp_obj_type_t list_type = { @@ -235,6 +250,7 @@ const mp_obj_type_t list_type = { { "insert", &list_insert_obj }, { "pop", &list_pop_obj }, { "remove", &list_remove_obj }, + { "reverse", &list_reverse_obj }, { "sort", &list_sort_obj }, { NULL, NULL }, // end-of-list sentinel }, diff --git a/tests/basics/tests/list_reverse.py b/tests/basics/tests/list_reverse.py new file mode 100644 index 0000000000..38acf1fd61 --- /dev/null +++ b/tests/basics/tests/list_reverse.py @@ -0,0 +1,5 @@ +a = [] +for i in range(100): + a.append(i) + a.reverse() +print(a) From e5ee1693ad6238846f2ccf495b67a3dc06ffa277 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 6 Jan 2014 17:49:46 +0200 Subject: [PATCH 2/3] Use constructor to create small int (avoid exposing mp_obj_t internals to VM). --- py/vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/vm.c b/py/vm.c index 8e7ef7485b..5e3ec0baf8 100644 --- a/py/vm.c +++ b/py/vm.c @@ -106,7 +106,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** case MP_BC_LOAD_CONST_SMALL_INT: unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000; ip += 3; - PUSH((mp_obj_t)(unum << 1 | 1)); + PUSH(MP_OBJ_NEW_SMALL_INT(unum)); break; case MP_BC_LOAD_CONST_DEC: From fe039b4f4fa70ce9193cbc5389203186eea96537 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 6 Jan 2014 17:49:21 +0200 Subject: [PATCH 3/3] Typo fix in comment. --- py/emit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/emit.h b/py/emit.h index ea65731038..66e87fd4c6 100644 --- a/py/emit.h +++ b/py/emit.h @@ -5,7 +5,7 @@ * is not known until the end of pass 1. * As a consequence, we don't know the maximum stack size until the end of pass 2. * This is problematic for some emitters (x64) since they need to know the maximum - * stack size to compile the entry to the function, and this effects code size. + * stack size to compile the entry to the function, and this affects code size. */ typedef enum {