Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better node display #137

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 110 additions & 5 deletions src/tree/display.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,117 @@
use crate::Node;
use crate::{Node, Operator};
use std::fmt::{Display, Error, Formatter};

fn write_binary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> {
write!(
f,
"({} {} {})",
node.children.get(0).ok_or(Error)?,
op,
node.children.get(1).ok_or(Error)?
)
}

fn write_unary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> {
write!(f, "{}{}", op, node.children.get(0).ok_or(Error)?,)
}

fn write_sequence(f: &mut Formatter, node: &Node, sep: &str) -> Result<(), Error> {
write!(f, "(")?;
for (i, c) in node.children.iter().enumerate() {
write!(f, "{}", c)?;
if i + 1 < node.children.len() {
write!(f, "{} ", sep)?;
}
}
write!(f, ")")
}

impl Display for Node {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
self.operator.fmt(f)?;
for child in self.children() {
write!(f, " {}", child)?;
match &self.operator {
Operator::RootNode => {
for (i, c) in self.children.iter().enumerate() {
write!(f, "{}", c)?;
if i + 1 < self.children.len() {
write!(f, " ")?;
}
}
Ok(())
},
Operator::Add => write_binary(f, self, "+"),
Operator::Sub => write_binary(f, self, "-"),
Operator::Neg => write_unary(f, self, "-"),
Operator::Mul => write_binary(f, self, "*"),
Operator::Div => write_binary(f, self, "/"),
Operator::Mod => write_binary(f, self, "%"),
Operator::Exp => write_binary(f, self, "^"),
Operator::Eq => write_binary(f, self, "=="),
Operator::Neq => write_binary(f, self, "!="),
Operator::Gt => write_binary(f, self, ">"),
Operator::Lt => write_binary(f, self, "<"),
Operator::Geq => write_binary(f, self, ">="),
Operator::Leq => write_binary(f, self, "<="),
Operator::And => write_binary(f, self, "&&"),
Operator::Or => write_binary(f, self, "||"),
Operator::Not => write_unary(f, self, "!"),
Operator::Assign => write_binary(f, self, "="),
Operator::AddAssign => write_binary(f, self, "+="),
Operator::SubAssign => write_binary(f, self, "-="),
Operator::MulAssign => write_binary(f, self, "*="),
Operator::DivAssign => write_binary(f, self, "/="),
Operator::ModAssign => write_binary(f, self, "%="),
Operator::ExpAssign => write_binary(f, self, "^="),
Operator::AndAssign => write_binary(f, self, "&&="),
Operator::OrAssign => write_binary(f, self, "||="),
Operator::Tuple => write_sequence(f, self, ","),
Operator::Chain => write_sequence(f, self, ";"),
ISibboI marked this conversation as resolved.
Show resolved Hide resolved
Operator::Const { value } => write!(f, "{}", value),
Operator::VariableIdentifierWrite { identifier } => {
write!(f, "{}", identifier)
},
Operator::VariableIdentifierRead { identifier } => {
write!(f, "{}", identifier)
},
Operator::FunctionIdentifier { identifier } => {
write!(f, "{} ", identifier)?;
for (i, c) in self.children.iter().enumerate() {
write!(f, "{}", c)?;
if i + 1 < self.children.len() {
write!(f, " ")?;
}
}
Ok(())
},
}
}
}

#[cfg(test)]
mod tests {
use crate::build_operator_tree;
#[test]
fn test_display() {
let pairs = [
("", ""),
("0", "0"),
("0.1", "0.1"),
("f (1, 2)", "f (1, 2)"),
("()", ""),
("a; b", "(a; b)"),
("a = b", "(a = b)"),
("5 + 3", "(5 + 3)"),
("5 + 3 * 2", "(5 + (3 * 2))"),
("f (g 4)", "f g 4"),
("f (g 4; 5)", "f (g 4; 5)"),
];
for (i, o) in pairs.iter() {
assert_eq!(
&&format!(
"{}",
build_operator_tree(i).expect("Could not build operator tree")
),
o
);
}
Ok(())
}
}
8 changes: 7 additions & 1 deletion src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ impl Display for Value {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
Value::String(string) => write!(f, "\"{}\"", string),
Value::Float(float) => write!(f, "{}", float),
Value::Float(float) => {
if let Some(precision) = f.precision() {
write!(f, "{1:.*}", precision, float)
} else {
write!(f, "{}", float)
}
},
Value::Int(int) => write!(f, "{}", int),
Value::Boolean(boolean) => write!(f, "{}", boolean),
Value::Tuple(tuple) => {
Expand Down