PHP Classes
elePHPant
Icontem

File: bf.class.php

Recommend this page to a friend!
  Classes of Name Removed  >  Brainf_uck Interpreter Class  >  bf.class.php  >  Download  
File: bf.class.php
Role: Class source
Content type: text/plain
Description: Class Source
Class: Brainf_uck Interpreter Class
Interpret and execute Brainf_uck language code
Author: By
Last change: Fixed a nested loops bug.
Date: 2 years ago
Size: 1,926 bytes
 

 

Contents

Class file image Download
<?php

/****
****
*** @ Brainf_ck interpreter class
*** @ Developed by Takis Maletsas
****
****/

class Brainfuck
{
    private
$source = [], $charset = [">", "<", "+", "-", ".", ",", "[", "]"], $cells, $pointer = 0;

    public function
__construct($source)
    {
        foreach (
str_split($source) as $ch)
            if (
in_array($ch, $this->charset))
               
$this->source[] = $ch;

       
$this->cells = array_fill(0, 30000, 0);

       
$this->parse($this->source);
    }

    private function
parse($source)
    {
        for (
$i = 0; $i < count($source); $i++)
            switch (
$source[$i])
            {
                case
'>': $this->incPtr(); break;
                case
'<': $this->decPtr(); break;
                case
'+': $this->incVal(); break;
                case
'-': $this->decVal(); break;
                case
',': $this->inpVal(); break;
                case
'.': $this->outVal(); break;
                case
'[':
                   
$pointer = $this->pointer;
                   
                   
$skip = 0;
                   
$char = @$source[++$i];

                    while (
$char)
                    {
                        if (
$char == '[')
                           
$skip++;
                       
                        if (
$char == ']')
                        {
                            if (!
$skip)
                                break;
                            else
                               
$skip--;
                        }
                   
                       
$loop[] = $char;
                       
$char = @$source[++$i];
                    }
                   
                    while (
$this->cells[$pointer])
                       
$this->parse($loop);

                    break;
            }
    }

    private function
incVal()
    {
       
$this->cells[$this->pointer]++;
       
$this->cells[$this->pointer] %= 255;
    }

    private function
decVal()
    {
        if (--
$this->cells[$this->pointer] < 0)
           
$this->cells[$this->pointer] = 255;
    }

    private function
incPtr()
    {
        if (++
$this->pointer > 29999)
            exit(
"Brainfuck: Cell overflow.");
    }

    private function
decPtr()
    {
        if (--
$this->pointer < 0)
            exit(
"Brainfuck: Cell underflow.");
    }

    private function
inpVal()
    {
        echo
"Input: ";
       
$this->cells[$this->pointer] = ord(fread(STDIN, 1));
    }

    private function
outVal()
    {
        echo
chr($this->cells[$this->pointer]);
    }
}

?>