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,
'+' => 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);
}