blob: 6e5c79017d0781938568347eafe79b672b497298 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 George66eaf842014-03-26 19:27:58 +000027#include <stdlib.h>
John R. Lenton39b174e2014-01-15 01:10:09 +000028#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damien George7a9d0c42014-01-15 22:27:16 +000031#include "nlr.h"
John R. Lenton39b174e2014-01-15 01:10:09 +000032#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
John R. Lenton39b174e2014-01-15 01:10:09 +000034#include "obj.h"
35#include "runtime.h"
36
37typedef struct _mp_obj_map_t {
38 mp_obj_base_t base;
39 machine_uint_t n_iters;
40 mp_obj_t fun;
41 mp_obj_t iters[];
42} mp_obj_map_t;
43
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020044STATIC mp_obj_t map_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000045 if (n_args < 2 || n_kw != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +010046 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "map must have at least 2 arguments and no keyword arguments"));
Damien George7a9d0c42014-01-15 22:27:16 +000047 }
John R. Lenton39b174e2014-01-15 01:10:09 +000048 assert(n_args >= 2);
Damien George7a9d0c42014-01-15 22:27:16 +000049 mp_obj_map_t *o = m_new_obj_var(mp_obj_map_t, mp_obj_t, n_args - 1);
Damien George3e1a5c12014-03-29 13:43:38 +000050 o->base.type = &mp_type_map;
John R. Lenton39b174e2014-01-15 01:10:09 +000051 o->n_iters = n_args - 1;
Damien George20006db2014-01-18 14:10:48 +000052 o->fun = args[0];
John R. Lenton39b174e2014-01-15 01:10:09 +000053 for (int i = 0; i < n_args - 1; i++) {
Damien Georged17926d2014-03-30 13:35:08 +010054 o->iters[i] = mp_getiter(args[i + 1]);
John R. Lenton39b174e2014-01-15 01:10:09 +000055 }
56 return o;
57}
58
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020059STATIC mp_obj_t map_iternext(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000060 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_map));
John R. Lenton39b174e2014-01-15 01:10:09 +000061 mp_obj_map_t *self = self_in;
62 mp_obj_t *nextses = m_new(mp_obj_t, self->n_iters);
63
64 for (int i = 0; i < self->n_iters; i++) {
Damien Georged17926d2014-03-30 13:35:08 +010065 mp_obj_t next = mp_iternext(self->iters[i]);
Damien Georgeea8d06c2014-04-17 23:19:36 +010066 if (next == MP_OBJ_STOP_ITERATION) {
John R. Lenton39b174e2014-01-15 01:10:09 +000067 m_del(mp_obj_t, nextses, self->n_iters);
Damien Georgeea8d06c2014-04-17 23:19:36 +010068 return MP_OBJ_STOP_ITERATION;
John R. Lenton39b174e2014-01-15 01:10:09 +000069 }
70 nextses[i] = next;
71 }
Damien Georged17926d2014-03-30 13:35:08 +010072 return mp_call_function_n_kw(self->fun, self->n_iters, 0, nextses);
John R. Lenton39b174e2014-01-15 01:10:09 +000073}
74
Damien George3e1a5c12014-03-29 13:43:38 +000075const mp_obj_type_t mp_type_map = {
Damien Georgec5966122014-02-15 16:10:44 +000076 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000077 .name = MP_QSTR_map,
John R. Lenton39b174e2014-01-15 01:10:09 +000078 .make_new = map_make_new,
Paul Sokolovskybf271402014-05-17 11:18:23 +030079 .getiter = mp_identity,
John R. Lenton39b174e2014-01-15 01:10:09 +000080 .iternext = map_iternext,
81};