Skip to content

Commit de9180f

Browse files
committed
fix+doc: docs for dict(), removed dict_push()
1 parent ca56d65 commit de9180f

File tree

3 files changed

+80
-52
lines changed

3 files changed

+80
-52
lines changed

src/_builtins.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::builtin::Builtin;
77

88
/*GENERATE cargo run -- _builtins.tok -- `find . -name "*.rs"` */
9-
pub static BUILTINS: [Builtin; 69] = [
9+
pub static BUILTINS: [Builtin; 68] = [
1010
Builtin {
1111
name: "Float",
1212
func: crate::value::token::tokay_token_float,
@@ -83,10 +83,6 @@ pub static BUILTINS: [Builtin; 69] = [
8383
name: "dict_pop",
8484
func: crate::value::dict::Dict::tokay_method_dict_pop,
8585
},
86-
Builtin {
87-
name: "dict_push",
88-
func: crate::value::dict::Dict::tokay_method_dict_push,
89-
},
9086
Builtin {
9187
name: "dict_set_item",
9288
func: crate::value::dict::Dict::tokay_method_dict_set_item,

src/value/dict.rs

+67-32
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl Object for Dict {
8181
}
8282
}
8383

84+
#[allow(unused_doc_comments)]
8485
impl Dict {
8586
pub fn new() -> Self {
8687
Self {
@@ -100,8 +101,25 @@ impl Dict {
100101
self.shift_remove(&RefValue::from(key)) // fixme: improve lookup!
101102
}
102103

103-
tokay_method!("dict : @", Ok(RefValue::from(Dict::new())));
104+
/** Creates a new `dict`.
104105
106+
Any provided `nargs` become key-value-pairs in the newly created dict.
107+
108+
This can also be shortcut by the `()` syntax.
109+
*/
110+
tokay_method!(
111+
"dict : @**nargs",
112+
Ok(RefValue::from(if let Some(nargs) = nargs {
113+
nargs.clone()
114+
} else {
115+
Dict::new()
116+
}))
117+
);
118+
119+
/** Creates an iterator over a `dict`.
120+
121+
The iterator is a method-iterator calling `iter_values()`.
122+
*/
105123
tokay_method!("dict_iter : @dict", {
106124
// If index is void, create an iterator on keys.
107125
if dict.is("dict") {
@@ -121,6 +139,7 @@ impl Dict {
121139
}
122140
});
123141

142+
/// Returns the number of items in the `dict`.
124143
tokay_method!("dict_len : @dict", {
125144
let dict = dict.borrow();
126145

@@ -136,6 +155,7 @@ impl Dict {
136155
}
137156
});
138157

158+
/// Clone `dict` into a standalone copy.
139159
tokay_method!("dict_clone : @dict", {
140160
let dict = dict.borrow();
141161

@@ -151,7 +171,12 @@ impl Dict {
151171
}
152172
});
153173

154-
// Method to retrieve or iterate the keys of a dict.
174+
/** Retrieve or iterate the keys of a `dict`.
175+
176+
When no `index` is given, the method returns an iterator over the keys.
177+
Otherwise, the key at the provided `index` is returned, or `default` in
178+
case the `index` is out of bounds.
179+
*/
155180
tokay_method!("dict_keys : @dict, index=void, default=void", {
156181
// If index is void, create an iterator on keys.
157182
if index.is_void() {
@@ -181,7 +206,12 @@ impl Dict {
181206
}
182207
});
183208

184-
// Method to retrieve or iterate the values of a dict.
209+
/** Retrieve or iterate the values of a `dict`.
210+
211+
When no `index` is given, the method returns an iterator over the values.
212+
Otherwise, the value at the provided `index` is returned, or `default` in
213+
case the `index` is out of bounds.
214+
*/
185215
tokay_method!("dict_values : @dict, index=void, default=void", {
186216
// If index is void, create an iterator on keys.
187217
if index.is_void() {
@@ -211,7 +241,14 @@ impl Dict {
211241
}
212242
});
213243

214-
// Method to retrieve or iterate a list of [key, value] from a dict by index
244+
/** Retrieve or iterate both keys and values of a `dict`.
245+
246+
The function returns a list of key-value for each result.
247+
248+
When no `index` is given, the method returns an iterator over the key-value-pairs.
249+
Otherwise, the key-value-pair at the provided `index` is returned, or `default` in
250+
case the `index` is out of bounds.
251+
*/
215252
tokay_method!("dict_items : @dict, index=void, default=void", {
216253
// If index is void, create an iterator on items.
217254
if index.is_void() {
@@ -241,21 +278,25 @@ impl Dict {
241278
}
242279
});
243280

244-
tokay_method!("dict_get_item : @dict, item, default=void", {
245-
if !item.is_hashable() {
281+
/** Retrieve item with `key` from `dict`. Returns `default` when key is not found.
282+
283+
This method is also invoked when using the `dict` item syntax.
284+
*/
285+
tokay_method!("dict_get_item : @dict, key, default=void", {
286+
if !key.is_hashable() {
246287
return Err(Error::from(format!(
247288
"{} unhashable type '{}'",
248289
__function,
249-
item.name()
290+
key.name()
250291
)));
251292
}
252293

253294
// todo: alias dict_get
254295
let dict = dict.borrow();
255296

256297
if let Some(dict) = dict.object::<Dict>() {
257-
if let Some(item) = dict.get(&item) {
258-
Ok(item.clone())
298+
if let Some(key) = dict.get(&key) {
299+
Ok(key.clone())
259300
} else {
260301
Ok(default)
261302
}
@@ -269,23 +310,32 @@ impl Dict {
269310
}
270311
});
271312

272-
tokay_method!("dict_set_item : @dict, item, value=void", {
273-
if !item.is_hashable() {
313+
/** Insert or replace `value` under the given `key` in `dict`.
314+
315+
When `value` is provided as void, the key is removed.
316+
317+
Returns the previous item's value if the key already existed in `dict`,
318+
otherwise void.
319+
320+
This method is also invoked when assigning to a `dict` item.
321+
*/
322+
tokay_method!("dict_set_item : @dict, key, value=void", {
323+
if !key.is_hashable() {
274324
return Err(Error::from(format!(
275325
"{} unhashable type '{}'",
276326
__function,
277-
item.name()
327+
key.name()
278328
)));
279329
}
280330

281331
let mut dict = dict.borrow_mut();
282332

283333
if let Some(dict) = dict.object_mut::<Dict>() {
284334
if value.is_void() {
285-
dict.shift_remove(&item);
335+
dict.shift_remove(&key);
286336
Ok(value![void])
287337
} else {
288-
dict.insert(item, value.clone());
338+
dict.insert(key, value.clone());
289339
Ok(value)
290340
}
291341
} else {
@@ -298,6 +348,7 @@ impl Dict {
298348
}
299349
});
300350

351+
/** Merges dict `other` into `dict`. */
301352
tokay_method!("dict_merge : @dict, other", {
302353
{
303354
let dict = &mut *dict.borrow_mut();
@@ -330,25 +381,9 @@ impl Dict {
330381
Ok(dict)
331382
});
332383

333-
tokay_method!("dict_push : @dict, key, value", {
334-
let dict = &mut *dict.borrow_mut();
335-
336-
if let Some(dict) = dict.object_mut::<Dict>() {
337-
Ok(if let Some(old) = dict.insert(key, value) {
338-
old
339-
} else {
340-
value!(void)
341-
})
342-
} else {
343-
Err(Error::from(format!(
344-
"{} only accepts '{}' as parameter, not '{}'",
345-
__function,
346-
"dict",
347-
dict.name()
348-
)))
349-
}
350-
});
384+
/** Returns and removes `key` from `dict`.
351385
386+
When the given `key` does not exist, `default` will be returned, */
352387
tokay_method!("dict_pop : @dict, key=void, default=void", {
353388
let dict = &mut *dict.borrow_mut();
354389

tests/dict.tok

+12-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ x : 10
1919
d = (b => 3 c => 1 a => 2)
2020
d
2121

22+
dict()
23+
dict(a=1, b=2, c=3)
24+
2225
## Comparison
2326

2427
a = (a => 1 b => 2)
@@ -83,19 +86,25 @@ d = (a => 1 b => 2)
8386
d.len()
8487
dict_len("Donkey") # invalid
8588

89+
# iter / values
90+
8691
list(iter(d))
8792
list(d.values)
8893
d.values(0)
8994
d.values(1)
9095
type(d.values(2))
9196
d.values(2, default="Esel")
9297

98+
# keys
99+
93100
list(d.keys)
94101
d.keys(0)
95102
d.keys(1)
96103
type(d.keys(2))
97104
d.keys(2, default="Esel")
98105

106+
# items
107+
99108
list(d.items)
100109
d.items(0)
101110
d.items(1)
@@ -119,18 +128,6 @@ d
119128
d.merge(d)
120129
(a => 23 b => 42) $1.merge($1)
121130

122-
## push
123-
124-
d = dict()
125-
d.push(1, 2)
126-
d.push(2, 3)
127-
d
128-
129-
d = dict()
130-
d.push(1, 2)
131-
d.push(2, 3)
132-
d.push(1, 4)
133-
134131
## pop
135132

136133
d = ("esel" => 1 "bert" => 2 "edgar" => 42 "klaus" => 23)
@@ -157,6 +154,9 @@ d
157154

158155
#(b => 3 c => 1 a => 2)
159156

157+
#()
158+
#(a => 1 b => 2 c => 3)
159+
160160
#(true, true, true, false, true, true, true, true, true, true)
161161

162162
#"John"
@@ -203,9 +203,6 @@ d
203203
#(a => 1 b => 2 c => 3)
204204
#((a => 23 b => 42), (a => 23 b => 42))
205205

206-
#(1 => 2 2 => 3)
207-
#2
208-
209206
#(esel => 1 bert => 2 edgar => 42 klaus => 23)
210207
#"eugen"
211208
#2

0 commit comments

Comments
 (0)