From 694083a446685ccfee3ffcd0155118ac0805ec93 Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Tue, 22 Apr 2025 09:47:45 +0900 Subject: [PATCH] ft: restructure with tests --- example/hello_world.bf | 1 + src/interpret.rs | 91 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 84 +++++++------------------------------- 3 files changed, 107 insertions(+), 69 deletions(-) create mode 100644 example/hello_world.bf create mode 100644 src/interpret.rs diff --git a/example/hello_world.bf b/example/hello_world.bf new file mode 100644 index 0000000..ea2b641 --- /dev/null +++ b/example/hello_world.bf @@ -0,0 +1 @@ +++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. \ No newline at end of file diff --git a/src/interpret.rs b/src/interpret.rs new file mode 100644 index 0000000..c0f2f2a --- /dev/null +++ b/src/interpret.rs @@ -0,0 +1,91 @@ +use std::io::{self, Read}; + +pub fn interpret(code: &str) -> Vec { + let mut memory = [0 as u8; 10_000]; + let mut head = 0 as usize; + let mut pc = 0 as usize; + let mut loop_stack = Vec::new(); + let mut out = Vec::new(); + + let code_bytes = code.as_bytes(); + while pc < code.len() { + match code_bytes[pc] as char { + '>' => head += 1, + '<' => head -= 1, + '+' => memory[head] = memory[head].wrapping_add(1), + '-' => memory[head] = memory[head].wrapping_sub(1), + '.' => { + print!("{}", memory[head] as char); + out.push(memory[head]); + } + ',' => { + let mut input_buf = [0u8; 1]; + if io::stdin().read_exact(&mut input_buf).is_ok() { + memory[head] = input_buf[0]; + } + } + '[' => { + // loop start + if memory[head] == 0 { + let mut count_brackets = 1; + + // find nested brackets + while count_brackets > 0 { + pc += 1; + if pc >= code_bytes.len() { + panic!("Missing closing loop!"); + } + match code_bytes[pc] as char { + // closing current loop + ']' => count_brackets -= 1, + // nested bracket + '[' => count_brackets += 1, + // ignore rest + _ => {} + } + } + } else { + // save pc to jump back to + loop_stack.push(pc); + } + } + ']' => { + // loop terminator + if memory[head] != 0 { + // jump back to loop beginning + if let Some(start_pc) = loop_stack.last() { + pc = *start_pc; + } else { + panic!("Loop stack empty, but still called!") + } + } else { + // loop done, continue normally + loop_stack.pop(); + } + } + _ => panic!("Invalid instruction"), + } + pc += 1; + } + print!("\n"); + return out; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_h() { + let code = "+++++++++[>++++++++<-]>."; + let out = interpret(code); + assert_eq!(std::str::from_utf8(&out).unwrap(), "H"); + } + + #[test] + fn test_hello() { + let code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; + let out = interpret(code); + assert_eq!(std::str::from_utf8(&out).unwrap(), "Hello World!\n"); + } +} diff --git a/src/main.rs b/src/main.rs index 04ac4c3..f3f20f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,73 +1,19 @@ -use std::io::{self, Read}; +use crate::interpret::interpret; +use std::{env, fs, process}; -fn interpret(code: &str) { - let mut memory = [0 as u8; 10_000]; - let mut head = 0 as usize; - let mut pc = 0 as usize; - let mut loop_stack = Vec::new(); - - let code_bytes = code.as_bytes(); - while pc < code.len() { - match code_bytes[pc] as char { - '>' => head += 1, - '<' => head -= 1, - '+' => memory[head] = memory[head].wrapping_add(1), - '-' => memory[head] = memory[head].wrapping_sub(1), - '.' => print!("{}", memory[head] as char), - ',' => { - let mut input_buf = [0u8; 1]; - if io::stdin().read_exact(&mut input_buf).is_ok() { - memory[head] = input_buf[0]; - } - } - '[' => { - // loop start - if memory[head] == 0 { - let mut count_brackets = 1; - - // find nested brackets - while count_brackets > 0 { - pc += 1; - if pc >= code_bytes.len() { - panic!("Missing closing loop!"); - } - match code_bytes[pc] as char { - // closing current loop - ']' => count_brackets -= 1, - // nested bracket - '[' => count_brackets += 1, - // ignore rest - _ => {} - } - } - } else { - // save pc to jump back to - loop_stack.push(pc); - } - } - ']' => { - // loop terminator - if memory[head] != 0 { - // jump back to loop beginning - if let Some(start_pc) = loop_stack.last() { - pc = *start_pc; - } else { - panic!("Loop stack empty, but still called!") - } - } else { - // loop done, continue normally - loop_stack.pop(); - } - } - _ => panic!("Invalid instruction"), - } - pc += 1; - } - print!("\n"); -} +mod interpret; fn main() { - // let code = "+++++++++[>++++++++<-]>."; - let code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; - interpret(code); + let args: Vec<_> = env::args().collect(); + if args.len() != 2 { + eprintln!("Usage: {} ", args[0]); + process::exit(1); + } + + let filename = &args[1]; + let code = fs::read_to_string(filename).unwrap_or_else(|err| { + panic!("Error reading file {}: {}", filename, err); + }); + + interpret(&code); }