2 use std::cell::RefCell;
3 //use std::collections::HashMap;
5 use itertools::Itertools;
7 use types::MalErr::{ErrString,ErrMalVal};
8 use types::MalVal::{Nil,Bool,Int,Str,Sym,List,Vector,Hash,Func,MalFunc,Atom};
9 use env::{Env,env_bind};
11 #[derive(Debug, Clone)]
19 List(Rc<Vec<MalVal>>, Rc<MalVal>),
20 Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
21 Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
22 Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
24 eval: fn(ast: MalVal, env: Env) -> MalRet,
31 Atom(Rc<RefCell<MalVal>>),
40 pub type MalArgs = Vec<MalVal>;
41 pub type MalRet = Result<MalVal,MalErr>;
43 // type utility macros
47 List(Rc::new($seq),Rc::new(Nil))
49 [$($args:expr),*] => {{
50 let v: Vec<MalVal> = vec![$($args),*];
51 List(Rc::new(v),Rc::new(Nil))
57 Vector(Rc::new($seq),Rc::new(Nil))
59 [$($args:expr),*] => {{
60 let v: Vec<MalVal> = vec![$($args),*];
61 Vector(Rc::new(v),Rc::new(Nil))
65 // type utility functions
67 pub fn error(s: &str) -> MalRet {
68 Err(ErrString(s.to_string()))
71 pub fn format_error(e: MalErr) -> String {
73 ErrString(s) => s.clone(),
74 ErrMalVal(mv) => mv.pr_str(true),
78 pub fn atom(mv: &MalVal) -> MalVal {
79 Atom(Rc::new(RefCell::new(mv.clone())))
83 pub fn keyword(&self) -> MalRet {
85 Str(s) if s.starts_with("\u{29e}") => Ok(Str(s.to_string())),
86 Str(s) => Ok(Str(format!("\u{29e}{}", s))),
87 _ => error("invalid type for keyword"),
91 pub fn empty_q(&self) -> MalRet {
93 List(l,_) | Vector(l,_) => Ok(Bool(l.len() == 0)),
94 Nil => Ok(Bool(true)),
95 _ => error("invalid type for empty?"),
99 pub fn count(&self) -> MalRet {
101 List(l,_) | Vector(l,_) => Ok(Int(l.len() as i64)),
103 _ => error("invalid type for count"),
107 pub fn apply(&self, args: MalArgs) -> MalRet {
109 Func(f,_) => f(args),
110 MalFunc{eval, ref ast, ref env, ref params, ..} => {
113 let fn_env = env_bind(Some(env.clone()), p.clone(), args)?;
114 Ok(eval(a.clone(), fn_env)?)
116 _ => error("attempt to call non-function"),
120 pub fn keyword_q(&self) -> bool {
122 Str(s) if s.starts_with("\u{29e}") => true,
127 pub fn deref(&self) -> MalRet {
129 Atom(a) => Ok(a.borrow().clone()),
130 _ => error("attempt to deref a non-Atom"),
134 pub fn reset_bang(&self, new: &MalVal) -> MalRet {
137 *a.borrow_mut() = new.clone();
140 _ => error("attempt to reset! a non-Atom"),
144 pub fn swap_bang(&self, args: &MalArgs) -> MalRet {
148 let mut fargs = args[1..].to_vec();
149 fargs.insert(0, a.borrow().clone());
150 *a.borrow_mut() = f.apply(fargs)?;
151 Ok(a.borrow().clone())
153 _ => error("attempt to swap! a non-Atom"),
157 pub fn get_meta(&self) -> MalRet {
159 List(_,meta) | Vector(_,meta) | Hash(_,meta) => Ok((&**meta).clone()),
160 Func(_,meta) => Ok((&**meta).clone()),
161 MalFunc{meta,..} => Ok((&**meta).clone()),
162 _ => error("meta not supported by type"),
166 pub fn with_meta(&mut self, new_meta: &MalVal) -> MalRet {
168 List(_, ref mut meta) |
169 Vector(_, ref mut meta) |
170 Hash(_, ref mut meta) |
171 Func(_,ref mut meta) |
172 MalFunc{ref mut meta, ..} => {
173 *meta = Rc::new((&*new_meta).clone());
175 _ => return error("with-meta not supported by type"),
181 impl PartialEq for MalVal {
182 fn eq(&self, other: &MalVal) -> bool {
183 match (self, other) {
185 (Bool(ref a),Bool(ref b)) => a == b,
186 (Int(ref a),Int(ref b)) => a == b,
187 (Str(ref a),Str(ref b)) => a == b,
188 (Sym(ref a),Sym(ref b)) => a == b,
189 (List(ref a,_),List(ref b,_)) |
190 (Vector(ref a,_),Vector(ref b,_)) |
191 (List(ref a,_),Vector(ref b,_)) |
192 (Vector(ref a,_),List(ref b,_)) => a == b,
193 (Hash(ref a,_),Hash(ref b,_)) => a == b,
194 (MalFunc{..}, MalFunc{..}) => false,
200 pub fn func(f: fn(MalArgs) -> MalRet) -> MalVal {
201 Func(f, Rc::new(Nil))
204 pub fn _assoc(mut hm: FnvHashMap<String,MalVal>, kvs: MalArgs) -> MalRet {
205 if kvs.len() % 2 != 0 {
206 return error("odd number of elements")
208 for (k, v) in kvs.iter().tuples() {
210 Str(s) => { hm.insert(s.to_string(), v.clone()); },
211 _ => { return error("key is not string") },
214 Ok(Hash(Rc::new(hm),Rc::new(Nil)))
217 pub fn _dissoc(mut hm: FnvHashMap<String,MalVal>, ks: MalArgs) -> MalRet {
220 Str(ref s) => { hm.remove(s); },
221 _ => { return error("key is not string") },
224 Ok(Hash(Rc::new(hm),Rc::new(Nil)))
227 pub fn hash_map(kvs: MalArgs) -> MalRet {
228 let hm: FnvHashMap<String,MalVal> = FnvHashMap::default();
232 // vim: ts=2:sw=2:expandtab