Skip to main content
Flagged more items as not required for turing completeness.
Source Link
Dewi Morgan
  • 607
  • 4
  • 11
<?PHP
// Create function refs, for those which have characters too high.
$FILL = 'ARRA' . CHR(89) . CHR(95) . 'FILL'; // Array_fill to create the tape.
$CHOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'LICE'; // Array_slice for array indexing.
$POP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'HIF' . CHR(84); // Array_shift for array indexing.
$DEPOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'PLICE'; // Array_splice for array inserting.

$LEN = CHR(83) . CHR(84) . 'RLEN'; // Strlen
$LOP = CHR(83) . CHR(84) . 'R' . CHR(95) . CHR(83) . 'PLI' . CHR(84); // Str_split

// "Hello world!" - note using round braces instead of square in the brainfsck code.
$IN =  (">+++++++++(<++++++++>-)<.>+++++++(<++++>-)<+.+++++++..+++.>>>++++++++(<++++>-)<.>>>++++++++++(<+++++++++>-)<---.<<<<.+++.------.--------.>>+.>++++++++++.");
$INLEN = $LEN($IN);
$IN = $LOP($IN);

// Init tape with 10 zeros (add more for longer tape).
$A = $FILL(0,10,0);

// Set $AA ptr to first cell of tape.
$AA = 0;

FOR ($I = 0; $I < $INLEN; $I++):
    // Extract element: $CH = $IN[$I].
    $CH = $CHOP($IN, $I);
    $CH = $POP($CH);
    // Increment element at $I.
    //$CH++;
    //$CH = $FN($AA, $I, 1, $CH);
    
    // Only need one of '+' or '-' for TC if memory wraps.
    IF ($CH == '>'):
        $AA++;
    ENDIF;
    IF ($CH == '<'):
        $AA--;
    ENDIF; 

    // Only one of '+' or '-' is critical for Turing completeness.
    IF ($CH == '+'):
        // Increment element: $A[$AA]++;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID++;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF;
    IF ($CH == '-'):
        // Decrement element: $A[$AA]--;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID--;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF; 

    IF ($CH == ')'):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        IF ($ID):
            FOR ($LOOP = 1; $LOOP > 0; ):
                $CH = $CHOP($IN, --$I);
                $CH = $POP($CH);
                IF ($CH == '('):
                    $LOOP--;
                ENDIF;
                IF ($CH == ')'):
                    $LOOP++;
                ENDIF;
            ENDFOR;
        ENDIF;
    ENDIF; 

    // I/O is non-critical for Turing completenessTC.
    IF ($CH == '.' ):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        ECHO CHR($ID);
    ENDIF;
ENDFOR;
<?PHP
// Create function refs, for those which have characters too high.
$FILL = 'ARRA' . CHR(89) . CHR(95) . 'FILL'; // Array_fill to create the tape.
$CHOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'LICE'; // Array_slice for array indexing.
$POP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'HIF' . CHR(84); // Array_shift for array indexing.
$DEPOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'PLICE'; // Array_splice for array inserting.

$LEN = CHR(83) . CHR(84) . 'RLEN'; // Strlen
$LOP = CHR(83) . CHR(84) . 'R' . CHR(95) . CHR(83) . 'PLI' . CHR(84); // Str_split

// "Hello world!" - note using round braces instead of square in the brainfsck code.
$IN =  (">+++++++++(<++++++++>-)<.>+++++++(<++++>-)<+.+++++++..+++.>>>++++++++(<++++>-)<.>>>++++++++++(<+++++++++>-)<---.<<<<.+++.------.--------.>>+.>++++++++++.");
$INLEN = $LEN($IN);
$IN = $LOP($IN);

// Init tape with 10 zeros (add more for longer tape).
$A = $FILL(0,10,0);

// Set $AA ptr to first cell of tape.
$AA = 0;

FOR ($I = 0; $I < $INLEN; $I++):
    // Extract element: $CH = $IN[$I].
    $CH = $CHOP($IN, $I);
    $CH = $POP($CH);
    // Increment element at $I.
    //$CH++;
    //$CH = $FN($AA, $I, 1, $CH);
    
    IF ($CH == '>'):
        $AA++;
    ENDIF;
    IF ($CH == '<'):
        $AA--;
    ENDIF;
    IF ($CH == '+'):
        // Increment element: $A[$AA]++;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID++;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF;
    IF ($CH == '-'):
        // Decrement element: $A[$AA]--;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID--;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF;
    IF ($CH == ')'):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        IF ($ID):
            FOR ($LOOP = 1; $LOOP > 0; ):
                $CH = $CHOP($IN, --$I);
                $CH = $POP($CH);
                IF ($CH == '('):
                    $LOOP--;
                ENDIF;
                IF ($CH == ')'):
                    $LOOP++;
                ENDIF;
            ENDFOR;
        ENDIF;
    ENDIF;
    // I/O is non-critical for Turing completeness.
    IF ($CH == '.' ):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        ECHO CHR($ID);
    ENDIF;
