2017 © Pedro Peláez
 

library phprainfuck

Brainfuck interpreter and virtual machine

image

deralex/phprainfuck

Brainfuck interpreter and virtual machine

  • Tuesday, March 4, 2014
  • by DerAlex
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

phprainfuck - Brainfuck interpreter and virtual machine in PHP

What the heck is this?

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)

Installation

Just add it to your composer.json:, (*2)

"require": {
    ...
    "AlexClooze/phprainfuck": "dev-master"
}

and run composer update., (*3)

Usage

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)

Interpreting brainfuck code "as is"

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)

Evaluating brainfuck code as PHP code

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)

Running tests

phprainfuck is developed with the help of phpspec. Run phpspec run to run the tests., (*12)

The Versions

04/03 2014

dev-master

9999999-dev

Brainfuck interpreter and virtual machine

  Sources   Download

MIT

The Development Requires

by Alexander Kluth