From 16ec612a746e0825e4047b9ee4a8efec0da316a5 Mon Sep 17 00:00:00 2001 From: malatrax Date: Wed, 6 Nov 2024 14:52:14 +0100 Subject: [PATCH 1/2] refactor: trim whitespace in compiler only --- crates/brainfuck_vm/src/bin/brainfuck_vm.rs | 4 +--- crates/brainfuck_vm/src/compiler.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/brainfuck_vm/src/bin/brainfuck_vm.rs b/crates/brainfuck_vm/src/bin/brainfuck_vm.rs index 3aa489d..7eed73a 100644 --- a/crates/brainfuck_vm/src/bin/brainfuck_vm.rs +++ b/crates/brainfuck_vm/src/bin/brainfuck_vm.rs @@ -29,9 +29,7 @@ fn main() { // Constructs a subscriber whose severity level is filtered by `RUST_LOG` fmt().with_env_filter(EnvFilter::from_default_env()).init(); - let code = fs::read_to_string(&args.filename) - .expect("Failed to read file") - .replace(' ', ""); + let code = fs::read_to_string(&args.filename).expect("Failed to read file"); let mut bf_compiler = Compiler::new(code); let ins = bf_compiler.compile(); tracing::info!( diff --git a/crates/brainfuck_vm/src/compiler.rs b/crates/brainfuck_vm/src/compiler.rs index 35eb8d2..a2077b8 100644 --- a/crates/brainfuck_vm/src/compiler.rs +++ b/crates/brainfuck_vm/src/compiler.rs @@ -54,6 +54,15 @@ mod tests { assert_eq!(expected_trimmed_code, compiler.code,); } + #[test] + fn test_whitespace() { + let code = " + +> , < [> + .< - ] ".to_string(); + let compiler = Compiler::new(code); + let expected_trimmed_code = + vec!['+', '+', '>', ',', '<', '[', '>', '+', '.', '<', '-', ']']; + assert_eq!(expected_trimmed_code, compiler.code,); + } + #[test] fn test_compile() { let code = "++>,<[>+.<-]".to_string(); From bc24c020e8792b9f49a10dcfa54c4ea80c307ac7 Mon Sep 17 00:00:00 2001 From: malatrax Date: Wed, 6 Nov 2024 14:56:16 +0100 Subject: [PATCH 2/2] chore: typo useless comma --- crates/brainfuck_vm/src/compiler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/brainfuck_vm/src/compiler.rs b/crates/brainfuck_vm/src/compiler.rs index a2077b8..0c9c697 100644 --- a/crates/brainfuck_vm/src/compiler.rs +++ b/crates/brainfuck_vm/src/compiler.rs @@ -51,7 +51,7 @@ mod tests { let compiler = Compiler::new(code); let expected_trimmed_code = vec!['+', '+', '>', ',', '<', '[', '>', '+', '.', '<', '-', ']']; - assert_eq!(expected_trimmed_code, compiler.code,); + assert_eq!(expected_trimmed_code, compiler.code); } #[test] @@ -60,7 +60,7 @@ mod tests { let compiler = Compiler::new(code); let expected_trimmed_code = vec!['+', '+', '>', ',', '<', '[', '>', '+', '.', '<', '-', ']']; - assert_eq!(expected_trimmed_code, compiler.code,); + assert_eq!(expected_trimmed_code, compiler.code); } #[test]