ENDFOR;
<?PHP
// Create function refs, for those which have characters too high.
$FILL = 'ARRA' . CHR(89) . CHR(95) . 'FILL'; // Array_fill to create the tape.
$CHOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'LICE'; // Array_slice for array indexing.
$POP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'HIF' . CHR(84); // Array_shift for array indexing.
$DEPOP = 'ARRA' . CHR(89) . CHR(95) . CHR(83) . 'PLICE'; // Array_splice for array inserting.

$LEN = CHR(83) . CHR(84) . 'RLEN'; // Strlen
$LOP = CHR(83) . CHR(84) . 'R' . CHR(95) . CHR(83) . 'PLI' . CHR(84); // Str_split

// "Hello world!" - note using round braces instead of square in the brainfsck code.
$IN =  (">+++++++++(<++++++++>-)<.>+++++++(<++++>-)<+.+++++++..+++.>>>++++++++(<++++>-)<.>>>++++++++++(<+++++++++>-)<---.<<<<.+++.------.--------.>>+.>++++++++++.");
$INLEN = $LEN($IN);
$IN = $LOP($IN);

// Init tape with 10 zeros (add more for longer tape).
$A = $FILL(0,10,0);

// Set $AA ptr to first cell of tape.
$AA = 0;

FOR ($I = 0; $I < $INLEN; $I++):
    // Extract element: $CH = $IN[$I].
    $CH = $CHOP($IN, $I);
    $CH = $POP($CH);
    // Increment element at $I.
    //$CH++;
    //$CH = $FN($AA, $I, 1, $CH);
    
    // Only need one of '+' or '-' for TC if memory wraps.
    IF ($CH == '>'):
        $AA++;
    ENDIF;
    IF ($CH == '<'):
        $AA--;
    ENDIF; 

    // Only one of '+' or '-' is critical for Turing completeness.
    IF ($CH == '+'):
        // Increment element: $A[$AA]++;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID++;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF;
    IF ($CH == '-'):
        // Decrement element: $A[$AA]--;
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        $ID--;
        $DEPOP($A, $AA, 1, $ID);
    ENDIF; 

    IF ($CH == ')'):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        IF ($ID):
            FOR ($LOOP = 1; $LOOP > 0; ):
                $CH = $CHOP($IN, --$I);
                $CH = $POP($CH);
                IF ($CH == '('):
                    $LOOP--;
                ENDIF;
                IF ($CH == ')'):
                    $LOOP++;
                ENDIF;
            ENDFOR;
        ENDIF;
    ENDIF; 

    // I/O is non-critical for TC.
    IF ($CH == '.' ):
        $ID = $CHOP($A, $AA);
        $ID = $POP($ID);
        ECHO CHR($ID);
    ENDIF;
ENDFOR;
link PHP
Source Link
Dewi Morgan
  • 607
  • 4
  • 11

PHPPHP, Score = 82 (R).

PHP, Score = 82 (R).

PHP, Score = 82 (R).

add subheaders; offer way to omit if/endif.
Source Link
Dewi Morgan
  • 607
  • 4
  • 11

Rationale:

Proof of concept:

Possible improvements:

If we can get rid of those two keywords, the next highest is the P in <?PHP, which is required, at least in later versions of PHP that deprecate short open tags. However, they have made a commitment that the short echo tag, <?= will always be supported, so that could perhaps be exploited to execute arbitrary PHP. Then there are the O's in ECHO. However, I/O isn't critical for Turing completeness, so we could just remove that. Then there's the N in ENDIF, and the I and F in IF, which could be replaced by the ternary operator, ?:.

If we can get rid of those two keywords, the next highest is the P in <?PHP, which is required, at least in later versions of PHP that deprecate short open tags. However, they have made a commitment that the short echo tag, <?= will always be supported, so that could perhaps be exploited to execute arbitrary PHP. Then there are the O's in ECHO. However, I/O isn't critical for Turing completeness, so we could just remove that. Then there's the N in ENDIF, and the I and F in IF.

Rationale:

Proof of concept:

Possible improvements:

If we can get rid of those two keywords, the next highest is the P in <?PHP, which is required, at least in later versions of PHP that deprecate short open tags. However, they have made a commitment that the short echo tag, <?= will always be supported, so that could perhaps be exploited to execute arbitrary PHP. Then there are the O's in ECHO. However, I/O isn't critical for Turing completeness, so we could just remove that. Then there's the N in ENDIF, and the I and F in IF, which could be replaced by the ternary operator, ?:.

OK, here's a brainfsck parser, because my previous answer was buggy.
Source Link
Dewi Morgan
  • 607
  • 4
  • 11
Loading
Expand on why FOR & CHR are required.
Source Link
Dewi Morgan
  • 607
  • 4
  • 11
Loading
added 13 characters in body
Source Link
Loading
Source Link
Dewi Morgan
  • 607
  • 4
  • 11
Loading