From 9418cd5b70bdfebe183d32d6d2630a48c0748289 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 14 Sep 2024 14:31:40 -0400 Subject: [PATCH] Add "styled" CLI option and debug logging for disassembler --- dust-lang/src/chunk.rs | 30 ++++++++++++++++++++++-------- dust-shell/src/main.rs | 14 ++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/dust-lang/src/chunk.rs b/dust-lang/src/chunk.rs index 3634250..dfb339e 100644 --- a/dust-lang/src/chunk.rs +++ b/dust-lang/src/chunk.rs @@ -211,7 +211,9 @@ impl Display for Chunk { write!( f, "{}", - self.disassembler("Chunk Display").styled().disassemble() + self.disassembler("Chunk Display") + .styled(true) + .disassemble() ) } } @@ -221,7 +223,9 @@ impl Debug for Chunk { write!( f, "{}", - self.disassembler("Chunk Debug Display").disassemble() + self.disassembler("Chunk Debug Display") + .styled(false) + .disassemble() ) } } @@ -309,9 +313,6 @@ impl<'a> ChunkDisassembler<'a> { }; let mut disassembled = String::with_capacity(self.predict_length()); - - println!("capactity: {}", disassembled.capacity()); - let name_line = style(center(self.name)); disassembled.push_str(&name_line); @@ -376,7 +377,20 @@ impl<'a> ChunkDisassembler<'a> { disassembled.push_str(¢er(&local_display)); } - println!("length: {}", disassembled.len()); + let expected_length = self.predict_length(); + let actual_length = disassembled.len(); + + if !self.styled && expected_length != actual_length { + log::debug!( + "Chunk disassembly was not optimized correctly, expected string length {expected_length}, got {actual_length}", + ); + } + + if self.styled && expected_length > actual_length { + log::debug!( + "Chunk disassembly was not optimized correctly, expected string length to be at least{expected_length}, got {actual_length}", + ); + } disassembled } @@ -387,8 +401,8 @@ impl<'a> ChunkDisassembler<'a> { self } - pub fn styled(&mut self) -> &mut Self { - self.styled = true; + pub fn styled(&mut self, styled: bool) -> &mut Self { + self.styled = styled; self } diff --git a/dust-shell/src/main.rs b/dust-shell/src/main.rs index b173a16..5ea02cc 100644 --- a/dust-shell/src/main.rs +++ b/dust-shell/src/main.rs @@ -13,6 +13,9 @@ struct Cli { #[arg(short, long)] parse: bool, + #[arg(short, long)] + styled: bool, + path: Option, } @@ -43,7 +46,7 @@ fn main() { if let Some(command) = &args.command { if args.parse { - parse_and_display_errors(command); + parse_and_display_errors(command, args.styled); } else { run_and_display_errors(command); } @@ -51,18 +54,21 @@ fn main() { let source = read_to_string(path).expect("Failed to read file"); if args.parse { - parse_and_display_errors(&source); + parse_and_display_errors(&source, args.styled); } else { run_and_display_errors(&source); } } } -fn parse_and_display_errors(source: &str) { +fn parse_and_display_errors(source: &str, pretty_print: bool) { match parse(source) { Ok(chunk) => println!( "{}", - chunk.disassembler("Dust CLI Input").styled().disassemble() + chunk + .disassembler("Dust CLI Input") + .styled(pretty_print) + .disassemble() ), Err(error) => { eprintln!("{}", error.report());