Simplify main interpreter

No need to interpret "multiple files in sequence".  Just execute the
one file given.
This commit is contained in:
2024-12-03 03:19:29 +00:00
parent d230dbb679
commit 14ee1d3f02

35
main.c
View File

@@ -74,8 +74,8 @@ void interpret(machine_t *cpu, node_t *ast, size_t num)
void usage(const char *name, FILE *fp) void usage(const char *name, FILE *fp)
{ {
fprintf(fp, fprintf(fp,
"Usage: %s [FILE]...\n\tExecutes FILES sequentially on the " "Usage: %s [FILE]...\n\tInterprets FILE as a brainfuck program"
"same machine\n", "\n",
name); name);
} }
@@ -87,16 +87,12 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
char *filepath = NULL, *file_data = NULL; int ret = 0;
char *filepath = argv[1], *file_data = NULL;
buffer_t *buffer = NULL; buffer_t *buffer = NULL;
struct PResult res = {0}; 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 #ifdef DEBUG
printf("[DEBUG]: Attempting to open file handle to %s\n", filepath); printf("[DEBUG]: Attempting to open file handle to %s\n", filepath);
@@ -104,7 +100,8 @@ int main(int argc, char *argv[])
if (!handle) if (!handle)
{ {
fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath); fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath);
goto error; ret = 1;
goto end;
} }
file_data = fread_all(handle); file_data = fread_all(handle);
fclose(handle); fclose(handle);
@@ -120,7 +117,8 @@ int main(int argc, char *argv[])
if (res.nodes == NULL) if (res.nodes == NULL)
{ {
fputs("Exiting early...\n", stderr); fputs("Exiting early...\n", stderr);
goto error; ret = 1;
goto end;
} }
#ifdef DEBUG #ifdef DEBUG
@@ -140,17 +138,14 @@ int main(int argc, char *argv[])
printf("\n\t"); printf("\n\t");
printf("%d ", machine.memory[i]); printf("%d ", machine.memory[i]);
} }
printf("\n");
#endif #endif
end:
free(res.nodes);
free(buffer);
free(file_data);
}
return 0;
error:
if (buffer)
free(buffer);
if (res.nodes) if (res.nodes)
free(res.nodes); free(res.nodes);
return 1; if (buffer)
free(buffer);
if (file_data)
free(file_data);
return ret;
} }