From b4f38caabea9574737a9edc7beb5b025ab72d6c7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 27 Jun 2022 10:56:17 +0200 Subject: [PATCH] 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 --- ast.cpp | 11 +++-- build.bat | 4 +- c_language_codegen.cpp | 49 +++++++++++---------- compiler.h | 2 + main.cpp | 2 +- {programs/modules => modules}/base.kl | 0 {programs/modules => modules}/gdi32.kl | 0 {programs/modules => modules}/kernel32.kl | 0 {programs/modules => modules}/language.kl | 0 {programs/modules => modules}/os_windows.kl | 0 {programs/modules => modules}/user32.kl | 0 {programs/modules => modules}/winmm.kl | 0 parsing.cpp | 18 ++++---- 13 files changed, 46 insertions(+), 40 deletions(-) rename {programs/modules => modules}/base.kl (100%) rename {programs/modules => modules}/gdi32.kl (100%) rename {programs/modules => modules}/kernel32.kl (100%) rename {programs/modules => modules}/language.kl (100%) rename {programs/modules => modules}/os_windows.kl (100%) rename {programs/modules => modules}/user32.kl (100%) rename {programs/modules => modules}/winmm.kl (100%) diff --git a/ast.cpp b/ast.cpp index f0760c2..16f1865 100644 --- a/ast.cpp +++ b/ast.cpp @@ -224,11 +224,11 @@ How does current declaration order resolver works: */ struct Ast_Scope: Ast{ - Intern_String name; // for debugging Array implicit_imports; Array decls; Array 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 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; } diff --git a/build.bat b/build.bat index 7e89c7a..f7e66b3 100644 --- a/build.bat +++ b/build.bat @@ -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 diff --git a/c_language_codegen.cpp b/c_language_codegen.cpp index aaa60d9..6f44e3e 100644 --- a/c_language_codegen.cpp +++ b/c_language_codegen.cpp @@ -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; } diff --git a/compiler.h b/compiler.h index ad70fd5..b0d374d 100644 --- a/compiler.h +++ b/compiler.h @@ -199,6 +199,7 @@ struct Parse_Ctx:Lexer{ Ast_Scope *currently_parsed_scope; Ast_File *currently_parsed_file; + U32 scope_ids; Array 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); diff --git a/main.cpp b/main.cpp index e605596..f6a398e 100644 --- a/main.cpp +++ b/main.cpp @@ -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]); } diff --git a/programs/modules/base.kl b/modules/base.kl similarity index 100% rename from programs/modules/base.kl rename to modules/base.kl diff --git a/programs/modules/gdi32.kl b/modules/gdi32.kl similarity index 100% rename from programs/modules/gdi32.kl rename to modules/gdi32.kl diff --git a/programs/modules/kernel32.kl b/modules/kernel32.kl similarity index 100% rename from programs/modules/kernel32.kl rename to modules/kernel32.kl diff --git a/programs/modules/language.kl b/modules/language.kl similarity index 100% rename from programs/modules/language.kl rename to modules/language.kl diff --git a/programs/modules/os_windows.kl b/modules/os_windows.kl similarity index 100% rename from programs/modules/os_windows.kl rename to modules/os_windows.kl diff --git a/programs/modules/user32.kl b/modules/user32.kl similarity index 100% rename from programs/modules/user32.kl rename to modules/user32.kl diff --git a/programs/modules/winmm.kl b/modules/winmm.kl similarity index 100% rename from programs/modules/winmm.kl rename to modules/winmm.kl diff --git a/parsing.cpp b/parsing.cpp index fd00265..7f5baf2 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -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; }