First pass at evaluation

Very incomplete
This commit is contained in:
greg 2015-07-29 00:52:17 -07:00
parent 2989ac338c
commit d2108f0f97
3 changed files with 34 additions and 7 deletions

25
src/evaluate.rs Normal file
View File

@ -0,0 +1,25 @@
use parser::AST;
use parser::AST::*;
pub fn evaluate(ast: AST) -> String {
match reduce(ast) {
None => return "error".to_string(),
Some(DoNothing) => "".to_string(),
Some(Number(n)) => return format!("{}", n),
Some(LangString(s)) => return format!("\"{}\"", s),
_ => return "not implemented".to_string()
}
}
fn reduce(ast: AST) -> Option<AST> {
match ast {
Statements(stmts) => {
let mut reduction = Some(DoNothing);
for stmt in stmts.into_iter() {
reduction = reduce(stmt);
}
reduction
},
other_ast => Some(other_ast)
}
}

View File

@ -5,9 +5,11 @@ use std::process;
use tokenizer::tokenize; use tokenizer::tokenize;
use parser::{parse, ParseResult}; use parser::{parse, ParseResult};
use evaluate::evaluate;
mod tokenizer; mod tokenizer;
mod parser; mod parser;
mod evaluate;
fn main() { fn main() {
@ -39,14 +41,13 @@ fn repl() {
println!("Tokens: {:?}", tokens); println!("Tokens: {:?}", tokens);
match parse(tokens) { match parse(tokens) {
ParseResult::Ok(ast) => println!("AST: {:?}", ast), ParseResult::Ok(ast) => {
println!("AST: {:?}", ast);
let eval = evaluate(ast);
println!("{}", eval);
},
ParseResult::Err(err) => println!("Error: {}", err) ParseResult::Err(err) => println!("Error: {}", err)
} }
/*
let eval = evaluate(&ast);
println!("{}", eval);
*/
}, },
Err(err) => { Err(err) => {
println!("Error: {}", err); println!("Error: {}", err);

View File

@ -13,7 +13,8 @@ pub enum AST {
Binding(String, Box<AST>), Binding(String, Box<AST>),
Statements(Vec<AST>), Statements(Vec<AST>),
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>), IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>),
WhileStatement(Box<AST>, Box<AST>) WhileStatement(Box<AST>, Box<AST>),
DoNothing
} }
pub enum ParseResult { pub enum ParseResult {