blob: 696aad80f5caa30d96f3e41e8985c986802b718a [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);
Damien Georgebcb6ca42014-06-03 12:53:44 +010043STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs);
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 Georgebcb6ca42014-06-03 12:53:44 +010064 mp_obj_t dict = mp_obj_new_dict(0);
65 if (n_args > 0 || n_kw > 0) {
66 mp_obj_t args2[2] = {dict, args[0]}; // args[0] is always valid, even if it's not a positional arg
67 mp_map_t kwargs;
68 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
69 dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
Damien Georged7aadcf2014-04-04 15:08:00 +010070 }
Damien Georged7aadcf2014-04-04 15:08:00 +010071 return dict;
Damien George71c51812014-01-04 20:21:15 +000072}
73
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020074STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000075 mp_obj_dict_t *self = self_in;
76 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010077 case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
78 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
Damien George6ac5dce2014-05-21 19:42:43 +010079 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +000080 }
81}
82
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020083STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000084 mp_obj_dict_t *o = lhs_in;
85 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +010086 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +000087 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +000088 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +000089 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +030090 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +030091 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
92 mp_obj_dict_t *rhs = rhs_in;
93 if (o->map.used != rhs->map.used) {
94 return mp_const_false;
95 }
96
97 machine_uint_t size = o->map.alloc;
98 mp_map_t *map = &o->map;
99
100 for (machine_uint_t i = 0; i < size; i++) {
101 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
102 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
103 if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
104 return mp_const_false;
105 }
106 }
107 }
108 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000109 } else {
110 // dict is not equal to instance of any other type
111 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300112 }
113 }
Damiend99b0522013-12-21 18:17:45 +0000114 default:
115 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100116 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000117 }
118}
119
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300120// TODO: Make sure this is inlined in dict_subscr() below.
121mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
122 mp_obj_dict_t *self = self_in;
123 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
124 if (elem == NULL) {
125 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
126 } else {
127 return elem->value;
128 }
129}
130
Damien George729f7b42014-04-17 22:10:53 +0100131STATIC 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 +0100132 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100133 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100134 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100135 return mp_const_none;
136 } else if (value == MP_OBJ_SENTINEL) {
137 // load
138 mp_obj_dict_t *self = self_in;
139 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
140 if (elem == NULL) {
141 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
142 } else {
143 return elem->value;
144 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100145 } else {
Damien George729f7b42014-04-17 22:10:53 +0100146 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100147 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100148 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100149 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100150}
John R. Lentona41fe312014-01-06 17:17:02 +0000151
152/******************************************************************************/
153/* dict iterator */
154
155typedef struct _mp_obj_dict_it_t {
156 mp_obj_base_t base;
157 mp_obj_dict_t *dict;
158 machine_uint_t cur;
159} mp_obj_dict_it_t;
160
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200161STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000162 mp_obj_dict_it_t *self = self_in;
163 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100164 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000165
166 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100167 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000168 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100169 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000170 }
171 }
172
Damien Georgeea8d06c2014-04-17 23:19:36 +0100173 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000174}
175
176mp_obj_t dict_it_iternext(mp_obj_t self_in) {
177 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
178
Damien Georgeea8d06c2014-04-17 23:19:36 +0100179 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lentona41fe312014-01-06 17:17:02 +0000180 return next->key;
181 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100182 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000183 }
184}
185
Damien George3e1a5c12014-03-29 13:43:38 +0000186STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000187 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000188 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300189 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000190 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000191};
192
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200193STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000194 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000195 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000196 o->dict = dict;
197 o->cur = cur;
198 return o;
199}
200
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200201STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000202 return mp_obj_new_dict_iterator(o_in, 0);
203}
204
205/******************************************************************************/
206/* dict methods */
207
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200208STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000209 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000210 mp_obj_dict_t *self = self_in;
211
212 mp_map_clear(&self->map);
213
214 return mp_const_none;
215}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200216STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000217
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200218STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000219 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000220 mp_obj_dict_t *self = self_in;
221 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
222 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300223 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
224 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000225 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
226 return other;
227}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200228STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000229
Damien Georgeeae16442014-01-11 19:22:29 +0000230// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200231STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000232 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100233 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000234 mp_obj_t len = mp_obj_len_maybe(iter);
235 mp_obj_t value = mp_const_none;
236 mp_obj_t next = NULL;
237 mp_obj_dict_t *self = NULL;
238
239 if (n_args > 2) {
240 value = args[2];
241 }
242
243 if (len == MP_OBJ_NULL) {
244 /* object's type doesn't have a __len__ slot */
245 self = mp_obj_new_dict(0);
246 } else {
247 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
248 }
249
Damien Georgeea8d06c2014-04-17 23:19:36 +0100250 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000251 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
252 }
253
254 return self;
255}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200256STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
257STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000258
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200259STATIC 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 +0000260 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000261 mp_obj_t value;
262 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000263 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000264 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100265 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000266 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000267 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000268 }
269 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000270 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000271 }
272 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000273 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000274 }
Damien George38a2da62014-01-08 17:33:12 +0000275 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000276 elem->value = value;
277 }
278 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000279}
280
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200281STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000282 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000283 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000284
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000285 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
286 args[1],
287 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000288 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000289}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200290STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000291
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200292STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000293 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000294 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000295
296 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
297 args[1],
298 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000299 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000300}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200301STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000302
303
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200304STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000305 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000306 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000307
308 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
309 args[1],
310 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000311 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000312}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200313STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000314
John R. Lentonf77dce82014-01-06 20:08:52 +0000315
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200316STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000317 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000318 mp_obj_dict_t *self = self_in;
319 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100320 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000321 }
322 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
323
324 mp_map_elem_t *next = dict_it_iternext_elem(iter);
325 self->map.used--;
326 mp_obj_t items[] = {next->key, next->value};
327 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000328 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000329 mp_obj_t tuple = mp_obj_new_tuple(2, items);
330
331 return tuple;
332}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200333STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000334
Damien Georgebcb6ca42014-06-03 12:53:44 +0100335STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
336 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
337 mp_obj_dict_t *self = args[0];
338
339 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
340
341 if (n_args == 2) {
342 // given a positional argument
343
344 if (MP_OBJ_IS_TYPE(args[1], &mp_type_dict)) {
345 // update from other dictionary (make sure other is not self)
346 if (args[1] != self) {
347 // TODO don't allocate heap object for this iterator
348 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(args[1], 0);
349 mp_map_elem_t *elem = NULL;
350 while ((elem = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
351 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
352 }
353 }
John R. Lenton88f30432014-01-06 22:58:17 +0000354 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100355 // update from a generic iterable of pairs
356 mp_obj_t iter = mp_getiter(args[1]);
357 mp_obj_t next = NULL;
358 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
359 mp_obj_t inneriter = mp_getiter(next);
360 mp_obj_t key = mp_iternext(inneriter);
361 mp_obj_t value = mp_iternext(inneriter);
362 mp_obj_t stop = mp_iternext(inneriter);
363 if (key == MP_OBJ_STOP_ITERATION
364 || value == MP_OBJ_STOP_ITERATION
365 || stop != MP_OBJ_STOP_ITERATION) {
366 nlr_raise(mp_obj_new_exception_msg(
367 &mp_type_ValueError,
368 "dictionary update sequence has the wrong length"));
369 } else {
370 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
371 }
372 }
373 }
374 }
375
376 // update the dict with any keyword args
377 for (machine_uint_t i = 0; i < kwargs->alloc; i++) {
378 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
379 mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
John R. Lenton88f30432014-01-06 22:58:17 +0000380 }
381 }
382
383 return mp_const_none;
384}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100385STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000386
John R. Lentonf77dce82014-01-06 20:08:52 +0000387
John R. Lentona41fe312014-01-06 17:17:02 +0000388/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000389/* dict views */
390
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200391STATIC const mp_obj_type_t dict_view_type;
392STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000393
394typedef enum _mp_dict_view_kind_t {
395 MP_DICT_VIEW_ITEMS,
396 MP_DICT_VIEW_KEYS,
397 MP_DICT_VIEW_VALUES,
398} mp_dict_view_kind_t;
399
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200400STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000401
402typedef struct _mp_obj_dict_view_it_t {
403 mp_obj_base_t base;
404 mp_dict_view_kind_t kind;
405 mp_obj_dict_it_t *iter;
406 machine_uint_t cur;
407} mp_obj_dict_view_it_t;
408
409typedef struct _mp_obj_dict_view_t {
410 mp_obj_base_t base;
411 mp_obj_dict_t *dict;
412 mp_dict_view_kind_t kind;
413} mp_obj_dict_view_t;
414
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200415STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000416 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
417 mp_obj_dict_view_it_t *self = self_in;
418 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
419
Damien Georgeea8d06c2014-04-17 23:19:36 +0100420 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000421 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000422 case MP_DICT_VIEW_ITEMS:
423 {
424 mp_obj_t items[] = {next->key, next->value};
425 return mp_obj_new_tuple(2, items);
426 }
427 case MP_DICT_VIEW_KEYS:
428 return next->key;
429 case MP_DICT_VIEW_VALUES:
430 return next->value;
431 default:
432 assert(0); /* can't happen */
433 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000434 }
435 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100436 return MP_OBJ_STOP_ITERATION;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000437 }
438}
439
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200440STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000441 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000442 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300443 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000444 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000445};
446
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200447STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000448 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
449 mp_obj_dict_view_t *view = view_in;
450 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
451 o->base.type = &dict_view_it_type;
452 o->kind = view->kind;
453 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
454 return o;
455}
456
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200457STATIC 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 +0000458 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
459 mp_obj_dict_view_t *self = self_in;
460 bool first = true;
461 print(env, mp_dict_view_names[self->kind]);
462 print(env, "([");
463 mp_obj_t *self_iter = dict_view_getiter(self);
464 mp_obj_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100465 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000466 if (!first) {
467 print(env, ", ");
468 }
469 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200470 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000471 }
472 print(env, "])");
473}
474
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200475STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100476 // only supported for the 'keys' kind until sets and dicts are refactored
John R. Lentonc1bef212014-01-11 12:39:33 +0000477 mp_obj_dict_view_t *o = lhs_in;
Damien Georged0a5bf32014-05-10 13:55:11 +0100478 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100479 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100480 }
481 if (op != MP_BINARY_OP_IN) {
Damien George6ac5dce2014-05-21 19:42:43 +0100482 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100483 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000484 return dict_binary_op(op, o->dict, rhs_in);
485}
486
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200487STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000488 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000489 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000490 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000491 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000492 .getiter = dict_view_getiter,
493};
494
495mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
496 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
497 o->base.type = &dict_view_type;
498 o->dict = dict;
499 o->kind = kind;
500 return o;
501}
502
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200503STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000504 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000505 mp_obj_dict_t *self = self_in;
506 return mp_obj_new_dict_view(self, kind);
507}
508
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200509STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000510 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
511}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200512STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000513
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200514STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000515 return dict_view(self_in, MP_DICT_VIEW_KEYS);
516}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200517STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000518
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200519STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000520 return dict_view(self_in, MP_DICT_VIEW_VALUES);
521}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200522STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000523
John R. Lenton4bee76e2014-01-10 11:25:03 +0000524/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000525/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000526
Damien George9b196cd2014-03-26 21:47:19 +0000527STATIC const mp_map_elem_t dict_locals_dict_table[] = {
528 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
529 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
530 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
531 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
532 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
533 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
534 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
535 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
536 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
537 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
538 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300539 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
Paul Sokolovskycd94b382014-04-13 23:41:49 +0300540 { MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj },
Paul Sokolovsky14de1142014-04-13 23:55:59 +0300541 { MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000542};
543
Damien George9b196cd2014-03-26 21:47:19 +0000544STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
545
Damien George3e1a5c12014-03-29 13:43:38 +0000546const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000547 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000548 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200549 .print = dict_print,
550 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000551 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200552 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100553 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000554 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000555 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000556};
557
Damien George8b0535e2014-04-05 21:53:54 +0100558void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
559 dict->base.type = &mp_type_dict;
560 mp_map_init(&dict->map, n_args);
561}
562
Damiend99b0522013-12-21 18:17:45 +0000563mp_obj_t mp_obj_new_dict(int n_args) {
564 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100565 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000566 return o;
567}
Damiendae7eb72013-12-29 22:32:51 +0000568
569uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000570 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000571}
572
573mp_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 +0000574 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000575 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000576 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000577 return self_in;
578}
Damien George062478e2014-01-09 20:57:50 +0000579
Damien George66edc5d2014-04-05 13:25:13 +0100580mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
581 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
582 mp_obj_dict_t *self = self_in;
583 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
584 return self_in;
585}
586
Damien George062478e2014-01-09 20:57:50 +0000587mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000588 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000589 mp_obj_dict_t *self = self_in;
590 return &self->map;
591}