Attempt to write tree printing for the parse tree
I have _no_ idea if this will work or not.
This commit is contained in:
parent
7f6ef1ac1e
commit
a7c6caec95
2 changed files with 30 additions and 8 deletions
|
@ -9,16 +9,32 @@ pub use self::constant::Constant;
|
||||||
mod constant;
|
mod constant;
|
||||||
mod program;
|
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<T: constant::ConstantValue> Expression for Constant<T> {}
|
||||||
|
|
||||||
|
/// Conveniently print out a node in the tree
|
||||||
trait TreeDebug: fmt::Debug {
|
trait TreeDebug: fmt::Debug {
|
||||||
fn tree_indent(indent: u8) -> String {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
(0..10).fold(String::new(), |mut acc, i| {
|
self.tree_fmt(f, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tree_indent(&self, indent: u8) -> String {
|
||||||
|
(0..10).fold(String::new(), |mut acc, _| {
|
||||||
acc.push(' ');
|
acc.push(' ');
|
||||||
acc
|
acc
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tree_fmt(&self, f: &mut fmt::Formatter, indent: u8) -> fmt::Result {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,17 +5,23 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use super::TreeDebug;
|
use super::TreeDebug;
|
||||||
use super::Constant;
|
use super::Expression;
|
||||||
|
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
forms: Vec<Constant>,
|
forms: Vec<Box<Expression>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Program {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
self.tree_fmt(f, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeDebug for Program {
|
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 spaces = self.tree_indent(indent);
|
||||||
let mut result = write!(f, "{}Program", spaces);
|
let mut result = write!(f, "{}Program", spaces);
|
||||||
for form in self.forms {
|
for form in self.forms.iter() {
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue