mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-14 21:35:02 +01:00
it actually seems to work now
This commit is contained in:
parent
219f419bae
commit
1cad1f2cc7
2 changed files with 30 additions and 31 deletions
|
|
@ -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
|
||||
++++++++++[>++++++++++<-]>>++++++++++>->>>>>>>>>>>>>>>>-->+++++++[->++
|
||||
++++++++<]>[->+>+>+>+<<<<]+++>>+++>>>++++++++[-<++++<++++<++++>>>]++++
|
||||
+[-<++++<++++>>]>>-->++++++[->+++++++++++<]>[->+>+>+>+<<<<]+++++>>+>++
|
||||
++++>++++++>++++++++[-<++++<++++<++++>>>]++++++[-<+++<+++<+++>>>]>>-->
|
||||
---+[-<+]-<[+[->+]-<<->>>+>[-]++[-->++]-->+++[---++[--<++]---->>-<+>[+
|
||||
+++[----<++++]--[>]++[-->++]--<]>++[--+[-<+]->>[-]+++++[---->++++]-->[
|
||||
->+<]>>[.>]++[-->++]]-->+++]---+[-<+]->>-[+>>>+[-<+]->>>++++++++++<<[-
|
||||
>+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>
|
||||
+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->+++
|
||||
+++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]+[-<+]->>]+[-]<<<.>>>+[
|
||||
-<+]-<<]
|
||||
|
|
@ -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<char>, 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<char>, 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<char>, number_debug: bool) -> String {
|
|||
_ => (),
|
||||
}
|
||||
}
|
||||
println!("found matching [ at location {}", pc + 1);
|
||||
|
||||
assert_eq!(pgm[pc], '[')
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue