From be36d4697d5477a926f61d5d65b0daa186d81609 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 22 Jan 2016 02:55:07 -0800 Subject: [PATCH] Pretty-print evaluated AST nodes --- src/eval.rs | 2 +- src/parser.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/eval.rs b/src/eval.rs index 53a79a5..7bd90c1 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -73,7 +73,7 @@ impl Evaluator { } } - format!("{:?}", node) //TODO make better + format!("{}", node) //TODO make better } fn step(&mut self, node: ASTNode) -> ASTNode { diff --git a/src/parser.rs b/src/parser.rs index e9e9f6f..e9b8cbe 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,3 +1,4 @@ +use std::fmt; use tokenizer::{Token, Kw, Op}; /* Grammar @@ -45,6 +46,28 @@ pub enum Expression { Call(String, Vec), } +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; type Precedence = u8;