From 15d452cae38bc0a9ce3ca8a0985fca6fbf5b24a6 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 27 Jun 2022 10:24:58 +0200 Subject: [PATCH] Module relative folders working --- ast.cpp | 3 ++- c_language_codegen.cpp | 37 ++++++++++++++++++++++++++-- compiler.h | 2 ++ main.cpp | 2 +- parsing.cpp | 19 ++------------ programs/{ => modules}/base.kl | 0 programs/{ => modules}/language.kl | 0 programs/{ => modules}/os_windows.kl | 0 8 files changed, 42 insertions(+), 21 deletions(-) rename programs/{ => modules}/base.kl (100%) rename programs/{ => modules}/language.kl (100%) rename programs/{ => modules}/os_windows.kl (100%) diff --git a/ast.cpp b/ast.cpp index 1ebb2ec..f0760c2 100644 --- a/ast.cpp +++ b/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 all_loaded_files; }; diff --git a/c_language_codegen.cpp b/c_language_codegen.cpp index 7028283..aaa60d9 100644 --- a/c_language_codegen.cpp +++ b/c_language_codegen.cpp @@ -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 diff --git a/compiler.h b/compiler.h index 75b43ee..ad70fd5 100644 --- a/compiler.h +++ b/compiler.h @@ -203,6 +203,7 @@ struct Parse_Ctx:Lexer{ Array 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); diff --git a/main.cpp b/main.cpp index 5b2cc7f..e605596 100644 --- a/main.cpp +++ b/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); diff --git a/parsing.cpp b/parsing.cpp index 942d210..fd00265 100644 --- a/parsing.cpp +++ b/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); diff --git a/programs/base.kl b/programs/modules/base.kl similarity index 100% rename from programs/base.kl rename to programs/modules/base.kl diff --git a/programs/language.kl b/programs/modules/language.kl similarity index 100% rename from programs/language.kl rename to programs/modules/language.kl diff --git a/programs/os_windows.kl b/programs/modules/os_windows.kl similarity index 100% rename from programs/os_windows.kl rename to programs/modules/os_windows.kl