Module relative pathing seems to work, managed to get out of having to have the exe where the files are,

Got rid of scope names, now unique names uses scope ids, module folder is in top folder
This commit is contained in:
Krzosa Karol
2022-06-27 10:56:17 +02:00
parent 15d452cae3
commit b4f38caabe
13 changed files with 46 additions and 40 deletions

View File

@@ -28,7 +28,8 @@ function String
string_scope_name(Allocator *a, Ast_Scope *scope){
String string = {};
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
if(scope->name.str) string = string_fmt(a, "%Q%Q_", string, scope->name);
assert_msg(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id");
string = string_fmt(a, "%QS%u_", string, scope->scope_id);
return string;
}
@@ -693,18 +694,15 @@ parse_file(Ast_File *file){
assert(file);
Scratch scratch;
file->filecontent = os_read_file(pctx->perm, file->absolute_path);
file->filecontent = os_read_file(pctx->perm, file->absolute_file_path);
if(file->filecontent.len == 0){
compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_path);
compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_file_path);
}
pctx->currently_parsed_file = file;
String name = string_chop_last_period(file->filename.s);
file->name = pctx->intern(name);
pctx->currently_parsed_scope = file;
lex_restream(pctx, file->filecontent, file->filename.s);
lex_restream(pctx, file->filecontent, file->absolute_file_path);
while(token_expect(SAME_SCOPE)){
if(token_match_pound(pctx->intern("load"_s))){
parse_load(true);
@@ -786,16 +784,10 @@ parse_all_modules(){
function Ast_Module *
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);
return it;
}
}
Scratch scratch;
Ast_Module *result = ast_new(Ast_Module, AST_MODULE, pos, 0);
String absolute_file_path = {};
String absolute_base_folder = {};
//
// Find in working directory
//
@@ -803,8 +795,8 @@ add_module(Token *pos, Intern_String filename, B32 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);
absolute_file_path = string_copy(scratch, path);
absolute_base_folder = string_chop_last_slash(path);
}
}
@@ -815,28 +807,37 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module){
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);
absolute_file_path = path;
absolute_base_folder = string_chop_last_slash(path);
break;
}
}
}
if(result->base_folder.len == 0){
if(absolute_file_path.len == 0){
compiler_error(pos, "Couldn't find the module with name %Q", filename);
}
For(pctx->modules){
if(string_compare(it->absolute_file_path, absolute_file_path)){
log_info("Returning registered module: %Q\n", absolute_file_path);
return it;
}
}
Ast_Module *result = ast_new(Ast_Module, AST_MODULE, pos, 0);
result->absolute_file_path = string_copy(pctx->perm, absolute_file_path);
result->absolute_base_folder = string_copy(pctx->perm, absolute_base_folder);
log_info("Adding module: %Q\n", filename);
result->name = filename;
result->module = result; // @warning: self referential
result->file = result; // @warning: self referential
result->all_loaded_files = {pctx->heap};
result->implicit_imports = {pctx->heap};
result->decls = {pctx->heap};
result->parent_scope = 0;
result->scope_id = pctx->scope_ids++;
register_ast_file(pos, result->name, result, GLOBAL_IMPLICIT_LOAD);
register_ast_file(pos, result->absolute_file_path, result, GLOBAL_IMPLICIT_LOAD);
pctx->modules.add(result);
return result;
}