3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP class InvalidStateException extends RuntimeException { } class Compiler { private $ptr; private $arr; private $code; private $commands = array('>', '<', '+', '-', '.', ',', '[', ']'); private $call_stack = array(); private $cellsize = 8; public function parseString($code, $throwException = true) { $this->code = $code; for ($i=0;$i<strlen($this->code);$i++) { $c = substr($this->code, $i, 1); if (!in_array($c, $this->commands)) { continue; } } } public function execute($code) { $this->arr = array(0 => 0); $this->ptr = 0; $this->code = $code; for ($this->i=0;$this->i<strlen($this->code);$this->i++) { $c = $this->c = substr($this->code, $this->i, 1); if (!in_array($c, $this->commands)) { continue; } elseif ($c == '>') { $this->increment_pointer(); } elseif ($c == '<') { $this->decrement_pointer(); } elseif ($c == '+') { $this->increment_val(); } elseif ($c == '-') { $this->decrement_val(); } elseif ($c == '.') { $this->output_val(); } elseif ($c == ',') { $this->store_val($this->input_val()); } elseif ($c == '[') { $this->loop($this->i); } } } private function stack($func) { if (is_null($func)) { if (count($this->call_stack) == 0) { throw new InvalidStateException("Trying to pop empty call stack."); return false; } array_pop($this->call_stack); return true; } else { $info = array('command' => $this->c, 'pos' => $this->i, 'internal_function' => $func); array_push($this->call_stack, $info); return true; } } public function minimum_cellsize() { return (-1*(pow(2, $this->cellsize-1))); } public function maximum_cellsize() { return pow(2, $this->cellsize-1)-1; } private function increment_pointer() { $this->stack(__FUNCTION__); $this->ptr++; if (!isset($this->arr[$this->ptr])) $this->arr[$this->ptr] = 0; $this->stack(); return true; } private function decrement_pointer() { $this->stack(__FUNCTION__); $this->ptr--; if ($this->ptr < 0) { throw new IllegalStateException("The data pointer is less than 0."); return false; } $this->stack(); return true; } private function increment_val() { $x = $this->arr[$this->ptr]++; if ($x < $this->minimum_cellsize()) { $this->arr[$this->ptr] = $this->maximum_cellsize(); } elseif ($x > $this->maximum_cellsize()) { $this->arr[$this->ptr] = $this->minimum_cellsize(); } return true; } public function getCallStack() { return $this->call_stack; } } $c = new Compiler(); $bf = '+++++++++ +++++++++ +++++++++ + > +++++++++ +++++++++ +++++++++ ++ < . < .'; $c->execute($bf); ?>
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Creation of dynamic property Compiler::$i is deprecated in /in/bgW9t on line 28 Deprecated: Creation of dynamic property Compiler::$c is deprecated in /in/bgW9t on line 29 Fatal error: Uncaught ArgumentCountError: Too few arguments to function Compiler::stack(), 0 passed in /in/bgW9t on line 85 and exactly 1 expected in /in/bgW9t:57 Stack trace: #0 /in/bgW9t(85): Compiler->stack() #1 /in/bgW9t(34): Compiler->increment_pointer() #2 /in/bgW9t(118): Compiler->execute('+++++++++ +++++...') #3 {main} thrown in /in/bgW9t on line 57
Process exited with code 255.
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Compiler::stack(), 0 passed in /in/bgW9t on line 85 and exactly 1 expected in /in/bgW9t:57 Stack trace: #0 /in/bgW9t(85): Compiler->stack() #1 /in/bgW9t(34): Compiler->increment_pointer() #2 /in/bgW9t(118): Compiler->execute('+++++++++ +++++...') #3 {main} thrown in /in/bgW9t on line 57
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Warning: Missing argument 1 for Compiler::stack(), called in /in/bgW9t on line 85 and defined in /in/bgW9t on line 57 Notice: Undefined variable: func in /in/bgW9t on line 58 Warning: Missing argument 1 for Compiler::stack(), called in /in/bgW9t on line 96 and defined in /in/bgW9t on line 57 Notice: Undefined variable: func in /in/bgW9t on line 58 Fatal error: Uncaught Error: Call to undefined method Compiler::output_val() in /in/bgW9t:46 Stack trace: #0 /in/bgW9t(118): Compiler->execute('+++++++++ +++++...') #1 {main} thrown in /in/bgW9t on line 46
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Warning: Missing argument 1 for Compiler::stack(), called in /in/bgW9t on line 85 and defined in /in/bgW9t on line 57 Notice: Undefined variable: func in /in/bgW9t on line 58 Warning: Missing argument 1 for Compiler::stack(), called in /in/bgW9t on line 96 and defined in /in/bgW9t on line 57 Notice: Undefined variable: func in /in/bgW9t on line 58 Fatal error: Call to undefined method Compiler::output_val() in /in/bgW9t on line 46
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Class 'RuntimeException' not found in /in/bgW9t on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/bgW9t on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/bgW9t on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/bgW9t on line 6
Process exited with code 255.

preferences:
228.78 ms | 401 KiB | 315 Q