From 7286f3ac3fe731cf40f596bf7115dcfcf9b5f736 Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Tue, 22 Apr 2025 09:20:49 +0900 Subject: [PATCH] fix: print in single line --- src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index e78bbd9..4476393 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn interpret(code: &str) { '<' => head -= 1, '+' => memory[head] = memory[head].wrapping_add(1), '-' => memory[head] = memory[head].wrapping_sub(1), - '.' => println!("{}", memory[head]), + '.' => print!("{}", memory[head] as char), ',' => { let mut input_buf = [0u8; 1]; if io::stdin().read_exact(&mut input_buf).is_ok() { @@ -50,6 +50,8 @@ fn interpret(code: &str) { if memory[pc] != 0 { if let Some(start_pc) = loop_stack.last() { pc = *start_pc; + } else { + panic!("Loop stack empty, but still called!") } } else { // loop done, continue normally @@ -60,10 +62,11 @@ fn interpret(code: &str) { } pc += 1; } + print!("\n"); } fn main() { - let code = "+++++++++[>++++++++<-]>."; - let hello_world = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; - interpret(hello_world); + // let code = "+++++++++[>++++++++<-]>."; + let code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; + interpret(code); }