rust: add step9_try. Refactor error handling.
[jackhill/mal.git] / rust / src / step1_read_print.rs
1 // support precompiled regexes in reader.rs
2 #![feature(phase)]
3 #[phase(plugin)]
4 extern crate regex_macros;
5 extern crate regex;
6
7 use types::{MalVal,MalRet,MalError,ErrString,ErrMalVal};
8 mod readline;
9 mod types;
10 mod env;
11 mod reader;
12 mod printer;
13
14 // read
15 fn read(str: String) -> MalRet {
16 reader::read_str(str)
17 }
18
19 // eval
20 fn eval(ast: MalVal) -> MalRet {
21 Ok(ast)
22 }
23
24 // print
25 fn print(exp: MalVal) -> String {
26 exp.pr_str(true)
27 }
28
29 fn rep(str: String) -> Result<String,MalError> {
30 match read(str) {
31 Err(e) => Err(e),
32 Ok(ast) => {
33 //println!("read: {}", ast);
34 match eval(ast) {
35 Err(e) => Err(e),
36 Ok(exp) => Ok(print(exp)),
37 }
38 }
39 }
40 }
41
42 fn main() {
43 loop {
44 let line = readline::mal_readline("user> ");
45 match line { None => break, _ => () }
46 match rep(line.unwrap()) {
47 Ok(str) => println!("{}", str),
48 Err(ErrMalVal(_)) => (), // Blank line
49 Err(ErrString(s)) => println!("Error: {}", s),
50 }
51 }
52 }