Environment persistent across repl loop

This commit is contained in:
greg 2015-07-31 01:35:39 -07:00
parent 08f1092b69
commit f88f115567
2 changed files with 11 additions and 8 deletions

View File

@ -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 {

View File

@ -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)
}