blob: 7cfd597ed4fe33a220ae2b687bb1f22be21526e7 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
11#include "runtime0.h"
12#include "runtime.h"
13#include "map.h"
14
15typedef struct _mp_obj_dict_t {
16 mp_obj_base_t base;
17 mp_map_t map;
18} mp_obj_dict_t;
19
John R. Lentona41fe312014-01-06 17:17:02 +000020static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
21static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
22
Damien George71c51812014-01-04 20:21:15 +000023static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +000024 mp_obj_dict_t *self = self_in;
25 bool first = true;
26 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000027 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
28 mp_map_elem_t *next = NULL;
29 while ((next = dict_it_iternext_elem(dict_iter)) != NULL) {
30 if (!first) {
31 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000032 }
John R. Lentona41fe312014-01-06 17:17:02 +000033 first = false;
34 mp_obj_print_helper(print, env, next->key);
35 print(env, ": ");
36 mp_obj_print_helper(print, env, next->value);
Damiend99b0522013-12-21 18:17:45 +000037 }
38 print(env, "}");
39}
40
Damien George71c51812014-01-04 20:21:15 +000041// args are reverse in the array
42static mp_obj_t dict_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
43 // TODO create from an iterable!
44 return rt_build_map(0);
45}
46
47static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000048 mp_obj_dict_t *o = lhs_in;
49 switch (op) {
50 case RT_BINARY_OP_SUBSCR:
51 {
52 // dict load
John R. Lenton0fcbaa42014-01-06 19:48:34 +000053 mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false, false);
Damiend99b0522013-12-21 18:17:45 +000054 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000055 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>"));
Damiend99b0522013-12-21 18:17:45 +000056 } else {
57 return elem->value;
58 }
59 }
60 default:
61 // op not supported
62 return NULL;
63 }
64}
65
John R. Lentona41fe312014-01-06 17:17:02 +000066
67/******************************************************************************/
68/* dict iterator */
69
70typedef struct _mp_obj_dict_it_t {
71 mp_obj_base_t base;
72 mp_obj_dict_t *dict;
73 machine_uint_t cur;
74} mp_obj_dict_it_t;
75
76static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
77 mp_obj_dict_it_t *self = self_in;
78 machine_uint_t max = self->dict->map.alloc;
79 mp_map_elem_t *table = self->dict->map.table;
80
81 for (int i = self->cur; i < max; i++) {
82 if (table[i].key != NULL) {
83 self->cur = i + 1;
84 return &(table[i]);
85 }
86 }
87
88 return NULL;
89}
90
91mp_obj_t dict_it_iternext(mp_obj_t self_in) {
92 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
93
94 if (next != NULL) {
95 return next->key;
96 } else {
97 return mp_const_stop_iteration;
98 }
99}
100
101static const mp_obj_type_t dict_it_type = {
102 { &mp_const_type },
103 "dict_iterator",
104 .iternext = dict_it_iternext,
105 .methods = { { NULL, NULL }, },
106};
107
108static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
109 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
110 o->base.type = &dict_it_type;
111 o->dict = dict;
112 o->cur = cur;
113 return o;
114}
115
116static mp_obj_t dict_getiter(mp_obj_t o_in) {
117 return mp_obj_new_dict_iterator(o_in, 0);
118}
119
120/******************************************************************************/
121/* dict methods */
122
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000123static mp_obj_t dict_clear(mp_obj_t self_in) {
124 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
125 mp_obj_dict_t *self = self_in;
126
127 mp_map_clear(&self->map);
128
129 return mp_const_none;
130}
John R. Lenton7d21d512014-01-06 17:54:04 +0000131static MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
132
John R. Lentond90b19e2014-01-06 18:11:20 +0000133static mp_obj_t dict_copy(mp_obj_t self_in) {
134 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
135 mp_obj_dict_t *self = self_in;
136 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
137 other->map.used = self->map.used;
138 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
139 return other;
140}
141static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000142
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000143static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop, bool set) {
144 mp_map_elem_t *elem = mp_map_lookup_helper(self, key, set, pop);
145 mp_obj_t value;
146 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000147 if (deflt == NULL) {
148 if (pop) {
149 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>"));
150 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000151 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000152 }
153 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000154 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000155 }
156 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000157 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000158 if (pop) {
159 /* catch the leak (from mp_map_lookup_helper) */
John R. Lenton88f30432014-01-06 22:58:17 +0000160 m_free(elem, sizeof(mp_map_elem_t));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000161 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000162 }
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000163 if (set) {
164 elem->value = value;
165 }
166 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000167}
168
John R. Lentoncd088732014-01-06 18:53:16 +0000169static mp_obj_t dict_get(int n_args, const mp_obj_t *args) {
170 assert(2 <= n_args && n_args <= 3);
171 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
172
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000173 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
174 args[1],
175 n_args == 3 ? args[2] : NULL,
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000176 false, false);
John R. Lentoncd088732014-01-06 18:53:16 +0000177}
178static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
179
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000180static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) {
181 assert(2 <= n_args && n_args <= 3);
182 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
183
184 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
185 args[1],
186 n_args == 3 ? args[2] : NULL,
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000187 true, false);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000188}
189static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
190
191
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000192static mp_obj_t dict_setdefault(int n_args, const mp_obj_t *args) {
193 assert(2 <= n_args && n_args <= 3);
194 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
195
196 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
197 args[1],
198 n_args == 3 ? args[2] : NULL,
199 false, true);
200}
201static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
202
John R. Lentonf77dce82014-01-06 20:08:52 +0000203
204static mp_obj_t dict_popitem(mp_obj_t self_in) {
205 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
206 mp_obj_dict_t *self = self_in;
207 if (self->map.used == 0) {
208 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "popitem(): dictionary is empty"));
209 }
210 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
211
212 mp_map_elem_t *next = dict_it_iternext_elem(iter);
213 self->map.used--;
214 mp_obj_t items[] = {next->key, next->value};
215 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000216 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000217 mp_obj_t tuple = mp_obj_new_tuple(2, items);
218
219 return tuple;
220}
221static MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
222
John R. Lenton88f30432014-01-06 22:58:17 +0000223static mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
224 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
225 mp_obj_dict_t *self = self_in;
226 /* TODO: check for the "keys" method */
227 mp_obj_t iter = rt_getiter(iterable);
228 mp_obj_t next = NULL;
229 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
230 mp_obj_t inneriter = rt_getiter(next);
231 mp_obj_t key = rt_iternext(inneriter);
232 mp_obj_t value = rt_iternext(inneriter);
233 mp_obj_t stop = rt_iternext(inneriter);
234 if (key == mp_const_stop_iteration
235 || value == mp_const_stop_iteration
236 || stop != mp_const_stop_iteration) {
237 nlr_jump(mp_obj_new_exception_msg(
238 MP_QSTR_ValueError,
239 "dictionary update sequence has the wrong length"));
240 } else {
241 mp_map_lookup_helper(&self->map, key, true, false)->value = value;
242 }
243 }
244
245 return mp_const_none;
246}
247static MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
248
John R. Lentonf77dce82014-01-06 20:08:52 +0000249
John R. Lentona41fe312014-01-06 17:17:02 +0000250/******************************************************************************/
251/* dict constructors & etc */
252
Damiend99b0522013-12-21 18:17:45 +0000253const mp_obj_type_t dict_type = {
254 { &mp_const_type },
255 "dict",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200256 .print = dict_print,
257 .make_new = dict_make_new,
258 .binary_op = dict_binary_op,
John R. Lentona41fe312014-01-06 17:17:02 +0000259 .getiter = dict_getiter,
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000260 .methods = {
261 { "clear", &dict_clear_obj },
John R. Lentond90b19e2014-01-06 18:11:20 +0000262 { "copy", &dict_copy_obj },
John R. Lentoncd088732014-01-06 18:53:16 +0000263 { "get", &dict_get_obj },
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000264 { "pop", &dict_pop_obj },
John R. Lentonf77dce82014-01-06 20:08:52 +0000265 { "popitem", &dict_popitem_obj },
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000266 { "setdefault", &dict_setdefault_obj },
John R. Lenton88f30432014-01-06 22:58:17 +0000267 { "update", &dict_update_obj },
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000268 { NULL, NULL }, // end-of-list sentinel
269 },
Damiend99b0522013-12-21 18:17:45 +0000270};
271
272mp_obj_t mp_obj_new_dict(int n_args) {
273 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
274 o->base.type = &dict_type;
275 mp_map_init(&o->map, MP_MAP_OBJ, n_args);
276 return o;
277}
Damiendae7eb72013-12-29 22:32:51 +0000278
279uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000280 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000281}
282
283mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
284 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
285 mp_obj_dict_t *self = self_in;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000286 mp_map_lookup_helper(&self->map, key, true, false)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000287 return self_in;
288}