diff --git a/src/main.c b/src/main.c index 4f88bd7..468ad3b 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) { - clock_t start_time = clock(); + clock_t start_time, end_time; int status = EXIT_FAILURE; @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) goto end; } + start_time = clock(); + // Strip program file while(true) { @@ -73,8 +75,6 @@ int main(int argc, char *argv[]) } } - rewind(stripped_file); - if(fclose(program_file) == EOF) { program_file = NULL; @@ -86,6 +86,8 @@ int main(int argc, char *argv[]) program_file = NULL; + rewind(stripped_file); + size_t brackets_pos[BRACKETS_NB_MAX]; size_t brackets_correspondances[BRACKETS_NB_MAX]; @@ -128,7 +130,7 @@ int main(int argc, char *argv[]) if(err) { - fprintf(stderr, "Error reading file: %d\n", argv[1], ferror(stripped_file)); + fprintf(stderr, "Error reading file: %d\n", ferror(stripped_file)); } else { @@ -163,6 +165,10 @@ int main(int argc, char *argv[]) } } + end_time = clock(); + + fprintf(stderr, "Parsing time : %fs\n", (float)(end_time - start_time) / (float)CLOCKS_PER_SEC); + rewind(stripped_file); // Program memory @@ -171,6 +177,8 @@ int main(int argc, char *argv[]) size_t bracket_index = 0; + start_time = clock(); + while(true) { int c = fgetc(stripped_file); @@ -195,6 +203,8 @@ int main(int argc, char *argv[]) bracket_index = brackets_correspondances[bracket_index]; fseek(stripped_file, brackets_pos[bracket_index], SEEK_SET); } + + bracket_index++; } else if(c == '>') { @@ -215,49 +225,17 @@ int main(int argc, char *argv[]) else if(c == '.') { putchar(mem[mem_pointer]); - fflush(stdout); } else if(c == ',') { -/* - static int next_input = -1; - int input; - - if(next_input != -1) - { - input = next_input; - next_input = -1; - } - else - { - input = getchar(); - } - - next_input = getchar(); - - if(input != '\n') - { - mem[mem_pointer] = input; - } - else - { - if(next_input == '\n') - { - mem[mem_pointer] = '\n'; - } - - next_input = -1; - } -*/ mem[mem_pointer] = getchar(); } - - if(c == '[' || c == ']') - { - bracket_index++; - } } + end_time = clock(); + + fprintf(stderr, "\nExecution time : %fs\n", (float)(end_time - start_time) / (float)CLOCKS_PER_SEC); + status = EXIT_SUCCESS; end: @@ -265,7 +243,7 @@ end: { if(fclose(stripped_file)) { - fprintf(stderr, "Failed to close tmp file: %d\n", errno); + fprintf(stderr, "Error closing tmp file: %d\n", errno); } } @@ -273,13 +251,9 @@ end: { if(fclose(program_file)) { - fprintf(stderr, "Failed to close tmp file: %d\n", errno); + fprintf(stderr, "Error closing tmp file: %d\n", errno); } } - clock_t end_time = clock(); - - fprintf(stderr, "\nExecution time : %fs\n", (float)(end_time - start_time) / (float)CLOCKS_PER_SEC); - return status; }