fix: print in single line

This commit is contained in:
2025-04-22 09:20:49 +09:00
parent 82ffbb8a31
commit 7286f3ac3f

View File

@@ -13,7 +13,7 @@ fn interpret(code: &str) {
'<' => head -= 1, '<' => head -= 1,
'+' => memory[head] = memory[head].wrapping_add(1), '+' => memory[head] = memory[head].wrapping_add(1),
'-' => memory[head] = memory[head].wrapping_sub(1), '-' => memory[head] = memory[head].wrapping_sub(1),
'.' => println!("{}", memory[head]), '.' => print!("{}", memory[head] as char),
',' => { ',' => {
let mut input_buf = [0u8; 1]; let mut input_buf = [0u8; 1];
if io::stdin().read_exact(&mut input_buf).is_ok() { if io::stdin().read_exact(&mut input_buf).is_ok() {
@@ -50,6 +50,8 @@ fn interpret(code: &str) {
if memory[pc] != 0 { if memory[pc] != 0 {
if let Some(start_pc) = loop_stack.last() { if let Some(start_pc) = loop_stack.last() {
pc = *start_pc; pc = *start_pc;
} else {
panic!("Loop stack empty, but still called!")
} }
} else { } else {
// loop done, continue normally // loop done, continue normally
@@ -60,10 +62,11 @@ fn interpret(code: &str) {
} }
pc += 1; pc += 1;
} }
print!("\n");
} }
fn main() { fn main() {
let code = "+++++++++[>++++++++<-]>."; // let code = "+++++++++[>++++++++<-]>.";
let hello_world = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; let code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
interpret(hello_world); interpret(code);
} }