Javagony/Brainfuck Reduction

From Esolang
Jump to navigation Jump to search
Back to Javagony

Javagony has already been proven turing complete by User:PythonshellDebugwindow, through the implementation of a brainfuck interpreter, however, it would be easy to prove it turing complete by simply reducing it TO brainfuck.

Variables

Brainfuck can provide near infinite variable storage through the means of a pointer, Java (and javagony) has this capability through variables.

Incremention / Decrementation

Simple enough, however, you can increment or decrement a variable with ++ or --

Input

Input can be received as an ASCII byte through the Scanner class. One way to receive the bytes value might be:

Scanner scanner = new Scanner(System.in);
pointerVar = scanner.nextLine().toCharArray()[0];

However, this is not the best way to do it.

Loops

Loops is the most restrictive thing about Javagony that could suggest it not being turing complete. Loops in brainfuck require a while loop, or similar, and to check if a value is non-zero. In Javagony, the most common way to replicate loops is through recursion. If your loop has too many steps, you will get a stack overflow error, however, this can just be caught. So, to create a loop, we can start with a function that calls itself, and a try catch.

public void loop() {
    try {
        loop();
    catch (StackOverflowException e) {
        loop();
    }
}

Now, we must check if a value is non-zero on each iteration. We can use the ArithmeticException for this, because it is called upon division by zero. Therefore, we can check if a number, divided by the pointer value is 0.

public void loop() {
    bool zero = false;
    try { double d = 7.0 / pointerVar } catch (ArithmeticException e) { zero = true; }
    if (zero) {
        return;
    }

    try {
        loop();
    catch (StackOverflowException e) {
        loop();
    }
}

This will divide 7, by the pointer var, and thus, if the value is 0, ArithmeicException will be called, where we will return the loop.

See Also