not working in rust

This commit is contained in:
nora 2021-04-03 21:15:48 +02:00
commit 948739ba4e
5 changed files with 136 additions and 0 deletions

2
bfi-rust/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
.idea

5
bfi-rust/Cargo.lock generated Normal file
View file

@ -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"

9
bfi-rust/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "bfinterpreter"
version = "0.1.0"
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

18
bfi-rust/bf.b Normal file
View file

@ -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

102
bfi-rust/src/main.rs Normal file
View file

@ -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<char>, 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
}