Version 13 of Brainfuck

Updated 2004-10-14 15:38:13 by RHS

I don't think it qualifies as an actual language, but it is so interesting it does deserve a wiki page. Learn more about it here:

http://www.muppetlabs.com/~breadbox/bf/

and

http://cydathria.com/bf/brainfuck.html

not to mention

http://www.hevanet.com/cristofd/brainfuck/brainfuck.html

Also lo and behold the classic "99 Bottles of Beer" written in brainfuck:

http://99-bottles-of-beer.ls-la.net/b.html#Brainfuck

Here's a random number generator:

 >>>++[<++++++++[<[<++>-]>>[>>]+>>+[-[->>+<<<[<[<<]<+>]>[>[>>]]]<[>>[-]]>[>[-
 <<]>[<+<]]+<<]<[>+<-]>>-]<.[-]>>] http://www.hevanet.com/cristofd/brainfuck/

And a brainfuck interpreter written in Tcl (you can type brainfuck -- or a close approximation -- directly into a tcl interpreter):

http://www.fishpool.com/~setok/proj/tclbf/

PT writes: And here is a BrainFuck interpreter as a Internet Explorer plugin language - http://brainscript.sourceforge.net/

And BF is a real language - it's Turing complete after all.

Now Malbolge on the other hand....


RHS A brainfuck interpreter written in Tcl... can take input as a filename, or from stdin

 #!/bin/sh
 # This line continues for Tcl, but is a single line for 'sh' \
        exec tclsh8.5 "$0" ${1+"$@"}

 interp alias {} = {} expr

 proc getp {} {
        global pc program
        lindex $program $pc
 }       

 proc getd {} {
        global xc data
        lindex $data $xc
 }

 proc setd {c} {
        global xc data
        lset data $xc $c
 }

 proc main {commands} {
        global program data
        global pc xc

        set program $commands

        set plen [llength $program]

        for {set xc 0} { $xc < 32768 } { incr xc } {
                lappend data 0
        }

        set xc 0

        for {set pc 0} {$pc < $plen} {incr pc} {
                switch -exact -- [lindex $commands $pc] {
                        > { incr xc }
                        < { incr xc -1 }

                        + { setd [= [getd] +1] }
                        - { setd [= [getd] -1] }

                        . { puts -nonewline [format "%c" [getd]] }
                        , { 
                                if { ![eof stdin] } {
                                        scan [read stdin 1] "%c" var
                                        setd $var
                                } else {
                                        setd 0
                                }
                        }

                        \[ {
                                if { [getd] == 0 } {
                                        incr pc
                                        set nest 0
                                        while { $nest || ![string equal [getp] "\]"] } {
                                                switch -exact -- [getp] {
                                                        \[ { incr nest }
                                                        \] { incr nest -1 }
                                                }
                                                incr pc
                                        }
                                }
                        }
                        \] {
                                if { [getd] != 0 } {
                                        incr pc -1
                                        set nest 0
                                        while { $nest || ![string equal [getp] "\["] } {
                                                switch -exact -- [getp] {
                                                        \[ { incr nest -1 }
                                                        \] { incr nest }
                                                }
                                                incr pc -1
                                        }
                                }
                        }
                        \# {
                                puts -nonewline "\nDebug: "
                                for {set tmp 0} {$tmp < 10} {incr tmp} {
                                        puts -nonewline "[lindex $data $tmp]:"
                                }
                                puts ""
                        }
                }
        }
 }

 proc readfile {argv} {
        switch -exact -- [llength $argv] {
                0 {
                        set fd stdin
                }
                1 {
                        set fd [open [lindex $argv 0] r]
                }
                default {
                        puts -stdout "Usage: [file tail [info script]] ?filename?"
                        exit
                }
        }

        set text [read $fd]
        if { ![string equal $fd stdin] } {
                close $fd
        }

        return [split $text ""]
 }

 fconfigure stdout -buffering none
 fconfigure stdin -buffering none

 set commands [readfile $argv]
 main $commands

 puts ""

Category Language