Created
October 30, 2015 15:45
-
-
Save rtgnx/891324b1d8417ab20312 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BrainFuck | |
{ | |
private $tape; | |
private $ptr; | |
public function __construct() | |
{ | |
$this->tape = [] | |
$this->tape = array_fill(0, 255,0); | |
$this->ptr = 0; | |
} | |
public function movePointerRight() | |
{ | |
$this->ptr += 1; | |
} | |
public function movePointerLeft() | |
{ | |
$this->ptr -= 1; | |
} | |
public function incrementValue() | |
{ | |
$this->tape[$this->ptr] += 1; | |
} | |
public function decrementValue() | |
{ | |
$this->tape[$this->ptr] -= 1; | |
} | |
public function printValue() | |
{ | |
print $this->tape[$this->ptr]; | |
} | |
public function scanValue() | |
{ | |
$this->tape[$this->ptr] = (int) trim(fgets(STDIN)); | |
} | |
public function runCode($code) { | |
for($i=0;$i<strlen($code);$i++) { | |
$function = $this->parse($code{$i}); | |
$this->$function(); | |
} | |
} | |
public function parse($char) | |
{ | |
switch ($char) { | |
case '>': | |
return "movePointerRight"; | |
break; | |
case '<': | |
return "movePointerLeft"; | |
break; | |
case '+': | |
return "incrementValue"; | |
break; | |
case '-': | |
return "decrementValue"; | |
break; | |
case '.': | |
return "printValue"; | |
break; | |
case ',': | |
return "scanValue"; | |
break; | |
default: | |
# code... | |
break; | |
} | |
} | |
} | |
$bf = new BrainFuck(); | |
$bf->runCode(">>+++++.>>>+++."); | |
var_dump($bf);Enter file contents here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bf