add help command

This commit is contained in:
Crispy 2024-09-09 20:17:11 +02:00
parent 91f3d32b2b
commit d658d1fcb9

View file

@ -2,6 +2,22 @@ use std::{env, fmt::Display, fs, io::stdin, process::exit};
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
const HELP_TEXT:&str = r#"HELP:
Code breakpoints:
The ! character will be treated as a breakpoint, stopping execution
[brackets] are optional parameters
Command list:
- step [n]
Step the progam n times (this is the default behavior when pressing enter with no command)
- run
Run the program until the end
- watch <cell> <value>
Create a breakpoint. Execution will stop when <cell> has <value>
- quit | q | exit
Exit the debugger
"#;
#[derive(Debug)] #[derive(Debug)]
struct BFInterpreter { struct BFInterpreter {
memory: Vec<u8>, memory: Vec<u8>,
@ -56,7 +72,7 @@ enum Command {
fn main() { fn main() {
let args: Vec<_> = env::args().collect(); let args: Vec<_> = env::args().collect();
if args.len() <= 1 { if args.len() <= 1 {
println!("usage: brainfuck source_file input_file"); println!("usage: brainfuck <source_file> <input_file>");
exit(0); exit(0);
} }
let filename = &args[1]; let filename = &args[1];
@ -84,6 +100,7 @@ fn main() {
stdin().read_line(&mut action).unwrap(); stdin().read_line(&mut action).unwrap();
let action: Vec<_> = action.trim().split_ascii_whitespace().collect(); let action: Vec<_> = action.trim().split_ascii_whitespace().collect();
match action.as_slice() { match action.as_slice() {
["help"] => println!("{}", HELP_TEXT.green()),
["step"] => interpreter.step_once(), ["step"] => interpreter.step_once(),
["step", num] => _ = num.parse().map(|n| interpreter.step(n)), ["step", num] => _ = num.parse().map(|n| interpreter.step(n)),
["watch"] => println!("usage: watch [memory index] [value]"), ["watch"] => println!("usage: watch [memory index] [value]"),
@ -132,7 +149,7 @@ impl BFInterpreter {
} }
println!(); println!();
println!( println!(
"source: {}:{}", "source (line:column): {}:{}",
self.program[self.program_ptr].line_number, self.program[self.program_ptr].column self.program[self.program_ptr].line_number, self.program[self.program_ptr].column
); );
print!("mem: "); print!("mem: ");