aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-04 23:50:46 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-04 23:50:46 +0300
commitfdae943090463526423f5e43e72cd2f0e8147a1b (patch)
tree5a7212555d8511df16fe6bbcc54b863ec9463b46 /src/main.rs
parent36d2818d39e61b752923e253f8455f50510cb428 (diff)
downloadmyslip-fdae943090463526423f5e43e72cd2f0e8147a1b.tar.gz
myslip-fdae943090463526423f5e43e72cd2f0e8147a1b.zip
Added repl and some documentation. Improved error messages. Removed dead code.
* Removed same_variant in parse::parsetree * Added SExp::multistep (for use of the repl) Improved error messages: * Added parenthesis around types * Changed how errors propagate inferring generics: added the error variant ArgumentsDontMatchGeneric, implemented the displaying of it, added tests for it. * Had to change some tests to match for the new changes
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}")),
+ }
+
+}
+