aboutsummaryrefslogtreecommitdiff
path: root/rust/src/bin/step1_read_print.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/bin/step1_read_print.rs')
-rw-r--r--rust/src/bin/step1_read_print.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/rust/src/bin/step1_read_print.rs b/rust/src/bin/step1_read_print.rs
new file mode 100644
index 0000000..8be7e0d
--- /dev/null
+++ b/rust/src/bin/step1_read_print.rs
@@ -0,0 +1,45 @@
+extern crate mal;
+
+use mal::types::{MalVal, MalRet, MalError};
+use mal::types::MalError::{ErrString, ErrMalVal};
+use mal::{readline, reader};
+
+// read
+fn read(str: String) -> MalRet {
+ reader::read_str(str)
+}
+
+// eval
+fn eval(ast: MalVal) -> MalRet {
+ Ok(ast)
+}
+
+// print
+fn print(exp: MalVal) -> String {
+ exp.pr_str(true)
+}
+
+fn rep(str: String) -> Result<String,MalError> {
+ match read(str) {
+ Err(e) => Err(e),
+ Ok(ast) => {
+ //println!("read: {}", ast);
+ match eval(ast) {
+ Err(e) => Err(e),
+ Ok(exp) => Ok(print(exp)),
+ }
+ }
+ }
+}
+
+fn main() {
+ loop {
+ let line = readline::mal_readline("user> ");
+ match line { None => break, _ => () }
+ match rep(line.unwrap()) {
+ Ok(str) => println!("{}", str),
+ Err(ErrMalVal(_)) => (), // Blank line
+ Err(ErrString(s)) => println!("Error: {}", s),
+ }
+ }
+}