Pretty-print evaluated AST nodes

This commit is contained in:
greg 2016-01-22 02:55:07 -08:00
parent ce8c511929
commit be36d4697d
2 changed files with 24 additions and 1 deletions

View File

@ -73,7 +73,7 @@ impl Evaluator {
}
}
format!("{:?}", node) //TODO make better
format!("{}", node) //TODO make better
}
fn step(&mut self, node: ASTNode) -> ASTNode {

View File

@ -1,3 +1,4 @@
use std::fmt;
use tokenizer::{Token, Kw, Op};
/* Grammar
@ -45,6 +46,28 @@ pub enum Expression {
Call(String, Vec<Expression>),
}
impl fmt::Display for ASTNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ASTNode::*;
match self {
&ExprNode(ref expr) => write!(f, "{}", expr),
&FuncNode(_) => write!(f, "UNIMPLEMENTED"),
}
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Expression::*;
match self {
&Null => write!(f, "null"),
&StringLiteral(ref s) => write!(f, "\"{}\"", s),
&Number(n) => write!(f, "{}", n),
_ => write!(f, "UNIMPLEMENTED"),
}
}
}
pub type AST = Vec<ASTNode>;
type Precedence = u8;