diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index b507cbd..8a09cfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use myslip::parse::parsetree::parse_to_ast; -use myslip::sexp::{SExp::*, SLeaf::{Nil, Let}, util::scons}; +use myslip::sexp::{SExp, SExp::*, SLeaf::Nil, util::scons}; +use myslip::r#type::Type::NilType; use std::{io, io::Write}; fn main() { @@ -26,7 +27,7 @@ fn repl() -> Result<(), io::Error> { let stdin = io::stdin(); let mut stdout = io::stdout(); - let mut binds = vec![]; + let mut binds: Vec<SExp> = vec![]; loop { @@ -36,7 +37,7 @@ fn repl() -> Result<(), io::Error> { break; } - let mut expression = match parse_to_ast(&input) { + let orig_expression = match parse_to_ast(&input) { Ok(SCons(a, b)) if *b == Atom(Nil) => *a, Ok(t) => t, Err(e) => { @@ -44,22 +45,27 @@ fn repl() -> Result<(), io::Error> { continue; }, }; - - match &expression { - SCons(l, _) if **l == Atom(Let) => match scons(expression.clone(), Nil).type_check() { - Ok(_) => { - binds.push(expression); - println!("Bind saved"); + if let Some((name, _value)) = orig_expression.clone().check_let() { + let mut expr = scons(orig_expression.clone(), scons(Nil, Nil)); + for i in 1..=binds.len() { + expr = scons(binds[binds.len() - i].clone(), expr); + } + match expr.type_check() { + Ok(NilType) => { + binds.push(orig_expression); + println!("{name} saved"); continue; }, Err(e) => { - println!("Type error: {e}"); + println!("Type error: {}", e); continue; - } - }, - _ => (), + }, + Ok(_) => (), + } } + let mut expression = orig_expression.clone(); + for i in 1..=binds.len() { expression = scons(binds[binds.len() - i].clone(), expression); } |