aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..80a4066 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,64 @@
+
+use melisp::parse::parsetree::parse_to_ast;
+use melisp::sexp::{SExp::*, SLeaf::Nil};
+use std::{io, io::Write};
+
fn main() {
- println!("Hello, world!");
+ match repl() {
+ Ok(()) => println!("bye :)"),
+ Err(e) => {
+ println!("Error: {}", e);
+ main();
+ },
+ }
}
+
+fn repl() -> Result<(), String> {
+
+ let stdin = io::stdin();
+
+ let mut input = String::new();
+
+ let mut stdout = io::stdout();
+
+ print!("> ");
+ match stdout.flush() {
+ Ok(_) => (),
+ Err(_) => println!("Enter s-expression:"),
+ };
+ let mut success = stdin.read_line(&mut input);
+
+ while success.is_ok() && input != "exit\n" {
+ if let Ok(0) = success {
+ print!("\n");
+ break;
+ }
+
+ let expression = match parse_to_ast(&input) {
+ Ok(SCons(a, b)) if *b == Atom(Nil) => Ok(*a),
+ t => t
+ }?;
+
+ let ty = expression.type_check().map_err(|e| e.to_string())?;
+
+ let result = expression.multistep()?;
+
+ println!("{} : {}", result, ty);
+
+ print!("> ");
+ match stdout.flush() {
+ Ok(_) => (),
+ Err(_) => println!("Enter s-expression:"),
+ };
+ input.clear();
+ success = stdin.read_line(&mut input);
+
+ }
+
+ match success {
+ Ok(_) => Ok(()),
+ Err(e) => Err(format!("read error: {e}")),
+ }
+
+}
+