Friday, June 11, 2010

BrainF**k & C++

Here is my bf to C translator, so I can write some bf sh*ts and test easily on my VC++08. To run the program with command line, _NO_ARGS must be cleared with 0.

#define _CRT_SECURE_NO_WARNINGS 1
#define _NO_ARGS 0
#include <stdio.h>

FILE *fin, *fout;

void print(char *str, int tab);
int unknown(char ch);

int main(int argc, char **argv) {
char ch, pr, temp[1024];
int tab = 0, cnt, carr;
if(_NO_ARGS) {
scanf("%s", temp);
fin = fopen(temp, "rb");
scanf("%s", temp);
fout = fopen(temp, "wb");
}
else if(argc < 2) {
printf("insufficient arguments...\n");
return 1;
}
else if(argc < 3) {
fin = fopen(argv[1], "rb");
fout = fopen("noname.c", "wb");
}
else {
fin = fopen(argv[1], "rb");
fout = fopen(argv[2], "wb");
}
if(!fin || !fout) {
printf("file error...\n");
return 2;
}
print("#include <stdio.h>", tab);
print("", tab);
print("char buff[1024];", tab);
print("", tab);
print("int main() {", tab++);
print("char *p = buff;", tab);
pr = 0;
cnt = 0;
carr = 0;
while((ch=fgetc(fin))!=EOF) {
if(unknown(ch)) continue;
if(ch=='.' || ch==',' || ch=='[' || ch==']') {
if(carr) {
if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);
else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);
else if(pr=='>') sprintf(temp, "p += %d;", cnt);
else if(pr=='<') sprintf(temp, "p -= %d;", cnt);
carr = cnt = 0;
print(temp, tab);
}
if(ch=='.') print("putchar(*p);", tab);
else if(ch==',') print("*p = getchar();", tab);
else if(ch=='[') print("while(*p) {", tab++);
else if(ch==']') print("}", --tab);
}
else if(ch==pr || !carr) {
carr = 1;
cnt++;
}
else {
if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);
else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);
else if(pr=='>') sprintf(temp, "p += %d;", cnt);
else if(pr=='<') sprintf(temp, "p -= %d;", cnt);
carr = cnt = 1;
print(temp, tab);
}
pr = ch;
}
if(carr) {
if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);
else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);
else if(pr=='>') sprintf(temp, "p += %d;", cnt);
else if(pr=='<') sprintf(temp, "p -= %d;", cnt);
print(temp, tab);
}
print("return 0;", tab);
print("}", --tab);
fclose(fin);
fclose(fout);
return 0;
}

void print(char *str, int tab) {
int i;
if(str[0]) {
for(i=0; i<tab; i++) fputc('\t', fout);
for(i=0; str[i]; i++) fputc(str[i], fout);
}
fputc('\n', fout);
}

int unknown(char ch) {
switch(ch) {
case '+':
case '-':
case '<':
case '>':
case '.':
case ',':
case '[':
case ']': return 0;
default : return 1;
}
return 1;
}

Here is a sample bf code I have written just now, and here is the translated C code.
But no matter the size, bf is really fun!!!

2 comments:

Catagories

academic study (17) access modifiers (1) algorithm (28) bash (1) beginner (17) bfs (1) bigint (1) binary tree (1) bitwise (4) blogger (5) bpm (2) brainfuck (1) bst (1) c (1) c++ (36) changes (1) character device driver (1) combinatorics (2) command prompt (1) comparator (1) compression (1) computational geometry (2) confusion (1) contest (7) crc (1) cse (3) css (1) customize (1) data structure (10) database (1) decoding (1) design (1) device driver (1) divide and conquer (2) dp (3) driver (1) dynamic programming (9) encoding (1) encryption (1) error (2) esoteric language (2) euler circuit (1) euler path (1) executable (1) expression evaluation (1) extended euclid (1) facebook (1) factorization (1) funny (14) gcd (2) geometry (4) git (1) github (1) graph (7) hashing (1) hiding window (1) hints (5) hopcroft karp (1) huffman (1) jar (1) java (5) javascript (1) jdbc (1) kernel programming (2) lab (1) like (1) linear algebra (3) linux (5) ls (1) makefile (1) math (16) matrix (2) matrix algebra (1) matrix exponentiation (1) matrix multiplication (1) maxflow (1) maximum bipartite matching (2) maximum flow (1) merge sort (3) mistake (1) modular arithmatic (1) module compiling (2) mysql (1) number system (1) number theory (8) online judge (3) operating system (1) os (1) other (8) parallel programming (1) pollard rho (1) primes (3) problem (3) problem classifier (2) problem solving (34) programming (51) pthreaded (1) puzzle (1) python (3) recursion (5) repository (1) shell (1) shell script (1) sieve (1) simulation (1) sort (3) spacing (1) sphere online judge (12) spoj (12) syntax highlighting (1) system programming (4) table tag (1) tc (1) template (4) thread (1) topcoder (1) training (3) tree (1) tutorial (2) ubuntu (1) usaco (1) uva (5) uva online judge (5) vector (1) windows (1)