Skip to content

Commit ede0377

Browse files
committed
feat: Adding list_clone() function
1 parent be76d76 commit ede0377

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/_builtins.rs

+5-1
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; 68] = [
9+
pub static BUILTINS: [Builtin; 69] = [
1010
Builtin {
1111
name: "Float",
1212
func: crate::value::token::tokay_token_float,
@@ -163,6 +163,10 @@ pub static BUILTINS: [Builtin; 68] = [
163163
name: "list_add",
164164
func: crate::value::list::List::tokay_method_list_add,
165165
},
166+
Builtin {
167+
name: "list_clone",
168+
func: crate::value::list::List::tokay_method_list_clone,
169+
},
166170
Builtin {
167171
name: "list_flatten",
168172
func: crate::value::list::List::tokay_method_list_flatten,

src/value/list.rs

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl Object for List {
5050
}
5151
}
5252

53+
#[allow(unused_doc_comments)]
5354
impl List {
5455
pub fn new() -> Self {
5556
Self {
@@ -84,6 +85,17 @@ impl List {
8485
Ok(RefValue::from(list))
8586
});
8687

88+
/// Clone `list` into a standalone copy.
89+
tokay_method!("list_clone : @list", {
90+
let borrowed = list.borrow();
91+
92+
if let Some(list) = borrowed.object::<List>() {
93+
Ok(RefValue::from(list.clone()))
94+
} else {
95+
Ok(RefValue::from(List { list: vec![list.clone()] }))
96+
}
97+
});
98+
8799
tokay_method!("list_len : @list", {
88100
let list = list.borrow();
89101

tests/list_clone.tok

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#testmode:repl
2+
3+
l = 1,2,3
4+
m = l.clone()
5+
l += 4
6+
m += 5, 6, 7
7+
8+
l
9+
m
10+
11+
#---
12+
#(1, 2, 3, 4)
13+
#(1, 2, 3, 5, 6, 7)

0 commit comments

Comments
 (0)