Talk:WTFZOMFG
Jump to navigation
Jump to search
WTFZOMFG to C compiler
#include <stdio.h> /* ************************* * Compiler For WTF to C * * Made By Jay Songdahl * ************************* */ int main (int argc, const char * argv[]) { char INSTR; char NAME[200]; printf("#include <stdio.h>\nint main() { int tape[30000]; int ptr = 0; "); //set up the C code while(INSTR != -1) { INSTR=getchar(); //below are the translations for each WTFZOMFG keyword to C. if(INSTR==':') { scanf("%s", &NAME); printf("goto %s; ", NAME);} else if(INSTR==';') { scanf("%s", &NAME); printf("\n%s:\n", NAME);} else if(INSTR=='+') { printf("++tape[ptr]; ");} else if(INSTR=='-') { printf("--tape[ptr]; ");} else if(INSTR=='^') { printf("tape[ptr]=getchar(); ");} else if(INSTR=='v') { printf("putchar(tape[ptr]); ");} else if(INSTR=='?') { scanf("%s", &NAME); printf("if(tape[ptr]) goto %s; ", NAME);} else if(INSTR=='!') { scanf("%s", &NAME); printf("if(! tape[ptr]) goto %s; ", NAME);} else if(INSTR=='>') { printf("++ptr; ");} else if(INSTR=='<') { printf("--ptr; ");} else if(INSTR=='=') { scanf("%s", &NAME); printf("tape[ptr] = %s; ", NAME);} else if(INSTR=='/') { printf("scanf(\"%%i\", &tape[ptr]); ");} else if(INSTR=='\\') { printf("printf(\"\%%i\", tape[ptr]); ");} else if(INSTR=='.') { NAME[0]=getchar(); printf("putchar(%i); ", NAME[0]);} else if(INSTR=='~') { scanf("%s", &NAME); printf("tape[ptr] += %s; ", NAME);} else if(INSTR=='_') { scanf("%s", &NAME); printf("ptr = %s; ", NAME);} else if(INSTR=='(') { printf("while(tape[ptr]){ ");} else if(INSTR==')') { printf("} ");} else if(INSTR=='{') { printf("if(tape[ptr]){ ");} else if(INSTR=='}') { printf("}");} else if(INSTR=='|') { printf("tape[ptr] = ! tape[ptr]; ");} else if(INSTR=='[') { while(getchar() != ']'); } else if(INSTR=='#') while(getchar() != '\n'); else if(INSTR=='a') printf("tape[ptr]+=tape[ptr+1]; "); else if(INSTR=='s') printf("tape[ptr]-=tape[ptr+1]; "); else if(INSTR=='m') printf("tape[ptr]*=tape[ptr+1]; "); else if(INSTR=='d') printf("tape[ptr]/=tape[ptr+1]; "); else if(INSTR=='@') {NAME[0]=getchar(); printf("tape[ptr]-=%i; ", NAME[0]); } else if(INSTR=='&') { printf("tape[ptr+1]=tape[ptr]; ");} else if(INSTR=='%') { scanf("%s", &NAME); printf("tape[%s]=tape[ptr]; ", NAME);} else if(INSTR=='\''){ printf("printf(\""); NAME[0]=0; while(NAME[0] != '"') {NAME[0]=getchar(); if(NAME[0]!='"') putchar(NAME[0]);} printf("\"); ");} else if(INSTR!=' ' && INSTR!= '\n' && INSTR!='\t' && INSTR!=EOF) fprintf( stderr, "Error: Unknown Keyword: %c\n", INSTR); //if the command isn't whitespace, flag an error } printf(" return 0; }"); return 0; }
This code had a bug (it wasn't initializing ptr to 0, causing compiled programs to crash). I fixed it. --Marinus 15:57, 13 September 2010 (UTC)