@ 2014-02-16T09:22:18Z <?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.007 0.037 12.39 5.4.24 0.007 0.038 12.39 5.4.23 0.005 0.039 12.38 5.4.22 0.007 0.037 12.38 5.4.21 0.004 0.039 12.38 5.4.20 0.006 0.042 12.38 5.4.19 0.008 0.040 12.38 5.4.18 0.010 0.036 12.37 5.4.17 0.006 0.037 12.38 5.4.16 0.008 0.041 12.37 5.4.15 0.008 0.041 12.37 5.4.14 0.009 0.040 12.06 5.4.13 0.006 0.039 12.04 5.4.12 0.008 0.038 12.00 5.4.11 0.007 0.039 12.00 5.4.10 0.006 0.040 12.00 5.4.9 0.007 0.042 12.00 5.4.8 0.006 0.042 12.00 5.4.7 0.007 0.037 12.00 5.4.6 0.007 0.041 12.00 5.4.5 0.006 0.037 12.00 5.4.4 0.007 0.039 11.99 5.4.3 0.009 0.037 11.98 5.4.2 0.009 0.041 11.98 5.4.1 0.006 0.036 11.98 5.4.0 0.008 0.035 11.48 5.3.28 0.008 0.037 12.71 5.3.27 0.006 0.043 12.72 5.3.26 0.007 0.039 12.72 5.3.25 0.007 0.038 12.72 5.3.24 0.006 0.038 12.72 5.3.23 0.005 0.041 12.70 5.3.22 0.006 0.038 12.68 5.3.21 0.008 0.040 12.68 5.3.20 0.009 0.035 12.68 5.3.19 0.008 0.039 12.68 5.3.18 0.007 0.037 12.67 5.3.17 0.011 0.033 12.67 5.3.16 0.005 0.039 12.67 5.3.15 0.007 0.041 12.67 5.3.14 0.005 0.040 12.66 5.3.13 0.007 0.047 12.66 5.3.12 0.008 0.041 12.66 5.3.11 0.006 0.040 12.66 5.3.10 0.003 0.040 12.12 5.3.9 0.006 0.037 12.09 5.3.8 0.009 0.040 12.08 5.3.7 0.007 0.041 12.08 5.3.6 0.008 0.039 12.07 5.3.5 0.008 0.040 12.01 5.3.4 0.004 0.039 12.01 5.3.3 0.007 0.035 11.96 5.3.2 0.008 0.035 11.74 5.3.1 0.005 0.036 11.70 5.3.0 0.008 0.037 11.69
preferences:dark mode live preview
137.07 ms | 1394 KiB | 7 Q