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)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.