ft: add error type instead of plain panic

This commit is contained in:
2025-04-22 10:49:38 +09:00
parent 471267f3d1
commit 1c6fa97eae
2 changed files with 19 additions and 7 deletions

View File

@@ -1,6 +1,13 @@
use std::io::{self, Read};
pub fn interpret(code: &str) -> Vec<u8> {
#[derive(Debug)]
pub enum BrainFuckError {
BracketMissing,
UserInput,
EmptyStack,
}
pub fn interpret(code: &str) -> Result<Vec<u8>, BrainFuckError> {
let mut memory = [0 as u8; 10_000];
let mut head = 0 as usize;
let mut pc = 0 as usize;
@@ -25,6 +32,8 @@ pub fn interpret(code: &str) -> Vec<u8> {
let mut input_buf = [0u8; 1];
if io::stdin().read_exact(&mut input_buf).is_ok() {
memory[head] = input_buf[0];
} else {
return Err(BrainFuckError::UserInput);
}
log::debug!("Read user input: {}", input_buf[0]);
}
@@ -37,7 +46,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
while count_brackets > 0 {
pc += 1;
if pc >= code_bytes.len() {
panic!("Missing closing loop!");
return Err(BrainFuckError::BracketMissing);
}
match code_bytes[pc] as char {
// closing current loop
@@ -62,7 +71,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
if let Some(start_pc) = loop_stack.last() {
pc = *start_pc;
} else {
panic!("Loop stack empty, but still called!")
return Err(BrainFuckError::EmptyStack);
}
// loop done, continue normally
} else {
@@ -76,7 +85,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
pc += 1;
}
print!("\n");
return out;
Ok(out)
}
#[cfg(test)]
@@ -86,14 +95,14 @@ mod tests {
#[test]
fn test_h() {
let code = "+++++++++[>++++++++<-]>.";
let out = interpret(code);
let out = interpret(code).unwrap();
assert_eq!(std::str::from_utf8(&out).unwrap(), "H");
}
#[test]
fn test_hello() {
let code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
let out = interpret(code);
let out = interpret(code).unwrap();
assert_eq!(std::str::from_utf8(&out).unwrap(), "Hello World!\n");
}
}

View File

@@ -18,5 +18,8 @@ fn main() {
});
println!("Running file {} with program {}", filename, code);
interpret(&code);
match interpret(&code) {
Ok(_) => {}
Err(err) => panic!("{:?}", err),
}
}