First pass at evaluation
Very incomplete
This commit is contained in:
parent
2989ac338c
commit
d2108f0f97
25
src/evaluate.rs
Normal file
25
src/evaluate.rs
Normal 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)
|
||||
}
|
||||
}
|
13
src/main.rs
13
src/main.rs
@ -5,9 +5,11 @@ use std::process;
|
||||
|
||||
use tokenizer::tokenize;
|
||||
use parser::{parse, ParseResult};
|
||||
use evaluate::evaluate;
|
||||
|
||||
mod tokenizer;
|
||||
mod parser;
|
||||
mod evaluate;
|
||||
|
||||
|
||||
fn main() {
|
||||
@ -39,14 +41,13 @@ fn repl() {
|
||||
println!("Tokens: {:?}", 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)
|
||||
}
|
||||
|
||||
/*
|
||||
let eval = evaluate(&ast);
|
||||
println!("{}", eval);
|
||||
*/
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Error: {}", err);
|
||||
|
@ -13,7 +13,8 @@ pub enum AST {
|
||||
Binding(String, Box<AST>),
|
||||
Statements(Vec<AST>),
|
||||
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>),
|
||||
WhileStatement(Box<AST>, Box<AST>)
|
||||
WhileStatement(Box<AST>, Box<AST>),
|
||||
DoNothing
|
||||
}
|
||||
|
||||
pub enum ParseResult {
|
||||
|
Loading…
Reference in New Issue
Block a user