From 6945425e6e53851a129b6f33c84d20426f291410 Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sat, 3 Jun 2023 11:35:43 -0700 Subject: [PATCH 1/8] better node display (recognizable expressions) --- src/tree/display.rs | 77 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index fbfff95..eaad627 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -1,12 +1,79 @@ -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 => write_sequence(f, self, ""), + 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, ";"), + 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(()) + }, } - Ok(()) } } From 9002e5103e49bd5ba11b9dead23870ded4339514 Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sat, 3 Jun 2023 12:17:47 -0700 Subject: [PATCH 2/8] don't add unnecessary parens to root node --- src/tree/display.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index eaad627..7e86fed 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -12,7 +12,19 @@ fn write_binary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> { } fn write_unary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> { - write!(f, "{}{}", op, node.children.get(0).ok_or(Error)?,) + write!( + f, + "{}{}", + op, + node.children.get(0).ok_or(Error)?, + ) +} + +fn write_root(f: &mut Formatter, node: &Node) -> Result<(), Error> { + for c in node.children.iter() { + write!(f, "{}", c)? + } + Ok(()) } fn write_sequence(f: &mut Formatter, node: &Node, sep: &str) -> Result<(), Error> { @@ -29,7 +41,7 @@ fn write_sequence(f: &mut Formatter, node: &Node, sep: &str) -> Result<(), Error impl Display for Node { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { match &self.operator { - Operator::RootNode => write_sequence(f, self, ""), + Operator::RootNode => write_root(f, self), Operator::Add => write_binary(f, self, "+"), Operator::Sub => write_binary(f, self, "-"), Operator::Neg => write_unary(f, self, "-"), From 5f1fe35650d689ded93528f61b58946efd8f92ba Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sat, 3 Jun 2023 12:22:50 -0700 Subject: [PATCH 3/8] cargo fmt --- src/tree/display.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index 7e86fed..25bb404 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -12,12 +12,7 @@ fn write_binary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> { } fn write_unary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> { - write!( - f, - "{}{}", - op, - node.children.get(0).ok_or(Error)?, - ) + write!(f, "{}{}", op, node.children.get(0).ok_or(Error)?,) } fn write_root(f: &mut Formatter, node: &Node) -> Result<(), Error> { From 2fe6e36c2b4700f4b52670503dcc7a5896de4956 Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sun, 4 Jun 2023 14:42:51 -0700 Subject: [PATCH 4/8] reorganize sequence display --- src/tree/display.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index 25bb404..074c501 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -15,28 +15,21 @@ fn write_unary(f: &mut Formatter, node: &Node, op: &str) -> Result<(), Error> { write!(f, "{}{}", op, node.children.get(0).ok_or(Error)?,) } -fn write_root(f: &mut Formatter, node: &Node) -> Result<(), Error> { - for c in node.children.iter() { - write!(f, "{}", c)? - } - Ok(()) -} 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, ")") + Ok(()) } impl Display for Node { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { match &self.operator { - Operator::RootNode => write_root(f, self), + Operator::RootNode => write_sequence(f, self, ""), Operator::Add => write_binary(f, self, "+"), Operator::Sub => write_binary(f, self, "-"), Operator::Neg => write_unary(f, self, "-"), @@ -62,7 +55,11 @@ impl Display for Node { 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::Tuple => { + write!(f, "(")?; + write_sequence(f, self, ",")?; + write!(f, ")") + }, Operator::Chain => write_sequence(f, self, ";"), Operator::Const { value } => write!(f, "{}", value), Operator::VariableIdentifierWrite { identifier } => { From 6894f3608e8c44ccc07c77bb79335f4b02f3d54b Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sun, 4 Jun 2023 14:57:19 -0700 Subject: [PATCH 5/8] obey precision when printing floats --- src/value/display.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/value/display.rs b/src/value/display.rs index 599baa4..b7e8e35 100644 --- a/src/value/display.rs +++ b/src/value/display.rs @@ -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) => { From 3593b8f7198e1e89c53c370dcba2d87173778be5 Mon Sep 17 00:00:00 2001 From: Ben Weinstein-Raun Date: Sun, 4 Jun 2023 15:51:14 -0700 Subject: [PATCH 6/8] add some tests for Node's display instance; make a couple tweaks --- src/tree/display.rs | 50 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index 074c501..3fbb8fe 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -15,21 +15,29 @@ 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)?; } } - Ok(()) + write!(f, ")") } impl Display for Node { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { match &self.operator { - Operator::RootNode => write_sequence(f, self, ""), + 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, "-"), @@ -55,11 +63,7 @@ impl Display for Node { Operator::ExpAssign => write_binary(f, self, "^="), Operator::AndAssign => write_binary(f, self, "&&="), Operator::OrAssign => write_binary(f, self, "||="), - Operator::Tuple => { - write!(f, "(")?; - write_sequence(f, self, ",")?; - write!(f, ")") - }, + Operator::Tuple => write_sequence(f, self, ","), Operator::Chain => write_sequence(f, self, ";"), Operator::Const { value } => write!(f, "{}", value), Operator::VariableIdentifierWrite { identifier } => { @@ -81,3 +85,33 @@ impl Display for Node { } } } + +#[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 + ); + } + } +} From 817004952ac1e386b8cacb9c9e2e54402b9a14ce Mon Sep 17 00:00:00 2001 From: ISibboI Date: Tue, 27 Jun 2023 11:06:15 +0300 Subject: [PATCH 7/8] Add test for altered float precision. --- src/tree/display.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tree/display.rs b/src/tree/display.rs index 3fbb8fe..88d3b6e 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -89,6 +89,7 @@ impl Display for Node { #[cfg(test)] mod tests { use crate::build_operator_tree; + #[test] fn test_display() { let pairs = [ @@ -114,4 +115,16 @@ mod tests { ); } } + + #[test] + fn test_display_with_float_precision() { + assert_eq!( + &format!( + "{:.1}", + build_operator_tree("0.111").expect("Could not build operator tree") + ), + "0.1" + ); + } + } } From e46b2f350cea63eb417ad6c1297c1187f3403f90 Mon Sep 17 00:00:00 2001 From: ISibboI Date: Tue, 27 Jun 2023 11:08:13 +0300 Subject: [PATCH 8/8] Fix syntax. --- src/tree/display.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/tree/display.rs b/src/tree/display.rs index 88d3b6e..031a439 100644 --- a/src/tree/display.rs +++ b/src/tree/display.rs @@ -109,22 +109,21 @@ mod tests { assert_eq!( &&format!( "{}", - build_operator_tree(i).expect("Could not build operator tree") + build_operator_tree(i).expect("Could not build operator tree"), ), - o + o, ); } } #[test] fn test_display_with_float_precision() { - assert_eq!( - &format!( - "{:.1}", - build_operator_tree("0.111").expect("Could not build operator tree") - ), - "0.1" - ); - } + assert_eq!( + &format!( + "{:.1}", + build_operator_tree("0.111").expect("Could not build operator tree"), + ), + "0.1", + ); } }