HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Brainfuck to Java converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
brainfuckconverterjava

Problem

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
[->+>+>+>+>+++>>> 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
[->+>+>+>+>+>++++++>++++++> 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 forward to the start position 255 marker
> Decrease countdown
>+ Increase counter
>[-] Reset boolean for if we have found

Solution

Simplification of generated code

In some of the example generated code, you have stuff like this:

memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;


This isn't ideal. Rather, you'd probably want something more along the lines of this:

memory[pointer] += 10;


The above tip can also apply to pointer increments as well, instead of just cell increments.

Ideally, something like this shouldn't be too hard to do. You just need to do an additional scan over the generated code, detect repetitions, and simplify them.

If you want an additional challenge, you could have Brainfuck code like this:

++[>++<-]


That compiles to something sort of like this (This isn't how your code seems to compile, but I couldn't understand it, so just try and work with me):

...
memory[pointer]++;
memory[pointer]++;
while(memory[pointer] != 0) {
    pointer++;
    memory[pointer]++;
    memory[pointer]++;
}
...


Rather than bothering with the loop, you could analyze it and determine that it simply just puts the number four in the second cell. Therefore, you could simplify it to this:

pointer++;
memory[pointer] += 4;


While it'd be harder for more complex structures, it'd certainly improve performance.

Code Snippets

memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer]++;
memory[pointer] += 10;
...
memory[pointer]++;
memory[pointer]++;
while(memory[pointer] != 0) {
    pointer++;
    memory[pointer]++;
    memory[pointer]++;
}
...
pointer++;
memory[pointer] += 4;

Context

StackExchange Code Review Q#108697, answer score: 4

Revisions (0)

No revisions yet.