Skip to content

Commit

Permalink
Add a restart command
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Jul 17, 2024
1 parent cff1cfc commit 2aca971
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
5 changes: 5 additions & 0 deletions interp/src/debugger/commands/command_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ impl CommandParser {
Ok(Command::Explain)
}

fn restart(_input: Node) -> ParseResult<Command> {
Ok(Command::Restart)
}

fn watch(input: Node) -> ParseResult<Command> {
Ok(match_nodes!(input.into_children();
[watch_position(wp), group(g), print_state(p)] => {
Expand Down Expand Up @@ -261,6 +265,7 @@ impl CommandParser {
[disable(dis), EOI(_)] => dis,
[exit(exit), EOI(_)] => exit,
[explain(ex), EOI(_)] => ex,
[restart(restart), EOI(_)] => restart,
[EOI(_)] => Command::Empty,
))
}
Expand Down
4 changes: 3 additions & 1 deletion interp/src/debugger/commands/commands.pest
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ comm_where = { (^"where" | "pc") ~ (code_calyx)? }

explain = { ^"explain" }

restart = { ^"restart" }

command = {
SOI ~ (watch | comm_where | print_state | print | print_fail | delete_watch | delete | brk | enable | disable | step_over | step // commands without input
| cont | help | info_break | info_watch | display | exit | explain)? ~ EOI
| cont | help | info_break | info_watch | display | exit | explain | restart)? ~ EOI
}
3 changes: 3 additions & 0 deletions interp/src/debugger/commands/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ pub enum Command {
),
PrintPC(bool),
Explain,
Restart,
}

type Description = &'static str;
Expand Down Expand Up @@ -420,6 +421,8 @@ lazy_static! {
// explain
CIBuilder::new().invocation("explain")
.description("Show examples of commands which take arguments").build(),
CIBuilder::new().invocation("restart")
.description("Restart the debugger from the beginning of the execution. This will reset all breakpoints, watchpoints, and the command history").build(),
// exit/quit
CIBuilder::new().invocation("exit")
.invocation("quit")
Expand Down
28 changes: 23 additions & 5 deletions interp/src/debugger/debugger_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ impl ProgramStatus {
}
}

pub enum DebuggerReturnStatus {
Restart,
Exit,
}

/// The interactive Calyx debugger. The debugger itself is run with the
/// [Debugger::main_loop] function while this struct holds auxiliary
/// information used to coordinate the debugging process.
Expand Down Expand Up @@ -172,7 +177,7 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {

// so on and so forth

pub fn main_loop(mut self) -> InterpreterResult<()> {
pub fn main_loop(mut self) -> InterpreterResult<DebuggerReturnStatus> {
let mut input_stream = Input::new()?;

println!(
Expand Down Expand Up @@ -218,7 +223,7 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {
}
}
Command::Help => {
print!("{}", Command::get_help_string().cyan())
print!("{}", Command::get_help_string())
}
Command::Break(targets) => self.create_breakpoints(targets),

Expand All @@ -227,7 +232,10 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {
| Command::Enable(_)
| Command::Disable(_)) => self.manipulate_breakpoint(comm),

Command::Exit => return Err(InterpreterError::Exit.into()),
Command::Exit => {
println!("Exiting.");
return Ok(DebuggerReturnStatus::Exit);
}

Command::InfoBreak => self
.debugging_context
Expand Down Expand Up @@ -266,6 +274,10 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {
Command::Explain => {
print!("{}", Command::get_explain_string().blue())
}

Command::Restart => {
return Ok(DebuggerReturnStatus::Restart);
}
}
}

Expand Down Expand Up @@ -301,12 +313,18 @@ impl<C: AsRef<Context> + Clone> Debugger<C> {
}

Command::Help => {
print!("{}", Command::get_help_string().blue())
print!("{}", Command::get_help_string())
}
Command::Exit => {
println!("Exiting.");
return Ok(DebuggerReturnStatus::Exit);
}
Command::Exit => return Err(InterpreterError::Exit.into()),
Command::Explain => {
print!("{}", Command::get_explain_string().blue().bold())
}
Command::Restart => {
return Ok(DebuggerReturnStatus::Restart);
}
_ => {
println!(
"This command is unavailable after program termination"
Expand Down
1 change: 1 addition & 0 deletions interp/src/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod macros;
pub mod source;

pub use debugger_core::Debugger;
pub use debugger_core::DebuggerReturnStatus;
pub use debugger_core::OwnedDebugger;
pub use debugger_core::ProgramStatus;

Expand Down
4 changes: 0 additions & 4 deletions interp/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ pub enum InterpreterError {
#[error(transparent)]
ReadlineError(#[from] ReadlineError),

/// An error for the exit command to the interactive debugger
#[error("exit")]
Exit,

/// Wrapper error for parsing & related compiler errors
#[error("{0:?}")]
CompilerError(Box<CalyxError>),
Expand Down
24 changes: 10 additions & 14 deletions interp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use argh::FromArgs;
use calyx_utils::OutputFile;
use interp::{
configuration,
debugger::Debugger,
errors::{InterpreterError, InterpreterResult},
debugger::{Debugger, DebuggerReturnStatus},
errors::InterpreterResult,
flatten::structures::environment::Simulator,
};

Expand Down Expand Up @@ -128,19 +128,15 @@ fn main() -> InterpreterResult<()> {
Ok(())
}
Command::Debug(_) => {
let debugger = Debugger::new(&i_ctx, &opts.data_file)?;

match debugger.main_loop() {
Ok(_) => {}
Err(e) => {
if let InterpreterError::Exit = *e {
println!("Exiting.");
} else {
return Err(e);
}
}
};
loop {
let debugger = Debugger::new(&i_ctx, &opts.data_file)?;

let result = debugger.main_loop()?;
match result {
DebuggerReturnStatus::Exit => break,
DebuggerReturnStatus::Restart => continue,
}
}
Ok(())
}
}
Expand Down

0 comments on commit 2aca971

Please sign in to comment.