Start reducing ASTs
Start writing code to reduce AST nodes q
This commit is contained in:
parent
5a9ebb188d
commit
785c916ece
68
src/eval.rs
68
src/eval.rs
@ -1,4 +1,4 @@
|
|||||||
use parser::{AST, ASTNode};
|
use parser::{AST, ASTNode, Expression};
|
||||||
|
|
||||||
pub struct Evaluator {
|
pub struct Evaluator {
|
||||||
data: bool,
|
data: bool,
|
||||||
@ -23,12 +23,60 @@ trait Evaluable {
|
|||||||
fn reduce(self) -> Self::Output;
|
fn reduce(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Evaluable for ASTNode {
|
||||||
|
type Output = ASTNode;
|
||||||
|
|
||||||
|
fn is_reducible(&self) -> bool {
|
||||||
|
use parser::ASTNode::*;
|
||||||
|
match self {
|
||||||
|
&ExprNode(ref expr) => expr.is_reducible(),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce(self) -> Self::Output {
|
||||||
|
use parser::ASTNode::*;
|
||||||
|
match self {
|
||||||
|
ExprNode(expr) => {
|
||||||
|
if expr.is_reducible() {
|
||||||
|
ExprNode(expr.reduce())
|
||||||
|
} else {
|
||||||
|
ExprNode(expr)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Evaluable for Expression {
|
||||||
|
type Output = Expression;
|
||||||
|
|
||||||
|
fn is_reducible(&self) -> bool {
|
||||||
|
use parser::Expression::*;
|
||||||
|
match *self {
|
||||||
|
StringLiteral(_) => false,
|
||||||
|
Number(_) => false,
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce(self) -> Self::Output {
|
||||||
|
use parser::Expression::*;
|
||||||
|
match self {
|
||||||
|
e@StringLiteral(_) => e,
|
||||||
|
e@Number(_) => e,
|
||||||
|
Variable(var) => Number(20.0),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Evaluator {
|
impl Evaluator {
|
||||||
fn reduce_node(&mut self, mut node: ASTNode) -> String {
|
fn reduce_node(&mut self, mut node: ASTNode) -> String {
|
||||||
loop {
|
loop {
|
||||||
let (newnode, finished) = self.step(node);
|
node = self.step(node);
|
||||||
node = newnode;
|
if !node.is_reducible() {
|
||||||
if finished {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,14 +84,8 @@ impl Evaluator {
|
|||||||
format!("{:?}", node) //TODO make better
|
format!("{:?}", node) //TODO make better
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step(&mut self, node: ASTNode) -> (ASTNode, bool) {
|
fn step(&mut self, node: ASTNode) -> ASTNode {
|
||||||
use parser::ASTNode::*;
|
println!("Doing one step, current node is {:?}", node);
|
||||||
use parser::Expression::*;
|
node.reduce()
|
||||||
println!("Doing one step");
|
|
||||||
match node {
|
|
||||||
n@ExprNode(StringLiteral(_)) => (n, true),
|
|
||||||
n@ExprNode(Number(_)) => (n, true),
|
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user