@ 2014-02-16T10:10:14Z <?PHP
if (!class_exists('RuntimeException')) {
class RuntimeException extends Exception { }
}
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;
$jump = false;
$loop = array();
$depth = 0;
for ($this->i=0;$this->i<strlen($this->code);$this->i++) {
$c = $this->c = substr($this->code, $this->i, 1);
$cell = $this->arr[$this->ptr];
if (!in_array($c, $this->commands)) {
continue;
}
else if ($jump > 0) {
if ($c == '[') {
$depth++;
}
if ($c == ']') {
$depth--;
if ($depth === $jump) {
$jump = 0;
$this->stack();
}
}
}
else {
if ($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 == '[') {
if ($cell == 0) {
$this->stack('jump');
$jump = $depth;
}
else {
array_push($loop, $this->i);
$depth++;
}
}
elseif ($c == ']') {
if ($cell > 0) {
$this->i = $loop[count($loop)-1];
continue;
}
else {
array_pop($loop);
$depth--;
continue;
}
}
}
}
}
private function stack($func=null) {
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);
foreach ($this->getCallStack() as $k => $call) {
echo '#'.$k.' '.$call['command'].' ('.$call['internal_function'],') on pos '.$call['pos'].PHP_EOL;
}
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 InvalidStateException("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;
}
private function decrement_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;
}
private function output_val() {
$c = $this->arr[$this->ptr];
echo ''.chr($c);
}
private function store_val($chr) {
$x = ord($chr);
$this->arr[$this->ptr] = $x;
}
private function input_val() {
return fgets(STDIN);
}
public function getCallStack() {
return $this->call_stack;
}
public function getCells() {
return $this->arr;
}
public function getPointer() {
return $this->ptr;
}
}
$c = new Compiler();
$bf = '
>++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>
>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.
';
try {
echo "{ ";
$c->execute($bf);
echo " }";
}
catch (Exception $e) {
echo PHP_EOL."An exception was thrown whilst processing your BF program.".PHP_EOL;
echo $e->getMessage().":".PHP_EOL;
}
echo "The pointer was set to ".$c->getPointer()." and the cell array resembled:".PHP_EOL;
echo "[".implode(", ", $c->getCells())."]".PHP_EOL;
echo "The current stack trace was:".PHP_EOL;
foreach ($c->getCallStack() as $k => $call) {
echo '#'.$k.' '.$call['command'].' ('.$call['internal_function'],') on pos '.$call['pos'].PHP_EOL;
}
echo PHP_EOL.'Finished.'.PHP_EOL;
?>
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) 8.3.7 0.010 0.021 18.31 8.3.6 0.026 0.010 18.43 8.3.5 0.027 0.007 21.27 8.3.4 0.039 0.013 18.84 8.3.3 0.013 0.013 20.38 8.3.2 0.027 0.005 20.29 8.3.1 0.009 0.006 22.14 8.3.0 0.016 0.005 22.43 8.2.19 0.024 0.000 18.43 8.2.18 0.021 0.018 16.63 8.2.17 0.027 0.009 22.96 8.2.16 0.029 0.007 20.70 8.2.15 0.014 0.007 24.18 8.2.14 0.019 0.013 24.66 8.2.13 0.029 0.006 26.16 8.2.12 0.018 0.007 19.66 8.2.11 0.023 0.006 22.02 8.2.10 0.025 0.006 18.09 8.2.9 0.013 0.004 19.42 8.2.8 0.009 0.009 19.53 8.2.7 0.011 0.006 17.75 8.2.6 0.010 0.005 18.17 8.2.5 0.014 0.006 18.07 8.2.4 0.009 0.009 18.47 8.2.3 0.016 0.000 18.20 8.2.2 0.014 0.005 17.87 8.2.1 0.010 0.007 17.93 8.2.0 0.021 0.000 17.84 8.1.28 0.023 0.003 25.92 8.1.27 0.024 0.009 20.37 8.1.26 0.015 0.005 26.35 8.1.25 0.013 0.006 28.09 8.1.24 0.010 0.015 23.91 8.1.23 0.013 0.017 21.05 8.1.22 0.000 0.014 17.91 8.1.21 0.007 0.007 18.77 8.1.20 0.007 0.011 17.47 8.1.19 0.010 0.003 17.66 8.1.18 0.004 0.009 19.13 8.1.17 0.009 0.013 18.59 8.1.16 0.010 0.003 22.30 8.1.15 0.007 0.007 18.79 8.1.14 0.011 0.003 17.55 8.1.13 0.007 0.007 18.03 8.1.12 0.004 0.008 17.52 8.1.11 0.004 0.012 17.66 8.1.10 0.006 0.009 17.48 8.1.9 0.008 0.008 17.60 8.1.8 0.008 0.004 17.52 8.1.7 0.009 0.004 17.57 8.1.6 0.007 0.007 17.72 8.1.5 0.006 0.013 17.65 8.1.4 0.000 0.012 17.55 8.1.3 0.006 0.006 17.61 8.1.2 0.014 0.003 17.68 8.1.1 0.009 0.005 17.72 8.1.0 0.003 0.010 17.62 8.0.30 0.007 0.007 18.77 8.0.29 0.003 0.010 17.05 8.0.28 0.009 0.004 18.54 8.0.27 0.012 0.002 17.29 8.0.26 0.007 0.007 16.85 8.0.25 0.004 0.008 17.05 8.0.24 0.008 0.008 17.11 8.0.23 0.010 0.003 17.01 8.0.22 0.003 0.009 17.03 8.0.21 0.003 0.013 16.98 8.0.20 0.006 0.009 17.02 8.0.19 0.003 0.010 16.91 8.0.18 0.018 0.004 16.99 8.0.17 0.010 0.005 17.02 8.0.16 0.004 0.013 16.83 8.0.15 0.004 0.012 16.93 8.0.14 0.007 0.010 16.97 8.0.13 0.003 0.009 13.57 8.0.12 0.003 0.010 16.86 8.0.11 0.000 0.014 16.83 8.0.10 0.008 0.004 16.79 8.0.9 0.006 0.006 16.77 8.0.8 0.024 0.011 16.93 8.0.7 0.005 0.010 16.93 8.0.6 0.000 0.012 16.98 8.0.5 0.008 0.004 16.88 8.0.3 0.051 0.030 17.24 8.0.2 0.025 0.028 17.40 8.0.1 0.000 0.012 17.04 8.0.0 0.039 0.027 16.78 7.4.33 0.004 0.007 15.00 7.4.32 0.008 0.004 16.58 7.4.30 0.003 0.010 16.54 7.4.29 0.008 0.005 16.50 7.4.28 0.004 0.012 16.55 7.4.27 0.004 0.008 16.51 7.4.26 0.008 0.008 16.54 7.4.25 0.010 0.003 16.55 7.4.24 0.006 0.007 16.49 7.4.23 0.006 0.006 16.60 7.4.22 0.022 0.016 16.60 7.4.21 0.022 0.026 16.59 7.4.20 0.004 0.011 16.52 7.4.19 0.000 0.014 16.38 7.4.16 0.078 0.029 16.54 7.4.15 0.034 0.036 17.40 7.4.14 0.051 0.027 17.86 7.4.13 0.046 0.021 16.56 7.4.12 0.029 0.018 16.52 7.4.11 0.041 0.032 16.45 7.4.10 0.057 0.013 16.45 7.4.9 0.103 0.023 16.53 7.4.8 0.020 0.028 19.39 7.4.7 0.004 0.025 16.46 7.4.6 0.027 0.027 16.69 7.4.5 0.015 0.008 16.39 7.4.4 0.039 0.029 22.77 7.4.3 0.035 0.017 16.57 7.4.0 0.008 0.025 14.69 7.3.33 0.000 0.016 13.36 7.3.32 0.006 0.006 13.55 7.3.31 0.010 0.003 16.46 7.3.30 0.003 0.012 16.41 7.3.29 0.013 0.017 16.38 7.3.28 0.024 0.026 16.44 7.3.27 0.035 0.035 17.40 7.3.26 0.037 0.036 16.66 7.3.25 0.052 0.027 16.42 7.3.24 0.045 0.023 16.52 7.3.23 0.027 0.007 16.42 7.3.21 0.035 0.050 16.48 7.3.20 0.036 0.015 19.39 7.3.19 0.053 0.028 16.33 7.3.18 0.036 0.020 16.55 7.3.17 0.055 0.036 16.35 7.3.16 0.049 0.039 16.65 7.3.12 0.004 0.026 15.13 7.2.33 0.021 0.040 16.82 7.2.32 0.013 0.039 16.57 7.2.31 0.058 0.025 16.91 7.2.30 0.040 0.022 16.73 7.2.29 0.030 0.019 16.84 7.2.6 0.014 0.011 16.73 7.2.5 0.013 0.010 16.98 7.1.20 0.013 0.013 15.78 7.1.7 0.010 0.010 16.98 7.1.6 0.007 0.030 19.40 7.1.5 0.008 0.012 17.06 7.0.20 0.005 0.009 16.90
preferences:dark mode live preview
41.98 ms | 401 KiB | 5 Q