Module relative folders working
This commit is contained in:
3
ast.cpp
3
ast.cpp
@@ -240,7 +240,8 @@ enum Ast_Module_State{
|
||||
};
|
||||
|
||||
struct Ast_Module: Ast_Scope{
|
||||
Ast_Module_State state;
|
||||
Ast_Module_State state;
|
||||
String base_folder;
|
||||
Array<Ast_File *> all_loaded_files;
|
||||
};
|
||||
|
||||
|
||||
@@ -785,7 +785,7 @@ parse_all_modules(){
|
||||
}
|
||||
|
||||
function Ast_Module *
|
||||
add_module(Token *pos, Intern_String filename){
|
||||
add_module(Token *pos, Intern_String filename, B32 command_line_module){
|
||||
For(pctx->modules){
|
||||
if(it->name == filename){
|
||||
log_info("Returning registered module: %Q\n", filename);
|
||||
@@ -793,8 +793,41 @@ add_module(Token *pos, Intern_String filename){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Scratch scratch;
|
||||
Ast_Module *result = ast_new(Ast_Module, AST_MODULE, pos, 0);
|
||||
//
|
||||
// Find in working directory
|
||||
//
|
||||
if(command_line_module){
|
||||
if(os_does_file_exist(filename.s)){
|
||||
String path = os_get_absolute_path(scratch, filename.s);
|
||||
string_path_normalize(path);
|
||||
path = string_chop_last_slash(path);
|
||||
result->base_folder = string_copy(pctx->perm, path);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Find in module folder
|
||||
//
|
||||
else{
|
||||
For(pctx->module_folders){
|
||||
String path = string_fmt(scratch, "%Q/%Q", it, filename);
|
||||
if(os_does_file_exist(path)){
|
||||
path = string_chop_last_slash(path);
|
||||
result->base_folder = string_copy(pctx->perm, path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(result->base_folder.len == 0){
|
||||
compiler_error(pos, "Couldn't find the module with name %Q", filename);
|
||||
}
|
||||
|
||||
log_info("Adding module: %Q\n", filename);
|
||||
Ast_Module *result = ast_new(Ast_Module, AST_MODULE, pos, 0);
|
||||
result->name = filename;
|
||||
result->module = result; // @warning: self referential
|
||||
result->file = result; // @warning: self referential
|
||||
|
||||
@@ -203,6 +203,7 @@ struct Parse_Ctx:Lexer{
|
||||
Array<String> module_folders;
|
||||
String module_folder;
|
||||
String exe_folder;
|
||||
String working_folder;
|
||||
|
||||
S64 indent;
|
||||
String_Builder gen;
|
||||
@@ -269,6 +270,7 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
|
||||
|
||||
// Init paths
|
||||
ctx->exe_folder = os_get_exe_dir(ctx->perm);
|
||||
ctx->working_folder = os_get_working_dir(ctx->perm);
|
||||
|
||||
ctx->module_folders = {ctx->heap};
|
||||
String main_module = string_fmt(ctx->perm, "%Q/modules", ctx->exe_folder);
|
||||
|
||||
2
main.cpp
2
main.cpp
@@ -246,7 +246,7 @@ int main(int argument_count, char **arguments){
|
||||
}
|
||||
|
||||
|
||||
Ast_Module *module = add_module(0, pctx->intern(program_name));
|
||||
Ast_Module *module = add_module(0, pctx->intern(program_name), true);
|
||||
parse_all_modules();
|
||||
assert(module);
|
||||
resolve_everything_in_module(module);
|
||||
|
||||
19
parsing.cpp
19
parsing.cpp
@@ -712,26 +712,11 @@ add_implicit_import(Ast_Scope *scope, Ast_Scope *add){
|
||||
}
|
||||
}
|
||||
|
||||
function String
|
||||
find_module(Intern_String filename){
|
||||
Scratch scratch;
|
||||
For(pctx->module_folders){
|
||||
String path = string_fmt(scratch, "%Q/%Q", it, filename);
|
||||
if(os_does_file_exist(path)){
|
||||
String result = string_copy(pctx->perm, path);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
enum{ GLOBAL_IMPLICIT_LOAD = 1 };
|
||||
|
||||
function Ast_File *
|
||||
register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 global_implicit_load){
|
||||
String absolute_path = find_module(filename);
|
||||
if(absolute_path.len == 0) absolute_path = os_get_absolute_path(pctx->perm, filename.s);
|
||||
String absolute_path = string_fmt(pctx->perm, "%Q/%Q", module->base_folder, filename);
|
||||
Ast_File *file = 0;
|
||||
|
||||
For(pctx->files){
|
||||
@@ -777,7 +762,7 @@ parse_load(B32 global_implicit_load){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Module *add_module(Token *pos, Intern_String filename);
|
||||
function Ast_Module *add_module(Token *pos, Intern_String filename, B32 command_line_module = false);
|
||||
function Ast_Module *
|
||||
parse_import(B32 global_implicit_import){
|
||||
Token *file = token_expect(TK_StringLit);
|
||||
|
||||
Reference in New Issue
Block a user