From b48c007bcaa79b250507c9bec03dba9a5d7f7299 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 9 Feb 2016 20:49:30 -0800 Subject: [PATCH] Get rid of old evaluator file No longer need it --- src/evaluator.rs | 104 ----------------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 src/evaluator.rs diff --git a/src/evaluator.rs b/src/evaluator.rs deleted file mode 100644 index 988612e..0000000 --- a/src/evaluator.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* -use parser::AST; - -struct Evaluator { - ast: AST -} - -pub type EvaluatorResult = Result; - -struct EvaluatorError { - err: String -} - -impl Evaluator { - pub fn run(self) -> String { - match reduce_full(self.ast) { - Err(e) => format!("{}", e.err), - Ok(ast) => format!("{}", ast) - } - } -} - -impl AST { - fn can_reduce(&self) -> bool { - use parser::AST::*; - match self { - &Number(_) => false, - &Name(_) => false, - _ => true - } - } -} - -fn reduce_full(ast: AST) -> EvaluatorResult { - let mut ast = ast; - while ast.can_reduce() { - ast = try!(reduce_step(ast)); - } - - Ok(ast) -} - -fn reduce_step(ast: AST) -> EvaluatorResult { - use parser::AST::*; - match ast { - Definition(name, value) => { - Ok(*value) - }, - Block(mut block_nodes) => { - match block_nodes.pop() { - None => Err(EvaluatorError { err: format!("Block with no statements") }), - Some(node) => reduce_full(node), - } - }, - BinOp(left, op, right) => { - let left = try!(reduce_full(*left)); - let op = try!(reduce_full(*op)); - let right = try!(reduce_full(*right)); - match (left, op, right) { - (Number(l), Name(op), Number(r)) => { - match &op[..] { - "+" => Ok(Number(l + r)), - "-" => Ok(Number(l - r)), - "*" => Ok(Number(l * r)), - "/" => { - if r == 0.0 { - Err(EvaluatorError { err: format!("Divide by zero") }) - } else { - Ok(Number(l / r)) - } - }, - _ => Err(EvaluatorError { err: format!("Bad BinOp operator") }) - } - }, - _ => Err(EvaluatorError { - err: format!("Bad arguments for BinOp") - }) - } - }, - Number(_) => Ok(ast), - Name(_) => Ok(ast), - - } -} - -pub fn evaluate(ast: AST) -> String { - let ev = Evaluator { ast: ast }; - ev.run() -} - -#[cfg(test)] -mod tests { - use tokenizer::tokenize; - use parser::{parse, AST}; - use super::evaluate; - - #[test] - fn test_basic_evaluation() { - let input = "1 + 2\n"; - let output = evaluate(parse(tokenize(input)).unwrap()); - assert_eq!(output, "3"); - } -} -*/