Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char *argv[]){
- // The file handling section
- // Make sure there is an argument to the interpter
- if(argc < 2){
- printf("\nCorrect usage is: bfc <source.txt>\n");
- exit(8); // Exit program
- }
- // Read in the input from a text based file
- FILE *file;
- file = fopen(argv[1], "r"); // Read the file to file...
- int p, size;
- p = size = 0; // Set stuff equal to zero
- char c; // Current char beind read
- // Read stuff into the char array.
- if(file == NULL){
- printf("\nFile could not be opened!\n");
- exit(8); // Exit the program
- }
- // make char input array size of required input
- fseek(file, 0, SEEK_END);
- size = ftell(file);
- fseek(file, 0, SEEK_SET);
- char input[size]; // Set up the input for proper size
- while((c=fgetc(file)) != EOF)
- input[p++] = (char) c; // Read in the file
- fclose(file); // Close the file, we have the input array
- // The writing section
- int i; // Iterator
- FILE *cfile; // output file
- cfile = fopen("temp.c", "a+");
- fprintf(cfile, "#include <stdio.h>\n\n");
- fprintf(cfile, "char tape[30000];\nchar *ptr = tape;\n");
- fprintf(cfile, "\nint main(){\n");
- for(i = 0; input[i] != 0; i++){ // Go through 1 command at a time
- c = input[i]; // get ")rrent char
- if(c == '+')
- fprintf(cfile, "\t++*ptr;\n");
- else if(c == '-')
- fprintf(cfile, "\t--*ptr;\n");
- else if(c == '>')
- fprintf(cfile, "\t++ptr;\n");
- else if(c == '<')
- fprintf(cfile, "\t--ptr;\n");
- else if(c == ',') // get input
- fprintf(cfile, "\t*ptr = getchar();\n");
- else if(c == '.') // print current char
- fprintf(cfile, "\tputchar(*ptr);\n");
- else if(c == '[')
- fprintf(cfile, "\twhile(*ptr){\n");
- else if(c == ']'){
- fprintf(cfile, "\t}\n");
- }
- }
- fprintf(cfile, "\treturn 0;\n}\n");
- fclose(cfile); // close out the file
- system("gcc -Wall -o out temp.c");
- system("del temp.c");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement