Merge pull request #445 from bjh21/bjh21-bbc-basic
[jackhill/mal.git] / rust / step1_read_print.rs
1 #[macro_use]
2 extern crate lazy_static;
3 extern crate regex;
4 extern crate itertools;
5 extern crate fnv;
6
7 extern crate rustyline;
8 use rustyline::error::ReadlineError;
9 use rustyline::Editor;
10
11 #[macro_use]
12 #[allow(dead_code)]
13 mod types;
14 use types::{format_error};
15 mod reader;
16 mod printer;
17 // TODO: figure out a way to avoid including env
18 #[allow(dead_code)]
19 mod env;
20
21 fn main() {
22 // `()` can be used when no completer is required
23 let mut rl = Editor::<()>::new();
24 if rl.load_history(".mal-history").is_err() {
25 eprintln!("No previous history.");
26 }
27
28 loop {
29 let readline = rl.readline("user> ");
30 match readline {
31 Ok(line) => {
32 rl.add_history_entry(&line);
33 rl.save_history(".mal-history").unwrap();
34 if line.len() > 0 {
35 match reader::read_str(line) {
36 Ok(mv) => {
37 println!("{}", mv.pr_str(true));
38 },
39 Err(e) => println!("Error: {}", format_error(e)),
40 }
41 }
42 },
43 Err(ReadlineError::Interrupted) => continue,
44 Err(ReadlineError::Eof) => break,
45 Err(err) => {
46 println!("Error: {:?}", err);
47 break
48 }
49 }
50 }
51 }
52
53 // vim: ts=2:sw=2:expandtab