From 4004b8b8d35c9b2a678c349cab0c4d71126c1cc7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 11 Oct 2022 10:20:18 +0200 Subject: [PATCH] Fixed leaky imports, modified log stuff --- .gitignore | 1 + base.cpp | 6 +++++- build.bat | 1 + core_compiler.cpp | 25 +++++++++++----------- core_main.cpp | 49 +++++++++++++++++++++++++++++-------------- core_parsing.cpp | 2 +- core_typechecking.cpp | 8 ++++--- os_linux.cpp | 16 +++++++------- 8 files changed, 67 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index a3e74de..69fae58 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.out *.c +tests/ backup* start.bat __pycache__ \ No newline at end of file diff --git a/base.cpp b/base.cpp index cf41056..0a1ce8d 100644 --- a/base.cpp +++ b/base.cpp @@ -561,7 +561,7 @@ arena_init(Arena *a, String debug_name){ a->debug_string = debug_name; } -enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; +enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); //----------------------------------------------------------------------------- // Thread Context @@ -753,6 +753,7 @@ array_make(Arena *a, S64 size = 16){ // Logging //----------------------------------------------------------------------------- #define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__) +#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__) #define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__) #define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__) function void @@ -764,6 +765,9 @@ handle_log_message(Log_Kind kind, int line, const char *file, const char *str, . if(thread_ctx.log_proc) thread_ctx.log_proc(kind, message, (char *)file, line); else{ printf("%s", message.str); + if(kind != Log_Kind_Normal_No_NewLine){ + printf("\n"); + } } } diff --git a/build.bat b/build.bat index f23a8be..2cffc7d 100644 --- a/build.bat +++ b/build.bat @@ -3,4 +3,5 @@ pushd %~dp0 rem cl main.cpp -I.. user32.lib clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib +ubuntu run clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o core.out popd diff --git a/core_compiler.cpp b/core_compiler.cpp index 9afd846..863735c 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -302,7 +302,7 @@ const U32 COMPILE_TESTING = 0x8; function void compile_file(String filename, U32 compile_flags = COMPILE_NULL){ if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ - log_info("\n%Q - ", filename); + log_info_no_nl("%Q - ", filename); } String result = compile_file_to_string(filename); @@ -322,20 +322,20 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){ F64 end = os_time(); if(is_flag_set(compile_flags, COMPILE_PRINT_STATS)){ - log_info("\ntotal = %f", os_time() - total_time); - log_info("\nclang = %f", end - begin); - log_info("\nparsing = %f", parsing_time_end - parsing_time_begin); - log_info("\nresolving = %f", resolving_time_end - resolving_time_begin); - log_info("\ngeneratin = %f", generating_time_end - generating_time_begin); + log_info("total = %f", os_time() - total_time); + log_info("clang = %f", end - begin); + log_info("parsing = %f", parsing_time_end - parsing_time_begin); + log_info("resolving = %f", resolving_time_end - resolving_time_begin); + log_info("generatin = %f", generating_time_end - generating_time_begin); } if(is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)){ Arena *p = (Arena *)pctx->perm; Arena *stage = (Arena *)&pctx->stage_arena; - log_info("\nPernament arena len: %llu commit: %llu", p->len, p->memory.commit); - log_info("\nStage arena len: %llu commit: %llu", stage->len, stage->memory.commit); - log_info("\nScratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); - log_info("\nScratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); + log_info("Pernament arena len: %llu commit: %llu", p->len, p->memory.commit); + log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit); + log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); + log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); } if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ @@ -348,11 +348,12 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){ int result = system((char *)sys.str); assert(result != -1); if(result == 0){ - log_info(PRINTF_GREEN "OK!" PRINTF_RESET); + log_info_no_nl(PRINTF_GREEN "OK!" PRINTF_RESET); } if(result != 0){ - log_info(PRINTF_RED "ERROR!" PRINTF_RESET); + log_info_no_nl(PRINTF_RED "ERROR!" PRINTF_RESET); } + log_info(""); } destroy_compiler(); diff --git a/core_main.cpp b/core_main.cpp index b0d69f4..3dbacd5 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -2,10 +2,9 @@ Current: -- [ ] DANGEROUS BUG! &(int64_t){32} creates a new value, we shouldn't do this with variables!!! - - [ ] String declaration in Language.core - [ ] Way to import and force evaluate #import_lazy #import ? +- [ ] Typeof operator - [ ] Imports are leaking names ! Multimedia leaks windows stuff - [ ] Test and bulletproof any, slices @@ -251,6 +250,17 @@ For modules it's a bit different cause they should be distributed as valid. int main(int argument_count, char **arguments){ + Scratch scratch; + Array args = {scratch}; + for(int i = 1; i < argument_count; i+=1){ + String arg = string_from_cstring(arguments[i]); + args.add(arg); + } + + if(!args.len){ + log_info("Please specify a file to compile!"); + } + #if OS_WINDOWS // Set output mode to handle virtual terminal sequences HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); @@ -267,11 +277,9 @@ int main(int argument_count, char **arguments){ if (!SetConsoleMode(hOut, dwMode)) { return GetLastError(); } -#endif - -#if OS_WINDOWS test_os_memory(); #endif + thread_ctx_init(); test_unicode(); @@ -281,19 +289,28 @@ int main(int argument_count, char **arguments){ // emit_line_directives = false; // emit_type_info = false; - if(argument_count > 1){ - String program_name = string_from_cstring(arguments[1]); - compile_file(program_name, COMPILE_PRINT_STATS); - } + For(args){ - else { - Scratch scratch; - Array examples = os_list_dir(scratch, "examples"_s); - For(examples){ - if(it.is_directory) continue; - compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); + if(it == "-testing"_s){ // @copy_paste + Scratch scratch; + Array examples = os_list_dir(scratch, "examples"_s); + Array tests = os_list_dir(scratch, "tests"_s); + For(examples){ + if(it.is_directory) continue; + compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); + } + For(tests){ + if(it.is_directory) continue; + compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); + } } + + else { + String program_name = string_from_cstring(arguments[1]); + compile_file(program_name, COMPILE_PRINT_STATS); + } + } - log_info("End of program\n"); + log_info("End of program"); return 0; } diff --git a/core_parsing.cpp b/core_parsing.cpp index e42abd4..4289737 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -746,7 +746,7 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32 file->absolute_file_path = absolute_file_path; file->absolute_base_folder = string_copy(pctx->perm, string_chop_last_slash(file->absolute_file_path)); file->module = module; - file->parent_scope = 0; + file->parent_scope = module; file->file = file; // @warning: self referential! file->pos = pos; file->debug_name = string_skip_to_last_slash(absolute_file_path); diff --git a/core_typechecking.cpp b/core_typechecking.cpp index a987443..1b91d48 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -455,13 +455,15 @@ inside_scope_search(Scope_Search *search, Ast_Scope *scope, int level){ } // Search for declarations in imported implicitly scopes - if(level < 2 || scope->kind != AST_FILE){ - Iter(&scope->implicit_imports){ + // We dont want to descent into foreign implicit imports + // so we cap it with level and then we need to make sure that we + // descent into module 1 level away and it's files but no more then that + Iter(&scope->implicit_imports){ + if(level > 0 && it.item[0]->kind == AST_MODULE) continue; inside_scope_search(search, it.item[0], level + 1); if(search->exit_on_find && search->results.len){ return; - } } } } diff --git a/os_linux.cpp b/os_linux.cpp index be12391..5d609f8 100644 --- a/os_linux.cpp +++ b/os_linux.cpp @@ -21,14 +21,14 @@ os_write_file(String filename, String filecontent){ } function String -os_read_file(Allocator *a, String name){ +os_read_file(Arena *a, String name){ String result = {0}; FILE *f = fopen((char *)name.str, "rb"); if(f){ fseek(f, 0, SEEK_END); result.len = ftell(f); fseek(f, 0, SEEK_SET); - result.str = (U8 *)exp_alloc(a, result.len + 1); + result.str = (U8 *)arena_push_size(a, result.len + 1); fread(result.str, result.len, 1, f); fclose(f); result.str[result.len] = 0; @@ -38,7 +38,7 @@ os_read_file(Allocator *a, String name){ } function String -os_get_exe_dir(Allocator *a){ +os_get_exe_dir(Arena *a){ char buffer[PATH_MAX] = {}; if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) { log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported"); @@ -52,7 +52,7 @@ os_get_exe_dir(Allocator *a){ } function String -os_get_absolute_path(Allocator *a, String path){ +os_get_absolute_path(Arena *a, String path){ assert(path.str[path.len] == 0); char buffer[PATH_MAX] = {}; realpath((char *)path.str, buffer); @@ -73,14 +73,14 @@ os_does_file_exist(String path){ } function String -os_get_working_dir(Allocator *allocator){ - char *buffer = exp_alloc_array(allocator, char, PATH_MAX); +os_get_working_dir(Arena *allocator){ + char *buffer = arena_push_array(allocator, char, PATH_MAX); char *result = getcwd(buffer, PATH_MAX); return string_from_cstring(result); } function Array -os_list_dir(Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ +os_list_dir(Arena *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ Scratch scratch(a); Array dirs_to_read = {scratch}; dirs_to_read.add(dir); @@ -120,7 +120,7 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ closedir(d); } else{ - log_info("ERROR Failed to read dir: %Q, errno: %s\n", *it, strerror(errno)); + log_info("ERROR Failed to read dir: %Q, errno: %s", *it, strerror(errno)); } }