Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1 | #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 George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 9 | #include "mpqstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 10 | #include "obj.h" |
| 11 | #include "runtime0.h" |
| 12 | #include "runtime.h" |
| 13 | #include "map.h" |
| 14 | |
| 15 | typedef struct _mp_obj_dict_t { |
| 16 | mp_obj_base_t base; |
| 17 | mp_map_t map; |
| 18 | } mp_obj_dict_t; |
| 19 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 20 | static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur); |
| 21 | static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in); |
| 22 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 23 | static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 24 | mp_obj_dict_t *self = self_in; |
| 25 | bool first = true; |
| 26 | print(env, "{"); |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 27 | 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, ", "); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 32 | } |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 33 | first = false; |
| 34 | mp_obj_print_helper(print, env, next->key); |
| 35 | print(env, ": "); |
| 36 | mp_obj_print_helper(print, env, next->value); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 37 | } |
| 38 | print(env, "}"); |
| 39 | } |
| 40 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 41 | // args are reverse in the array |
| 42 | static 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 | |
| 47 | static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 48 | mp_obj_dict_t *o = lhs_in; |
| 49 | switch (op) { |
| 50 | case RT_BINARY_OP_SUBSCR: |
| 51 | { |
| 52 | // dict load |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 53 | mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false, false); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 54 | if (elem == NULL) { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 55 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>")); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 56 | } else { |
| 57 | return elem->value; |
| 58 | } |
| 59 | } |
| 60 | default: |
| 61 | // op not supported |
| 62 | return NULL; |
| 63 | } |
| 64 | } |
| 65 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 66 | |
| 67 | /******************************************************************************/ |
| 68 | /* dict iterator */ |
| 69 | |
| 70 | typedef 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 | |
| 76 | static 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 | |
| 91 | mp_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 | |
| 101 | static 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 | |
| 108 | static 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 | |
| 116 | static 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. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 123 | static 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. Lenton | 7d21d51 | 2014-01-06 17:54:04 +0000 | [diff] [blame] | 131 | static MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear); |
| 132 | |
John R. Lenton | d90b19e | 2014-01-06 18:11:20 +0000 | [diff] [blame] | 133 | static 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 | } |
| 141 | static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 142 | |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 143 | static 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. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 147 | if (deflt == NULL) { |
| 148 | if (pop) { |
| 149 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>")); |
| 150 | } else { |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 151 | value = mp_const_none; |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 152 | } |
| 153 | } else { |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 154 | value = deflt; |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 155 | } |
| 156 | } else { |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 157 | value = elem->value; |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 158 | if (pop) { |
| 159 | /* catch the leak (from mp_map_lookup_helper) */ |
John R. Lenton | 88f3043 | 2014-01-06 22:58:17 +0000 | [diff] [blame^] | 160 | m_free(elem, sizeof(mp_map_elem_t)); |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 161 | } |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 162 | } |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 163 | if (set) { |
| 164 | elem->value = value; |
| 165 | } |
| 166 | return value; |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 167 | } |
| 168 | |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 169 | static 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. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 173 | return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, |
| 174 | args[1], |
| 175 | n_args == 3 ? args[2] : NULL, |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 176 | false, false); |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 177 | } |
| 178 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get); |
| 179 | |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 180 | static 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. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 187 | true, false); |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 188 | } |
| 189 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop); |
| 190 | |
| 191 | |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 192 | static 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 | } |
| 201 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault); |
| 202 | |
John R. Lenton | f77dce8 | 2014-01-06 20:08:52 +0000 | [diff] [blame] | 203 | |
| 204 | static 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. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 216 | next->value = NULL; |
John R. Lenton | f77dce8 | 2014-01-06 20:08:52 +0000 | [diff] [blame] | 217 | mp_obj_t tuple = mp_obj_new_tuple(2, items); |
| 218 | |
| 219 | return tuple; |
| 220 | } |
| 221 | static MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem); |
| 222 | |
John R. Lenton | 88f3043 | 2014-01-06 22:58:17 +0000 | [diff] [blame^] | 223 | static 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 | } |
| 247 | static MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update); |
| 248 | |
John R. Lenton | f77dce8 | 2014-01-06 20:08:52 +0000 | [diff] [blame] | 249 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 250 | /******************************************************************************/ |
| 251 | /* dict constructors & etc */ |
| 252 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 253 | const mp_obj_type_t dict_type = { |
| 254 | { &mp_const_type }, |
| 255 | "dict", |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 256 | .print = dict_print, |
| 257 | .make_new = dict_make_new, |
| 258 | .binary_op = dict_binary_op, |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 259 | .getiter = dict_getiter, |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 260 | .methods = { |
| 261 | { "clear", &dict_clear_obj }, |
John R. Lenton | d90b19e | 2014-01-06 18:11:20 +0000 | [diff] [blame] | 262 | { "copy", &dict_copy_obj }, |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 263 | { "get", &dict_get_obj }, |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 264 | { "pop", &dict_pop_obj }, |
John R. Lenton | f77dce8 | 2014-01-06 20:08:52 +0000 | [diff] [blame] | 265 | { "popitem", &dict_popitem_obj }, |
John R. Lenton | be8fe5b | 2014-01-06 20:25:51 +0000 | [diff] [blame] | 266 | { "setdefault", &dict_setdefault_obj }, |
John R. Lenton | 88f3043 | 2014-01-06 22:58:17 +0000 | [diff] [blame^] | 267 | { "update", &dict_update_obj }, |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 268 | { NULL, NULL }, // end-of-list sentinel |
| 269 | }, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | mp_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 | } |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 278 | |
| 279 | uint mp_obj_dict_len(mp_obj_t self_in) { |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 280 | return ((mp_obj_dict_t *)self_in)->map.used; |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | mp_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. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame] | 286 | mp_map_lookup_helper(&self->map, key, true, false)->value = value; |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 287 | return self_in; |
| 288 | } |