blob: 190d901055c9bc27ef0504d2e7a986ca843e14cb [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <string.h>
3#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
Damien George9b196cd2014-03-26 21:47:19 +000010#include "map.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "runtime0.h"
12#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000013
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020014STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
15STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
John R. Lentona41fe312014-01-06 17:17:02 +000016
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020017STATIC 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 +000018 mp_obj_dict_t *self = self_in;
19 bool first = true;
20 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000021 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
22 mp_map_elem_t *next = NULL;
23 while ((next = dict_it_iternext_elem(dict_iter)) != NULL) {
24 if (!first) {
25 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000026 }
John R. Lentona41fe312014-01-06 17:17:02 +000027 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020028 mp_obj_print_helper(print, env, next->key, PRINT_REPR);
John R. Lentona41fe312014-01-06 17:17:02 +000029 print(env, ": ");
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020030 mp_obj_print_helper(print, env, next->value, PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000031 }
32 print(env, "}");
33}
34
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020035STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George71c51812014-01-04 20:21:15 +000036 // TODO create from an iterable!
37 return rt_build_map(0);
38}
39
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020040STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000041 mp_obj_dict_t *self = self_in;
42 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +020043 case RT_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
Damien George09a0c642014-01-30 10:05:33 +000044 case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
Damien George4e8dc8c2014-01-27 23:15:32 +000045 default: return MP_OBJ_NULL; // op not supported for None
46 }
47}
48
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020049STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000050 mp_obj_dict_t *o = lhs_in;
51 switch (op) {
52 case RT_BINARY_OP_SUBSCR:
53 {
54 // dict load
Damien George38a2da62014-01-08 17:33:12 +000055 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damiend99b0522013-12-21 18:17:45 +000056 if (elem == NULL) {
Damien Georgec5966122014-02-15 16:10:44 +000057 nlr_jump(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
Damiend99b0522013-12-21 18:17:45 +000058 } else {
59 return elem->value;
60 }
61 }
Damien George9aa2a522014-02-01 23:04:09 +000062 case RT_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +000063 {
64 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +000065 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +000066 }
Damiend99b0522013-12-21 18:17:45 +000067 default:
68 // op not supported
69 return NULL;
70 }
71}
72
John R. Lentona41fe312014-01-06 17:17:02 +000073
74/******************************************************************************/
75/* dict iterator */
76
77typedef struct _mp_obj_dict_it_t {
78 mp_obj_base_t base;
79 mp_obj_dict_t *dict;
80 machine_uint_t cur;
81} mp_obj_dict_it_t;
82
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020083STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +000084 mp_obj_dict_it_t *self = self_in;
85 machine_uint_t max = self->dict->map.alloc;
86 mp_map_elem_t *table = self->dict->map.table;
87
88 for (int i = self->cur; i < max; i++) {
89 if (table[i].key != NULL) {
90 self->cur = i + 1;
91 return &(table[i]);
92 }
93 }
94
95 return NULL;
96}
97
98mp_obj_t dict_it_iternext(mp_obj_t self_in) {
99 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
100
101 if (next != NULL) {
102 return next->key;
103 } else {
Damien George66eaf842014-03-26 19:27:58 +0000104 return MP_OBJ_NULL;
John R. Lentona41fe312014-01-06 17:17:02 +0000105 }
106}
107
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200108STATIC const mp_obj_type_t dict_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000109 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000110 .name = MP_QSTR_iterator,
John R. Lentona41fe312014-01-06 17:17:02 +0000111 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000112};
113
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200114STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000115 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
116 o->base.type = &dict_it_type;
117 o->dict = dict;
118 o->cur = cur;
119 return o;
120}
121
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200122STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000123 return mp_obj_new_dict_iterator(o_in, 0);
124}
125
126/******************************************************************************/
127/* dict methods */
128
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200129STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000130 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
131 mp_obj_dict_t *self = self_in;
132
133 mp_map_clear(&self->map);
134
135 return mp_const_none;
136}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200137STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000138
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200139STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
John R. Lentond90b19e2014-01-06 18:11:20 +0000140 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
141 mp_obj_dict_t *self = self_in;
142 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
143 other->map.used = self->map.used;
144 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
145 return other;
146}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200147STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000148
Damien Georgeeae16442014-01-11 19:22:29 +0000149// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200150STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000151 assert(2 <= n_args && n_args <= 3);
152 mp_obj_t iter = rt_getiter(args[1]);
153 mp_obj_t len = mp_obj_len_maybe(iter);
154 mp_obj_t value = mp_const_none;
155 mp_obj_t next = NULL;
156 mp_obj_dict_t *self = NULL;
157
158 if (n_args > 2) {
159 value = args[2];
160 }
161
162 if (len == MP_OBJ_NULL) {
163 /* object's type doesn't have a __len__ slot */
164 self = mp_obj_new_dict(0);
165 } else {
166 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
167 }
168
Damien George66eaf842014-03-26 19:27:58 +0000169 while ((next = rt_iternext(iter)) != MP_OBJ_NULL) {
Damien Georgeeae16442014-01-11 19:22:29 +0000170 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
171 }
172
173 return self;
174}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200175STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
176STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000177
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200178STATIC 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 +0000179 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000180 mp_obj_t value;
181 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000182 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000183 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgec5966122014-02-15 16:10:44 +0000184 nlr_jump(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000185 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000186 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000187 }
188 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000189 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000190 }
191 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000192 value = elem->value;
Damien George38a2da62014-01-08 17:33:12 +0000193 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
194 // catch the leak (from mp_map_lookup)
John R. Lenton88f30432014-01-06 22:58:17 +0000195 m_free(elem, sizeof(mp_map_elem_t));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000196 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000197 }
Damien George38a2da62014-01-08 17:33:12 +0000198 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000199 elem->value = value;
200 }
201 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000202}
203
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200204STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000205 assert(2 <= n_args && n_args <= 3);
206 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
207
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000208 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
209 args[1],
210 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000211 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000212}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200213STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000214
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200215STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000216 assert(2 <= n_args && n_args <= 3);
217 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
218
219 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
220 args[1],
221 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000222 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000223}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200224STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000225
226
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200227STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000228 assert(2 <= n_args && n_args <= 3);
229 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
230
231 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
232 args[1],
233 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000234 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000235}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200236STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000237
John R. Lentonf77dce82014-01-06 20:08:52 +0000238
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200239STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
John R. Lentonf77dce82014-01-06 20:08:52 +0000240 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
241 mp_obj_dict_t *self = self_in;
242 if (self->map.used == 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000243 nlr_jump(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000244 }
245 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
246
247 mp_map_elem_t *next = dict_it_iternext_elem(iter);
248 self->map.used--;
249 mp_obj_t items[] = {next->key, next->value};
250 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000251 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000252 mp_obj_t tuple = mp_obj_new_tuple(2, items);
253
254 return tuple;
255}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200256STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000257
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200258STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
John R. Lenton88f30432014-01-06 22:58:17 +0000259 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
260 mp_obj_dict_t *self = self_in;
261 /* TODO: check for the "keys" method */
262 mp_obj_t iter = rt_getiter(iterable);
263 mp_obj_t next = NULL;
Damien George66eaf842014-03-26 19:27:58 +0000264 while ((next = rt_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton88f30432014-01-06 22:58:17 +0000265 mp_obj_t inneriter = rt_getiter(next);
266 mp_obj_t key = rt_iternext(inneriter);
267 mp_obj_t value = rt_iternext(inneriter);
268 mp_obj_t stop = rt_iternext(inneriter);
Damien George66eaf842014-03-26 19:27:58 +0000269 if (key == MP_OBJ_NULL
270 || value == MP_OBJ_NULL
271 || stop != MP_OBJ_NULL) {
John R. Lenton88f30432014-01-06 22:58:17 +0000272 nlr_jump(mp_obj_new_exception_msg(
Damien Georgec5966122014-02-15 16:10:44 +0000273 &mp_type_ValueError,
John R. Lenton88f30432014-01-06 22:58:17 +0000274 "dictionary update sequence has the wrong length"));
275 } else {
Damien George38a2da62014-01-08 17:33:12 +0000276 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
John R. Lenton88f30432014-01-06 22:58:17 +0000277 }
278 }
279
280 return mp_const_none;
281}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200282STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000283
John R. Lentonf77dce82014-01-06 20:08:52 +0000284
John R. Lentona41fe312014-01-06 17:17:02 +0000285/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000286/* dict views */
287
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200288STATIC const mp_obj_type_t dict_view_type;
289STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000290
291typedef enum _mp_dict_view_kind_t {
292 MP_DICT_VIEW_ITEMS,
293 MP_DICT_VIEW_KEYS,
294 MP_DICT_VIEW_VALUES,
295} mp_dict_view_kind_t;
296
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200297STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000298
299typedef struct _mp_obj_dict_view_it_t {
300 mp_obj_base_t base;
301 mp_dict_view_kind_t kind;
302 mp_obj_dict_it_t *iter;
303 machine_uint_t cur;
304} mp_obj_dict_view_it_t;
305
306typedef struct _mp_obj_dict_view_t {
307 mp_obj_base_t base;
308 mp_obj_dict_t *dict;
309 mp_dict_view_kind_t kind;
310} mp_obj_dict_view_t;
311
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200312STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000313 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
314 mp_obj_dict_view_it_t *self = self_in;
315 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
316
317 if (next != NULL) {
318 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000319 case MP_DICT_VIEW_ITEMS:
320 {
321 mp_obj_t items[] = {next->key, next->value};
322 return mp_obj_new_tuple(2, items);
323 }
324 case MP_DICT_VIEW_KEYS:
325 return next->key;
326 case MP_DICT_VIEW_VALUES:
327 return next->value;
328 default:
329 assert(0); /* can't happen */
330 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000331 }
332 } else {
Damien George66eaf842014-03-26 19:27:58 +0000333 return MP_OBJ_NULL;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000334 }
335}
336
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200337STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000338 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000339 .name = MP_QSTR_iterator,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000340 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000341};
342
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200343STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000344 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
345 mp_obj_dict_view_t *view = view_in;
346 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
347 o->base.type = &dict_view_it_type;
348 o->kind = view->kind;
349 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
350 return o;
351}
352
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200353STATIC 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 +0000354 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
355 mp_obj_dict_view_t *self = self_in;
356 bool first = true;
357 print(env, mp_dict_view_names[self->kind]);
358 print(env, "([");
359 mp_obj_t *self_iter = dict_view_getiter(self);
360 mp_obj_t *next = NULL;
Damien George66eaf842014-03-26 19:27:58 +0000361 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000362 if (!first) {
363 print(env, ", ");
364 }
365 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200366 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000367 }
368 print(env, "])");
369}
370
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200371STATIC 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 +0000372 /* only supported for the 'keys' kind until sets and dicts are refactored */
373 mp_obj_dict_view_t *o = lhs_in;
374 if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
Damien George9aa2a522014-02-01 23:04:09 +0000375 if (op != RT_BINARY_OP_IN) return NULL;
John R. Lentonc1bef212014-01-11 12:39:33 +0000376 return dict_binary_op(op, o->dict, rhs_in);
377}
378
379
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200380STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000381 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000382 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000383 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000384 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000385 .getiter = dict_view_getiter,
386};
387
388mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
389 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
390 o->base.type = &dict_view_type;
391 o->dict = dict;
392 o->kind = kind;
393 return o;
394}
395
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200396STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000397 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
398 mp_obj_dict_t *self = self_in;
399 return mp_obj_new_dict_view(self, kind);
400}
401
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200402STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000403 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
404}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200405STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000406
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200407STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000408 return dict_view(self_in, MP_DICT_VIEW_KEYS);
409}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200410STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000411
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200412STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000413 return dict_view(self_in, MP_DICT_VIEW_VALUES);
414}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200415STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000416
John R. Lenton4bee76e2014-01-10 11:25:03 +0000417/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000418/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000419
Damien George9b196cd2014-03-26 21:47:19 +0000420STATIC const mp_map_elem_t dict_locals_dict_table[] = {
421 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
422 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
423 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
424 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
425 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
426 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
427 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
428 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
429 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
430 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
431 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000432};
433
Damien George9b196cd2014-03-26 21:47:19 +0000434STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
435
Damiend99b0522013-12-21 18:17:45 +0000436const mp_obj_type_t dict_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000437 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000438 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200439 .print = dict_print,
440 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000441 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200442 .binary_op = dict_binary_op,
John R. Lentona41fe312014-01-06 17:17:02 +0000443 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000444 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000445};
446
447mp_obj_t mp_obj_new_dict(int n_args) {
448 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
449 o->base.type = &dict_type;
Damien George38a2da62014-01-08 17:33:12 +0000450 mp_map_init(&o->map, n_args);
Damiend99b0522013-12-21 18:17:45 +0000451 return o;
452}
Damiendae7eb72013-12-29 22:32:51 +0000453
454uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000455 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000456}
457
458mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
459 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
460 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000461 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000462 return self_in;
463}
Damien George062478e2014-01-09 20:57:50 +0000464
465mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
466 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
467 mp_obj_dict_t *self = self_in;
468 return &self->map;
469}