aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TUTORIAL.md6
-rw-r--r--src/main.rs32
2 files changed, 22 insertions, 16 deletions
diff --git a/TUTORIAL.md b/TUTORIAL.md
index 74384b1..1ba6642 100644
--- a/TUTORIAL.md
+++ b/TUTORIAL.md
@@ -89,7 +89,7 @@ false : bool
Values can be bound to variables using the let expression.
```console
> (let x 1)
-Bind saved
+x saved
> x
1 : int
```
@@ -119,11 +119,11 @@ and a function for checking if a integer is between two
others.
```console
> (let ++ (fn a int int (+ a 1)))
-Bind saved
+++ saved
> (++ 1)
2 : int
> (let between (fn (a b c) (int int int) bool (and (< b c) (> b a))))
-Bind saved
+between saved
> (between 1 2 3)
true : bool
> (between 1 0 3)
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);
}