From f88f115567470d4be6f8b5656e51173975762cad Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 31 Jul 2015 01:35:39 -0700 Subject: [PATCH] Environment persistent across repl loop --- src/evaluate.rs | 14 ++++++++------ src/main.rs | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/evaluate.rs b/src/evaluate.rs index 2924930..c2ff8d7 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -35,17 +35,19 @@ impl Environment { } } -pub fn evaluate(ast: AST, env: Environment) -> String { +pub fn evaluate(ast: AST, env: Environment) -> (String, Environment) { let (mut reduced_ast, final_env) = reduce((ast, env)); - match reduced_ast { + let output = match reduced_ast { DoNothing => "".to_string(), - Number(n) => return format!("{}", n), - LangString(s) => return format!("\"{}\"", s), + Number(n) => format!("{}", n), + LangString(s) => format!("\"{}\"", s), Null => "null".to_string(), - _ => return "not implemented".to_string() - } + _ => "not implemented".to_string() + }; + + (output, final_env) } fn reduce(evr: EvalResult) -> EvalResult { diff --git a/src/main.rs b/src/main.rs index 51b21ff..8d7774c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ fn repl() { let stdin = io::stdin(); let mut stdout = io::stdout(); let mut buf = String::with_capacity(20); + let mut env = Environment::new(); loop { buf.clear(); print!(">> "); @@ -44,9 +45,9 @@ fn repl() { ParseResult::Ok(ast) => { println!("AST: {:?}", ast); - let env = Environment::new(); - let eval = evaluate(ast, env); + let (eval, new_env) = evaluate(ast, env); println!("{}", eval); + env = new_env; }, ParseResult::Err(err) => println!("Error: {}", err) }