Simplify main interpreter
No need to interpret "multiple files in sequence". Just execute the one file given.
This commit is contained in:
117
main.c
117
main.c
@@ -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,70 +87,65 @@ 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};
|
FILE *handle = fopen(filepath, "r");
|
||||||
|
#ifdef DEBUG
|
||||||
for (int i = 1; i < argc; ++i)
|
printf("[DEBUG]: Attempting to open file handle to %s\n", filepath);
|
||||||
|
#endif
|
||||||
|
if (!handle)
|
||||||
{
|
{
|
||||||
filepath = argv[i];
|
fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath);
|
||||||
|
ret = 1;
|
||||||
FILE *handle = fopen(filepath, "r");
|
goto end;
|
||||||
#ifdef DEBUG
|
|
||||||
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);
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("[DEBUG]: Read data from file %s\n", filepath);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
buffer = buffer_init_str(filepath, file_data, strlen(file_data));
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("[DEBUG]: Initialised buffer");
|
|
||||||
#endif
|
|
||||||
res = parse_buffer(buffer);
|
|
||||||
if (res.nodes == NULL)
|
|
||||||
{
|
|
||||||
fputs("Exiting early...\n", stderr);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
#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);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
free(res.nodes);
|
|
||||||
free(buffer);
|
|
||||||
free(file_data);
|
|
||||||
}
|
}
|
||||||
return 0;
|
file_data = fread_all(handle);
|
||||||
error:
|
fclose(handle);
|
||||||
if (buffer)
|
#ifdef DEBUG
|
||||||
free(buffer);
|
printf("[DEBUG]: Read data from file %s\n", filepath);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
buffer = buffer_init_str(filepath, file_data, strlen(file_data));
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("[DEBUG]: Initialised buffer");
|
||||||
|
#endif
|
||||||
|
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);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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("\n");
|
||||||
|
#endif
|
||||||
|
end:
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user