Advertisement
opsftw

BF to PHP Class

Aug 11th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. <?php
  2. class BrainFuck
  3. {
  4.     /* * *\
  5.      * BrainF*ck Class  *
  6.      * By: opsftw     *
  7.     \* * */
  8.  
  9.     public function bf2php($code) {
  10.         $this->bf = "\$cell = array();\n\$curCell = 0;\n\$marker=0;\n";
  11.         foreach(str_split($code) as $char) {
  12.             $this->php .= ($char=='$' ? "\$marker=\$curCell;\n"     : '');
  13.             $this->php .= ($char=='@' ? "\$curCell=\$marker;\n"     : '');
  14.             $this->php .= ($char==']' ? "}\n"     : '');
  15.             $this->php .= ($char=='[' ? "while(1==1){\n\$tmp=\$curCell;\nif(\$cell[\$tmp]==0){ break; }\n"  : '');
  16.             $this->php .= ($char=='-' ? "\$cell[\$curCell]--;\n"    : '');
  17.             $this->php .= ($char=='<' ? "\$curCell--;\n"    : '');
  18.             $this->php .= ($char=='+' ? "\$cell[\$curCell]++;\n"    : '');
  19.             $this->php .= ($char=='>' ? "\$curCell++;\n"    : '');
  20.             $this->php .= ($char=='.' ? "echo chr(\$cell[\$curCell]);\n"    : '');
  21.             $this->php .= ($char=='#' ? "\$cell[\$curCell]=0;\n"    : '');
  22.             $this->php .= ($char=='*' ? "unset(\$cell);\n\$cell = array();\n\$curCell=0;\n"     : '');
  23.             $this->bits++;
  24.         }
  25.         return $this;
  26.     }
  27. }
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement