From fdae943090463526423f5e43e72cd2f0e8147a1b Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 4 Aug 2025 23:50:46 +0300 Subject: 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 --- src/main.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'src/main.rs') 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}")), + } + +} + -- cgit v1.2.3