aboutsummaryrefslogtreecommitdiff
path: root/rust/src/step2_eval.rs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-10-27 20:45:27 -0500
committerJoel Martin <github@martintribe.org>2015-01-06 21:58:59 -0600
commit3744d56621ff4c1c6c8a1fd6beb1d03123df53a5 (patch)
tree579e72e9bd66a8e8ea30d30e0c4a6c52dc51cc04 /rust/src/step2_eval.rs
parent5939404b0fb822fd29a483dc0d0a4d716cef206e (diff)
downloadmal-3744d56621ff4c1c6c8a1fd6beb1d03123df53a5.tar.gz
mal-3744d56621ff4c1c6c8a1fd6beb1d03123df53a5.zip
rust: add step9_try. Refactor error handling.
Additional core functions.
Diffstat (limited to 'rust/src/step2_eval.rs')
-rw-r--r--rust/src/step2_eval.rs20
1 files changed, 11 insertions, 9 deletions
diff --git a/rust/src/step2_eval.rs b/rust/src/step2_eval.rs
index 4a0fcb5..29c53d9 100644
--- a/rust/src/step2_eval.rs
+++ b/rust/src/step2_eval.rs
@@ -6,13 +6,14 @@ extern crate regex;
use std::collections::HashMap;
-use types::{MalVal,MalRet,Int,Sym,List,Vector,Hash_Map,
+use types::{MalVal,MalRet,MalError,ErrString,ErrMalVal,err_str,
+ Int,Sym,List,Vector,Hash_Map,
_nil,_int,list,vector,hash_map,func};
mod readline;
mod types;
-mod env;
mod reader;
mod printer;
+mod env; // because types uses env
// read
fn read(str: String) -> MalRet {
@@ -71,7 +72,7 @@ fn eval(ast: MalVal, env: &HashMap<String,MalVal>) -> MalRet {
let ref f = args.clone()[0];
f.apply(args.slice(1,args.len()).to_vec())
}
- _ => Err("Invalid apply".to_string()),
+ _ => err_str("Invalid apply"),
}
}
}
@@ -82,8 +83,8 @@ fn print(exp: MalVal) -> String {
exp.pr_str(true)
}
-fn rep(str: String, env: &HashMap<String,MalVal>) -> Result<String,String> {
- match read(str) {
+fn rep(str: &str, env: &HashMap<String,MalVal>) -> Result<String,MalError> {
+ match read(str.to_string()) {
Err(e) => Err(e),
Ok(ast) => {
//println!("read: {}", ast);
@@ -99,9 +100,9 @@ fn int_op(f: |i:int,j:int|-> int, a:Vec<MalVal>) -> MalRet {
match *a[0] {
Int(a0) => match *a[1] {
Int(a1) => Ok(_int(f(a0,a1))),
- _ => Err("second arg must be an int".to_string()),
+ _ => err_str("second arg must be an int"),
},
- _ => Err("first arg must be an int".to_string()),
+ _ => err_str("first arg must be an int"),
}
}
fn add(a:Vec<MalVal>) -> MalRet { int_op(|i,j| { i+j }, a) }
@@ -119,9 +120,10 @@ fn main() {
loop {
let line = readline::mal_readline("user> ");
match line { None => break, _ => () }
- match rep(line.unwrap(), &repl_env) {
+ match rep(line.unwrap().as_slice(), &repl_env) {
Ok(str) => println!("{}", str),
- Err(str) => println!("Error: {}", str),
+ Err(ErrMalVal(_)) => (), // Blank line
+ Err(ErrString(s)) => println!("Error: {}", s),
}
}
}