From a7c6caec95af6e5473040c30ebf72372a2fccc52 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Dec 2016 15:14:56 -0500 Subject: [PATCH] Attempt to write tree printing for the parse tree I have _no_ idea if this will work or not. --- src/parser/nodes/mod.rs | 24 ++++++++++++++++++++---- src/parser/nodes/program.rs | 14 ++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/parser/nodes/mod.rs b/src/parser/nodes/mod.rs index 28a824d..83aeedc 100644 --- a/src/parser/nodes/mod.rs +++ b/src/parser/nodes/mod.rs @@ -9,16 +9,32 @@ pub use self::constant::Constant; mod constant; mod program; +use self::constant::ConstantValue; + +trait Expression {} +impl fmt::Debug for Expression { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Expression") + } +} +impl TreeDebug for Expression {} +impl Expression for Constant {} + +/// Conveniently print out a node in the tree trait TreeDebug: fmt::Debug { - fn tree_indent(indent: u8) -> String { - (0..10).fold(String::new(), |mut acc, i| { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_fmt(f, 0) + } + + fn tree_indent(&self, indent: u8) -> String { + (0..10).fold(String::new(), |mut acc, _| { acc.push(' '); acc }) } fn tree_fmt(&self, f: &mut fmt::Formatter, indent: u8) -> fmt::Result { - write!(f, "{}{:?}", self.tree_indent(indent), self) + let spaces: String = self.tree_indent(indent); + write!(f, "{}{:?}", spaces, self) } } - diff --git a/src/parser/nodes/program.rs b/src/parser/nodes/program.rs index 0c47ce0..cdf9ed6 100644 --- a/src/parser/nodes/program.rs +++ b/src/parser/nodes/program.rs @@ -5,17 +5,23 @@ use std::fmt; use super::TreeDebug; -use super::Constant; +use super::Expression; pub struct Program { - forms: Vec, + forms: Vec>, +} + +impl fmt::Debug for Program { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_fmt(f, 0) + } } impl TreeDebug for Program { - fn tree_fmt(&self, f: fmt::Formatter, indent: u8) -> fmt::Result { + fn tree_fmt(&self, f: &mut fmt::Formatter, indent: u8) -> fmt::Result { let spaces = self.tree_indent(indent); let mut result = write!(f, "{}Program", spaces); - for form in self.forms { + for form in self.forms.iter() { if result.is_err() { break; }