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

11
ast.cpp
View File

@@ -224,11 +224,11 @@ How does current declaration order resolver works:
*/
struct Ast_Scope: Ast{
Intern_String name; // for debugging
Array<Ast_Scope *> implicit_imports;
Array<Ast_Decl *> decls;
Array<Ast *> stmts;
U32 scope_id;
Ast_Scope *file; // Self referential for scope and module
Ast_Module *module;
};
@@ -241,13 +241,13 @@ enum Ast_Module_State{
struct Ast_Module: Ast_Scope{
Ast_Module_State state;
String base_folder;
String absolute_base_folder;
String absolute_file_path;
Array<Ast_File *> all_loaded_files;
};
struct Ast_File: Ast_Scope{
String absolute_path;
Intern_String filename;
String absolute_file_path;
String filecontent;
};
@@ -464,6 +464,7 @@ begin_decl_scope(Allocator *scratch, Token *pos){
result->decls = {scratch};
result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module;
result->scope_id = pctx->scope_ids++;
assert(result->file);
pctx->currently_parsed_scope = result;
return result;
@@ -482,6 +483,7 @@ begin_stmt_scope(Allocator *scratch, Token *pos){
result->decls = {pctx->heap};
result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module;
result->scope_id = pctx->scope_ids++;
assert(result->file);
pctx->currently_parsed_scope = result;
return result;
@@ -547,6 +549,7 @@ ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){
AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->decls = {allocator};
result->file = file;
result->scope_id = pctx->scope_ids++;
assert(result->file);
return result;
}

View File

@@ -2,7 +2,7 @@
pushd %~dp0\programs
pushd %~dp0
rem cl main.cpp -I.. user32.lib
clang ../main.cpp -O0 -I.. -I../.. -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
clang main.cpp -O0 -I.. -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
popd

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;
}

View File

@@ -199,6 +199,7 @@ struct Parse_Ctx:Lexer{
Ast_Scope *currently_parsed_scope;
Ast_File *currently_parsed_file;
U32 scope_ids;
Array<String> module_folders;
String module_folder;
@@ -262,6 +263,7 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
ctx->all_types = {ctx->heap};
ctx->helper_builder= {ctx->heap};
ctx->files = {ctx->heap};
ctx->scope_ids = 1;
bigint_allocator = ctx->perm;
arena_init(&ctx->stage_arena, "Compiler stage arena"_s);

View File

@@ -222,7 +222,7 @@ int main(int argument_count, char **arguments){
test_bucket_arrays();
emit_line_directives = true;
String program_name = "main.kl"_s;
String program_name = "programs/main.kl"_s;
if(argument_count > 1){
program_name = string_from_cstring(arguments[1]);
}

View File

@@ -715,29 +715,27 @@ add_implicit_import(Ast_Scope *scope, Ast_Scope *add){
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 = string_fmt(pctx->perm, "%Q/%Q", module->base_folder, filename);
register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32 global_implicit_load){
Ast_File *file = 0;
For(pctx->files){
if(string_compare(it->absolute_path, absolute_path)){
if(string_compare(it->absolute_file_path, absolute_file_path)){
if(module == it->module){
log_info("%Q :: Returning registered file: %Q\n", module->name, absolute_path);
log_info("%Q :: Returning registered file: %Q\n", module->absolute_file_path, absolute_file_path);
file = it;
break;
}
print_token_context(it->pos);
compiler_error(pos, "This file is already loaded by module: %Q, try importing that module to get access to it", module->name);
compiler_error(pos, "This file is already loaded by module: %Q, try importing that module to get access to it", module->absolute_file_path);
}
}
if(!file){
log_info("%Q :: Registering file: %Q\n", module->name, filename);
log_info("%Q :: Registering file: %Q\n", module->absolute_file_path, absolute_file_path);
AST_NEW(File, FILE, 0, 0);
file = result;
file->absolute_path = absolute_path;
file->filename = filename;
file->absolute_file_path = absolute_file_path;
file->module = module;
file->parent_scope = 0;
file->file = file; // @warning: self referential!
@@ -745,6 +743,7 @@ register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 gl
file->implicit_imports = {pctx->heap};
file->pos = pos;
file->module->all_loaded_files.add(file);
file->scope_id = pctx->scope_ids++;
pctx->files.add(file);
}
@@ -758,7 +757,8 @@ register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 gl
function Ast_File *
parse_load(B32 global_implicit_load){
Token *file = token_expect(TK_StringLit);
Ast_File *result = register_ast_file(file, file->intern_val, pctx->currently_parsed_file->module, global_implicit_load);
String absolute_path = string_fmt(pctx->perm, "%Q/%Q", pctx->currently_parsed_file->module->absolute_base_folder, file->intern_val);
Ast_File *result = register_ast_file(file, absolute_path, pctx->currently_parsed_file->module, global_implicit_load);
return result;
}