-
Notifications
You must be signed in to change notification settings - Fork 63
Better node display #137
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
Open
benwr
wants to merge
8
commits into
ISibboI:main
Choose a base branch
from
benwr:better_node_display_rebased
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Better node display #137
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6945425
better node display (recognizable expressions)
benwr 9002e51
don't add unnecessary parens to root node
benwr 5f1fe35
cargo fmt
benwr 2fe6e36
reorganize sequence display
benwr 6894f36
obey precision when printing floats
benwr 3593b8f
add some tests for Node's display instance; make a couple tweaks
benwr 8170049
Add test for altered float precision.
ISibboI e46b2f3
Fix syntax.
ISibboI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,129 @@ | ||
| 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, ";"), | ||
| 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(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_display_with_float_precision() { | ||
| assert_eq!( | ||
| &format!( | ||
| "{:.1}", | ||
| build_operator_tree("0.111").expect("Could not build operator tree"), | ||
| ), | ||
| "0.1", | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.