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/step3_env.rs | |
| parent | a77e2b31de9d1c1f5767e6bff56062f8b3c71211 (diff) | |
| download | mal-5939404b0fb822fd29a483dc0d0a4d716cef206e.tar.gz mal-5939404b0fb822fd29a483dc0d0a4d716cef206e.zip | |
rust: add vector and hash-map support.
Diffstat (limited to 'rust/src/step3_env.rs')
| -rw-r--r-- | rust/src/step3_env.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/rust/src/step3_env.rs b/rust/src/step3_env.rs index 4646a97..f9f79ea 100644 --- a/rust/src/step3_env.rs +++ b/rust/src/step3_env.rs @@ -4,8 +4,10 @@ extern crate regex_macros; extern crate regex; -use types::{MalVal,MalRet,Int,Sym,List,Vector, - _int,list,func}; +use std::collections::HashMap; + +use types::{MalVal,MalRet,Int,Sym,List,Vector,Hash_Map, + _int,list,vector,hash_map,func}; use env::{Env,env_new,env_set,env_get}; mod readline; mod types; @@ -26,7 +28,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet { Sym(ref sym) => { env_get(env.clone(), sym.clone()) }, - 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(); @@ -35,7 +37,18 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet { 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.clone()) { + Ok(mv) => { new_hm.insert(key.to_string(), mv); }, + Err(e) => return Err(e), + } + } + Ok(hash_map(new_hm)) }, _ => { Ok(ast) |
