+ Reply to Thread
Results 1 to 7 of 7

Thread: [PHP] BrainF*ck interpreter

  1. #1
    luruke's Avatar
    luruke is offline Newbie
    Join Date
    Feb 2009
    Location
    Montesarchio (BN), Italy
    Posts
    26
    Rep Power
    11

    [PHP] BrainF*ck interpreter

    it's an interpreter (by me) of brainf*uck code.
    (brainf*uck is an esoteric programming languages)

    Code:
    <?php

    function brainfuck($code){
            
            
    $out '';
            
                    for(
    $a=array(),$p=0,$l=array(),$i=0$i<strlen($code); $i++){
                
                            if(!
    $code[$i]) continue;
                    
                    
                            switch(
    $code[$i]){
                    
                                case         
    '>':    $p++;
                                                    break;
                        
                                case         
    '<':     $p--;
                                                    break;
                                            
                                case        
    '+':    $a[$p]++;
                                                    break;
                                
                                case        
    '-':    $a[$p]--;
                                                    break;
                                
                                case        
    '[':    $l[] = $i;
                                                    break;
                                
                                case        
    ']':    if($a[$p]>0)
                                                            
    $i=$l[sizeof($l)-1];
                                                    else
                                                            
    array_pop($l);
                                                    break;
                                
                                case        
    ',':    $c trim(fgets(STDIN));
                                                    
    $a[$p] = ord($c[0]);
                                                    break;
                                
                                case        
    '.':    $out.=chr($a[$p]);
                                                    break;
                            }
                    }

        return 
    $out;                            
    }


    print 
    brainfuck(">+++++++[<+++++++++>-]<+++.>+++++[<++++++++++>-]<--.>+++++[<--->-]<--.
                     ++++++++.+++++.>++++[<---------->-]<.>+++++[<++++++++++>-]<---.>+++[<------>-]<.
                     ++++++++.>+++++[<--------------->-]<.>++++[<++++++++++>-]<+.
                     >++++++++[<+++++>-]<---.++++++.>+++++[<--->-]<.>++++++[<++>-]<+.
                     --.++.>++++++[<-->-]<-.>++++++[<++>-]<+++.>++++++[<-->-]<---.
                     >++++++[<++>-]<+.>++++++++[<---------->-]<--.>+++++++[<++++++++++>-]<+++.
                     +++++.>++++++++[<---------->-]<++.>+++++[<+++++++++>-]<+++.
                     >+++[<--->-]<+.>+++[<+++>-]<-.[-]++++++++++."
    );
    ?>
    bash-3.2$ time php brainfuck.php
    BrainFuck Interpreter in PHP

    real 0m0.032s
    user 0m0.021s
    sys 0m0.009s

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: [PHP] BrainF*ck interpreter

    Moved to the code section. First interpreter I've seen in PHP. Thanks for the share!

  4. #3
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: [PHP] BrainF*ck interpreter

    What is this ment to do?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  5. #4
    luruke's Avatar
    luruke is offline Newbie
    Join Date
    Feb 2009
    Location
    Montesarchio (BN), Italy
    Posts
    26
    Rep Power
    11

    Re: [PHP] BrainF*ck interpreter

    Quote Originally Posted by Brandon W View Post
    What is this ment to do?
    is a brainfuck interpreter, run the brainfuck codes!
    check brainfuck on wikipedia or google

  6. #5
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: [PHP] BrainF*ck interpreter

    OK I will later, I see the results but what does it actually do?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  7. #6
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: [PHP] BrainF*ck interpreter

    What do you mean...it runs brainfuck code....its essentially a language with only pointer functions and stdin/stdout...

  8. #7
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: [PHP] BrainF*ck interpreter

    Oh. If someone said that brainfuck was a language, I would of understood
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. brainf*ck interpreter in php
    By __ak in forum PHP Development
    Replies: 1
    Last Post: 03-17-2011, 12:02 AM
  2. Whitespace Interpreter
    By MeTh0Dz in forum Classes and Code Snippets
    Replies: 2
    Last Post: 02-02-2010, 08:42 AM
  3. Interpreter and translator
    By Apprentice123 in forum General Programming
    Replies: 4
    Last Post: 05-05-2009, 01:47 PM
  4. Interpreter for JScript
    By whoda in forum JavaScript and CSS
    Replies: 4
    Last Post: 05-01-2009, 11:11 AM
  5. T Interpreter 1.1
    By Kernel in forum Software Development Tools
    Replies: 0
    Last Post: 10-03-2006, 09:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts