BrainPhuck

BrainPhuck is a PHP5 class for handling the brainfuck programming language.

Its features include an optimizing parser, executor, debugger, PHP translator, and x86 compiler for both windows and linux.

It is distributed under the terms of the GNU Lesser General Public License.

Source code

The easiest way to install the class is PEAR 1.4+ :

~# pear channel-discover pear.si.kz
~# pear install pear.si.kz/brainphuck


Other files of interest :
brainphuck_1.0.4.tgz    BrainPhuck complete tarball distribution
 
brainphuck.php BrainPhuck class
bfi.php BrainPhuck interpreter (can use executor or translator)
bfc.php BrainPhuck compiler (outputs 16 bit windows/dos .com and 32 bit linux a.out binaries)
 
README.b BrainPhuck README ;)

Usage

<?php

require "brainphuck.php";
$bf = new BrainPhuck();
$bf->Load_File("bockbeer.b");
echo $bf->Execute();

?>

you can also use $bf = new BrainPhuck("> my bf program ++++++++.,.,.");

Load_File() and Load_String() methods load and convert BF code to the intermediate BPH code.

Execute() has two ways of executing your BF program :

- the internal BPH executor, which is quite fast and nice for fine debugging
- the PHP converter, which converts BPH to PHP code and can be a LOT faster

the default executor is the internal executor, you have to call Execute(true) in order to use the PHP converter.

you can also compile the loaded BPH code to x86 binary code with the Compile() method just like this :

file_put_contents("mybf.com", $bf->Compile(BrainPhuck::COMPILE_COM)); // compile for windows
file_put_contents("mybf", $bf->Compile(BrainPhuck::COMPILE_AOUT)); // compile for linux

the resulting file will execute many times faster than a call to Execute().

the Dump_Debug_Data() method can be used to dump the BPH code along with some parser / executor numbers (sample here)

use the Set_Input() method to set the BF input to some string

you may also extend the BrainPhuck class and define your own BF_Input() and/or BF_Output($str) methods to deal with interactive stuff. (see the bfi.php source for a good example)


This site and all its contents are (c)2005 by sIX of the aEGiS corp.