diff --git a/bfi-rust/bf.b b/bfi-rust/bf.b index 005678e..004f459 100644 --- a/bfi-rust/bf.b +++ b/bfi-rust/bf.b @@ -1,18 +1,11 @@ -++++++++ Set Cell #0 to 8 -[ - >++++ Add 4 to Cell #1; this will always set Cell #1 to 4 - [ as the cell will be cleared by the loop - >++ Add 2 to Cell #2 - >+++ Add 3 to Cell #3 - >+++ Add 3 to Cell #4 - >+ Add 1 to Cell #5 - <<<<- Decrement the loop counter in Cell #1 - ] Loop till Cell #1 is zero; number of iterations is 4 - >+ Add 1 to Cell #2 - >+ Add 1 to Cell #3 - >- Subtract 1 from Cell #4 - >>+ Add 1 to Cell #6 - [<] Move back to the first zero cell you find; this will - be Cell #1 which was cleared by the previous loop - <- Decrement the loop Counter in Cell #0 -] Loop till Cell #0 is zero; number of iterations is 8 \ No newline at end of file +++++++++++[>++++++++++<-]>>++++++++++>->>>>>>>>>>>>>>>>-->+++++++[->++ +++++++++<]>[->+>+>+>+<<<<]+++>>+++>>>++++++++[-<++++<++++<++++>>>]++++ ++[-<++++<++++>>]>>-->++++++[->+++++++++++<]>[->+>+>+>+<<<<]+++++>>+>++ +++++>++++++>++++++++[-<++++<++++<++++>>>]++++++[-<+++<+++<+++>>>]>>--> +---+[-<+]-<[+[->+]-<<->>>+>[-]++[-->++]-->+++[---++[--<++]---->>-<+>[+ ++++[----<++++]--[>]++[-->++]--<]>++[--+[-<+]->>[-]+++++[---->++++]-->[ +->+<]>>[.>]++[-->++]]-->+++]---+[-<+]->>-[+>>>+[-<+]->>>++++++++++<<[- +>+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]> ++>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->+++ ++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]+[-<+]->>]+[-]<<<.>>>+[ +-<+]-<<] \ No newline at end of file diff --git a/bfi-rust/src/main.rs b/bfi-rust/src/main.rs index ed4e96a..6860d7f 100644 --- a/bfi-rust/src/main.rs +++ b/bfi-rust/src/main.rs @@ -1,5 +1,6 @@ use std::io::{stdin, Read}; use std::{env, fs}; +use std::time::SystemTime; fn main() { let path = env::args().skip(1).next(); @@ -20,9 +21,10 @@ fn run(path: String) { let program = minify(program); println!("Minified program: {}", program); + let start = SystemTime::now(); let out = interpret(program.chars().collect(), false); - println!("{}", out); + println!("Finished in {}ms: {}", start.elapsed().unwrap().as_millis(), out); } fn minify(program: String) -> String { @@ -30,19 +32,20 @@ fn minify(program: String) -> String { program.chars().filter(|c| allowed.contains(c)).collect() } +const MEM_SIZE: usize = 0xFFFF; + fn interpret(pgm: Vec, number_debug: bool) -> String { let mut out = String::new(); let mut pointer: usize = 0; - let mut mem: [i8; 30000] = [0; 30000]; + let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE]; let mut in_buffer = [0; 1]; let mut pc = 0; let len = pgm.len(); while pc < len { - //println!("pc: {} instruction: {}, pointer: {}", pc, pgm[pc], pointer); match pgm[pc] { - '>' => pointer += 1, - '<' => pointer -= 1, + '>' => if pointer == MEM_SIZE - 1 { pointer = 0 } else { pointer += 1 }, + '<' => if pointer == 0 { pointer = MEM_SIZE - 1 } else { pointer -= 1 }, '+' => mem[pointer] = mem[pointer].wrapping_add(1), '-' => mem[pointer] = mem[pointer].wrapping_sub(1), '.' => { @@ -54,35 +57,36 @@ fn interpret(pgm: Vec, number_debug: bool) -> String { } ',' => { stdin().read(&mut in_buffer).unwrap(); - mem[pointer] = in_buffer[0] as i8; + mem[pointer] = in_buffer[0] as u8; } '[' => { //jump to corresponding ] so that it will get to the command after the ] if mem[pointer] == 0 { - println!("[ found"); + println!("jumping forward at location {}", pc + 1); let mut level = 0; - while pgm[pc] != ']' || level > 0 { + while pgm[pc] != ']' || level > -1 { pc += 1; match pgm[pc] { '[' => { - println!("level up"); level += 1 - }, + } ']' => { - println!("level down"); level -= 1 - }, + } _ => (), } } + println!("found matching ] at location {}", pc + 1); assert_eq!(pgm[pc], ']') } } ']' => { if mem[pointer] != 0 { + println!("jumping back at location {}", pc + 1); + //jump to corresponding [ let mut level = 0; - while pgm[pc] != '[' || level > 0 { + while pgm[pc] != '[' || level > -1 { pc -= 1; match pgm[pc] { '[' => level -= 1, @@ -90,6 +94,8 @@ fn interpret(pgm: Vec, number_debug: bool) -> String { _ => (), } } + println!("found matching [ at location {}", pc + 1); + assert_eq!(pgm[pc], '[') } }