@@ -113,15 +113,19 @@ limo_data **dict_get_place(limo_data *dict, limo_data *key)
113
113
limo_error ("dict_get_place(): this should not happen!" );
114
114
}
115
115
116
- limo_data * dict_remove (limo_data * dict , limo_data * key )
116
+ void dict_remove (limo_data * dict , limo_data * key )
117
117
{
118
- limo_data * * place = dict_get_place (dict , key );
119
- (* place ) = NULL ;
120
- dict -> data .d_dict -> used -- ;
118
+ limo_data * * place ;
121
119
122
- // dict_resize MUST be done, or else items, which should have been stored in the same bucket as *key
123
- // are unfindable after removal of this. SERIOUSLY hard to find bug!
124
- dict_resize (dict );
120
+ place = dict_get_place (dict , key );
121
+ if (place != NULL ) {
122
+ (* place ) = NULL ;
123
+ dict -> data .d_dict -> used -- ;
124
+
125
+ // dict_resize MUST be done, or else items, which should have been stored in the same bucket as *key
126
+ // are unfindable after removal of this. SERIOUSLY hard to find bug!
127
+ dict_resize (dict );
128
+ }
125
129
}
126
130
127
131
limo_data * dict_to_list (limo_data * dict )
@@ -135,3 +139,103 @@ limo_data *dict_to_list(limo_data *dict)
135
139
136
140
return res ;
137
141
}
142
+
143
+ ////////// BUILTINS ////////////
144
+
145
+ BUILTIN (builtin_make_dict )
146
+ {
147
+ return make_dict ();
148
+ }
149
+
150
+ BUILTIN (builtin_dict_get )
151
+ {
152
+ limo_data * dict ;
153
+ limo_data * key ;
154
+ limo_data * * res ;
155
+
156
+ if (list_length (arglist ) != 3 )
157
+ limo_error ("(dict-get DICT KEY)" );
158
+
159
+ dict = eval (FIRST_ARG , env );
160
+ if (dict -> type != limo_TYPE_DICT )
161
+ limo_error ("(dict-get DICT KEY)" );
162
+
163
+ key = eval (SECOND_ARG , env );
164
+ res = dict_get_place (dict , key );
165
+ if (res == NULL )
166
+ throw (make_cons (make_string ("Could not find key" ), key ));
167
+ return * res ;
168
+ }
169
+
170
+ BUILTIN (builtin_dict_set )
171
+ {
172
+ limo_data * dict ;
173
+ limo_data * key ;
174
+ limo_data * value ;
175
+
176
+ if (list_length (arglist ) != 4 )
177
+ limo_error ("(dict-set DICT KEY VALUE)" );
178
+
179
+ dict = eval (FIRST_ARG , env );
180
+ if (dict -> type != limo_TYPE_DICT )
181
+ limo_error ("(dict-set DICT KEY VALUE)" );
182
+
183
+ key = eval (SECOND_ARG , env );
184
+ value = eval (THIRD_ARG , env );
185
+
186
+ dict_put (dict , key , value );
187
+ return make_nil ();
188
+ }
189
+
190
+
191
+ BUILTIN (builtin_dict_unset )
192
+ {
193
+ limo_data * dict ;
194
+ limo_data * key ;
195
+
196
+ if (list_length (arglist ) != 3 )
197
+ limo_error ("(dict-unset DICT KEY)" );
198
+
199
+ dict = eval (FIRST_ARG , env );
200
+ if (dict -> type != limo_TYPE_DICT )
201
+ limo_error ("(dict-unset DICT KEY)" );
202
+
203
+ key = eval (SECOND_ARG , env );
204
+ dict_remove (dict , key );
205
+ return make_nil ();
206
+ }
207
+
208
+ BUILTIN (builtin_dict_has_key )
209
+ {
210
+ limo_data * dict ;
211
+ limo_data * key ;
212
+ limo_data * * res ;
213
+
214
+ if (list_length (arglist ) != 3 )
215
+ limo_error ("(dict-has-key DICT KEY)" );
216
+
217
+ dict = eval (FIRST_ARG , env );
218
+ if (dict -> type != limo_TYPE_DICT )
219
+ limo_error ("(dict-has-key DICT KEY)" );
220
+
221
+ key = eval (SECOND_ARG , env );
222
+ res = dict_get_place (dict , key );
223
+ if (res == NULL )
224
+ return make_nil ();
225
+ else
226
+ return sym_true ;
227
+ }
228
+
229
+ BUILTIN (builtin_dict_to_list )
230
+ {
231
+ limo_data * dict ;
232
+
233
+ if (list_length (arglist ) != 2 )
234
+ limo_error ("(dict-unset DICT KEY)" );
235
+
236
+ dict = eval (FIRST_ARG , env );
237
+ if (dict -> type != limo_TYPE_DICT )
238
+ limo_error ("(dict-unset DICT KEY)" );
239
+
240
+ return dict_to_list (dict );
241
+ }
0 commit comments