';
$pointer = 0; //The pointer of our application
$array = array(); //The array the includes all the elements' values.
$code = $_POST['code']; //The code
$vertype = $_POST['verbose']; //The verbose type
if ($vertype < 3) //In case of less verbose, there is no need for this link.
echo 'Goto output...
';
$len = strlen($code); //Let's get the length of the code.
$startsub = 0; //Instead of displaying each time an element goes up or down, this will put them all together for less verbose.
$startcnt = 0; //Same as above, just subtract
$bcnt = 0; //The amount of borders or loops ( [ ] )
$whilec = 0; //A system to check if there is loop going...
$crinput = 0; //The position in the input.
$input = $_POST['input']; //The input.
for ($q = 0; $q < $len; $q++) { //This system will check to make sure there is the same amount of [ and ].
if ($code[$q] == '[') {
$bcnt++;
} elseif ($code[$q] == ']') {
if ($bcnt == 0) { //In case a ] is encountered before any [, there is something wrong, therefore terminate.
echo 'Error. A \']\' was found before any \'[\', terminating interpreter
';
$q = $len * 2;
$bcnt = 3;
}
$bcnt--;
}
}
if ($bcnt == 0) { //Since we have a good result in our test, let's interpret the application.
for ($i = 0; $i < $len; $i++) { //This is our master for loop, that will go on, untill we don't have any more code.
if ($code[$i] == '+') { //In case we encounter a '+' we want to add one to our current element.
if ($startsub > 0) { //This is to see if there has been doing some subtracting before, and therefore it should be displayed to user. This is seen in every sign we check, though not '-' itself. And '+' as the same system, just so I don't have to repeat myself over again.
print_subtract($startsub, $pointer);
$startsub = 0;
}
$array[$pointer]++; //Add one.
if ($vertype < 2) { //If we have a lot of verbose, we want to display that we added one.
echo 'Added 1 to element '.$pointer."
\nElement ".$pointer.'\'s current value: '.$array[$pointer].'
'."\n";
} elseif ($vertype == 2) { //This is the system that will only display a message when we stopped adding ( in case of something like: '++++++' )
$startcnt++;
}
} elseif ($code[$i] == '-') { //Same as above, just subtract
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
}
$array[$pointer]--;
if ($vertype < 2) {
echo 'Subtracted 1 from element '.$pointer."
\nElement ".$pointer.'\'s current value: '.$array[$pointer].'
'."\n";
} elseif ($vertype == 2) {
$startsub++;
}
} elseif ($code[$i] == '<') { //Let's see if we can move the system to the left.
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
if ($pointer == 0) { //In case we are at the left most element, we cannot move further.
echo 'We are pointing at element 0, cannot go further left.
';
$j = $i;
while ($whilec>0) { //In case we are in a while loop, we want to stop the while loop, as this could mean it will go infinity now, so we look for the ] that will let us out of here.
$j++;
if ($code[$j] == '[') {
$whilec++;
} elseif ($code[$j] == ']') {
$whilec--;
}
}
if ($vertype < 3) //The HTML will fuck up if we don't end the
now.
echo '
Ending while loop for security reasons, ince the pointer tried going to an element that doesn\'t exist!
';
$i = $j;
} else { //In case everything is alright, do as normal...
$old = $pointer; //That's just for the verbose.
$pointer--; //Go down.
if (!$array[$pointer]) { $array[$pointer] = 0; } //If the element hasn't go any value, set it to 0. Though, it should have, cause we will always come from the left.
if ($vertype < 3) //The verbose.
echo 'Going to element '.$pointer.' from element '.$old.'
Element '.$pointer.'\'s value: '.$array[$pointer].'
';
}
} elseif ($code[$i] == '>') { //Now turning the other way.
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
$old = $pointer;
$pointer++;
$maxpointer = ($maxpointer<$pointer)?$pointer:$maxpointer; //In case we haven't been here before, we will use this to check how many elements we have touched.
if (!$array[$pointer]) { $array[$pointer] = 0; } //Setting the value to 0, if null.
if ($vertype < 3)
echo 'Going to element '.$pointer.' from element '.$old.'
Element '.$pointer.'\'s value: '.$array[$pointer].'
';
} elseif ($code[$i] == '[') { //In case we stumble over a start of a loop.
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
if ($array[$pointer] != 0) { //If our current element ain't 0, the loop can start.
if ($vertype < 3)
echo 'Starting while loop ( until current element is 0 )...
';
$whilec++; //Those adding the fact that it's a loop.
} else { //If not, then we have to find the end of it.
if ($vertype < 4)
echo 'Skipping while loop, since current element is 0
';
$j = $i;
$cnt = 1;
while ($cnt>0) { //With this while system...
$j++;
if ($code[$j] == '[') {
$cnt++;
} elseif ($code[$j] == ']') {
$cnt--;
}
}
$i = $j;
}
} elseif ($code[$i] == ']') { //If it's an end loop.
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
if ($array[$pointer] != 0) { //Okay, we have to try again.
$j = $i;
$cnt = 1;
while($cnt > 0) { //Finding the start of the system.
$j--;
if ($code[$j] == '[') {
$cnt--;
} elseif ($code[$j] == ']') {
$cnt++;
}
}
$i = $j;
} elseif ($vertype < 3) { //If not, end the loop.
echo '
Ending while loop...
';
$whilec--;
}
} elseif ($code[$i] == '.') { //Our output system.
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
if ($array[$pointer] < 32) { //If the character ain't a valid ASCII character ( below 32 ), we'll have to notice about that.
if (($array[$pointer] == 0) or ($array[$pointer] == 10)) { //However, if it's 0 or 10, then it's a breakline ( notice: 0 acts like a breakline in Brainfuck )
if ($vertype < 2)
echo 'This is a break line.
';
$endstring .= '
'; //Add to the final output.
} else { //Or just do an error if it's not 0 or 10.
if ($vertype < 4)
echo 'Element '.$pointer.'\'s ASCII value is a not valid ASCII value.
';
}
} elseif ($array[$pointer] == 32) { //If it's 32, it's a space, and we'll treat that a bit different.
if ($vertype < 3)
echo 'Printing element '.$pointer.' in ASCII: [space]
';
$endstring .= ' '; //Add to the final output.
} elseif ($array[$pointer] > 255) { //Should it be higher than 255 ( not a valid ASCII number either, then return an error.
if ($vertype < 4)
echo 'The value of element '.$pointer.' is higher than 255, and thus a valid ASCII value.
';
} else { //If all of that ain't true, then we got ourselve one we can work with.
if ($vertype < 3)
echo 'Printing element '.$pointer.' in ASCII: '.chr($array[$pointer]).'
';
$endstring .= chr($array[$pointer]); //Add to the output.
}
} elseif ($code[$i] == ',') { //If we stumble over an input system, then we'll do this...
if ($startcnt > 0) {
print_adding($startcnt, $pointer);
$startcnt = 0;
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
$startsub = 0;
}
if ($input[$crinput]) { //First we check if there is an input for the current position in the complete input.
$array[$pointer] = ord($input[$crinput]); //Then we inserts the characters value.
if ($vertype < 3) //The verbose...
echo 'Inserted ASCII character ( '.$input[$crinput].' ) value ( '.ord($input[$crinput]).' ) into element '.$pointer.'
Element '.$pointer.'\'s current value: '.$array[$pointer].'
';
$crinput++; //And we move our system up one.
} else { //If there isn't anything in the system right now, we have to notice the user, in case it's an error.
if ($array[$pointer] != 10) { //If the current one ain't 10, we'll have to try that.
$array[$pointer] = 10; //Since 10 acts like a new line, we'll use that, some brainfuck applications assumes that 10 is the value to go after and would end a loop when 10 is the value ( with some subtracting of course ).
if ($vertype < 4)
echo 'There was found no data to insert at element '.$pointer.'. Acts like a newline...
Please insert more data in your input.
';
} else { //If we have tried 10, and that didn't work, then we set the element to 0, cause the creater haven't thought of using a system that would subtract if 10 is happening.
$array[$pointer] = 0;
}
}
} else { //If our character should be ignored...
if ($vertype < 2)
echo 'Character \''.$code[$i].'\' unknown... skipping...
'."\n";
}
}
if ($startcnt > 0) { //If our applications ends with an adding going on, let's finish it up.
print_adding($startcnt, $pointer);
} elseif ($startsub > 0) {
print_subtract($startsub, $pointer);
}
echo '
'; //Do some line breaking...
$arraystring = ''; //Set our arraystring
$pointerstring = ''; //And pointerstring.
for ($i = 0; $i <= $maxpointer; $i++) { //Let $i goto the highest element we managed in our application.
$nown = (!$array[$i])?'0':$array[$i]; //If the value of it is null, make it to 0.
$space = ($array[$i]<100)?(($array[$i]<10)?' ':' '):' '; //To make it look good, add the amount of spaces need to the value.
$arraystring .= '['.$nown.']'.$space; //Add to our complete string.
if ($i == $pointer) { //Should $i be at where the pointer ended, then add some spaces so the user can see it point.
for ($j = 0; $j <= ($pointer * 6); $j += 2) {
$pointerstring .= ' ';
}
$pointerstring .= '^';
}
}
echo ''.$arraystring.'
'.$pointerstring; //The bookmark link is at the top with verbose, so let's make the link's direction here. And then add the arraystring and the pointerstring.
echo '
Complete output:
'.$endstring; //And at last, the output.
} else { //In case our application failed, we have to tell the user that we couldn't start the application.
echo 'Cannot start, there is no harmony between [\'s and ]\'s, in order for the application to work, there must be the same amount of [\'s and ]\'s';
}
echo ''; //And end...
}
function print_adding($startcnt, $pointer) //This function is the function that is seen being used so many times, cause it will echo that the value ($startcnt) has been added to the element $pointer.
{
global $array; //Let's grab the array.
echo 'Added '.$startcnt.' to element '.$pointer."
\nElement ".$pointer.'\'s current value: '.$array[$pointer].'
'."\n"; //Let's tell the user.
}
function print_subtract($startsub, $pointer) //Same above, just subtract.
{
global $array;
echo 'Subtracted '.$startsub.' from element '.$pointer."
\nElement ".$pointer.'\'s current value: '.$array[$pointer].'
'."\n";
}
function print_output($vbmsg, $pointer) //The output verboser system, not really that much need for commenting...
{
global $array, $endstring;
if ($array[$pointer] < 32) {
if (($array[$pointer] == 0) or ($array[$pointer] == 10)) {
if ($vbmsg < 2)
return 'This is a break line.
';
$endstring .= '
';
} else {
if ($vbmsg < 4)
return 'Element '.$pointer.'\'s ASCII value is a not valid ASCII value.
';
}
} elseif ($array[$pointer] == 32) {
if ($vbmsg < 3)
return 'Printing element '.$pointer.' in ASCII: [space]
';
$endstring .= ' ';
} elseif ($array[$pointer] > 255) {
if ($vbmsg < 4)
return 'The value of element '.$pointer.' is higher than 255, and thus a valid ASCII value.
';
} else {
if ($vbmsg < 3)
return 'Printing element '.$pointer.' in ASCII: '.chr($array[$pointer]).'
';
$endstring .= chr($array[$pointer]);
}
}//In fact, I don't even think it's used... Heh.
?>