diff options
| author | Joel Martin <github@martintribe.org> | 2014-10-25 14:02:42 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-06 21:58:57 -0600 |
| commit | 0a4d62f2f8362290dfc956ba5c948794a77a6e4d (patch) | |
| tree | edf2cd63393da7c4f27068e236d13a3a4e32d530 /rust/src/types.rs | |
| parent | 8f5b0f1040de849da3fb5ade645308e7bbe7f025 (diff) | |
| download | mal-0a4d62f2f8362290dfc956ba5c948794a77a6e4d.tar.gz mal-0a4d62f2f8362290dfc956ba5c948794a77a6e4d.zip | |
rust: add step4_if_fn_do
Diffstat (limited to 'rust/src/types.rs')
| -rw-r--r-- | rust/src/types.rs | 81 |
1 files changed, 51 insertions, 30 deletions
diff --git a/rust/src/types.rs b/rust/src/types.rs index 75aecbc..8bd658f 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -1,7 +1,8 @@ use std::rc::Rc; use std::collections; use std::fmt; -use super::printer::escape_str; +use super::printer::{escape_str,pr_list}; +use super::env::Env; #[deriving(Clone)] pub enum MalType { @@ -17,12 +18,20 @@ pub enum MalType { Func(fn(Vec<MalVal>) -> MalRet), //Func(fn(&[MalVal]) -> MalRet), //Func(|Vec<MalVal>|:'a -> MalRet), + MalFunc(MalFuncData), } pub type MalVal = Rc<MalType>; pub type MalRet = Result<MalVal,String>; +#[deriving(Clone)] +pub struct MalFuncData { + pub exp: MalVal, + pub env: Env, + pub params: MalVal, +} + impl MalType { pub fn pr_str(&self, print_readably: bool) -> String { @@ -40,49 +49,61 @@ impl MalType { } else { res.push_str(v.as_slice()) } - } + }, List(ref v) => { + res = pr_list(v, _r, "(", ")", " ") + }, + Vector(ref v) => { + res = pr_list(v, _r, "[", "]", " ") + }, + HashMap(ref v) => { let mut first = true; - res.push_str("("); - for item in v.iter() { + res.push_str("{"); + for (key, value) in v.iter() { if first { first = false; } else { res.push_str(" "); } - res.push_str(item.pr_str(_r).as_slice()); + res.push_str(key.as_slice()); + res.push_str(" "); + res.push_str(value.pr_str(_r).as_slice()); } - res.push_str(")") - } - // TODO: better function representation + res.push_str("}") + }, + // TODO: better native function representation //Func(ref v) => { Func(_) => { res.push_str(format!("#<function ...>").as_slice()) - } + }, + MalFunc(ref mf) => { + res.push_str(format!("(fn* {} {})", mf.params, mf.exp).as_slice()) + }, /* - Vector(ref v) => { - let mut first = true; - write!(f, "["); - for item in v.iter() { - if first { first = false; } else { write!(f, " ") } - item.fmt(f); - } - write!(f, "]"); - } - Hash_Map(ref v) => { - let mut first = true; - write!(f, "{}", "{"); - for (key, value) in v.iter() { - if first { first = false; } else { write!(f, " ") } - write!(f, "\"{}\"", *key); - write!(f, " "); - value.fmt(f); - } - write!(f, "{}", "}"); - } // Atom(ref v) => v.fmt(f), */ - _ => { res.push_str("#<unknown type>") } + //_ => { res.push_str("#<unknown type>") }, }; res } + +} + +impl PartialEq for MalType { + fn eq(&self, other: &MalType) -> bool { + match (self, other) { + (&Nil, &Nil) | + (&True, &True) | + (&False, &False) => true, + (&Int(ref a), &Int(ref b)) => a == b, + (&Strn(ref a), &Strn(ref b)) => a == b, + (&Sym(ref a), &Sym(ref b)) => a == b, + (&List(ref a), &List(ref b)) | + (&Vector(ref a), &Vector(ref b)) => a == b, + (&HashMap(ref a), &HashMap(ref b)) => a == b, + // TODO: fix this + (&Func(_), &Func(_)) => false, + (&MalFunc(_), &MalFunc(_)) => false, + _ => return false, + } + } } impl fmt::Show for MalType { |
