diff --git a/WL.c b/WL.c index 5aa92f9..ac890b0 100644 --- a/WL.c +++ b/WL.c @@ -3585,7 +3585,7 @@ WL_Compiler *wl_compiler_init(WL_Arena *arena) return compiler; } -WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String content) +WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String path, WL_String content) { if (compiler->err) return (WL_AddResult) { .type=WL_ADD_ERROR }; @@ -3596,6 +3596,38 @@ WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String content) return (WL_AddResult) { .type=WL_ADD_ERROR }; } + // Make include paths relative to the parent file + if (path.len > 0) { + + String parent = { path.ptr, path.len }; + + char sep = '/'; + while (parent.len > 0 && parent.ptr[parent.len-1] != sep) + parent.len--; + + if (parent.len > 0) { + Node *include = pres.includes; + while (include) { + + char *dst = alloc(compiler->arena, parent.len + include->include_path.len + 1, 1); + if (dst == NULL) { + // TODO + } + + memcpy(dst, + parent.ptr, + parent.len); + memcpy(dst + parent.len, + include->include_path.ptr, + include->include_path.len); + + include->include_path = (String) { dst, parent.len + include->include_path.len }; + + include = include->include_next; + } + } + } + CompiledFile compiled_file = { .file = compiler->waiting_file, .root = pres.node, diff --git a/WL.h b/WL.h index ceebb50..0db07d8 100644 --- a/WL.h +++ b/WL.h @@ -46,7 +46,7 @@ typedef struct { } WL_EvalResult; WL_Compiler* wl_compiler_init (WL_Arena *arena); -WL_AddResult wl_compiler_add (WL_Compiler *compiler, WL_String content); +WL_AddResult wl_compiler_add (WL_Compiler *compiler, WL_String path, WL_String content); int wl_compiler_link (WL_Compiler *compiler, WL_Program *program); WL_String wl_compiler_error (WL_Compiler *compiler); int wl_dump_ast (WL_Compiler *compiler, char *dst, int cap);