@ 2014-02-16T09:24:34Z <?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.020 0.043 18.93 5.4.24 0.007 0.053 18.98 5.4.23 0.013 0.047 18.93 5.4.22 0.007 0.057 18.77 5.4.21 0.020 0.040 18.93 5.4.20 0.010 0.053 18.93 5.4.19 0.017 0.063 18.78 5.4.18 0.027 0.050 19.03 5.4.17 0.013 0.047 18.73 5.4.16 0.010 0.060 18.92 5.4.15 0.013 0.057 18.94 5.4.14 0.007 0.053 16.37 5.4.13 0.007 0.053 16.59 5.4.12 0.010 0.050 16.41 5.4.11 0.007 0.050 16.43 5.4.10 0.013 0.043 16.45 5.4.9 0.010 0.050 16.76 5.4.8 0.010 0.053 16.60 5.4.7 0.007 0.070 16.58 5.4.6 0.010 0.047 16.32 5.4.5 0.010 0.053 16.62 5.4.4 0.023 0.033 16.32 5.4.3 0.007 0.047 16.45 5.4.2 0.017 0.040 16.44 5.4.1 0.003 0.073 16.48 5.4.0 0.013 0.043 16.02 5.3.28 0.013 0.050 14.69 5.3.27 0.013 0.043 14.73 5.3.26 0.010 0.067 14.86 5.3.25 0.013 0.047 14.63 5.3.24 0.020 0.040 14.54 5.3.23 0.010 0.063 14.73 5.3.22 0.007 0.050 14.69 5.3.21 0.010 0.050 14.61 5.3.20 0.007 0.050 14.76 5.3.19 0.017 0.063 14.61 5.3.18 0.013 0.047 14.60 5.3.17 0.010 0.053 14.76 5.3.16 0.003 0.057 14.58 5.3.15 0.010 0.047 14.57 5.3.14 0.007 0.047 14.66 5.3.13 0.013 0.067 14.59 5.3.12 0.010 0.050 14.57 5.3.11 0.013 0.063 14.57 5.3.10 0.013 0.043 14.20 5.3.9 0.010 0.070 14.11 5.3.8 0.020 0.037 13.88 5.3.7 0.017 0.060 14.14 5.3.6 0.010 0.050 14.00 5.3.5 0.000 0.053 14.07 5.3.4 0.010 0.043 14.06 5.3.3 0.010 0.043 14.16 5.3.2 0.000 0.053 13.91 5.3.1 0.010 0.067 13.65 5.3.0 0.017 0.053 13.74
preferences:dark mode live preview
140.94 ms | 1394 KiB | 7 Q