blob: 935829ddb58371254270930f48322c9d404781fd [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. Lenton0fcbaa42014-01-06 19:48:34 +0000143static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop) {
144 mp_map_elem_t *elem = mp_map_lookup_helper(self, key, false, pop);
145 if (elem == NULL) {
146 if (deflt == NULL) {
147 if (pop) {
148 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>"));
149 } else {
150 return mp_const_none;
151 }
152 } else {
153 return deflt;
154 }
155 } else {
156 mp_obj_t value = elem->value;
157 if (pop) {
158 /* catch the leak (from mp_map_lookup_helper) */
159 m_free(elem, 2 * sizeof(mp_obj_t));
160 }
161 return value;
162 }
163
164}
165
John R. Lentoncd088732014-01-06 18:53:16 +0000166static mp_obj_t dict_get(int n_args, const mp_obj_t *args) {
167 assert(2 <= n_args && n_args <= 3);
168 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
169
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000170 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
171 args[1],
172 n_args == 3 ? args[2] : NULL,
173 false);
John R. Lentoncd088732014-01-06 18:53:16 +0000174}
175static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
176
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000177static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) {
178 assert(2 <= n_args && n_args <= 3);
179 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
180
181 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
182 args[1],
183 n_args == 3 ? args[2] : NULL,
184 true);
185}
186static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
187
188
John R. Lentona41fe312014-01-06 17:17:02 +0000189/******************************************************************************/
190/* dict constructors & etc */
191
Damiend99b0522013-12-21 18:17:45 +0000192const mp_obj_type_t dict_type = {
193 { &mp_const_type },
194 "dict",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200195 .print = dict_print,
196 .make_new = dict_make_new,
197 .binary_op = dict_binary_op,
John R. Lentona41fe312014-01-06 17:17:02 +0000198 .getiter = dict_getiter,
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000199 .methods = {
200 { "clear", &dict_clear_obj },
John R. Lentond90b19e2014-01-06 18:11:20 +0000201 { "copy", &dict_copy_obj },
John R. Lentoncd088732014-01-06 18:53:16 +0000202 { "get", &dict_get_obj },
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000203 { "pop", &dict_pop_obj },
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000204 { NULL, NULL }, // end-of-list sentinel
205 },
Damiend99b0522013-12-21 18:17:45 +0000206};
207
208mp_obj_t mp_obj_new_dict(int n_args) {
209 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
210 o->base.type = &dict_type;
211 mp_map_init(&o->map, MP_MAP_OBJ, n_args);
212 return o;
213}
Damiendae7eb72013-12-29 22:32:51 +0000214
215uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000216 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000217}
218
219mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
220 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
221 mp_obj_dict_t *self = self_in;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000222 mp_map_lookup_helper(&self->map, key, true, false)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000223 return self_in;
224}