Fixes and added include statement
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user