Skip to content

Commit

Permalink
Use macro for debugger printing
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Oct 6, 2024
1 parent 9a95b67 commit bfed798
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
41 changes: 25 additions & 16 deletions src/debugger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// macro_rules! dprint {
// ( $($tt:tt)* ) => {{
// eprint!(concat!("\x1b[{}m", $fmt, "\x1b[0m"), DEBUGGER_COLOR $($tt)*);
// }};
// }
macro_rules! dprintln {
() => {{
eprintln!();
}};
( $fmt:literal $($tt:tt)* ) => {{
eprintln!(concat!("\x1b[{}m", $fmt, "\x1b[0m"), DEBUGGER_COLOR $($tt)*);
}};
}

mod command;
mod source;

Expand All @@ -7,6 +21,8 @@ use source::{SourceMode, SourceReader};

// TODO(feat): Use stderr for all debugger output (except in terminal mode?)

const DEBUGGER_COLOR: u8 = 34;

// TODO(refactor): Perhaps there is `clap` trait that can be implemented for
// this struct, to avoid field duplication in `Command` enum
#[derive(Debug)]
Expand Down Expand Up @@ -55,23 +71,21 @@ impl Debugger {
}

pub fn wait_for_action(&mut self) -> Action {
println!("");
dprintln!();
let Some(command) = self.next_command() else {
return Action::StopDebugger;
};
println!("{:?}", command);
eprintln!("{:?}", command); // Never use color

match command {
Command::Continue => {
self.status = Status::ContinueUntilBreakpoint;
println!("Continuing...");
dprintln!("Continuing...");
}

Command::Get { location } => match location {
Location::Register(register) => {
print!("\x1b[34m");
println!("Register R{}:", register as u16);
print!("\x1b[0m");
dprintln!("Register R{}:", register as u16);
let value = *self.state().reg(register as u16);
Self::print_integer(value);
}
Expand All @@ -80,16 +94,13 @@ impl Debugger {
MemoryLocation::Address(address) => address,
MemoryLocation::PC => self.state().pc,
MemoryLocation::Label(_) => {
eprintln!("unimplemented: labels");
dprintln!("unimplemented: labels");
return Action::None;
}
};

print!("\x1b[34m");
println!("Memory at address 0x{:04x}:", address);
print!("\x1b[0m");
dprintln!("Memory at address 0x{:04x}:", address);
let b = self.state().mem(address);
println!("b");
Self::print_integer(*b);
}
},
Expand All @@ -104,7 +115,7 @@ impl Debugger {
},

_ => {
eprintln!("unimplemented");
dprintln!("unimplemented");
}
}

Expand All @@ -123,7 +134,7 @@ impl Debugger {
let command = match Command::try_from(line) {
Ok(command) => command,
Err(error) => {
eprintln!("{:?}", error);
dprintln!("{:?}", error);
continue;
}
};
Expand All @@ -138,8 +149,6 @@ impl Debugger {
}

fn print_integer(value: u16) {
print!("\x1b[34m");
println!("0x{:04x}\t{}", value, value);
print!("\x1b[0m");
dprintln!("0x{:04x}\t{}", value, value);
}
}
25 changes: 12 additions & 13 deletions src/debugger/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::io::{self, IsTerminal, Read, Write};

use console::Key;

use super::DEBUGGER_COLOR;

#[allow(private_interfaces)] // Perhaps a bad practice
#[derive(Debug)]
pub enum SourceMode {
Expand Down Expand Up @@ -47,13 +49,6 @@ pub trait SourceReader {
/// `None` indicates EOF
/// Returned string slice MAY include leading or trailing whitespace
fn read(&mut self) -> Option<&str>;

fn write_prompt(f: &mut impl io::Write) -> io::Result<()> {
write!(f, "\x1b[1;34m")?;
write!(f, "Command: ")?;
write!(f, "\x1b[0m")?;
Ok(())
}
}

impl SourceMode {
Expand All @@ -77,9 +72,9 @@ impl SourceReader for SourceMode {
Self::Terminal(terminal) => return terminal.read(),
};
// Echo prompt and command for non-terminal source
// TODO(opt): This recreates the stdout handle each time
Self::write_prompt(&mut io::stdout()).unwrap();
println!("{}", command.unwrap_or("").trim());
// Equivalent code found in terminal source
dprintln!("\x1b[1mCommand");
dprintln!("{}", command.unwrap_or("").trim());
command
}
}
Expand Down Expand Up @@ -213,9 +208,13 @@ impl Terminal {
// Clear line, print prompt, set cursor position
self.term.clear_line().unwrap();

Self::write_prompt(&mut self.term).unwrap();

// TODO: What in the world is this...
// Print prompt and current input
// Equivalent code found in non-terminal source
write!(self.term, "\x1b[1;{}m", DEBUGGER_COLOR).unwrap();
write!(self.term, "Command: ").unwrap();
write!(self.term, "\x1b[0m").unwrap();
// This is necessary as cannot borrow `self.term` mutably and `self.get_current()` immutably
// TODO(refactor): There must be a better way to do this
let current = unsafe { &*(self.get_current() as *const str) };
write!(self.term, "{}", current).unwrap();

Expand Down

0 comments on commit bfed798

Please sign in to comment.