diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-12-03 03:19:29 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-12-03 03:19:29 +0000 |
commit | 14ee1d3f02f8b1c569e06102548aa5b41cae23cd (patch) | |
tree | 729b2b5600fce83e63f6e8af4b58f14040861dd0 | |
parent | d230dbb67937734d756ddaef5bf8cae46f038f05 (diff) | |
download | obf-14ee1d3f02f8b1c569e06102548aa5b41cae23cd.tar.gz obf-14ee1d3f02f8b1c569e06102548aa5b41cae23cd.tar.bz2 obf-14ee1d3f02f8b1c569e06102548aa5b41cae23cd.zip |
Simplify main interpreter
No need to interpret "multiple files in sequence". Just execute the
one file given.
-rw-r--r-- | main.c | 91 |
1 files changed, 43 insertions, 48 deletions
@@ -74,8 +74,8 @@ void interpret(machine_t *cpu, node_t *ast, size_t num) void usage(const char *name, FILE *fp) { fprintf(fp, - "Usage: %s [FILE]...\n\tExecutes FILES sequentially on the " - "same machine\n", + "Usage: %s [FILE]...\n\tInterprets FILE as a brainfuck program" + "\n", name); } @@ -87,70 +87,65 @@ int main(int argc, char *argv[]) return 1; } - char *filepath = NULL, *file_data = NULL; + int ret = 0; + char *filepath = argv[1], *file_data = NULL; buffer_t *buffer = NULL; struct PResult res = {0}; + machine_t machine = {0}; - machine_t machine = {0}; - - for (int i = 1; i < argc; ++i) - { - filepath = argv[i]; - - FILE *handle = fopen(filepath, "r"); + FILE *handle = fopen(filepath, "r"); #ifdef DEBUG - printf("[DEBUG]: Attempting to open file handle to %s\n", filepath); + printf("[DEBUG]: Attempting to open file handle to %s\n", filepath); #endif - if (!handle) - { - fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath); - goto error; - } - file_data = fread_all(handle); - fclose(handle); + if (!handle) + { + fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath); + ret = 1; + goto end; + } + file_data = fread_all(handle); + fclose(handle); #ifdef DEBUG - printf("[DEBUG]: Read data from file %s\n", filepath); + printf("[DEBUG]: Read data from file %s\n", filepath); #endif - buffer = buffer_init_str(filepath, file_data, strlen(file_data)); + buffer = buffer_init_str(filepath, file_data, strlen(file_data)); #ifdef DEBUG - puts("[DEBUG]: Initialised buffer"); + puts("[DEBUG]: Initialised buffer"); #endif - res = parse_buffer(buffer); - if (res.nodes == NULL) - { - fputs("Exiting early...\n", stderr); - goto error; - } + res = parse_buffer(buffer); + if (res.nodes == NULL) + { + fputs("Exiting early...\n", stderr); + ret = 1; + goto end; + } #ifdef DEBUG - char *str = ast_to_str(res.nodes, res.size); - printf("[DEBUG]: Parsed buffer (%lu nodes parsed)\n\t[DEBUG]: Out=%s\n", - res.size, str); - free(str); + char *str = ast_to_str(res.nodes, res.size); + printf("[DEBUG]: Parsed buffer (%lu nodes parsed)\n\t[DEBUG]: Out=%s\n", + res.size, str); + free(str); #endif - interpret(&machine, res.nodes, res.size); + interpret(&machine, res.nodes, res.size); #ifdef DEBUG - printf("[DEBUG]: Finished interpreting, memory:"); - for (size_t i = 0; i < machine.dp_max; ++i) - { - if (i % 8 == 0) - printf("\n\t"); - printf("%d ", machine.memory[i]); - } + printf("[DEBUG]: Finished interpreting, memory:"); + for (size_t i = 0; i < machine.dp_max; ++i) + { + if (i % 8 == 0) + printf("\n\t"); + printf("%d ", machine.memory[i]); + } + printf("\n"); #endif - +end: + if (res.nodes) free(res.nodes); - free(buffer); - free(file_data); - } - return 0; -error: if (buffer) free(buffer); - if (res.nodes) - free(res.nodes); - return 1; + if (file_data) + free(file_data); + return ret; } |