summaryrefslogtreecommitdiff
path: root/rust/src/lambda.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/lambda.rs')
-rw-r--r--rust/src/lambda.rs33
1 files changed, 32 insertions, 1 deletions
diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs
index 97b6f08..416c31e 100644
--- a/rust/src/lambda.rs
+++ b/rust/src/lambda.rs
@@ -1,5 +1,9 @@
use rand::Rng;
-use std::{collections::HashMap, fs::read_to_string};
+use std::{
+ collections::HashMap,
+ fs::read_to_string,
+ io::{BufRead, BufReader, Read, Write},
+};
mod ast;
use ast::*;
@@ -58,6 +62,33 @@ pub fn eval_file(file_name: &str) -> String {
.join(" ")
}
+pub fn repl<I: Read, O: Write>(inp: &mut I, outp: &mut O) {
+ let mut env = Environment::new();
+ let mut reader = BufReader::new(inp);
+ loop {
+ let mut input = String::new();
+ write!(outp, "> ").unwrap();
+ outp.flush().unwrap();
+ match reader.read_line(&mut input) {
+ Ok(0) => break,
+ Ok(_) => (),
+ Err(e) => {
+ writeln!(outp, "{}", e).unwrap();
+ break;
+ }
+ }
+ let values = parse(&input);
+ let results = values
+ .iter()
+ .map(|v| eval(v, &mut env))
+ .collect::<Vec<Value>>();
+ for result in results {
+ writeln!(outp, "{}", result).unwrap();
+ outp.flush().unwrap();
+ }
+ }
+}
+
fn eval_all(values: &[Value]) -> Vec<Value> {
let mut env = Environment::new();
values.iter().map(|v| eval(v, &mut env)).collect()