aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-11 01:46:39 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-11 01:46:39 +0300
commitc2a293e46e6bf7563138ea852191ae70a7b7652e (patch)
tree24d71d74bc85af825857a30b80b7c0c7613ac249 /src
parent633756fc83273b0f1f2411838844339692bf21db (diff)
downloadmyslip-c2a293e46e6bf7563138ea852191ae70a7b7652e.tar.gz
myslip-c2a293e46e6bf7563138ea852191ae70a7b7652e.zip
Fixed bug in repl not allowing using variables previously bound in REPL in new binding expression
Diffstat (limited to 'src')
-rw-r--r--src/main.rs32
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);
}