blob: 9d4ebb6279dcb44d048bb8aaf3fdd3739a0d7072 [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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +000028#include <string.h>
29#include <assert.h>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "obj.h"
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030036#include "objtuple.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "runtime0.h"
38#include "runtime.h"
Paul Sokolovsky68e7c512014-04-13 11:53:15 +030039#include "builtin.h"
Damiend99b0522013-12-21 18:17:45 +000040
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020041STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
42STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
Paul Sokolovskyea85a122014-04-06 20:08:56 +030043STATIC mp_obj_t dict_copy(mp_obj_t self_in);
John R. Lentona41fe312014-01-06 17:17:02 +000044
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020045STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000046 mp_obj_dict_t *self = self_in;
47 bool first = true;
48 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000049 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
50 mp_map_elem_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +010051 while ((next = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentona41fe312014-01-06 17:17:02 +000052 if (!first) {
53 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000054 }
John R. Lentona41fe312014-01-06 17:17:02 +000055 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020056 mp_obj_print_helper(print, env, next->key, PRINT_REPR);
John R. Lentona41fe312014-01-06 17:17:02 +000057 print(env, ": ");
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020058 mp_obj_print_helper(print, env, next->value, PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000059 }
60 print(env, "}");
61}
62
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020063STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georged7aadcf2014-04-04 15:08:00 +010064 mp_obj_t dict;
65 switch (n_args) {
66 case 0:
67 dict = mp_obj_new_dict(0);
68 break;
69
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030070 case 1: {
Paul Sokolovskyea85a122014-04-06 20:08:56 +030071 if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
72 return dict_copy(args[0]);
73 }
74 // TODO create dict from an arbitrary mapping!
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030075
76 // Make dict from iterable of pairs
77 mp_obj_t iterable = mp_getiter(args[0]);
78 mp_obj_t dict = mp_obj_new_dict(0);
79 // TODO: support arbitrary seq as a pair
Damien George686afc52014-04-11 09:13:30 +010080 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +010081 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George686afc52014-04-11 09:13:30 +010082 mp_obj_t *sub_items;
83 mp_obj_get_array_fixed_n(item, 2, &sub_items);
84 mp_obj_dict_store(dict, sub_items[0], sub_items[1]);
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030085 }
86 return dict;
87 }
Damien Georged7aadcf2014-04-04 15:08:00 +010088
89 default:
Damien Georgeea13f402014-04-05 18:32:08 +010090 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));
Damien Georged7aadcf2014-04-04 15:08:00 +010091 }
92
93 // add to the new dict any keyword args
94 for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) {
95 mp_obj_dict_store(dict, a[0], a[1]);
96 }
97
98 return dict;
Damien George71c51812014-01-04 20:21:15 +000099}
100
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200101STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +0000102 mp_obj_dict_t *self = self_in;
103 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100104 case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
105 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100106 default: return MP_OBJ_NOT_SUPPORTED;
Damien George4e8dc8c2014-01-27 23:15:32 +0000107 }
108}
109
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200110STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +0000111 mp_obj_dict_t *o = lhs_in;
112 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100113 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +0000114 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +0000115 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000116 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300117 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300118 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
119 mp_obj_dict_t *rhs = rhs_in;
120 if (o->map.used != rhs->map.used) {
121 return mp_const_false;
122 }
123
124 machine_uint_t size = o->map.alloc;
125 mp_map_t *map = &o->map;
126
127 for (machine_uint_t i = 0; i < size; i++) {
128 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
129 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
130 if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
131 return mp_const_false;
132 }
133 }
134 }
135 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000136 } else {
137 // dict is not equal to instance of any other type
138 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300139 }
140 }
Damiend99b0522013-12-21 18:17:45 +0000141 default:
142 // op not supported
Damien Georgeea8d06c2014-04-17 23:19:36 +0100143 return MP_OBJ_NOT_SUPPORTED;
Damiend99b0522013-12-21 18:17:45 +0000144 }
145}
146
Damien George729f7b42014-04-17 22:10:53 +0100147STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100148 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100149 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100150 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100151 return mp_const_none;
152 } else if (value == MP_OBJ_SENTINEL) {
153 // load
154 mp_obj_dict_t *self = self_in;
155 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
156 if (elem == NULL) {
157 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
158 } else {
159 return elem->value;
160 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100161 } else {
Damien George729f7b42014-04-17 22:10:53 +0100162 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100163 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100164 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100165 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100166}
John R. Lentona41fe312014-01-06 17:17:02 +0000167
168/******************************************************************************/
169/* dict iterator */
170
171typedef struct _mp_obj_dict_it_t {
172 mp_obj_base_t base;
173 mp_obj_dict_t *dict;
174 machine_uint_t cur;
175} mp_obj_dict_it_t;
176
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200177STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000178 mp_obj_dict_it_t *self = self_in;
179 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100180 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000181
182 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100183 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000184 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100185 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000186 }
187 }
188
Damien Georgeea8d06c2014-04-17 23:19:36 +0100189 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000190}
191
192mp_obj_t dict_it_iternext(mp_obj_t self_in) {
193 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
194
Damien Georgeea8d06c2014-04-17 23:19:36 +0100195 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lentona41fe312014-01-06 17:17:02 +0000196 return next->key;
197 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100198 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000199 }
200}
201
Damien George3e1a5c12014-03-29 13:43:38 +0000202STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000203 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000204 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300205 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000206 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000207};
208
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200209STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000210 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000211 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000212 o->dict = dict;
213 o->cur = cur;
214 return o;
215}
216
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200217STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000218 return mp_obj_new_dict_iterator(o_in, 0);
219}
220
221/******************************************************************************/
222/* dict methods */
223
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200224STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000225 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000226 mp_obj_dict_t *self = self_in;
227
228 mp_map_clear(&self->map);
229
230 return mp_const_none;
231}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200232STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000233
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200234STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000235 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000236 mp_obj_dict_t *self = self_in;
237 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
238 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300239 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
240 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000241 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
242 return other;
243}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200244STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000245
Damien Georgeeae16442014-01-11 19:22:29 +0000246// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200247STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000248 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100249 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000250 mp_obj_t len = mp_obj_len_maybe(iter);
251 mp_obj_t value = mp_const_none;
252 mp_obj_t next = NULL;
253 mp_obj_dict_t *self = NULL;
254
255 if (n_args > 2) {
256 value = args[2];
257 }
258
259 if (len == MP_OBJ_NULL) {
260 /* object's type doesn't have a __len__ slot */
261 self = mp_obj_new_dict(0);
262 } else {
263 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
264 }
265
Damien Georgeea8d06c2014-04-17 23:19:36 +0100266 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000267 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
268 }
269
270 return self;
271}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200272STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
273STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000274
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200275STATIC mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, mp_map_lookup_kind_t lookup_kind) {
Damien George38a2da62014-01-08 17:33:12 +0000276 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000277 mp_obj_t value;
278 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000279 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000280 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100281 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000282 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000283 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000284 }
285 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000286 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000287 }
288 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000289 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000290 }
Damien George38a2da62014-01-08 17:33:12 +0000291 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000292 elem->value = value;
293 }
294 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000295}
296
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200297STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000298 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000299 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000300
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000301 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
302 args[1],
303 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000304 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000305}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200306STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000307
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000309 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000310 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000311
312 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
313 args[1],
314 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000315 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000316}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200317STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000318
319
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200320STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000321 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000322 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000323
324 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
325 args[1],
326 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000327 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000328}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200329STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000330
John R. Lentonf77dce82014-01-06 20:08:52 +0000331
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200332STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000333 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000334 mp_obj_dict_t *self = self_in;
335 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100336 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000337 }
338 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
339
340 mp_map_elem_t *next = dict_it_iternext_elem(iter);
341 self->map.used--;
342 mp_obj_t items[] = {next->key, next->value};
343 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000344 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000345 mp_obj_t tuple = mp_obj_new_tuple(2, items);
346
347 return tuple;
348}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200349STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000350
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200351STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
Damien George3e1a5c12014-03-29 13:43:38 +0000352 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton88f30432014-01-06 22:58:17 +0000353 mp_obj_dict_t *self = self_in;
354 /* TODO: check for the "keys" method */
Damien Georged17926d2014-03-30 13:35:08 +0100355 mp_obj_t iter = mp_getiter(iterable);
John R. Lenton88f30432014-01-06 22:58:17 +0000356 mp_obj_t next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100357 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100358 mp_obj_t inneriter = mp_getiter(next);
359 mp_obj_t key = mp_iternext(inneriter);
360 mp_obj_t value = mp_iternext(inneriter);
361 mp_obj_t stop = mp_iternext(inneriter);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100362 if (key == MP_OBJ_STOP_ITERATION
363 || value == MP_OBJ_STOP_ITERATION
364 || stop != MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100365 nlr_raise(mp_obj_new_exception_msg(
Damien Georgec5966122014-02-15 16:10:44 +0000366 &mp_type_ValueError,
John R. Lenton88f30432014-01-06 22:58:17 +0000367 "dictionary update sequence has the wrong length"));
368 } else {
Damien George38a2da62014-01-08 17:33:12 +0000369 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
John R. Lenton88f30432014-01-06 22:58:17 +0000370 }
371 }
372
373 return mp_const_none;
374}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200375STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000376
John R. Lentonf77dce82014-01-06 20:08:52 +0000377
John R. Lentona41fe312014-01-06 17:17:02 +0000378/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000379/* dict views */
380
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200381STATIC const mp_obj_type_t dict_view_type;
382STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000383
384typedef enum _mp_dict_view_kind_t {
385 MP_DICT_VIEW_ITEMS,
386 MP_DICT_VIEW_KEYS,
387 MP_DICT_VIEW_VALUES,
388} mp_dict_view_kind_t;
389
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200390STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000391
392typedef struct _mp_obj_dict_view_it_t {
393 mp_obj_base_t base;
394 mp_dict_view_kind_t kind;
395 mp_obj_dict_it_t *iter;
396 machine_uint_t cur;
397} mp_obj_dict_view_it_t;
398
399typedef struct _mp_obj_dict_view_t {
400 mp_obj_base_t base;
401 mp_obj_dict_t *dict;
402 mp_dict_view_kind_t kind;
403} mp_obj_dict_view_t;
404
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200405STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000406 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
407 mp_obj_dict_view_it_t *self = self_in;
408 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
409
Damien Georgeea8d06c2014-04-17 23:19:36 +0100410 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000411 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000412 case MP_DICT_VIEW_ITEMS:
413 {
414 mp_obj_t items[] = {next->key, next->value};
415 return mp_obj_new_tuple(2, items);
416 }
417 case MP_DICT_VIEW_KEYS:
418 return next->key;
419 case MP_DICT_VIEW_VALUES:
420 return next->value;
421 default:
422 assert(0); /* can't happen */
423 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000424 }
425 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100426 return MP_OBJ_STOP_ITERATION;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000427 }
428}
429
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200430STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000431 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000432 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300433 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000434 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000435};
436
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200437STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000438 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
439 mp_obj_dict_view_t *view = view_in;
440 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
441 o->base.type = &dict_view_it_type;
442 o->kind = view->kind;
443 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
444 return o;
445}
446
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200447STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000448 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
449 mp_obj_dict_view_t *self = self_in;
450 bool first = true;
451 print(env, mp_dict_view_names[self->kind]);
452 print(env, "([");
453 mp_obj_t *self_iter = dict_view_getiter(self);
454 mp_obj_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100455 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000456 if (!first) {
457 print(env, ", ");
458 }
459 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200460 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000461 }
462 print(env, "])");
463}
464
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200465STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
John R. Lentonc1bef212014-01-11 12:39:33 +0000466 /* only supported for the 'keys' kind until sets and dicts are refactored */
467 mp_obj_dict_view_t *o = lhs_in;
Damien Georged0a5bf32014-05-10 13:55:11 +0100468 if (o->kind != MP_DICT_VIEW_KEYS) {
469 return MP_OBJ_NOT_SUPPORTED;
470 }
471 if (op != MP_BINARY_OP_IN) {
472 return MP_OBJ_NOT_SUPPORTED;
473 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000474 return dict_binary_op(op, o->dict, rhs_in);
475}
476
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200477STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000478 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000479 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000480 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000481 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000482 .getiter = dict_view_getiter,
483};
484
485mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
486 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
487 o->base.type = &dict_view_type;
488 o->dict = dict;
489 o->kind = kind;
490 return o;
491}
492
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200493STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000494 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000495 mp_obj_dict_t *self = self_in;
496 return mp_obj_new_dict_view(self, kind);
497}
498
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200499STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000500 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
501}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200502STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000503
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200504STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000505 return dict_view(self_in, MP_DICT_VIEW_KEYS);
506}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200507STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000508
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200509STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000510 return dict_view(self_in, MP_DICT_VIEW_VALUES);
511}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200512STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000513
John R. Lenton4bee76e2014-01-10 11:25:03 +0000514/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000515/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000516
Damien George9b196cd2014-03-26 21:47:19 +0000517STATIC const mp_map_elem_t dict_locals_dict_table[] = {
518 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
519 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
520 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
521 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
522 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
523 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
524 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
525 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
526 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
527 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
528 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300529 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
Paul Sokolovskycd94b382014-04-13 23:41:49 +0300530 { MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj },
Paul Sokolovsky14de1142014-04-13 23:55:59 +0300531 { MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000532};
533
Damien George9b196cd2014-03-26 21:47:19 +0000534STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
535
Damien George3e1a5c12014-03-29 13:43:38 +0000536const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000537 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000538 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200539 .print = dict_print,
540 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000541 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200542 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100543 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000544 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000545 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000546};
547
Damien George8b0535e2014-04-05 21:53:54 +0100548void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
549 dict->base.type = &mp_type_dict;
550 mp_map_init(&dict->map, n_args);
551}
552
Damiend99b0522013-12-21 18:17:45 +0000553mp_obj_t mp_obj_new_dict(int n_args) {
554 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100555 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000556 return o;
557}
Damiendae7eb72013-12-29 22:32:51 +0000558
559uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000560 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000561}
562
563mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000564 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000565 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000566 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000567 return self_in;
568}
Damien George062478e2014-01-09 20:57:50 +0000569
Damien George66edc5d2014-04-05 13:25:13 +0100570mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
571 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
572 mp_obj_dict_t *self = self_in;
573 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
574 return self_in;
575}
576
Damien George062478e2014-01-09 20:57:50 +0000577mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000578 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000579 mp_obj_dict_t *self = self_in;
580 return &self->map;
581}