ft: add debug and info logging
This commit is contained in:
@@ -7,8 +7,11 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
||||
let mut loop_stack = Vec::new();
|
||||
let mut out = Vec::new();
|
||||
|
||||
log::info!("Program size: {}", code.len());
|
||||
|
||||
let code_bytes = code.as_bytes();
|
||||
while pc < code.len() {
|
||||
log::debug!("Next instruction: {}", code_bytes[pc] as char);
|
||||
match code_bytes[pc] as char {
|
||||
'>' => head += 1,
|
||||
'<' => head -= 1,
|
||||
@@ -23,6 +26,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
||||
if io::stdin().read_exact(&mut input_buf).is_ok() {
|
||||
memory[head] = input_buf[0];
|
||||
}
|
||||
log::debug!("Read user input: {}", input_buf[0]);
|
||||
}
|
||||
'[' => {
|
||||
// loop start
|
||||
@@ -46,6 +50,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
||||
}
|
||||
} else {
|
||||
// save pc to jump back to
|
||||
log::info!("Pushing location {} onto loop stack", pc);
|
||||
loop_stack.push(pc);
|
||||
}
|
||||
}
|
||||
@@ -60,10 +65,12 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
||||
}
|
||||
} else {
|
||||
// loop done, continue normally
|
||||
loop_stack.pop();
|
||||
let location = loop_stack.pop();
|
||||
log::info!("Done with loop at location {}", location.unwrap());
|
||||
}
|
||||
}
|
||||
_ => panic!("Invalid instruction"),
|
||||
// ignore unknown characters (e.g. weird line endings)
|
||||
_ => {}
|
||||
}
|
||||
pc += 1;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ use std::{env, fs, process};
|
||||
mod interpret;
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let args: Vec<_> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
eprintln!("Usage: {} <filename>", args[0]);
|
||||
@@ -15,5 +17,6 @@ fn main() {
|
||||
panic!("Error reading file {}: {}", filename, err);
|
||||
});
|
||||
|
||||
println!("Running file {} with program {}", filename, code);
|
||||
interpret(&code);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user