@ 2014-02-16T10:19:43Z <?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 = -1;
$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 == ']') {
if ($depth === $jump) {
$jump = -1;
$this->stack();
}
$depth--;
}
}
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 == '[') {
$depth++;
if ($cell == 0) {
$this->stack('jump');
$jump = $depth;
}
else {
array_push($loop, $this->i);
}
}
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);
echo 'Stacking '.$func.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 {
$c->execute($bf);
}
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.4 0.011 0.007 18.83 8.3.3 0.007 0.014 20.21 8.3.2 0.003 0.007 20.19 8.3.1 0.003 0.007 22.00 8.3.0 0.012 0.000 20.82 8.2.17 0.010 0.010 22.96 8.2.16 0.010 0.010 20.42 8.2.15 0.006 0.003 24.18 8.2.14 0.010 0.003 24.66 8.2.13 0.018 0.004 26.16 8.2.12 0.003 0.007 19.66 8.2.11 0.022 0.000 22.25 8.2.10 0.004 0.027 18.03 8.2.9 0.003 0.006 19.47 8.2.8 0.004 0.009 19.41 8.2.7 0.009 0.004 17.63 8.2.6 0.013 0.000 18.18 8.2.5 0.004 0.007 18.07 8.2.4 0.004 0.007 18.16 8.2.3 0.013 0.003 18.32 8.2.2 0.004 0.007 17.82 8.2.1 0.003 0.009 17.96 8.2.0 0.009 0.000 17.91 8.1.27 0.008 0.003 23.99 8.1.26 0.010 0.000 26.35 8.1.25 0.005 0.005 28.09 8.1.24 0.010 0.003 23.97 8.1.23 0.018 0.005 19.10 8.1.22 0.012 0.006 17.74 8.1.21 0.010 0.003 18.77 8.1.20 0.017 0.003 17.48 8.1.19 0.006 0.006 17.78 8.1.18 0.012 0.000 19.14 8.1.17 0.007 0.010 18.59 8.1.16 0.000 0.009 22.25 8.1.15 0.006 0.003 18.74 8.1.14 0.004 0.008 17.48 8.1.13 0.004 0.007 17.98 8.1.12 0.007 0.003 17.52 8.1.11 0.003 0.009 17.58 8.1.10 0.005 0.010 17.65 8.1.9 0.003 0.006 17.59 8.1.8 0.004 0.004 17.68 8.1.7 0.003 0.006 17.54 8.1.6 0.007 0.004 17.77 8.1.5 0.007 0.004 17.74 8.1.4 0.012 0.006 17.59 8.1.3 0.003 0.007 17.80 8.1.2 0.007 0.013 17.61 8.1.1 0.009 0.009 17.63 8.1.0 0.003 0.010 17.63 8.0.30 0.003 0.006 18.77 8.0.29 0.003 0.007 17.18 8.0.28 0.003 0.007 18.51 8.0.27 0.006 0.003 17.21 8.0.26 0.006 0.003 16.75 8.0.25 0.006 0.009 16.96 8.0.24 0.006 0.006 16.89 8.0.23 0.006 0.003 16.98 8.0.22 0.003 0.006 16.97 8.0.21 0.000 0.008 16.82 8.0.20 0.009 0.000 17.03 8.0.19 0.004 0.007 16.91 8.0.18 0.005 0.010 16.85 8.0.17 0.005 0.005 16.95 8.0.16 0.009 0.012 16.91 8.0.15 0.012 0.004 16.93 8.0.14 0.003 0.009 16.88 8.0.13 0.000 0.010 13.41 8.0.12 0.010 0.003 16.92 8.0.11 0.003 0.006 16.91 8.0.10 0.005 0.009 16.73 8.0.9 0.003 0.006 16.96 8.0.8 0.007 0.015 16.83 8.0.7 0.007 0.007 16.77 8.0.6 0.006 0.010 16.82 8.0.5 0.004 0.008 16.73 8.0.3 0.034 0.025 17.06 8.0.2 0.019 0.020 17.40 8.0.1 0.000 0.017 16.93 8.0.0 0.037 0.033 16.91 7.4.33 0.007 0.007 15.00 7.4.32 0.003 0.007 16.50 7.4.30 0.004 0.008 16.59 7.4.29 0.004 0.007 16.47 7.4.28 0.017 0.004 16.57 7.4.27 0.003 0.012 16.57 7.4.26 0.006 0.006 16.58 7.4.25 0.011 0.011 16.46 7.4.24 0.006 0.005 16.52 7.4.23 0.004 0.007 16.38 7.4.22 0.003 0.024 16.54 7.4.21 0.021 0.016 16.58 7.4.20 0.003 0.007 16.58 7.4.19 0.016 0.000 16.67 7.4.16 0.007 0.016 16.55 7.4.15 0.029 0.042 17.40 7.4.14 0.022 0.026 17.86 7.4.13 0.013 0.021 16.46 7.4.12 0.024 0.024 16.56 7.4.11 0.059 0.030 16.60 7.4.10 0.007 0.027 16.66 7.4.9 0.010 0.025 16.44 7.4.8 0.013 0.010 19.39 7.4.7 0.010 0.016 16.39 7.4.6 0.026 0.023 16.60 7.4.5 0.013 0.013 16.49 7.4.4 0.013 0.007 22.77 7.4.3 0.009 0.042 16.24 7.4.0 0.012 0.009 14.90 7.3.33 0.012 0.006 13.48 7.3.32 0.003 0.005 13.33 7.3.31 0.009 0.006 16.17 7.3.30 0.006 0.006 16.24 7.3.29 0.015 0.047 16.40 7.3.28 0.029 0.021 16.38 7.3.27 0.024 0.029 17.40 7.3.26 0.046 0.028 16.43 7.3.25 0.045 0.020 16.43 7.3.24 0.039 0.039 16.69 7.3.23 0.063 0.063 16.46 7.3.21 0.007 0.023 16.55 7.3.20 0.013 0.013 19.39 7.3.19 0.037 0.042 16.52 7.3.18 0.014 0.010 16.62 7.3.17 0.010 0.014 16.43 7.3.16 0.041 0.023 16.55 7.3.12 0.016 0.013 15.21 7.2.33 0.036 0.027 16.65 7.2.32 0.057 0.064 16.84 7.2.31 0.046 0.022 16.75 7.2.30 0.022 0.032 16.55 7.2.29 0.015 0.018 16.82 7.2.6 0.007 0.007 16.72 7.2.5 0.003 0.014 16.97 7.2.0 0.007 0.010 19.19 7.1.20 0.028 0.010 15.76 7.1.10 0.007 0.007 17.75 7.1.7 0.000 0.010 17.01 7.1.6 0.007 0.021 19.21 7.1.5 0.011 0.007 17.11 7.0.20 0.028 0.004 14.80
preferences:dark mode live preview
40.62 ms | 400 KiB | 5 Q