Add basic evaluation

This commit is contained in:
greg 2016-01-18 02:24:14 -08:00
parent 70bf68d9bd
commit 16e8d969be
2 changed files with 51 additions and 1 deletions

43
src/eval.rs Normal file
View File

@ -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<String> {
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!(),
}
}
}

View File

@ -14,6 +14,9 @@ mod tokenizer;
use parser::{parse};
mod parser;
use eval::{Evaluator};
mod eval;
fn main() {
let args: Vec<String> = 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<String> = evaluator.run(ast);
//for now only handle last output
output.pop().unwrap_or("".to_string())
}