enjoying salad since 1978.

Thursday, December 02, 2004

BF to C in awk

I wrote a Brainfuck to C translator in awk. It only takes a few minutes and I noticed that no awk version of this existed.

I haven't run it through it's paces (I just wrote a few small brainfuck programs to test it out) so if you find a bug, please let me know.
#!/sw/bin/awk -f                                                                                                                                                                           
# a brainfuck to C translator.                                                                                                                                                             
# Needs a recent version of gawk, if on OS X,                                                                                                                                              
# try using Fink's version.                                                                                                                                                                
#                                                                                                                                                                                          
# steve jenson                                                                                                                                                                             

BEGIN {
  print "#include <stdio.h>\n";
  print "int main() {";
  print "  int c = 0;";
  print "  static int b[30000];\n";
}

{
  #Note: the order in which these are                                                                                                                                                      
  #substituted is very important.                                                                                                                                                          
  gsub(/\]/, "  }\n");
  gsub(/\[/, "  while(b[c] != 0) {\n");
  gsub(/\+/, "  ++b[c];\n");
  gsub(/\-/, "  --b[c];\n");
  gsub(/>/, "  ++c;\n");
  gsub(/</, "  --c;\n");
  gsub(/\./, "  putchar(b[c]);\n");
  gsub(/\,/, "  b[c] = getchar();\n");
  print $0
}

END {
  print "\n  return 0;";
  print "}";
}
update: You can blame this on Evan Martin, his recent post wherein half the universe decided to weigh in about PHP had a mention of mod_bf. Am I easily distracted or what?

update: Last update, I swear. Darius said that I don't need to initialize b if I declare it static. Also, I realized that my previous version wouldn't understand if you had multiple operators on the same line since it was matching records and not fields. This version works on all of the programs I tried in the Brainfuck archive (as long as you strip comments).

4 Comments:

Anonymous Anonymous said...

Shouldn't the Brainfuck to C converter be written in Brainfuck?

9:57 PM

 
Blogger Steve Jenson said...

I was trying to write something that I couldn't already find and I had already found a brainfuck-to-C translator written in brainfuck.

10:11 PM

 
Anonymous Anonymous said...

Well then, how about an x86 brainfuck compiler written in brainfuck?

11:51 PM

 
Blogger Steve Jenson said...

Like this?

12:43 AM

 

Post a Comment

Links to this post:

Create a Link

<< Home