YaK:: Brainfuck Compiler in Tcl [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

Brainfuck Compiler in Tcl

This translates BrainFuck to C, using the classical mapping.

Try it on this program : http://www.muppetlabs.com/~breadbox/bf/factor.b.txt

e.g. save the following code as "bfc.tcl"; then do tclsh bfc.tcl <factor.b.txt >factor.c ; gcc factor.c ; echo 111 | ./a.out


puts "
#include <stdio.h>

 unsigned char mem\[99999];
 main()
  \{
    unsigned char* p= mem+50000;
    \{
"

while {[gets stdin b]>=0} {
        set b [string trim $b]

        regsub -all {[^]\055+<>,.[]} $b { } b

        puts [string map [
list < {--p;} > {++p;} + {++*p;} - {--*p;} . {putchar(*p);} , {*p=getchar();} \[ "while(*p)\{" \] "\}"
        ] $b]

}


puts " \}\} "

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

Warning: Unchecked buffer. Only use with brainfuck programs that don't move the pointer more than the maximum allowed 30000 positions.

  • factor.b.txt
  • (unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
    (last modified 2004-11-05)       [Login]
    (No back references.)