Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | 66eaf84 | 2014-03-26 19:27:58 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 30 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 31 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 32 | #include "qstr.h" |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 33 | #include "obj.h" |
| 34 | #include "runtime.h" |
| 35 | |
| 36 | typedef struct _mp_obj_enumerate_t { |
| 37 | mp_obj_base_t base; |
| 38 | mp_obj_t iter; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 39 | mp_int_t cur; |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 40 | } mp_obj_enumerate_t; |
| 41 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 42 | STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in); |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 43 | |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 44 | STATIC const mp_arg_t enumerate_make_new_args[] = { |
| 45 | { MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 46 | { MP_QSTR_start, MP_ARG_INT, {.u_int = 0} }, |
| 47 | }; |
Emmanuel Blot | f6932d6 | 2014-06-19 18:54:34 +0200 | [diff] [blame] | 48 | #define ENUMERATE_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(enumerate_make_new_args) |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 49 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 50 | STATIC mp_obj_t enumerate_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
Paul Sokolovsky | 47d3bd3 | 2014-05-06 19:25:25 +0300 | [diff] [blame] | 51 | #if MICROPY_CPYTHON_COMPAT |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 52 | // parse args |
| 53 | mp_arg_val_t vals[ENUMERATE_MAKE_NEW_NUM_ARGS]; |
| 54 | mp_arg_parse_all_kw_array(n_args, n_kw, args, ENUMERATE_MAKE_NEW_NUM_ARGS, enumerate_make_new_args, vals); |
| 55 | |
| 56 | // create enumerate object |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 57 | mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 58 | o->base.type = &mp_type_enumerate; |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 59 | o->iter = mp_getiter(vals[0].u_obj); |
| 60 | o->cur = vals[1].u_int; |
Damien George | c53b408 | 2014-05-06 16:52:35 +0000 | [diff] [blame] | 61 | #else |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 62 | mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); |
| 63 | o->base.type = &mp_type_enumerate; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 64 | o->iter = mp_getiter(args[0]); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 65 | o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0; |
Damien George | c53b408 | 2014-05-06 16:52:35 +0000 | [diff] [blame] | 66 | #endif |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 67 | |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 68 | return o; |
| 69 | } |
| 70 | |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 71 | const mp_obj_type_t mp_type_enumerate = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 72 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 73 | .name = MP_QSTR_enumerate, |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 74 | .make_new = enumerate_make_new, |
| 75 | .iternext = enumerate_iternext, |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 76 | .getiter = mp_identity, |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 79 | STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) { |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 80 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_enumerate)); |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 81 | mp_obj_enumerate_t *self = self_in; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 82 | mp_obj_t next = mp_iternext(self->iter); |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 83 | if (next == MP_OBJ_STOP_ITERATION) { |
| 84 | return MP_OBJ_STOP_ITERATION; |
John R. Lenton | 9daa789 | 2014-01-14 23:55:01 +0000 | [diff] [blame] | 85 | } else { |
| 86 | mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(self->cur++), next}; |
| 87 | return mp_obj_new_tuple(2, items); |
| 88 | } |
| 89 | } |