dev-master
9999999-devBrainfuck interpreter and virtual machine
MIT
The Development Requires
by Alexander Kluth
Wallogit.com
2017 © Pedro Peláez
Brainfuck interpreter and virtual machine
It's a Brainfuck interpreter and (somewhat) virtual machine which makes it possible to both evaluate brainfuck code as "pure" brainfuck code and to evaluate brainfuck code as PHP code., (*1)
Just add it to your composer.json:, (*2)
"require": {
...
"AlexClooze/phprainfuck": "dev-master"
}
and run composer update., (*3)
There are two ways of using phprainfuck., (*4)
It all starts with creating a new instance of phprainfuck:, (*5)
use AlexClooze\Phprainfuck\Phprainfuck; $phprain = new Phprainfuck();
After this you've got two options., (*6)
Here's an example on using the evaluate method to just interprete pure brainfuck
code:, (*7)
$code = <<<EOT +++++ +++[- >++++ ++++< ]>+++ +++++ .<+++ ++[-> +++++ <]>++ ++.++ +++++ ..+++ .<+++ +++++ [->-- ----- -<]>- ----- ----- ----. <++++ +++[- >++++ +++<] >++++ ++.<+ +++[- >++++ <]>++ +++++ +.+++ .---- --.-- ----- -.<++ +++++ +[->- ----- --<]> ---.< EOT; print $phprain->evaluate($code);
This will print "Hello World!"., (*8)
Here's an example on creating a new virtual machine and executing brainfuck code as PHP code in it., (*9)
Hint: All code executed via the virtual machine is executed in a seperate new PHP process., (*10)
$code = <<<EOT +++++ +++++ [->++ +++++ +++<] >++++ +++++ +++++ ++++. <++++ [->-- --<]> ----- .<+++ +[->+ +++<] >+.<+ +++[- >---- <]>-- -.+++ ++.<+ +++[- >++++ <]>+. ----- ---.+ ++.<+ +++++ ++[-> ----- ---<] >---- ----. ----- -.<++ ++++[ ->+++ +++<] >++.< +++++ [->++ +++<] >++++ .++++ +++.. +++.< +++++ +++[- >---- ----< ]>--- ----- ----- --.<+ +++++ +[->+ +++++ +<]>+ +++++ .<+++ +[->+ +++<] >++++ ++++. +++.- ----- .---- ----. <++++ ++++[ ->--- ----- <]>-- -.+.+ +++++ +.<++ ++[-> ++++< ]>++. < EOT; $vm = $phprain->createVirtualMachine(255); $vm->run($code);
The argument given by createVirtualMachine is the actual heap size.
The above code will evaluate to var_dump("Hello World!"); and will print
string(12) "Hello World!"., (*11)
phprainfuck is developed with the help of phpspec. Run phpspec run to run
the tests., (*12)
Brainfuck interpreter and virtual machine
MIT