blob: 963e1880746a29b0c3d107ae0b688d039de6e2d9 [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"
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030010#include "objtuple.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);
Paul Sokolovskyea85a122014-04-06 20:08:56 +030016STATIC mp_obj_t dict_copy(mp_obj_t self_in);
John R. Lentona41fe312014-01-06 17:17:02 +000017
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020018STATIC 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 +000019 mp_obj_dict_t *self = self_in;
20 bool first = true;
21 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000022 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
23 mp_map_elem_t *next = NULL;
24 while ((next = dict_it_iternext_elem(dict_iter)) != NULL) {
25 if (!first) {
26 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000027 }
John R. Lentona41fe312014-01-06 17:17:02 +000028 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020029 mp_obj_print_helper(print, env, next->key, PRINT_REPR);
John R. Lentona41fe312014-01-06 17:17:02 +000030 print(env, ": ");
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020031 mp_obj_print_helper(print, env, next->value, PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000032 }
33 print(env, "}");
34}
35
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020036STATIC 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 +010037 mp_obj_t dict;
38 switch (n_args) {
39 case 0:
40 dict = mp_obj_new_dict(0);
41 break;
42
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030043 case 1: {
Paul Sokolovskyea85a122014-04-06 20:08:56 +030044 if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
45 return dict_copy(args[0]);
46 }
47 // TODO create dict from an arbitrary mapping!
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030048
49 // Make dict from iterable of pairs
50 mp_obj_t iterable = mp_getiter(args[0]);
51 mp_obj_t dict = mp_obj_new_dict(0);
52 // TODO: support arbitrary seq as a pair
Damien George686afc52014-04-11 09:13:30 +010053 mp_obj_t item;
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030054 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
Damien George686afc52014-04-11 09:13:30 +010055 mp_obj_t *sub_items;
56 mp_obj_get_array_fixed_n(item, 2, &sub_items);
57 mp_obj_dict_store(dict, sub_items[0], sub_items[1]);
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030058 }
59 return dict;
60 }
Damien Georged7aadcf2014-04-04 15:08:00 +010061
62 default:
Damien Georgeea13f402014-04-05 18:32:08 +010063 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));
Damien Georged7aadcf2014-04-04 15:08:00 +010064 }
65
66 // add to the new dict any keyword args
67 for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) {
68 mp_obj_dict_store(dict, a[0], a[1]);
69 }
70
71 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 George4e8dc8c2014-01-27 23:15:32 +000079 default: return MP_OBJ_NULL; // op not supported for None
80 }
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 Georged17926d2014-03-30 13:35:08 +010086 case MP_BINARY_OP_SUBSCR:
Damiend99b0522013-12-21 18:17:45 +000087 {
88 // dict load
Damien George38a2da62014-01-08 17:33:12 +000089 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damiend99b0522013-12-21 18:17:45 +000090 if (elem == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +010091 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
Damiend99b0522013-12-21 18:17:45 +000092 } else {
93 return elem->value;
94 }
95 }
Damien Georged17926d2014-03-30 13:35:08 +010096 case MP_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +000097 {
98 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +000099 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000100 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300101 case MP_BINARY_OP_EQUAL: {
102 // TODO: Support equality to other object types
103 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
104 mp_obj_dict_t *rhs = rhs_in;
105 if (o->map.used != rhs->map.used) {
106 return mp_const_false;
107 }
108
109 machine_uint_t size = o->map.alloc;
110 mp_map_t *map = &o->map;
111
112 for (machine_uint_t i = 0; i < size; i++) {
113 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
114 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
115 if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
116 return mp_const_false;
117 }
118 }
119 }
120 return mp_const_true;
121 }
122 }
Damiend99b0522013-12-21 18:17:45 +0000123 default:
124 // op not supported
125 return NULL;
126 }
127}
128
Damien Georgef4c9b332014-04-08 21:32:29 +0100129STATIC bool dict_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
130 if (value == MP_OBJ_NULL) {
131 mp_obj_dict_delete(self_in, index);
132 } else {
133 mp_obj_dict_store(self_in, index, value);
134 }
135 return true;
136}
John R. Lentona41fe312014-01-06 17:17:02 +0000137
Paul Sokolovsky12a04392014-04-11 05:24:44 +0300138STATIC mp_obj_t dict_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
139 return dict_binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
140}
141
142STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_getitem_obj, dict_getitem);
143
John R. Lentona41fe312014-01-06 17:17:02 +0000144/******************************************************************************/
145/* dict iterator */
146
147typedef struct _mp_obj_dict_it_t {
148 mp_obj_base_t base;
149 mp_obj_dict_t *dict;
150 machine_uint_t cur;
151} mp_obj_dict_it_t;
152
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200153STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000154 mp_obj_dict_it_t *self = self_in;
155 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100156 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000157
158 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100159 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000160 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100161 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000162 }
163 }
164
165 return NULL;
166}
167
168mp_obj_t dict_it_iternext(mp_obj_t self_in) {
169 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
170
171 if (next != NULL) {
172 return next->key;
173 } else {
Damien George66eaf842014-03-26 19:27:58 +0000174 return MP_OBJ_NULL;
John R. Lentona41fe312014-01-06 17:17:02 +0000175 }
176}
177
Damien George3e1a5c12014-03-29 13:43:38 +0000178STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000179 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000180 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300181 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000182 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000183};
184
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200185STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000186 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000187 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000188 o->dict = dict;
189 o->cur = cur;
190 return o;
191}
192
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200193STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000194 return mp_obj_new_dict_iterator(o_in, 0);
195}
196
197/******************************************************************************/
198/* dict methods */
199
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200200STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000201 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000202 mp_obj_dict_t *self = self_in;
203
204 mp_map_clear(&self->map);
205
206 return mp_const_none;
207}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200208STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000209
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200210STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000211 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000212 mp_obj_dict_t *self = self_in;
213 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
214 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300215 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
216 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000217 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
218 return other;
219}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200220STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000221
Damien Georgeeae16442014-01-11 19:22:29 +0000222// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200223STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000224 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100225 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000226 mp_obj_t len = mp_obj_len_maybe(iter);
227 mp_obj_t value = mp_const_none;
228 mp_obj_t next = NULL;
229 mp_obj_dict_t *self = NULL;
230
231 if (n_args > 2) {
232 value = args[2];
233 }
234
235 if (len == MP_OBJ_NULL) {
236 /* object's type doesn't have a __len__ slot */
237 self = mp_obj_new_dict(0);
238 } else {
239 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
240 }
241
Damien Georged17926d2014-03-30 13:35:08 +0100242 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien Georgeeae16442014-01-11 19:22:29 +0000243 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
244 }
245
246 return self;
247}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200248STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
249STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000250
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200251STATIC 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 +0000252 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000253 mp_obj_t value;
254 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000255 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000256 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100257 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000258 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000259 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000260 }
261 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000262 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000263 }
264 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000265 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000266 }
Damien George38a2da62014-01-08 17:33:12 +0000267 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000268 elem->value = value;
269 }
270 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000271}
272
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200273STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000274 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000275 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000276
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000277 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
278 args[1],
279 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000280 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000281}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200282STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000283
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200284STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000285 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000286 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000287
288 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
289 args[1],
290 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000291 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000292}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200293STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000294
295
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200296STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000297 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000298 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000299
300 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
301 args[1],
302 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000303 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000304}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200305STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000306
John R. Lentonf77dce82014-01-06 20:08:52 +0000307
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000309 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000310 mp_obj_dict_t *self = self_in;
311 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100312 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000313 }
314 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
315
316 mp_map_elem_t *next = dict_it_iternext_elem(iter);
317 self->map.used--;
318 mp_obj_t items[] = {next->key, next->value};
319 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000320 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000321 mp_obj_t tuple = mp_obj_new_tuple(2, items);
322
323 return tuple;
324}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200325STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000326
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200327STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
Damien George3e1a5c12014-03-29 13:43:38 +0000328 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton88f30432014-01-06 22:58:17 +0000329 mp_obj_dict_t *self = self_in;
330 /* TODO: check for the "keys" method */
Damien Georged17926d2014-03-30 13:35:08 +0100331 mp_obj_t iter = mp_getiter(iterable);
John R. Lenton88f30432014-01-06 22:58:17 +0000332 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100333 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
334 mp_obj_t inneriter = mp_getiter(next);
335 mp_obj_t key = mp_iternext(inneriter);
336 mp_obj_t value = mp_iternext(inneriter);
337 mp_obj_t stop = mp_iternext(inneriter);
Damien George66eaf842014-03-26 19:27:58 +0000338 if (key == MP_OBJ_NULL
339 || value == MP_OBJ_NULL
340 || stop != MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100341 nlr_raise(mp_obj_new_exception_msg(
Damien Georgec5966122014-02-15 16:10:44 +0000342 &mp_type_ValueError,
John R. Lenton88f30432014-01-06 22:58:17 +0000343 "dictionary update sequence has the wrong length"));
344 } else {
Damien George38a2da62014-01-08 17:33:12 +0000345 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
John R. Lenton88f30432014-01-06 22:58:17 +0000346 }
347 }
348
349 return mp_const_none;
350}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200351STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000352
John R. Lentonf77dce82014-01-06 20:08:52 +0000353
John R. Lentona41fe312014-01-06 17:17:02 +0000354/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000355/* dict views */
356
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200357STATIC const mp_obj_type_t dict_view_type;
358STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000359
360typedef enum _mp_dict_view_kind_t {
361 MP_DICT_VIEW_ITEMS,
362 MP_DICT_VIEW_KEYS,
363 MP_DICT_VIEW_VALUES,
364} mp_dict_view_kind_t;
365
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200366STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000367
368typedef struct _mp_obj_dict_view_it_t {
369 mp_obj_base_t base;
370 mp_dict_view_kind_t kind;
371 mp_obj_dict_it_t *iter;
372 machine_uint_t cur;
373} mp_obj_dict_view_it_t;
374
375typedef struct _mp_obj_dict_view_t {
376 mp_obj_base_t base;
377 mp_obj_dict_t *dict;
378 mp_dict_view_kind_t kind;
379} mp_obj_dict_view_t;
380
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200381STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000382 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
383 mp_obj_dict_view_it_t *self = self_in;
384 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
385
386 if (next != NULL) {
387 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000388 case MP_DICT_VIEW_ITEMS:
389 {
390 mp_obj_t items[] = {next->key, next->value};
391 return mp_obj_new_tuple(2, items);
392 }
393 case MP_DICT_VIEW_KEYS:
394 return next->key;
395 case MP_DICT_VIEW_VALUES:
396 return next->value;
397 default:
398 assert(0); /* can't happen */
399 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000400 }
401 } else {
Damien George66eaf842014-03-26 19:27:58 +0000402 return MP_OBJ_NULL;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000403 }
404}
405
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200406STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000407 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000408 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300409 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000410 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000411};
412
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200413STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000414 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
415 mp_obj_dict_view_t *view = view_in;
416 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
417 o->base.type = &dict_view_it_type;
418 o->kind = view->kind;
419 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
420 return o;
421}
422
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200423STATIC 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 +0000424 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
425 mp_obj_dict_view_t *self = self_in;
426 bool first = true;
427 print(env, mp_dict_view_names[self->kind]);
428 print(env, "([");
429 mp_obj_t *self_iter = dict_view_getiter(self);
430 mp_obj_t *next = NULL;
Damien George66eaf842014-03-26 19:27:58 +0000431 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000432 if (!first) {
433 print(env, ", ");
434 }
435 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200436 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000437 }
438 print(env, "])");
439}
440
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200441STATIC 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 +0000442 /* only supported for the 'keys' kind until sets and dicts are refactored */
443 mp_obj_dict_view_t *o = lhs_in;
444 if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100445 if (op != MP_BINARY_OP_IN) return NULL;
John R. Lentonc1bef212014-01-11 12:39:33 +0000446 return dict_binary_op(op, o->dict, rhs_in);
447}
448
449
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200450STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000451 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000452 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000453 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000454 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000455 .getiter = dict_view_getiter,
456};
457
458mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
459 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
460 o->base.type = &dict_view_type;
461 o->dict = dict;
462 o->kind = kind;
463 return o;
464}
465
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200466STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000467 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000468 mp_obj_dict_t *self = self_in;
469 return mp_obj_new_dict_view(self, kind);
470}
471
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200472STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000473 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
474}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200475STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000476
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200477STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000478 return dict_view(self_in, MP_DICT_VIEW_KEYS);
479}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200480STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000481
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200482STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000483 return dict_view(self_in, MP_DICT_VIEW_VALUES);
484}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200485STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000486
John R. Lenton4bee76e2014-01-10 11:25:03 +0000487/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000488/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000489
Damien George9b196cd2014-03-26 21:47:19 +0000490STATIC const mp_map_elem_t dict_locals_dict_table[] = {
491 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
492 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
493 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
494 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
495 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
496 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
497 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
498 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
499 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
500 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
501 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky12a04392014-04-11 05:24:44 +0300502 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&dict_getitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000503};
504
Damien George9b196cd2014-03-26 21:47:19 +0000505STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
506
Damien George3e1a5c12014-03-29 13:43:38 +0000507const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000508 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000509 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200510 .print = dict_print,
511 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000512 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200513 .binary_op = dict_binary_op,
Damien Georgef4c9b332014-04-08 21:32:29 +0100514 .store_item = dict_store_item,
John R. Lentona41fe312014-01-06 17:17:02 +0000515 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000516 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000517};
518
Damien George8b0535e2014-04-05 21:53:54 +0100519void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
520 dict->base.type = &mp_type_dict;
521 mp_map_init(&dict->map, n_args);
522}
523
Damiend99b0522013-12-21 18:17:45 +0000524mp_obj_t mp_obj_new_dict(int n_args) {
525 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100526 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000527 return o;
528}
Damiendae7eb72013-12-29 22:32:51 +0000529
530uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000531 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000532}
533
534mp_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 +0000535 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000536 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000537 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000538 return self_in;
539}
Damien George062478e2014-01-09 20:57:50 +0000540
Damien George66edc5d2014-04-05 13:25:13 +0100541mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
542 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
543 mp_obj_dict_t *self = self_in;
544 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
545 return self_in;
546}
547
Damien George062478e2014-01-09 20:57:50 +0000548mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000549 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000550 mp_obj_dict_t *self = self_in;
551 return &self->map;
552}