Execute Brain****/Tcl

From Rosetta Code
Revision as of 17:49, 16 May 2009 by rosettacode>Dkf (Here's a BF interpreter in Tcl)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Execute Brain****/Tcl is an implementation of Brainf***. Other implementations of Brainf***.
Execute Brain****/Tcl is part of RCBF. You may find other members of RCBF at Category:RCBF.

This Brainfuck interpreter is derived from code on The Tcler's Wiki, and is written to be short but not particularly clear. To use it, save it to a file (e.g., called “bf.tcl”) and run that against tclsh with either the name of the file containing the BF program or just input the program on stdin; the program will only begin execution after you do end-of-file (however that's done on your OS). For example:

 tclsh8.5 bf.tcl helloWorld.bf

Interpreter Implementation

Works with: Tcl version 8.5

<lang tcl>package require Tcl 8.5 fconfigure stdout -buffering none fconfigure stdin -buffering none if {![llength $argv]} {

   set p [split [read stdin] {}]

} else {

   set fd [open [lindex $argv 0]]
   set p [split [read $fd] {}]
   close $fd

} set d [lrepeat 30000 0] set dc 0 for {set pc 0} {$pc < [llength $p]} {incr pc} {

   switch [lindex $p $pc] {

">" { incr dc } "<" { incr dc -1 } "+" { lset d $dc [expr {[lindex $d $dc] + 1}] } "-" { lset d $dc [expr {[lindex $d $dc] - 1}] } "." { puts -nonewline [format "%c" [lindex $d $dc]] } "," { lset d $dc [scan [read stdin 1] "%c"] } "\[" { if {![lindex $d $dc]} { incr pc for {set n 0} {$n || [lindex $p $pc] ne "\]"} {incr pc} { switch -- [lindex $p $pc] \[ {incr n} \] {incr n -1} } } } \] { if {[lindex $d $dc]} { incr pc -1 for {set n 0} {$n || [lindex $p $pc] ne "\["} {incr pc -1} { switch -- [lindex $p $pc] \[ {incr n -1} \] {incr n} } } }

   }

}</lang>