From 09a3791cd8194fef28be95305835d4851eb0a854 Mon Sep 17 00:00:00 2001 From: FranchuFranchu <38839219+FranchuFranchu@users.noreply.github.com> Date: Tue, 19 Mar 2024 12:57:40 -0300 Subject: [PATCH] [sc-524] Change Display impl of `Book` and `Net` to include lines between empty defs and indent redexes (#90) Co-authored-by: tjjfvi --- src/ast.rs | 9 ++++++--- tests/cli.rs | 19 +++++++++++++------ tests/transform.rs | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 0e9adb7f..a9182fb2 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -458,8 +458,11 @@ impl FromStr for Tree { impl fmt::Display for Book { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for (name, net) in self.iter() { - writeln!(f, "@{name} = {net}")?; + for (i, (name, net)) in self.iter().enumerate() { + if i != 0 { + f.write_str("\n\n")?; + } + write!(f, "@{name} = {net}")?; } Ok(()) } @@ -469,7 +472,7 @@ impl fmt::Display for Net { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", &self.root)?; for (a, b) in &self.redexes { - write!(f, "\n& {a} ~ {b}")?; + write!(f, "\n & {a} ~ {b}")?; } Ok(()) } diff --git a/tests/cli.rs b/tests/cli.rs index 0435285e..b42dc098 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -126,12 +126,16 @@ fn test_cli_transform() { ]).unwrap().1, @r###" @add = (<+ a b> (a b)) + @div = ( (a b)) + @main = ({3 <% c d>} ({5 a c} [b d])) + @mod = (<% a b> (a b)) + @mul = (<* a b> (a b)) - @sub = (<- a b> (a b)) + @sub = (<- a b> (a b)) "### ); @@ -144,14 +148,18 @@ fn test_cli_transform() { ]).unwrap().1, @r###" @add = (<+ a b> (a b)) + @div = ( (a b)) + @main = ({3 a b} ({5 c d} [e f])) - & @mod ~ (b (d f)) - & @div ~ (a (c e)) + & @mod ~ (b (d f)) + & @div ~ (a (c e)) + @mod = (<% a b> (a b)) + @mul = (<* a b> (a b)) - @sub = (<- a b> (a b)) + @sub = (<- a b> (a b)) "### ); @@ -165,8 +173,7 @@ fn test_cli_transform() { ]).unwrap().1, @r###" @main = a - & @HVM.log ~ (#1 (#2 a)) - + & @HVM.log ~ (#1 (#2 a)) "### ); } diff --git a/tests/transform.rs b/tests/transform.rs index b201f673..0d938561 100644 --- a/tests/transform.rs +++ b/tests/transform.rs @@ -78,7 +78,7 @@ pub fn test_eta() { assert_display_snapshot!(parse_and_reduce("<+ (a b) (a b)>"), @"<+ a a>"); assert_display_snapshot!(parse_and_reduce("(a b) & ((a b) (c d)) ~ (c d) "), @r###" a - & (a c) ~ c + & (a c) ~ c "###); assert_display_snapshot!(parse_and_reduce("((a b) [a b])"), @"((a b) [a b])"); assert_display_snapshot!(parse_and_reduce("((a b c) b c)"), @"((a b) b)");