aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-04-19 23:27:40 -0400
committerbnewbold <bnewbold@robocracy.org>2016-04-19 23:27:40 -0400
commite57b363b6cc9be3c4e3cd3cb04e37a29368ec532 (patch)
tree1adadc3f2563421bcb18af61c06608c72dbf322b
parentd9e5aa88baf747be6740717948be1c3910a594a5 (diff)
downloadspectrum-e57b363b6cc9be3c4e3cd3cb04e37a29368ec532.zip
spectrum-e57b363b6cc9be3c4e3cd3cb04e37a29368ec532.tar.gz
rust: handle errors more gracefully
-rw-r--r--minimal.rs31
1 files changed, 19 insertions, 12 deletions
diff --git a/minimal.rs b/minimal.rs
index 59013c3..ee7c8f1 100644
--- a/minimal.rs
+++ b/minimal.rs
@@ -85,13 +85,8 @@ fn scheme_parse_num(s: &String) -> Result<f64, &'static str> {
return Ok(num);
}
-fn scheme_parse_sexpr<'a>(sexpr: &Vec<&'a str>) -> Result<SchemeExpr<'a>, &'static str> {
- let ret = sexpr.into_iter().map(|el| SchemeExpr::SchemeStr(el)).collect();
- return Ok(SchemeExpr::SchemeList(ret))
-}
-
-fn scheme_parse<'a>(tokens: &SchemeExpr) -> Result<SchemeExpr<'a>, &'static str> {
- return Ok(SchemeExpr::SchemeNull);
+fn scheme_parse<'a>(tokens: &Vec<&'a str>) -> Result<(SchemeExpr<'a>, usize), &'static str> {
+ return Ok((SchemeExpr::SchemeNull, 0));
}
fn scheme_eval<'a>(ast: &SchemeExpr) -> Result<SchemeExpr<'a>, &'static str> {
@@ -129,11 +124,23 @@ fn main() {
stdout.write(b"\nCiao!\n").unwrap();
return;
}
- let tokens = scheme_tokenize(&raw_input).unwrap();
- println!("Tokens: {}", tokens.join(", "));
- let sexpr = scheme_parse_sexpr(&tokens).unwrap();
- let ast = scheme_parse(&sexpr).unwrap();
- let resp = scheme_eval(&ast).unwrap();
+ let tokens = match scheme_tokenize(&raw_input) {
+ Ok(tokens) => {
+ println!("Tokens: {}", tokens.join(", "));
+ tokens},
+ Err(e) => {
+ println!("couldn't tokenize: {}", e);
+ continue}};
+ let ast = match scheme_parse(&tokens) {
+ Ok((ast, _)) => ast,
+ Err(e) => {
+ println!("couldn't parse: {}", e);
+ continue}};
+ let resp = match scheme_eval(&ast) {
+ Ok(x) => x,
+ Err(e) => {
+ println!("couldn't eval: {}", e);
+ continue}};
println!("{}", scheme_repr(&resp).unwrap());
}
}