diff --git a/compiler.cpp b/compiler.cpp index 2f551a6..2c76c08 100644 --- a/compiler.cpp +++ b/compiler.cpp @@ -10,7 +10,7 @@ function void begin_compilation(){ init_ctx_time_begin = os_time(); OS_Heap *heap = exp_alloc_type(&pernament_arena, OS_Heap, AF_ZeroMemory); - *heap = win32_os_heap_create(false, mib(4), 0); + *heap = win32_os_heap_create(false, mib(4), 0, "Compiler heap"_s); Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory); parse_init(ctx, &pernament_arena, heap); init_ctx_time_end = os_time(); @@ -20,6 +20,7 @@ function void destroy_compiler(){ exp_destroy(pctx->heap); exp_free_all(pctx->perm); + exp_destroy(&pctx->stage_arena); } function void @@ -225,11 +226,17 @@ compile_file_to_string(String filename){ return result; } -const U32 COMPILE_NULL = 0x0; -const U32 COMPILE_PRINT_STATS = 0x1; +const U32 COMPILE_NULL = 0x0; +const U32 COMPILE_PRINT_STATS = 0x1; +const U32 COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY = 0x2; +const U32 COMPILE_AND_RUN = 0x4; 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); + } + String result = compile_file_to_string(filename); assert(os_write_file("program.c"_s, result)); @@ -239,12 +246,33 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){ system((const char *)compiler_call.str); F64 end = os_time(); - if(compile_flags & COMPILE_PRINT_STATS){ - printf("\ntotal = %f", os_time() - total_time); - printf("\nclang = %f", end - begin); - printf("\nparsing = %f", parsing_time_end - parsing_time_begin); - printf("\nresolving = %f", resolving_time_end - resolving_time_begin); - printf("\ngeneratin = %f", generating_time_end - generating_time_begin); + 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); } + + 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); + } + + if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ + int result = system("a.exe"); + assert(result != -1); + if(result == 0){ + log_info(PRINTF_GREEN "OK!" PRINTF_RESET); + } + if(result != 0){ + log_info(PRINTF_RED "ERROR!" PRINTF_RESET); + } + } + destroy_compiler(); } \ No newline at end of file diff --git a/examples/runtime_type_information.kl b/examples/runtime_type_information.kl index c5c60bb..3adbf08 100644 --- a/examples/runtime_type_information.kl +++ b/examples/runtime_type_information.kl @@ -21,30 +21,32 @@ main :: (): int else;; assert(false, "We expected S64 here! What a boomer!") + // + // @todo: This should work + // + // any_thing: Any = 10 -// _gcvt :: #foreign (value: F64, digits: int, buffer: *char): *char -// print_float :: (value: F64) -// buff: [100]char -// _gcvt(value, 10, &buff[0]) -// OutputDebugStringA(&buff[0]) + // + // Any type allows us to combine a value pointer and + // it's corresponding type_id + // + value_to_be_wrapped := 10 + any_value: Any = value_to_be_wrapped + + if any_value.type == S64 + // Void pointers get implicitly cast + value: *S64 = any_value.data + *value = 20 + elif any_value.type == int + value: *int = any_value.data + *value = 30 + elif any_value.type == char;; assert(false, "No bueno") + + assert(value_to_be_wrapped == 20) + -// print_type :: (t: Type) -// type_info := get_type_info(t) -// if !type_info -// return -// switch type_info.kind -// Type_Info_Kind.S64, Type_Info_Kind.S32, Type_Info_Kind.S16, Type_Info_Kind.S8, Type_Info_Kind.INT -// OutputDebugStringA("Integer") -// Type_Info_Kind.U64, Type_Info_Kind.U32, Type_Info_Kind.U16, Type_Info_Kind.U8 -// OutputDebugStringA("Unsigned") -// Type_Info_Kind.F64, Type_Info_Kind.F32 -// OutputDebugStringA("Float") -// Type_Info_Kind.POINTER -// OutputDebugStringA("*") -// print_type(type_info.base_type) -// default;; OutputDebugStringA("Unknown") // print :: (a: Any) // type_info := get_type_info(a.type) diff --git a/main.cpp b/main.cpp index 36b2875..3d0ad60 100644 --- a/main.cpp +++ b/main.cpp @@ -228,7 +228,7 @@ int main(int argument_count, char **arguments){ program_name = string_from_cstring(arguments[1]); } - compile_file("examples/runtime_type_information.kl"_s); - compile_file("examples/types_as_first_class_values.kl"_s); + compile_file("examples/runtime_type_information.kl"_s, COMPILE_AND_RUN); + compile_file("examples/types_as_first_class_values.kl"_s, COMPILE_AND_RUN); __debugbreak(); } diff --git a/parsing.cpp b/parsing.cpp index edaaf83..5635e02 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -4,6 +4,7 @@ function void print_token_line(Token *token){ if(!token) return; +#define PRINTF_GREEN "\033[32m" #define PRINTF_RED "\033[31m" #define PRINTF_RESET "\033[0m"