3v4l.org

run code in 300+ PHP versions simultaneously
<?php function interpreter(string $code, int $iterations, int $width, int $height): string { $dataGrid = []; $output = ''; $codeOffset = -1; $skip = 0; $commands = $code; $pointer = ['x' => 0, 'y' => 0]; $jmps = new SplStack(); for ($row = 0; $row < $width; ++$row) { for ($col = 0; $col < $height; ++$col) { $dataGrid[$col][$row] = '0'; } } while (strlen($commands) && $iterations) { if (!$skip) echo "{$commands} ({$pointer['x']}, {$pointer['y']} = {$dataGrid[$pointer['x']][$pointer['y']]})\n"; ++$codeOffset; $command = $commands[0]; $commands = substr($commands, 1); if ($skip) { if ($command === ']') { $jmps->pop(); var_dump("pop"); --$skip; } elseif ($command === '[') { ++$skip; } continue; } //var_dump($command); switch ($command) { case 'n': if (--$pointer['x'] === -1) { $pointer['x'] = $height - 1; } break; case 'e': if (++$pointer['y'] === $width) { $pointer['y'] = 0; } break; case 's': if (++$pointer['x'] === $height) { $pointer['x'] = 0; } break; case 'w': if (--$pointer['y'] === -1) { $pointer['y'] = $width - 1; } break; case '*': if ($dataGrid[$pointer['x']][$pointer['y']] === '0') { echo "{$pointer['x']}, {$pointer['y']} = 1\n"; $dataGrid[$pointer['x']][$pointer['y']] = '1'; } else { echo "{$pointer['x']}, {$pointer['y']} = 0\n"; $dataGrid[$pointer['x']][$pointer['y']] = '0'; } break; case '[': $jmps->push($codeOffset); var_dump("push"); if ($dataGrid[$pointer['x']][$pointer['y']] === '0') { $skip = 1; } break; case ']': $jmpOffset = $jmps->top(); var_dump("pop"); if ($dataGrid[$pointer['x']][$pointer['y']] === '1') { $commands = substr($code, $jmpOffset + 1); $codeOffset = $jmpOffset; // ++$iterations; } break; default: continue 2; } --$iterations; } $rows = []; foreach ($dataGrid as $row) { $rows[] = implode('', $row); } return implode("\r\n", $rows); } var_dump(interpreter("*[es*]", 52, 5, 6)); /* 11000 01100 00110 00011 00001 10000*/ // var_dump(interpreter("*[s[e]*]", 9, 5, 5)); /*10000 10000 00000 00000 00000*/
Output for git.master, git.master_jit, rfc.property-hooks
*[es*] (0, 0 = 0) 0, 0 = 1 [es*] (0, 0 = 1) string(4) "push" es*] (0, 0 = 1) s*] (0, 1 = 0) *] (1, 1 = 0) 1, 1 = 1 ] (1, 1 = 1) string(3) "pop" es*] (1, 1 = 1) s*] (1, 2 = 0) *] (2, 2 = 0) 2, 2 = 1 ] (2, 2 = 1) string(3) "pop" es*] (2, 2 = 1) s*] (2, 3 = 0) *] (3, 3 = 0) 3, 3 = 1 ] (3, 3 = 1) string(3) "pop" es*] (3, 3 = 1) s*] (3, 4 = 0) *] (4, 4 = 0) 4, 4 = 1 ] (4, 4 = 1) string(3) "pop" es*] (4, 4 = 1) s*] (4, 0 = 0) *] (5, 0 = 0) 5, 0 = 1 ] (5, 0 = 1) string(3) "pop" es*] (5, 0 = 1) s*] (5, 1 = 0) *] (0, 1 = 0) 0, 1 = 1 ] (0, 1 = 1) string(3) "pop" es*] (0, 1 = 1) s*] (0, 2 = 0) *] (1, 2 = 0) 1, 2 = 1 ] (1, 2 = 1) string(3) "pop" es*] (1, 2 = 1) s*] (1, 3 = 0) *] (2, 3 = 0) 2, 3 = 1 ] (2, 3 = 1) string(3) "pop" es*] (2, 3 = 1) s*] (2, 4 = 0) *] (3, 4 = 0) 3, 4 = 1 ] (3, 4 = 1) string(3) "pop" es*] (3, 4 = 1) s*] (3, 0 = 0) *] (4, 0 = 0) 4, 0 = 1 ] (4, 0 = 1) string(3) "pop" es*] (4, 0 = 1) s*] (4, 1 = 0) *] (5, 1 = 0) 5, 1 = 1 ] (5, 1 = 1) string(3) "pop" es*] (5, 1 = 1) s*] (5, 2 = 0) *] (0, 2 = 0) 0, 2 = 1 ] (0, 2 = 1) string(3) "pop" es*] (0, 2 = 1) s*] (0, 3 = 0) string(40) "11100 01100 00110 00011 10001 11000"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
68.8 ms | 403 KiB | 8 Q