Environment persistent across repl loop
This commit is contained in:
parent
08f1092b69
commit
f88f115567
@ -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));
|
let (mut reduced_ast, final_env) = reduce((ast, env));
|
||||||
|
|
||||||
match reduced_ast {
|
let output = match reduced_ast {
|
||||||
DoNothing => "".to_string(),
|
DoNothing => "".to_string(),
|
||||||
Number(n) => return format!("{}", n),
|
Number(n) => format!("{}", n),
|
||||||
LangString(s) => return format!("\"{}\"", s),
|
LangString(s) => format!("\"{}\"", s),
|
||||||
Null => "null".to_string(),
|
Null => "null".to_string(),
|
||||||
_ => return "not implemented".to_string()
|
_ => "not implemented".to_string()
|
||||||
}
|
};
|
||||||
|
|
||||||
|
(output, final_env)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce(evr: EvalResult) -> EvalResult {
|
fn reduce(evr: EvalResult) -> EvalResult {
|
||||||
|
@ -21,6 +21,7 @@ fn repl() {
|
|||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
let mut buf = String::with_capacity(20);
|
let mut buf = String::with_capacity(20);
|
||||||
|
let mut env = Environment::new();
|
||||||
loop {
|
loop {
|
||||||
buf.clear();
|
buf.clear();
|
||||||
print!(">> ");
|
print!(">> ");
|
||||||
@ -44,9 +45,9 @@ fn repl() {
|
|||||||
ParseResult::Ok(ast) => {
|
ParseResult::Ok(ast) => {
|
||||||
println!("AST: {:?}", ast);
|
println!("AST: {:?}", ast);
|
||||||
|
|
||||||
let env = Environment::new();
|
let (eval, new_env) = evaluate(ast, env);
|
||||||
let eval = evaluate(ast, env);
|
|
||||||
println!("{}", eval);
|
println!("{}", eval);
|
||||||
|
env = new_env;
|
||||||
},
|
},
|
||||||
ParseResult::Err(err) => println!("Error: {}", err)
|
ParseResult::Err(err) => println!("Error: {}", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user