simplifying stuff

This commit is contained in:
cozis
2022-03-12 23:38:23 +01:00
parent 2dcfbd194e
commit af089fb81f
+11 -44
View File
@@ -42,12 +42,12 @@ static void print_error(const char *type, Error *error)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
static _Bool interpret(Source *src) static Executable *build(Source *src)
{ {
// Compile the code. This section transforms // Compile the code. This section transforms
// a [Source] into an [Executable]. // a [Source] into an [Executable].
Executable *exe; Executable *exe;
{
// Create a bump-pointer allocator to hold the AST. // Create a bump-pointer allocator to hold the AST.
BPAlloc *alloc = BPAlloc_Init(-1); BPAlloc *alloc = BPAlloc_Init(-1);
@@ -86,10 +86,17 @@ static _Bool interpret(Source *src)
Error_Free(&error); Error_Free(&error);
return 0; return 0;
} }
return exe;
} }
// Now execute it. static _Bool interpret(Source *src)
{ {
Executable *exe = build(src);
if(exe == NULL)
return 0;
Runtime *runt = Runtime_New(-1, -1, NULL, NULL); Runtime *runt = Runtime_New(-1, -1, NULL, NULL);
if(runt == NULL) if(runt == NULL)
@@ -148,53 +155,13 @@ static _Bool interpret(Source *src)
return o != NULL; return o != NULL;
} }
}
static _Bool disassemble(Source *src) static _Bool disassemble(Source *src)
{ {
// Compile the code. This section transforms Executable *exe = build(src);
// a [Source] into an [Executable].
Executable *exe;
{
// Create a bump-pointer allocator to hold the AST.
BPAlloc *alloc = BPAlloc_Init(-1);
if(alloc == NULL)
{
fprintf(stderr, "Internal Error: Couldn't allocate bump-pointer allocator to hold the AST.\n");
return 0;
}
Error error;
Error_Init(&error);
// NOTE: The AST is stored in the BPAlloc. It's
// lifetime is the same as the pool.
AST *ast = parse(src, alloc, &error);
if(ast == NULL)
{
assert(error.occurred);
print_error("Parsing", &error);
Error_Free(&error);
BPAlloc_Free(alloc);
return 0;
}
exe = compile(ast, alloc, &error);
// We're done with the AST, independently from
// the compilation result.
BPAlloc_Free(alloc);
if(exe == NULL) if(exe == NULL)
{
assert(error.occurred);
print_error("Compilation", &error);
Error_Free(&error);
return 0; return 0;
}
}
Executable_Dump(exe); Executable_Dump(exe);
return 1; return 1;