diff options
| author | Joel Martin <github@martintribe.org> | 2014-10-27 20:07:29 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-10-27 20:07:29 -0500 |
| commit | 5939404b0fb822fd29a483dc0d0a4d716cef206e (patch) | |
| tree | 8196739b701df4fb7b61fed967eaca28f61d4b57 /rust/src/step2_eval.rs | |
| parent | a77e2b31de9d1c1f5767e6bff56062f8b3c71211 (diff) | |
| download | mal-5939404b0fb822fd29a483dc0d0a4d716cef206e.tar.gz mal-5939404b0fb822fd29a483dc0d0a4d716cef206e.zip | |
rust: add vector and hash-map support.
Diffstat (limited to 'rust/src/step2_eval.rs')
| -rw-r--r-- | rust/src/step2_eval.rs | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/rust/src/step2_eval.rs b/rust/src/step2_eval.rs index f8046b9..4a0fcb5 100644 --- a/rust/src/step2_eval.rs +++ b/rust/src/step2_eval.rs @@ -6,8 +6,8 @@ extern crate regex; use std::collections::HashMap; -use types::{MalVal,MalRet,Int,Sym,List, - _nil,_int,list,func}; +use types::{MalVal,MalRet,Int,Sym,List,Vector,Hash_Map, + _nil,_int,list,vector,hash_map,func}; mod readline; mod types; mod env; @@ -28,16 +28,26 @@ fn eval_ast(ast: MalVal, env: &HashMap<String,MalVal>) -> MalRet { None => Ok(_nil()), } }, - List(ref a) => { + List(ref a) | Vector(ref a) => { let mut ast_vec : Vec<MalVal> = vec![]; for mv in a.iter() { - let mv2 = mv.clone(); - match eval(mv2, env) { - Ok(mv) => { ast_vec.push(mv); }, - Err(e) => { return Err(e); }, + match eval(mv.clone(), env) { + Ok(mv) => ast_vec.push(mv), + Err(e) => return Err(e), } } - Ok(list(ast_vec)) + Ok(match *ast { List(_) => list(ast_vec), + _ => vector(ast_vec) }) + }, + Hash_Map(ref hm) => { + let mut new_hm: HashMap<String,MalVal> = HashMap::new(); + for (key, value) in hm.iter() { + match eval(value.clone(), env) { + Ok(mv) => { new_hm.insert(key.to_string(), mv); }, + Err(e) => return Err(e), + } + } + Ok(hash_map(new_hm)) }, _ => { Ok(ast.clone()) |
