Skip to content

Instantly share code, notes, and snippets.

@rtgnx
Created October 30, 2015 15:45
Show Gist options
  • Save rtgnx/891324b1d8417ab20312 to your computer and use it in GitHub Desktop.
Save rtgnx/891324b1d8417ab20312 to your computer and use it in GitHub Desktop.
<?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
@rtgnx
Copy link
Author

rtgnx commented Oct 30, 2015

bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment