# Brainfuck => D compiler translator # This produces the faster executable from a BF program # Program inspired by BF2C (x) 2003 Thomas Cort from sys import argv def convert(prog, outfile): conv = {'+': "++*p;", '-': "--*p;", '.': "putchar(*p);", ',': "*p=getchar();", '>': "++p;", '<': "--p;", '[': "while(*p){", ']': "}"} # Generate first part of the D program print >> outfile, """\ import std.c.stdio: getchar, putchar; void main() { ubyte[30000] x; // ubyte or ushort auto p = x.ptr; """ # convert all BF commands for c in prog: if c in conv: print >> outfile, conv[c] # End D program print >> outfile, '}' prog = file(argv[1]).read() outfile = file(argv[1].partition(".")[0]+".d", "w") convert(prog, outfile)