Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Brainf*ck interpreter

by Juerd (Abbot)
on Dec 16, 2001 at 22:00 UTC ( #132368=snippet: print w/ replies, xml ) Need Help??

Description: This is an interpreter for the (in)famous, but turing-complete, Brainf*ck programming language.
Update 200112161916+0100: s/f.ck/f*ck/g

#!/usr/bin/perl -s # |bf; bf $file; bf -e=,
$|++;undef$/;$_=$e||<>;tr/-+,.[]<>//cd;for$
a(qw%]} [while($p[$p]){ -$p[$p]-- +$p[$p]++
,$p[$p]=ord(getc) .print(chr($p[$p])) <$p--
>$p++%){s/\Q${\substr$a,0,1}/${\substr$a,1}
;/g}eval; # by Juerd <juerd@juerd.nl>, 2001

2;0 juerd@ouranos:~$ ./bf -e='++++++++[>++++++++<-]+++++[>>+++++<<-]>>
++
[<+.>-]++++++++++.'
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Comment on Brainf*ck interpreter
Select or Download Code
Re: Brainf*ck interpreter
by mtve (Chaplain) on May 09, 2002 at 13:34 UTC

    Can't stop after last perlgolf.

    #!/usr/bin/perl -s # |bf; bf $file; bf -e=; $|++;$/=$a;$_=$e||<>;%a=qw%[ while($p[$p]){ , $p[$p]=ord(getc) . print+chr$p[$p] < $p-- > $p++ + $p[$p]++ - $p[$p]-- ] }%;s/./$a{$& };/g;eval

    "Please shoot me" (C) Jay Tilton

    Update on August 10, 2002: another 14 bytes

    #!/usr/bin/perl -s # |bf; bf $file; bf -e=; $_=($/=$e)||<>;$p=w;%a=qw%[ while($$p){ ] } . print+chr$ , $$p=ord(getc); + ++$ - --$ < chop > $p=w.%;$|=s/./$a{$&}\$p;/g;eval

    Update on May 19, 2004: Rick Klement's wonderful solution to PCLP#2.5 Polish golf

    $_.=qw(++ }while$ -- ++$ $$p=shift; --$ print$ ${)[$p%60%8].'$p+0;'while$p=ord getc;eval

      Brainfuck interpreters must discard unknown characters. Yours doesn't.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        I think it does - unknown chars are replaced with noop ';'

        Dear Juerd, can I put "(C) Juerd & Mtve" on it?

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://132368]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-07-11 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls