Fixes and added include statement

This commit is contained in:
2025-08-08 11:41:25 +02:00
parent 3c7aafba10
commit acf2ea7b97
16 changed files with 738 additions and 235 deletions
+54 -18
View File
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "WL.h"
@@ -12,29 +14,64 @@ int main(int argc, char **argv)
}
char *file = argv[1];
FILE *f = fopen(file, "rb");
if (f == NULL)
return -1;
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
char *file_data = malloc(file_size);
fread(file_data, 1, file_size, f);
fclose(f);
char err[1<<9];
char *mem = malloc(1<<20);
WL_Arena a = { mem, 1<<20, 0 };
WL_Program program;
int ret = WL_compile(file_data, file_size, a, &program, err, (int) sizeof(err));
if (ret < 0) {
printf("%s\n", err);
WL_Compiler *compiler = WL_Compiler_init(&a);
if (compiler == NULL) {
assert(0); // TODO
}
WL_CompileResult result;
WL_String path = { file, strlen(file) };
for (int i = 0;; i++) {
char buf[1<<10];
if (path.len >= (int) sizeof(buf)) {
assert(0); // TODO
}
memcpy(buf, path.ptr, path.len);
buf[path.len] = '\0';
FILE *f = fopen(buf, "rb");
if (f == NULL) {
printf("File not found '%.*s'\n", path.len, path.ptr);
return -1;
}
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
char *file_data = malloc(file_size);
fread(file_data, 1, file_size, f);
fclose(f);
result = WL_compile(compiler, path, (WL_String) { file_data, file_size });
free(file_data);
if (result.type == WL_COMPILE_RESULT_ERROR) {
printf("Compilation of '%.*s' failed\n", path.len, path.ptr);
break;
}
if (result.type == WL_COMPILE_RESULT_DONE)
break;
assert(result.type == WL_COMPILE_RESULT_FILE);
path = result.path;
}
WL_Compiler_free(compiler);
if (result.type == WL_COMPILE_RESULT_ERROR) {
printf("Compilation error\n");
return -1;
}
WL_Program program = result.program;
WL_State *state = WL_State_init(&a, program, err, (int) sizeof(err));
@@ -71,7 +108,6 @@ int main(int argc, char **argv)
}
WL_State_free(state);
free(file_data);
free(mem);
return 0;
}