Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- $instructionpointer = 0;
- $memorypointer = 0;
- $memory = array();
- $callstack = array();
- // clear the memory cell array
- for ($i = 0; $i < 1000; $i++) {
- $memory[$i] = 0;
- }
- $brainfuckcode = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.';
- $length = strlen($brainfuckcode);
- while ($instructionpointer < $length) {
- $opcode = substr($brainfuckcode,$instructionpointer,1);
- switch ($opcode) {
- case "." :
- echo chr($memory[$memorypointer]);
- break;
- case "+" :
- $memory[$memorypointer]++;
- break;
- case "-" :
- $memory[$memorypointer]--;
- break;
- case ">" :
- $memorypointer++;
- break;
- case "<" :
- $memorypointer--;
- break;
- case "[" :
- $callstack[] = $instructionpointer;
- break;
- case "]" :
- if ($memory[$memorypointer] != 0) {
- $instructionpointer = $callstack[count($callstack)-1];
- } else {
- array_pop($callstack);
- }
- break;
- }
- $instructionpointer++;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement