Skip to content

Commit

Permalink
Don't rely on tabs when formatting instructions (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
irh authored Jan 17, 2025
1 parent 996bddf commit 43ed683
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 99 deletions.
11 changes: 10 additions & 1 deletion crates/bytecode/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ impl Chunk {

/// Returns a [String] displaying the annotated instructions contained in the compiled [Chunk]
pub fn instructions_as_string(chunk: Ptr<Chunk>, source_lines: &[&str]) -> String {
let ip_width = 5 + chunk.bytes.len().ilog10() as usize;

let mut result = String::new();
let mut reader = InstructionReader::new(chunk);
let mut ip = reader.ip;
Expand Down Expand Up @@ -120,7 +122,14 @@ impl Chunk {
span = Some(instruction_span);
}

writeln!(result, "{ip}\t{instruction:?}").ok();
for (i, line) in format!("{instruction:?}").lines().enumerate() {
if i == 0 {
writeln!(result, "{ip:<ip_width$}{line}").ok();
} else {
writeln!(result, "{:ip_width$}{line}", "").ok();
}
}

ip = reader.ip;
}

Expand Down
Loading

0 comments on commit 43ed683

Please sign in to comment.