From 16e8d969be492a0f720c88d9435606d9bbb49025 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 18 Jan 2016 02:24:14 -0800 Subject: [PATCH] Add basic evaluation --- src/eval.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/eval.rs diff --git a/src/eval.rs b/src/eval.rs new file mode 100644 index 0000000..77aae78 --- /dev/null +++ b/src/eval.rs @@ -0,0 +1,43 @@ +use parser::{AST, ASTNode}; + +pub struct Evaluator { + data: bool, +} + +impl Evaluator { + + pub fn new() -> Evaluator { + Evaluator { data: false } + } + + pub fn run(&mut self, ast: AST) -> Vec { + ast.into_iter().map(|astnode| { + self.reduce_node(astnode) + }).collect() + } +} + +impl Evaluator { + fn reduce_node(&mut self, mut node: ASTNode) -> String { + loop { + let (newnode, finished) = self.step(node); + node = newnode; + if finished { + break + } + } + + format!("{:?}", node) //TODO make better + } + + fn step(&mut self, node: ASTNode) -> (ASTNode, bool) { + use parser::ASTNode::*; + use parser::Expression::*; + println!("Doing one step"); + match node { + n@ExprNode(StringLiteral(_)) => (n, true), + n@ExprNode(Number(_)) => (n, true), + _ => unimplemented!(), + } + } +} diff --git a/src/main.rs b/src/main.rs index 9640465..a7112c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,9 @@ mod tokenizer; use parser::{parse}; mod parser; +use eval::{Evaluator}; +mod eval; + fn main() { let args: Vec = std::env::args().collect(); println!("Schala v 0.02"); @@ -73,5 +76,9 @@ fn repl_handler(input: &str, state: &mut InterpreterState) -> String { println!("AST: {:?}", ast); } - format!("{:?}", ast) + let mut evaluator = Evaluator::new(); + let mut output: Vec = evaluator.run(ast); + + //for now only handle last output + output.pop().unwrap_or("".to_string()) }