All pastes #633832 Raw Edit

Someone

public text v1 · immutable
#633832 ·published 2007-07-25 23:16 UTC
rendered paste body
<?php
if (count($argv) == 1 || !file_exists($argv[ 1 ])) {
	echo "\nBrainFuck PHP Executor\n\n";
	echo "Usage: bf.php filename.bf\n\n";
	die();
}

$rSTDIN   = fopen( 'php://stdin', 'r' );
$szFile   = file_get_contents( $argv[ 1 ] );
$szFile   = preg_replace( '/([\s]+)/', '', $szFile );

$aMemory  = array( );
$iPointer = $iPos = 0;

function Parse( $szCode, $bWhile = false )
{
	global $aMemory, $iPointer;
	
	$aChunk = preg_split( '//', $szCode );
	for ($i = 0; $i < count( $aChunk ); $i++) {
		$cOpcode = $aChunk[ $i ];

		switch($cOpcode) {
			case '+': $aMemory[ $iPointer ]++; break;
			case '-': $aMemory[ $iPointer ]--; break;
			case '>': $iPointer ++; break;
			case '<': $iPointer --; break;
			case '.': echo chr($aMemory[ $iPointer ]); break;
			case ',': $aMemory[ $iPointer ] = fread( $rSTDIN, 1 ); break;
			case '[':
				$szChunk = ''; $i++;
				while ($aChunk[$i] != ']') {
					$szChunk .= $aChunk[ $i ];
					$i++;
				}
				
				while ($aMemory[ $iPointer ] != 0) {
					Parse( $szChunk, true );
				}
			break;
			case ']': break;
		}
	}
}

Parse( $szFile );
?>