Add boilerplate for evaluation

Just wires everything up, doesn't actually evaluate yet
This commit is contained in:
greg 2015-12-30 15:09:31 -08:00
parent 6da20cbfaf
commit 1c23329656
3 changed files with 50 additions and 1 deletions

30
src/evaluator.rs Normal file
View File

@ -0,0 +1,30 @@
use parser::AST;
struct Evaluator {
ast: AST
}
impl Evaluator {
pub fn run(&mut self) -> String {
while self.ast.can_reduce() {
self.ast.reduce();
}
format!("{}", self.ast)
}
}
impl AST {
fn can_reduce(&self) -> bool {
false
}
fn reduce(&mut self) {
}
}
pub fn evaluate(ast: AST) -> String {
let mut ev = Evaluator { ast: ast };
ev.run()
}

View File

@ -14,6 +14,9 @@ mod tokenizer;
use parser::{ParseResult, parse};
mod parser;
use evaluator::{evaluate};
mod evaluator;
fn main() {
let args: Vec<String> = std::env::args().collect();
@ -63,5 +66,14 @@ fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
if state.show_parse {
println!("Parse: {:?}", parse(tokenize(input)))
}
format!("{:?}", parse(tokenize(input)))
let parse_result = parse(tokenize(input));
match parse_result {
Ok(ast) => {
format!("{}", evaluate(ast))
},
Err(err) => {
format!("Parse error: {:?}", err)
}
}
}

View File

@ -1,5 +1,6 @@
use std::iter::Peekable;
use std::vec::IntoIter;
use std::fmt;
use tokenizer::Token;
@ -10,6 +11,12 @@ pub enum AST {
Name(String),
}
impl fmt::Display for AST {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", "GENERIC DISPLAY")
}
}
#[derive(Debug)]
pub struct ParseError {
err: String