Implement while-reduction

This commit is contained in:
greg 2015-08-13 21:59:24 -07:00
parent eb5ce2ef9e
commit 9a4760d44f
2 changed files with 13 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::clone::Clone;
use parser::AST;
use parser::AST::*;
@ -67,6 +68,17 @@ fn reduce(evr: EvalResult) -> EvalResult {
}
},
WhileStatement(condition, body) => {
let (continue_loop, env) = reduce((*condition.clone(), env));
match continue_loop {
Null => (DoNothing, env),
_ => {
let (_, new_env) = reduce((*body.clone(), env));
reduce((WhileStatement(condition, body), new_env))
}
}
},
BinOp(op, lhs, rhs) => {
let (reduced_lhs, new_env) = reduce((*lhs, env));
let (reduced_rhs, new_env2) = reduce((*rhs, new_env));

View File

@ -4,7 +4,7 @@ use std::iter::Peekable;
use tokenizer::{Token, Kw};
use tokenizer::Token::*;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum AST {
Null,
Name(String),