From 0a7fe8caad23667aa6e7e006ced7a2f8f75375ef Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 30 Sep 2022 09:52:54 +0200 Subject: [PATCH] Moving more code to list --- core_codegen_c_language.cpp | 36 +++++++++++++++++++----------------- core_compiler.cpp | 26 +++++++++++--------------- core_compiler.h | 11 +++++------ core_parsing.cpp | 15 ++++++++------- core_typechecking.cpp | 4 ++-- 5 files changed, 45 insertions(+), 47 deletions(-) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 2ef3e3d..9236740 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -757,9 +757,10 @@ typedef struct String{ )=="); // Generate struct forward decls - For(pctx->ordered_decls){ - if(it->kind == AST_STRUCT){ - genln("typedef struct %Q %Q;", it->name, it->name); + Iter(&pctx->ordered_decls){ + auto i = it.item[0]; + if(i->kind == AST_STRUCT){ + genln("typedef struct %Q %Q;", i->name, i->name); } } @@ -801,20 +802,20 @@ typedef struct String{ Ast_Decl *win_main = 0; // Generate lambda forward decls - For(pctx->ordered_decls){ - if(it->kind == AST_LAMBDA){ - if(it->name == intern_main){ - main = it; - it->unique_name = it->name; + Iter(&pctx->ordered_decls){ + if(it.item[0]->kind == AST_LAMBDA){ + if(it.item[0]->name == intern_main){ + main = it.item[0]; + it.item[0]->unique_name = it.item[0]->name; } - if(it->name == intern_win_main){ - win_main = it; - it->unique_name = it->name; + if(it.item[0]->name == intern_win_main){ + win_main = it.item[0]; + it.item[0]->unique_name = it.item[0]->name; } genln(""); - gen_lambda(it->unique_name, it->lambda, false); + gen_lambda(it.item[0]->unique_name, it.item[0]->lambda, false); } } @@ -824,7 +825,7 @@ typedef struct String{ // Generate language.kl for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){ - Ast_Decl *it = pctx->ordered_decls[i]; + Ast_Decl *it = get(&pctx->ordered_decls, i); genln(""); gen_ast(it); } @@ -872,10 +873,11 @@ typedef struct String{ gen("};"); // Generate actual code - for(S32 i = pctx->base_language_ordered_decl_len; i < pctx->ordered_decls.len; i++){ - Ast_Decl *it = pctx->ordered_decls[i]; - genln(""); - gen_ast(it); + Iter(&pctx->ordered_decls){ + if(it.index >= pctx->base_language_ordered_decl_len){ + genln(""); + gen_ast(it.item[0]); + } } String string_result = string_flatten(pctx->perm, &pctx->gen); diff --git a/core_compiler.cpp b/core_compiler.cpp index 46fc72f..afa0632 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -82,15 +82,11 @@ function void parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){ pctx = ctx; ctx->perm = perm_allocator; - ctx->perm_arena = perm_allocator; ctx->heap = heap_allocator; ctx->gen = {ctx->heap}; - ctx->ordered_decls = {ctx->heap}; ctx->type_map = {ctx->heap}; - ctx->modules = {ctx->heap}; 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); @@ -102,9 +98,8 @@ parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){ ctx->exe_folder = os_get_exe_dir(ctx->perm); ctx->working_folder = os_get_working_dir(ctx->perm); - ctx->module_folders = {ctx->heap}; String main_module = string_fmt(ctx->perm, "%Q/modules", ctx->exe_folder); - ctx->module_folders.add(main_module); + add(ctx->perm, &ctx->module_folders, main_module); } @@ -162,8 +157,9 @@ global F64 parsing_time_end; function void parse_all_modules(){ parsing_time_begin = os_time(); - for(S64 i = 0; i < pctx->modules.len; i++){ - Ast_Module *module = pctx->modules[i]; + + Iter_Named(&pctx->modules, mod_it){ + Ast_Module *module = mod_it.item[0]; if(module->state != MODULE_REGISTERED) continue; Iter(&module->all_loaded_files){ @@ -200,8 +196,8 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module){ // Find in module folder // else{ - For(pctx->module_folders){ - String path = string_fmt(scratch, "%Q/%Q", it, filename); + Iter(&pctx->module_folders){ + String path = string_fmt(scratch, "%Q/%Q", it.item[0], filename); if(os_does_file_exist(path)){ absolute_file_path = path; absolute_base_folder = string_chop_last_slash(path); @@ -214,10 +210,10 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module){ 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)){ + Iter(&pctx->modules){ + if(string_compare(it.item[0]->absolute_file_path, absolute_file_path)){ log_trace("Returning registered module: %Q\n", absolute_file_path); - return it; + return it.item[0]; } } @@ -234,7 +230,7 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module){ result->scope_id = pctx->scope_ids++; register_ast_file(pos, result->absolute_file_path, result, GLOBAL_IMPLICIT_LOAD); - pctx->modules.add(result); + add(pctx->perm, &pctx->modules, result); return result; } @@ -267,7 +263,7 @@ compile_file_to_string(String filename){ resolve_everything_in_module(module); // @note: language stuff needs to be declared before type_info data // so we mark where it ends - pctx->base_language_ordered_decl_len = pctx->ordered_decls.len; + pctx->base_language_ordered_decl_len = length(&pctx->ordered_decls); Ast_Decl *any_decl = search_for_single_decl(module, pctx->intern("Any"_s)); assert(any_decl->type == type_type); type_any = any_decl->type_val; diff --git a/core_compiler.h b/core_compiler.h index c6aa2af..94132ce 100644 --- a/core_compiler.h +++ b/core_compiler.h @@ -157,8 +157,7 @@ struct Lexer{ // struct Parse_Ctx:Lexer{ - Allocator *perm; // Stores: AST, tokens, interns - Arena *perm_arena; + Arena *perm; // Stores: AST, tokens, interns Allocator *heap; Arena stage_arena; @@ -170,9 +169,9 @@ struct Parse_Ctx:Lexer{ Ast_Module *language_base_module; - Array files; - Array modules; - Array ordered_decls; + List files; + List modules; + List ordered_decls; S32 base_language_ordered_decl_len; Ast_Scope *currently_parsed_scope; @@ -180,7 +179,7 @@ struct Parse_Ctx:Lexer{ U32 scope_ids; U32 scope_visit_id; - Array module_folders; + List module_folders; String module_folder; String exe_folder; String working_folder; diff --git a/core_parsing.cpp b/core_parsing.cpp index a35d51f..bbf6479 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -726,15 +726,16 @@ function Ast_File * 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_file_path, absolute_file_path)){ - if(module == it->module){ + Iter(&pctx->files){ + Ast_File *it_file = it.item[0]; + if(string_compare(it_file->absolute_file_path, absolute_file_path)){ + if(module == it_file->module){ log_trace("%Q :: Returning registered file: %Q\n", module->absolute_file_path, absolute_file_path); - file = it; + file = it_file; break; } - compiler_error(it->pos, pos, "This file is already loaded by module: %Q, try importing that module to get access to it", module->absolute_file_path); + compiler_error(it_file->pos, pos, "This file is already loaded by module: %Q, try importing that module to get access to it", module->absolute_file_path); } } @@ -750,9 +751,9 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32 file->implicit_imports = {pctx->heap}; file->pos = pos; file->debug_name = string_skip_to_last_slash(absolute_file_path); - add(pctx->perm_arena, &file->module->all_loaded_files, file); + add(pctx->perm, &file->module->all_loaded_files, file); file->scope_id = pctx->scope_ids++; - pctx->files.add(file); + add(pctx->perm, &pctx->files, file); } if(global_implicit_load) { diff --git a/core_typechecking.cpp b/core_typechecking.cpp index 9a1c3cb..fa93579 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -353,7 +353,7 @@ type_complete(Ast_Type *type){ } type_struct_complete(type, (Ast_Decl *)type->ast); - pctx->ordered_decls.add((Ast_Decl *)type->ast); + add(pctx->perm, &pctx->ordered_decls, (Ast_Decl *)type->ast); } function void @@ -2061,5 +2061,5 @@ resolve_decl(Ast_Decl *ast){ ast->state = DECL_RESOLVED; if(is_flag_set(ast->flags, AST_GLOBAL)) - pctx->ordered_decls.add(ast); + add(pctx->perm, &pctx->ordered_decls, ast); }