commit 948739ba4ebe39350f9d483ee7d0c4e88e9437a9 Author: Nilstrieb Date: Sat Apr 3 21:15:48 2021 +0200 not working in rust diff --git a/bfi-rust/.gitignore b/bfi-rust/.gitignore new file mode 100644 index 0000000..2a0038a --- /dev/null +++ b/bfi-rust/.gitignore @@ -0,0 +1,2 @@ +/target +.idea \ No newline at end of file diff --git a/bfi-rust/Cargo.lock b/bfi-rust/Cargo.lock new file mode 100644 index 0000000..f25fde6 --- /dev/null +++ b/bfi-rust/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bfinterpreter" +version = "0.1.0" diff --git a/bfi-rust/Cargo.toml b/bfi-rust/Cargo.toml new file mode 100644 index 0000000..eb1a870 --- /dev/null +++ b/bfi-rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bfinterpreter" +version = "0.1.0" +authors = ["Nilstrieb "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/bfi-rust/bf.b b/bfi-rust/bf.b new file mode 100644 index 0000000..005678e --- /dev/null +++ b/bfi-rust/bf.b @@ -0,0 +1,18 @@ +++++++++ 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 diff --git a/bfi-rust/src/main.rs b/bfi-rust/src/main.rs new file mode 100644 index 0000000..ed4e96a --- /dev/null +++ b/bfi-rust/src/main.rs @@ -0,0 +1,102 @@ +use std::io::{stdin, Read}; +use std::{env, fs}; + +fn main() { + let path = env::args().skip(1).next(); + let path = match path { + Some(p) => p, + None => { + println!("Please specify a path"); + return; + } + }; + + run(path); +} + +fn run(path: String) { + println!("Path: {}", path); + let program = fs::read_to_string(path).unwrap(); + let program = minify(program); + println!("Minified program: {}", program); + + let out = interpret(program.chars().collect(), false); + + println!("{}", out); +} + +fn minify(program: String) -> String { + let allowed = vec!['>', '<', '+', '-', '.', ',', '[', ']']; + program.chars().filter(|c| allowed.contains(c)).collect() +} + +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 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, + '+' => mem[pointer] = mem[pointer].wrapping_add(1), + '-' => mem[pointer] = mem[pointer].wrapping_sub(1), + '.' => { + if number_debug { + out.push_str(&(mem[pointer].to_string() + " ")) + } else { + out.push(mem[pointer] as u8 as char) + } + } + ',' => { + stdin().read(&mut in_buffer).unwrap(); + mem[pointer] = in_buffer[0] as i8; + } + '[' => { + //jump to corresponding ] so that it will get to the command after the ] + if mem[pointer] == 0 { + println!("[ found"); + let mut level = 0; + while pgm[pc] != ']' || level > 0 { + pc += 1; + match pgm[pc] { + '[' => { + println!("level up"); + level += 1 + }, + ']' => { + println!("level down"); + level -= 1 + }, + _ => (), + } + } + assert_eq!(pgm[pc], ']') + } + } + ']' => { + if mem[pointer] != 0 { + //jump to corresponding [ + let mut level = 0; + while pgm[pc] != '[' || level > 0 { + pc -= 1; + match pgm[pc] { + '[' => level -= 1, + ']' => level += 1, + _ => (), + } + } + assert_eq!(pgm[pc], '[') + } + } + _ => (), + } + pc += 1; + } + + out +}