Similar to the previous post Brainfuck Interpreter: Slower than a Snail?, it runs BF in java. The only difference is that the converter will convert the BF code into a compilable and fairly readable Java code, and run it.
Example:
BF code:
++++++++++[>++++++++++<-]> Initialize 100 (number of times to perform FizzBuzz) TAPE MEANINGS 255 Start 254 A Fizz or Buzz text to print 253 End of Fizzes and Buzzes 252 Currently processed FizzBuzz calculation TAPE OVERVIEW Remaining Iterations 10 for Line Break 255 Start Marker Counter Boolean 1 or 0 for whether or not a fizzbuzz matches current counter Some empty space for converting counter to string Any Number of Sequences of the following 254 Indicator for FizzBuzz sequence Counter Countdown until next text output Text any number of characters Zero Zero 254 and 253 marker to indicate the end of sequences >++++++++++ Line break >- Start marker >>>>>>>>>>>>>>>> Empty space for counter to string conversion SETUP Create the Fizz and Buzz sequences on the tape FIZZ --> Create indicator +++++++[->++++++++++<]> Create F [->+>+>+>+<<<<] Copy 4x F +++ Set modulo operator to 3 >>+++>>> Adjust second letter to I ++++++++[-<++++<++++<++++>>>] Make the last three lowercase to Fiff +++++[-<++++<++++>>] Modify the last two F to Z by adding 20 >> Leave two zeros at the end BUZZ --> Create indicator ++++++[->+++++++++++<]> Create B [->+>+>+>+<<<<] Copy 4x B +++++ Set modulo operator to 5 >>+>++++++>++++++> Adjust BBBB to BCHH ++++++++[-<++++<++++<++++>>>] Make lower case ++++++[-<+++<+++<+++>>>] Adjust Bchh to Buzz >> Leave two zeros at the end -->--- Mark the ending with 254 and 253 END OF SETUP ALGORITHM START +[-<+]- Go backwards to the 255 mark < [ +[->+]- Go forward to the start position 255 marker <<->> Decrease countdown >+ Increase counter >[-] Reset boolean for if we have found a match or not ++[-->++]--> Find next 254 and go one step beyond it Loop through all 254s +++[--- Make sure that we are not at 253 (end) ++[--<++]-- Find last value 254 -- Change marker to 252 to indicate that we're processing it >> Go to the countdown -< Decrease fizzbuzz countdown +> Increase fizzbuzz counter If current marker is NOT zero [ ++++[----<++++]-- Find value 252 and change to 254 [>] Position to a place that is zero to avoid repeat ++[-->++]-- < Find NEXT 254 marker and stop right before it ] >++ Check if we are positioned on a 254 already then if skip this [-- We have a match so find start position and mark match +[-<+]- >> Find 255 marker and go to the boolean [-]+ Set boolean to 1 whatever the previous value is ++++[---->++++]-- Find value 252 and change to 254 >[->+<] Reset the current FizzBuzz countdown >>[.>] Print the text ++[-->++] Go to next 254 change to 256 to break loop ] --> +++ # Detect if we are at the 253 end ] --- ALL FIZZBUZZES PROCESSED Use the boolean to check whether or not to print the number +[-<+]- Go back to the 255 marker >> Go to boolean -[ If boolean is zero then print the number +>>> +[-<+]- Go back to the 255 marker >> We are positioned after the counter Code taken from StackOverflow below for printing a number >++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[- <+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++ <]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>] End of StackOverflow code +[-<+]->> Locate the 255 marker and goto the boolean ]+ Boolean is always one here so print new line [-]<<<.>>> Print new line +[-<+]- Go back to the 255 marker << Go to the countdown to find out if we should go another round ]
(From Fizzbuzz in Brainfuck, @SimonForsberg)
Generated Java:
import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class BF { public static final int BF_MEMORY_SIZE = 30000; private static final InputStreamReader INPUT = new InputStreamReader(System.in); private static final int BYTE_SIZE = 256; private static final int HALF_BYTE = BYTE_SIZE / 2; public static void main(String[] args) { byte[] memory = new byte[30000]; Arrays.fill(memory, Byte.MIN_VALUE); int pointer = 0; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]--; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; } memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; memory[pointer]--; memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { System.out.print((char) (memory[pointer] + HALF_BYTE)); if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; } } memory[pointer]--; memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; } memory[pointer]--; memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; while (memory[pointer] != Byte.MIN_VALUE) { if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } System.out.print((char) (memory[pointer] + HALF_BYTE)); if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } System.out.print((char) (memory[pointer] + HALF_BYTE)); while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } System.out.print((char) (memory[pointer] + HALF_BYTE)); while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } } memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } System.out.print((char) (memory[pointer] + HALF_BYTE)); if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } if (++pointer == BF_MEMORY_SIZE) { pointer = 0; } memory[pointer]++; while (memory[pointer] != Byte.MIN_VALUE) { memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } memory[pointer]++; } memory[pointer]--; if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } if (--pointer == -1) { pointer += BF_MEMORY_SIZE; } } } }
Note that this was written before posting the previous question, and I haven't edited anything yet.
BFConverter.java:
Gives you a BFProgram
that you can run.
public class BFConverter {
public static BFProgram convertToJava(BFCode code) {
StringBuilder sourceCode = new StringBuilder();
sourceCode.append("import java.io.IOException;\n")
.append("import java.io.InputStreamReader;\n")
.append("import java.util.Arrays;\n\n")
.append("public class BF {\n\n")
.append("\tpublic static final int BF_MEMORY_SIZE = 30000;\n")
.append("\tprivate static final InputStreamReader INPUT = new InputStreamReader(System.in);\n")
.append("\tprivate static final int BYTE_SIZE = 256;\n")
.append("\tprivate static final int HALF_BYTE = BYTE_SIZE / 2;\n\n")
.append("\tpublic static void main(String[] args) {\n")
.append("\t\tbyte[] memory = new byte[30000];\n")
.append("\t\tArrays.fill(memory, Byte.MIN_VALUE);\n")
.append("\t\tint pointer = 0;\n");
char[] commands = code.getOptimizedCode().toCharArray();
String currentTabs = "\t\t";
for (int i = 0, len = commands.length; i < len; i++) {
sourceCode.append(currentTabs);
switch (commands[i]) {
case '+':
sourceCode.append("memory[pointer]++;");
break;
case '-':
sourceCode.append("memory[pointer]--;");
break;
case '<':
sourceCode.append("if (--pointer == -1) {\n")
.append(currentTabs).append("\tpointer += BF_MEMORY_SIZE;\n")
.append(currentTabs).append('}');
break;
case '>':
sourceCode.append("if (++pointer == BF_MEMORY_SIZE) {\n")
.append(currentTabs).append("\tpointer = 0;\n")
.append(currentTabs).append('}');
break;
case '.':
sourceCode.append("System.out.print((char) (memory[pointer] + HALF_BYTE));");
break;
case ',':
sourceCode.append("try {\n")
.append(currentTabs).append("\tmemory[pointer] = (byte) (INPUT.read() - HALF_BYTE);")
.append(currentTabs).append("catch (IOException e) {}");
break;
case '[':
sourceCode.append("while (memory[pointer] != Byte.MIN_VALUE) {");
currentTabs += '\t';
break;
case ']':
sourceCode.deleteCharAt(sourceCode.length() - 1).append('}');
currentTabs = currentTabs.substring(0, currentTabs.length() - 1);
}
sourceCode.append('\n');
}
sourceCode.append("\t}\n\n")
.append("}");
System.out.println(sourceCode);
return new BFProgram(sourceCode.toString(), "java");
}
}
BFProgram.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;
public class BFProgram {
private static final Pattern ALLOWED_FILE_EXTENSIONS = Pattern
.compile("(java)|(cpp)|(c)");
private static final String BF = "BF";
private String sourceCode;
private File codeFile;
public BFProgram(String sourceCode, String extension) {
if (!ALLOWED_FILE_EXTENSIONS.matcher(extension).matches()) {
throw new IllegalArgumentException("the extension \"" + extension
+ "\" is not valid");
}
this.sourceCode = sourceCode;
this.codeFile = new File(BF + "." + extension);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(
this.codeFile))) {
this.codeFile.createNewFile();
writer.write(sourceCode);
} catch (IOException e) {
throw new IllegalArgumentException("File error in creation.");
}
}
public String getSourceCode() {
return sourceCode;
}
public void run() throws IOException, InterruptedException {
JavaProgramRunner.run(codeFile);
}
}
JavaProgramRunner.java
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JavaProgramRunner {
private static final String JAVA = ".java";
private static final String COMPILE = "javac ";
private static final String RUN = "java ";
public static void run(File javaFile) throws IOException, InterruptedException {
if (!javaFile.getName().endsWith(JAVA)) {
throw new IllegalArgumentException("file \"" + javaFile.getAbsolutePath() + "\"is not a java file");
}
String name = javaFile.getName();
runProcess(COMPILE + name);
long time = System.nanoTime();
runProcess(RUN + name.substring(0, name.length() - JAVA.length()));
System.out.println(System.nanoTime() - time);
}
private static void runProcess(String command) throws IOException,
InterruptedException {
Process process = Runtime.getRuntime().exec(command);
printLines(command + " stdout: ", process.getInputStream());
printLines(command + " stderr: ", process.getErrorStream());
process.waitFor();
System.out.println(command + " exitValue() " + process.exitValue());
}
private static void printLines(String name, InputStream ins)
throws IOException {
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + line);
}
}
}
Concerns:
- Just like last time, grouping commands. Probably coming next revision.
- Length of code