rust: add step9_try. Refactor error handling.
[jackhill/mal.git] / rust / src / step1_read_print.rs
CommitLineData
abdd56eb
JM
1// support precompiled regexes in reader.rs
2#![feature(phase)]
3#[phase(plugin)]
4extern crate regex_macros;
5extern crate regex;
6
3744d566 7use types::{MalVal,MalRet,MalError,ErrString,ErrMalVal};
abdd56eb
JM
8mod readline;
9mod types;
0a4d62f2 10mod env;
abdd56eb
JM
11mod reader;
12mod printer;
13
14// read
0ab374bc 15fn read(str: String) -> MalRet {
abdd56eb
JM
16 reader::read_str(str)
17}
18
19// eval
0ab374bc 20fn eval(ast: MalVal) -> MalRet {
abdd56eb
JM
21 Ok(ast)
22}
23
24// print
25fn print(exp: MalVal) -> String {
26 exp.pr_str(true)
27}
28
3744d566 29fn rep(str: String) -> Result<String,MalError> {
abdd56eb
JM
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
42fn 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),
3744d566
JM
48 Err(ErrMalVal(_)) => (), // Blank line
49 Err(ErrString(s)) => println!("Error: {}", s),
abdd56eb
JM
50 }
51 }
52}