Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- unsigned char tape[30000] = {0}; // Set the tape equal to zero, bf only calls for 30000 bytes available in the tape
- unsigned char* ptr = tape; // The pointer to the tape, this is what we work with!
- 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: bfi <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 interpter section
- int i, loop; // Iterator and loop counter
- for(i = 0; input[i] != 0; i++){ // Go through 1 command at a time
- c = input[i]; // get current char
- // Interpet the commands according to wikipedia's article
- if(c == '+')
- ++*ptr;
- else if(c == '-')
- --*ptr;
- else if(c == '>')
- ++ptr;
- else if(c == '<')
- --ptr;
- else if(c == ',') // get input
- *ptr = getchar();
- else if(c == '.') // print current char
- putchar(*ptr);
- else if(c == '[') // skip over this, go to ] section
- continue;
- else if(c == ']' && *ptr){ // make sure there is something in *ptr
- loop = 1; // We are in a loop!
- while(0 < loop){ // While we are still in a loop
- c = input [--i]; // Get c
- if(c == '[')
- loop--; // No more loop!
- else if(c == ']')
- loop++; // Another loop!
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement