@ 2014-02-16T09:22:35Z <?PHP
class InvalidStateException extends RuntimeException { }
class Compiler {
private $ptr;
private $arr;
private $code;
private $commands = array('>', '<', '+', '-', '.', ',', '[', ']');
private $call_stack = array();
const 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();
$this->ptr = 0;
$this->code = $code;
for ($this->i=0;$this->i<strlen($this->code);$this->i++) {
$c = $this->c = substr($this->code, $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;
}
}
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);
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.25 0.010 0.057 18.73 5.4.24 0.023 0.053 19.01 5.4.23 0.013 0.047 18.79 5.4.22 0.013 0.050 18.78 5.4.21 0.013 0.050 18.64 5.4.20 0.020 0.053 18.64 5.4.19 0.003 0.057 18.78 5.4.18 0.020 0.040 18.93 5.4.17 0.010 0.050 18.75 5.4.16 0.007 0.057 18.77 5.4.15 0.027 0.043 19.01 5.4.14 0.010 0.047 16.42 5.4.13 0.013 0.043 16.53 5.4.12 0.003 0.053 16.39 5.4.11 0.007 0.073 16.53 5.4.10 0.003 0.053 16.52 5.4.9 0.020 0.037 16.45 5.4.8 0.010 0.047 16.44 5.4.7 0.017 0.053 16.50 5.4.6 0.010 0.047 16.35 5.4.5 0.003 0.050 16.42 5.4.4 0.007 0.050 16.53 5.4.3 0.010 0.043 16.58 5.4.2 0.013 0.057 16.48 5.4.1 0.017 0.043 16.40 5.4.0 0.010 0.047 15.81 5.3.28 0.007 0.050 14.65 5.3.27 0.017 0.047 14.72 5.3.26 0.007 0.073 14.71 5.3.25 0.023 0.060 14.66 5.3.24 0.013 0.063 14.61 5.3.23 0.010 0.050 14.46 5.3.22 0.007 0.053 14.59 5.3.21 0.013 0.047 14.59 5.3.20 0.007 0.060 14.69 5.3.19 0.000 0.057 14.62 5.3.18 0.010 0.053 14.82 5.3.17 0.003 0.070 14.82 5.3.16 0.003 0.073 14.61 5.3.15 0.013 0.050 14.59 5.3.14 0.007 0.050 14.75 5.3.13 0.013 0.047 14.80 5.3.12 0.007 0.057 14.59 5.3.11 0.013 0.047 14.41 5.3.10 0.010 0.053 13.89 5.3.9 0.007 0.053 14.27 5.3.8 0.017 0.043 14.02 5.3.7 0.007 0.050 14.23 5.3.6 0.007 0.050 14.20 5.3.5 0.010 0.043 14.20 5.3.4 0.017 0.040 14.16 5.3.3 0.013 0.047 14.00 5.3.2 0.010 0.050 13.79 5.3.1 0.010 0.057 13.85 5.3.0 0.003 0.073 13.74
preferences:dark mode live preview
141.77 ms | 1394 KiB | 7 Q