Hello monks. I tried to write a rough brainf*ck-2-c translator.
#!/usr/bin/perl
use strict;
open (DATA,$ARGV[0]) or die "unable to open";
print "#include <stdio.h>\n#include <stdlib.h>\n\n";
print "int main(void) {\n\tunsigned char *k = calloc(2000, 1);\n";
while (<DATA>) {
s/>/\t\t++k;\n/g;
s/</\t\t--k;\n/g;
s/\+/\t\t++*k;\n/g;
s/\-/\t\t--*k;\n/g;
s/\./\t\tputchar(*k);\n/g;
s/\,/\t\t*k = getchar();\n/g;
s/\[/\twhile (*k) {\n/g;
s/\]/\t}\n/g;
print "$_";
}
close DATA;
print "\treturn 0;\n}\n";
I can't understand why output of s/>/\t\t++k;\n/g; and s/</\t\t--k;\n/g; is so weird, ie. missing plus and minus signs, missing tabs.
Sample bf-code:
+[----->+++<]>+.---.+++++++..+++.[--->+<]>-----.--[->++++<]>-.--------.+++.------.--------.
$ perl -v
This is perl 5, version 20, subversion 2 (v5.20.2)