1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 02:45:44 +03:00

mal, rust: add seq/string?

Issue #166.
This commit is contained in:
Joel Martin 2016-02-11 14:38:29 -06:00
parent a968e287c1
commit ee5f9da46d
3 changed files with 54 additions and 0 deletions

View File

@ -4,6 +4,7 @@
["nil?" nil?]
["true?" true?]
["false?" false?]
["string?" string?]
["symbol" symbol]
["symbol?" symbol?]
["keyword" keyword]
@ -50,6 +51,7 @@
["map" map]
["conj" conj]
["seq" seq]
["with-meta" with-meta]
["meta" meta]

View File

@ -379,6 +379,39 @@ pub fn conj(a: Vec<MalVal>) -> MalRet {
}
}
pub fn seq(a: Vec<MalVal>) -> MalRet {
if a.len() != 1 {
return err_str("Wrong arity to seq call");
}
let mut new_v: Vec<MalVal> = vec![];
match *a[0] {
List(ref l,_) |
Vector(ref l,_) => {
if l.len() == 0 {
Ok(_nil())
} else {
new_v.extend(l.clone());
Ok(list(new_v))
}
},
Strn(ref s) => {
if s.len() == 0 {
Ok(_nil())
} else if s.starts_with("\u{29e}") {
err_str("seq: called with non-sequence")
} else {
for c in s.chars() {
new_v.push(string(c.to_string()));
}
Ok(list(new_v))
}
},
Nil => Ok(_nil()),
_ => err_str("seq: called with non-sequence"),
}
}
// Metadata functions
fn with_meta(a: Vec<MalVal>) -> MalRet {
@ -461,6 +494,7 @@ pub fn ns() -> HashMap<String,MalVal> {
ns.insert("nil?".to_string(), func(types::nil_q));
ns.insert("true?".to_string(), func(types::true_q));
ns.insert("false?".to_string(), func(types::false_q));
ns.insert("string?".to_string(), func(types::string_q));
ns.insert("symbol".to_string(), func(types::_symbol));
ns.insert("symbol?".to_string(), func(types::symbol_q));
ns.insert("keyword".to_string(), func(types::_keyword));
@ -507,7 +541,9 @@ pub fn ns() -> HashMap<String,MalVal> {
ns.insert("count".to_string(), func(count));
ns.insert("apply".to_string(), func(apply));
ns.insert("map".to_string(), func(map));
ns.insert("conj".to_string(), func(conj));
ns.insert("seq".to_string(), func(seq));
ns.insert("with-meta".to_string(), func(with_meta));
ns.insert("meta".to_string(), func(meta));

View File

@ -190,6 +190,22 @@ pub fn false_q(a:Vec<MalVal>) -> MalRet {
}
}
pub fn string_q(a:Vec<MalVal>) -> MalRet {
if a.len() != 1 {
return err_str("Wrong arity to string? call");
}
match *a[0].clone() {
Strn(ref s) => {
if s.starts_with("\u{29e}") {
Ok(_false())
} else {
Ok(_true())
}
},
_ => Ok(_false()),
}
}
pub fn _int(i: isize) -> MalVal { Rc::new(Int(i)) }