Add basic evaluation
This commit is contained in:
parent
70bf68d9bd
commit
16e8d969be
43
src/eval.rs
Normal file
43
src/eval.rs
Normal 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!(),
|
||||
}
|
||||
}
|
||||
}
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user