diff --git a/bfi-java/src/Brainfuck.java b/bfi-java/src/Brainfuck.java index a997f76..dcdc8ff 100644 --- a/bfi-java/src/Brainfuck.java +++ b/bfi-java/src/Brainfuck.java @@ -24,7 +24,6 @@ public class Brainfuck { int pc = 0; while (pc < pgm.size()) { - if (pointer > 1000) break; switch (pgm.get(pc)) { case '>' -> { if (pointer == MEM_SIZE - 1) { @@ -103,8 +102,10 @@ public class Brainfuck { String program = Files.readString(Paths.get(args[0])); List minified = brainfuck.minify(program); + long time1 = System.currentTimeMillis(); String result = brainfuck.interpret(minified); - + long time = System.currentTimeMillis() - time1; System.out.println(result); + System.out.println("Finished execution in " + time + "ms"); } } diff --git a/bfi-rust/bf.b b/bfi-rust/bf.b index c68579e..b695ab4 100644 --- a/bfi-rust/bf.b +++ b/bfi-rust/bf.b @@ -1 +1,11 @@ -++++++++++[>+++++++++<-]. \ 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 ec64c17..491f303 100644 --- a/bfi-rust/src/main.rs +++ b/bfi-rust/src/main.rs @@ -1,5 +1,5 @@ -use std::io::{stdin, Read}; use std::{env, fs}; +use std::io::{Read, stdin}; use std::time::SystemTime; fn main() { @@ -92,3 +92,94 @@ fn interpret(pgm: Vec) -> String { out } + +/// +/// # optimization time +/// +/// first parse the bf so that it can be exectued faster +/// most importantly: loop jumps should be immediate +/// +mod o1 { + const MEM_SIZE: usize = 0xFFFF; + + type Memory = [u8; MEM_SIZE]; + + /// + /// A single Statement, can be an instruction or a nestable loop + #[derive(Debug, PartialOrd, PartialEq)] + enum Statement { + Inc, + Dec, + R, + L, + Out, + In, + Loop(Vec), + } + + + fn minify(code: String) -> String { + let allowed: Vec = vec!['>', '<', '+', '-', '.', ',', '[', ']']; + code.chars().filter(|c| allowed.contains(c)).collect() + } + + fn parse(chars: Vec) -> Vec { + let mut loop_stack = vec![vec![]]; + + for c in chars { + match c { + '+' => loop_stack.last_mut().unwrap().push(Statement::Inc), + '-' => loop_stack.last_mut().unwrap().push(Statement::Dec), + '>' => loop_stack.last_mut().unwrap().push(Statement::R), + '<' => loop_stack.last_mut().unwrap().push(Statement::L), + '.' => loop_stack.last_mut().unwrap().push(Statement::Out), + ',' => loop_stack.last_mut().unwrap().push(Statement::In), + '[' => loop_stack.push(vec![]), + ']' => { + let statement = Statement::Loop(loop_stack.pop().unwrap()); + loop_stack.last_mut().unwrap().push(statement); + }, + _ => () + } + } + + return loop_stack.pop().unwrap(); + } + + fn interpret_o1(pgm: Vec) { + + } + + + #[cfg(test)] + mod test { + use crate::o1::{parse, Statement::{self, Inc, Dec, R, L, In, Out, Loop}}; + + #[test] + fn parse_no_loop() { + let program = "+-<>,."; + let statements = vec![Inc, Dec, L, R, In, Out]; + let result = parse(program.chars().collect()); + + assert_eq!(statements, result); + } + + #[test] + fn parse_simple_loop() { + let program = "+[<<]-"; + let statements = vec![Inc, Loop(vec![L, L]), Dec]; + let result = parse(program.chars().collect()); + + assert_eq!(statements, result); + } + + #[test] + fn parse_complex_loops() { + let program = ">[<[][<[<]>]>[>]]"; + let statements = vec![R, Loop(vec![L, Loop(vec![]), Loop(vec![L, Loop(vec![L]), R]), R, Loop(vec![R])])]; + let result = parse(program.chars().collect()); + + assert_eq!(statements, result); + } + } +} \ No newline at end of file