#!/usr/local/bin/php
<?php

require "brainphuck/brainphuck.php";

class 
BrainPhuck_I extends BrainPhuck {

    private 
$stdin;

    public function 
__construct() {

        
$this->stdin fopen("php://stdin""r");

    }

    public function 
__destruct() {

        
fclose($this->stdin);

    }

    protected function 
BF_Input() {

        return 
fgetc($this->stdin);

    }

    protected function 
BF_Output($s) {

        
parent::BF_Output($s);
        echo 
$s;

    }

}

if (!
$argv[1]) {

    echo 
"usage: ".basename($argv[0])." [options] filename.b\n\n";
    echo 
"  -d    Dump debugging data\n";
    echo 
"  -f    Use the faster PHP translator instead of internal executor\n";
    echo 
"  -n    Load the file but don't execute it\n";
    echo 
"  -s    Use strict parsing mode (ignore # and ! BF extensions)\n";
    echo 
"  -V    Print version number and exit\n";
    
    die(
"\n");

}

$filename $argv[$argc 1];
$options getopt("dfnsV");

isset(
$options["V"]) && die(BrainPhuck::VERSION."\n");

$debug = isset($options["d"]);
$fast  = isset($options["f"]);
$noex  = isset($options["n"]);
$strc  = isset($options["s"]);

try {

    
$bf = new BrainPhuck_I();
    
$bf->Load_File($filename$strc BrainPhuck::PARSE_STRICT BrainPhuck::PARSE_EXT);
    
$bf->Optimize();
    if (!
$noex$bf->Execute($fast);
    if (
$debug$bf->Dump_Debug_Data();

} catch (
Exception $e) {

    die(
"error: ".$e->getMessage()."\n");

}

?>