From 07205ec323ca98d1c23af51e099119954d0fe7a4 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 02:31:00 +0000 Subject: added zip() --- py/runtime.c | 1 + 1 file changed, 1 insertion(+) (limited to 'py/runtime.c') diff --git a/py/runtime.c b/py/runtime.c index 2af86b6abd..02f965dd63 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -113,6 +113,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type); mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type); mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type); + mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type); // built-in user functions; TODO covert all to &mp_builtin_xxx's mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs)); -- cgit v1.2.3 From 5c7683955994b965270598559e979c581ddc1950 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 05:12:50 +0000 Subject: sorted --- py/builtin.c | 26 ++++++++++++++++++++++++++ py/builtin.h | 1 + py/mpqstrraw.h | 1 + py/objlist.c | 3 ++- py/runtime.c | 1 + py/vm.c | 15 +++++++++++---- tests/basics/tests/sorted.py | 2 ++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/basics/tests/sorted.py (limited to 'py/runtime.c') diff --git a/py/builtin.c b/py/builtin.c index 078f4b49c3..f73d41b0d8 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -296,3 +296,29 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } + +extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs); +static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { + mp_obj_t *args_items = NULL; + uint args_len = 0; + + assert(MP_OBJ_IS_TYPE(args, &tuple_type)); + mp_obj_tuple_get(args, &args_len, &args_items); + assert(args_len >= 1); + if (args_len > 1) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, + "must use keyword argument for key function")); + } + mp_obj_t iterable = rt_getiter(args_items[0]); + mp_obj_t self = rt_build_list(0, NULL); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + rt_list_append(self, item); + } + + mp_obj_t new_args = rt_build_tuple(1, &self); + list_sort(new_args, kwargs); + + return self; +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted); diff --git a/py/builtin.h b/py/builtin.h index db7d517a06..a69f1166ac 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -23,3 +23,4 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj); diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index e33194fd45..c3cda84b4d 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -57,6 +57,7 @@ Q(pow) Q(print) Q(range) Q(set) +Q(sorted) Q(sum) Q(tuple) Q(type) diff --git a/py/objlist.c b/py/objlist.c index fa8ec67d09..d5ea7f47b2 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r } } -static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) { +mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) { mp_obj_t *args_items = NULL; uint args_len = 0; assert(MP_OBJ_IS_TYPE(args, &tuple_type)); mp_obj_tuple_get(args, &args_len, &args_items); assert(args_len >= 1); + assert(MP_OBJ_IS_TYPE(args_items[0], &list_type)); if (args_len > 1) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "list.sort takes no positional arguments")); diff --git a/py/runtime.c b/py/runtime.c index 02f965dd63..9815452fc3 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -134,6 +134,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow)); mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print)); mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range)); + mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum)); next_unique_code_id = 1; // 0 indicates "no code" diff --git a/py/vm.c b/py/vm.c index baef6988c5..5c74b43caa 100644 --- a/py/vm.c +++ b/py/vm.c @@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** case MP_BC_CALL_FUNCTION: DECODE_UINT; - assert((unum & 0xff00) == 0); // n_keyword - unum &= 0xff; // n_positional - sp += unum; - *sp = rt_call_function_n(*sp, unum, sp - unum); + if ((unum & 0xff00) == 0) { + // no keywords + unum &= 0xff; // n_positional + sp += unum; + *sp = rt_call_function_n(*sp, unum, sp - unum); + } else { + // keywords + int argsize = (unum & 0xff) + ((unum >> 7) & 0x1fe); + sp += argsize; + *sp = rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp - argsize); + } break; case MP_BC_CALL_METHOD: diff --git a/tests/basics/tests/sorted.py b/tests/basics/tests/sorted.py new file mode 100644 index 0000000000..bbec319460 --- /dev/null +++ b/tests/basics/tests/sorted.py @@ -0,0 +1,2 @@ +print(sorted(set(range(100)))) +print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2))) -- cgit v1.2.3